summary refs log tree commit diff stats
path: root/lisp/+util.el
blob: 0870a7177d1992b22ac66b62d896335f3725e138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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