diff options
Diffstat (limited to 'lisp/+util.el')
-rw-r--r-- | lisp/+util.el | 81 |
1 files changed, 81 insertions, 0 deletions
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 @@ | |||
1 | ;;; +util.el --- utility whatevers -*- lexical-binding: t -*- | ||
2 | |||
3 | ;;; Commentary: | ||
4 | |||
5 | ;; This file is going to be my version of like, subr.el -- lots of | ||
6 | ;; random shit that all goes in here. | ||
7 | |||
8 | ;;; Code: | ||
9 | |||
10 | (require 'cl-lib) | ||
11 | |||
12 | (defgroup +util nil | ||
13 | "Utility whatevers." | ||
14 | :group 'convenience) | ||
15 | |||
16 | ;;; STRINGS | ||
17 | |||
18 | (defcustom +string-default-alignment 'left | ||
19 | "Default alignment." | ||
20 | :type '(choice (const :tag "Left" 'left) | ||
21 | (const :tag "Right" 'right))) | ||
22 | |||
23 | ;; stolen from s.el | ||
24 | (defun +string-repeat (n s) | ||
25 | "Make a string of S repeated N times." | ||
26 | (declare (pure t) | ||
27 | (side-effect-free t)) | ||
28 | (let (ss) | ||
29 | (while (> n 0) | ||
30 | (setq ss (cons s ss) | ||
31 | n (1- n))) | ||
32 | (apply 'concat ss))) | ||
33 | |||
34 | (defun +string-truncate (s length &optional ellipsis alignment) | ||
35 | "Return S, shortened to LENGTH including ELLIPSIS and aligned to ALIGNMENT. | ||
36 | |||
37 | ELLIPSIS defaults to \"...\". | ||
38 | |||
39 | ALIGNMENT defaults to `+string-default-alignment'." | ||
40 | (declare (pure t) | ||
41 | (side-effect-free t)) | ||
42 | (let ((ellipsis (or ellipsis "...")) | ||
43 | (alignment (or alignment +string-default-alignment))) | ||
44 | (if (> (length s) length) | ||
45 | (format "%s%s" | ||
46 | (substring s 0 (- length (length ellipsis))) | ||
47 | ellipsis) | ||
48 | s))) | ||
49 | |||
50 | (cl-defun +string-align (s len | ||
51 | &key | ||
52 | (before "") (after "") (fill " ") | ||
53 | (ellipsis "...") | ||
54 | (alignment +string-default-alignment)) | ||
55 | "Print S to fit in LEN characters. | ||
56 | Optional arguments BEFORE and AFTER specify strings to go on | ||
57 | either side of S. | ||
58 | |||
59 | FILL is the string to fill extra space with (default \" \"). | ||
60 | |||
61 | ELLIPSIS is the string to show when S is too long to fit (default \"...\"). | ||
62 | |||
63 | ALIGNMENT can be one of these: | ||
64 | - nil: align to `+string-default-alignment' | ||
65 | - `left': align left | ||
66 | - `right': align right" | ||
67 | (let* ((s-length (length s)) | ||
68 | (before-length (length before)) | ||
69 | (after-length (length after)) | ||
70 | (max-length (- len (+ before-length after-length))) | ||
71 | (left-over (max 0 (- max-length s-length))) | ||
72 | (filler (+string-repeat left-over fill))) | ||
73 | (format "%s%s%s%s%s" | ||
74 | before | ||
75 | (if (eq alignment 'left) "" filler) | ||
76 | (+string-truncate s max-length ellipsis alignment) | ||
77 | (if (eq alignment 'right) "" filler) | ||
78 | after))) | ||
79 | |||
80 | (provide '+util) | ||
81 | ;;; +util.el ends here | ||