From 59c161560ad146123d9941cb1a22e04a39146364 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Mon, 22 Jul 2024 17:19:07 +1200 Subject: [PATCH] catchup.el: init --- catchup.el | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 catchup.el diff --git a/catchup.el b/catchup.el new file mode 100644 index 0000000..aeb7509 --- /dev/null +++ b/catchup.el @@ -0,0 +1,106 @@ +;;; catchup.el --- catch up with system stats -*- lexical-binding:t -*- + +;; Copyright (C) 2024 Jeremy Baxter + +;; Author: Jeremy Baxter +;; Maintainer: Jeremy Baxter +;; Created: July 2024 + +;; This file is not part of GNU Emacs. + +;; catchup.el is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. +;; +;; catchup.el is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with catchup.el. If not, see . + +;;; Commentary: + +;; catchup.el provides `catchup', a command for fetching system +;; stats or other information. Custom shell commands can be run +;; or a file can be read, and that information is then formatted +;; and displayed in the minibuffer. The output style and look can +;; be customized with `catchup-spec' and `catchup-separator'. + +;; TODO: add a memory statistic using `memory-info' + +;;; Code: + +(defvar catchup-buffer "*Catchup*" + "Temporary buffer used by `catchup' types.") + +(defvar catchup-separator "\t" + "Separator between `catchup' data.") + +(defvar catchup-spec '((format-time-string "%Y/%m/%d\n%T")) + "List representing data to show in `catchup'. + +Each element is a list: the car of each sublist being a symbol representing +the function to call, and the cdr being the rest of the arguments to pass +to said function. + +catchup.el provides some helper functions you can use; `catchup-file' and +`catchup-shell', which are primitives, and `catchup-battery', a helper +function built from the primitives. + +The default value represents a call to `format-time-string', which shows +the current date and time.") + +(defun catchup--strip (str) + (string-match "\\`\\s-*\\(.*?\\)\\s-*\n?\\'" str) + (match-string 1 str)) + +(defun catchup-file (file-name) + "Return the contents of the file FILE-NAME." + (let* + ((result nil) + (exists (get-file-buffer file-name)) + (buffer (if exists exists (find-file-noselect file-name)))) + (with-current-buffer buffer + (setq result (buffer-string)) + (unless exists + (kill-this-buffer))) + (catchup--strip result))) + +(defun catchup-battery (&optional battery-format) + "Return the battery capacity formatted according to BATTERY-FORMAT." + (format (or battery-format "%s") + (catchup-file "/sys/class/power_supply/BAT0/capacity"))) + +(defun catchup-shell (command) + "Return the output of running COMMAND in the shell." + (let ((result nil)) + (shell-command command catchup-buffer) + (with-current-buffer catchup-buffer + (setq result (buffer-string)) + (kill-this-buffer)) + result)) + +(defun catchup () + "Display system statistics. + +Only information in `catchup-spec' is displayed; see its documentation for +more information on configuring it. + +A separator or divider to divide information can be provided with +`catchup-separator'. This string is placed between every result." + (interactive) + (let + ((final "")) + (dolist (sublist catchup-spec) + (setq final + (concat final (catchup--strip + (apply (car sublist) (cdr sublist))) + catchup-separator))) + (message "%s" (catchup--strip final)))) + +(provide 'catchup) + +;;; catchup.el ends here