aboutsummaryrefslogtreecommitdiff
path: root/module/text/util.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2020-06-01 16:02:54 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2020-06-01 21:42:53 +0200
commit38f708452bc1032ee1e42cf0e345ca8851316e4a (patch)
treee569c12648b2ad848583299f1d7170340e1e54f7 /module/text/util.scm
parentRemove *TOP* tags from descriptions. (diff)
downloadcalp-38f708452bc1032ee1e42cf0e345ca8851316e4a.tar.gz
calp-38f708452bc1032ee1e42cf0e345ca8851316e4a.tar.xz
Break text procedures into modules.
Diffstat (limited to '')
-rw-r--r--module/text/util.scm45
1 files changed, 45 insertions, 0 deletions
diff --git a/module/text/util.scm b/module/text/util.scm
new file mode 100644
index 00000000..eda2df98
--- /dev/null
+++ b/module/text/util.scm
@@ -0,0 +1,45 @@
+(define-module (text util)
+ :use-module (util))
+
+(define-public (words str) (string-split str #\space))
+(define-public (unwords list) (string-join list " " 'infix))
+
+(define-public (lines str) (string-split str #\newline))
+(define-public (unlines list) (string-join list "\n" 'infix))
+
+;; Alternative string-length whith counts ANSI escapes as 0-length.
+;; NOTE Some way to opt in and out of different features would be nice.
+(define-public (true-string-length word)
+ (let loop ((chars (string->list word)))
+ (if (null? chars)
+ 0
+ (let ((char (car chars)))
+ (if (eqv? #\escape char)
+ (loop (cdr (memv #\m chars)))
+ (1+ (loop (cdr chars))))))))
+
+(define*-public (true-string-pad str len optional: (chr #\space))
+ (let ((strlen (true-string-length str)))
+ (if (> strlen len)
+ str
+ (string-append (make-string (- len strlen) chr)
+ str))))
+
+
+(define-public (trim-to-width str len)
+ (let ((trimmed (string-pad-right str len)))
+ (if (< (string-length trimmed)
+ (string-length str))
+ (string-append (string-drop-right trimmed 1)
+ "…")
+ trimmed)))
+
+;; TODO more options for infix strings
+(define*-public (add-enumeration-punctuation
+ list optional: (final-delim "&"))
+ (cond [(null? list) ""]
+ [(= 1 (length list)) (car list)]
+ [else
+ (let* (((tail . rest) (reverse list)))
+ (reverse (cons* tail " " final-delim " "
+ (intersperce ", " rest))))]))