From a2657993bad828af6743c68931a0e848bfcdec53 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 21 Nov 2021 23:57:41 -0600 Subject: I DECLARE BANKRUPTCY ... 8 Didn't think to do this till pretty .. written, so here we are. --- lisp/+util.el | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 lisp/+util.el (limited to 'lisp/+util.el') diff --git a/lisp/+util.el b/lisp/+util.el new file mode 100644 index 0000000..0870a71 --- /dev/null +++ b/lisp/+util.el @@ -0,0 +1,81 @@ +;;; +util.el --- utility whatevers -*- lexical-binding: t -*- + +;;; Commentary: + +;; This file is going to be my version of like, subr.el -- lots of +;; random shit that all goes in here. + +;;; Code: + +(require 'cl-lib) + +(defgroup +util nil + "Utility whatevers." + :group 'convenience) + +;;; STRINGS + +(defcustom +string-default-alignment 'left + "Default alignment." + :type '(choice (const :tag "Left" 'left) + (const :tag "Right" 'right))) + +;; stolen from s.el +(defun +string-repeat (n s) + "Make a string of S repeated N times." + (declare (pure t) + (side-effect-free t)) + (let (ss) + (while (> n 0) + (setq ss (cons s ss) + n (1- n))) + (apply 'concat ss))) + +(defun +string-truncate (s length &optional ellipsis alignment) + "Return S, shortened to LENGTH including ELLIPSIS and aligned to ALIGNMENT. + +ELLIPSIS defaults to \"...\". + +ALIGNMENT defaults to `+string-default-alignment'." + (declare (pure t) + (side-effect-free t)) + (let ((ellipsis (or ellipsis "...")) + (alignment (or alignment +string-default-alignment))) + (if (> (length s) length) + (format "%s%s" + (substring s 0 (- length (length ellipsis))) + ellipsis) + s))) + +(cl-defun +string-align (s len + &key + (before "") (after "") (fill " ") + (ellipsis "...") + (alignment +string-default-alignment)) + "Print S to fit in LEN characters. +Optional arguments BEFORE and AFTER specify strings to go on +either side of S. + +FILL is the string to fill extra space with (default \" \"). + +ELLIPSIS is the string to show when S is too long to fit (default \"...\"). + +ALIGNMENT can be one of these: +- nil: align to `+string-default-alignment' +- `left': align left +- `right': align right" + (let* ((s-length (length s)) + (before-length (length before)) + (after-length (length after)) + (max-length (- len (+ before-length after-length))) + (left-over (max 0 (- max-length s-length))) + (filler (+string-repeat left-over fill))) + (format "%s%s%s%s%s" + before + (if (eq alignment 'left) "" filler) + (+string-truncate s max-length ellipsis alignment) + (if (eq alignment 'right) "" filler) + after))) + +(provide '+util) +;;; +util.el ends here -- cgit 1.4.1-21-gabe81