about summary refs log tree commit diff stats
path: root/yolk.attrs.ss
diff options
context:
space:
mode:
Diffstat (limited to 'yolk.attrs.ss')
-rw-r--r--yolk.attrs.ss87
1 files changed, 87 insertions, 0 deletions
diff --git a/yolk.attrs.ss b/yolk.attrs.ss new file mode 100644 index 0000000..6a06034 --- /dev/null +++ b/yolk.attrs.ss
@@ -0,0 +1,87 @@
1;;; (yolk attrs) --- text attributes
2
3(import (scheme base)
4 (scheme write)
5 (chicken string)
6 (yolk common))
7
8(define %props-alist
9 '((reset . "0")
10 ;; name . (on . off)
11 (bold . ("1" . "22"))
12 (dim . ("2" . "22"))
13 (italic . ("3" . "23"))
14 (underline . ("4" . "24"))
15 (blink . ("5" . "25"))
16 (inverse . ("7" . "27"))
17 (hidden . ("8" . "28"))
18 (strikethrough . ("9" . "29"))))
19
20(define (prop? x)
21 (let ((x? (memq x (map car %props-alist))))
22 (if x? x #f)))
23
24(define %colors-alist
25 '((reset . "0")
26 ;; color . (fg . bg)
27 (black . ("30" . "40"))
28 (red . ("31" . "41"))
29 (green . ("32" . "42"))
30 (yellow . ("33" . "43"))
31 (blue . ("34" . "44"))
32 (magenta . ("35" . "45"))
33 (cyan . ("36" . "46"))
34 (white . ("37" . "47"))
35 (default . ("39" . "49"))))
36
37(define (color? x)
38 (let ((x? (memq x (map car %colors-alist))))
39 (if x? x #f)))
40
41(define (attrs as)
42 (let loop ((as as)
43 (acc '()))
44 (cond
45 ((null? as)
46 (attr-list->escape (reverse acc)))
47 ((eq? as 'reset)
48 (csi "0m"))
49 (else
50 (loop (cdr as)
51 (cons (attr->code (car as))
52 acc))))))
53
54(define (attr-list->escape al)
55 (let ((code-string (string-intersperse (map ->string al) ";")))
56 (csi code-string "m")))
57
58(define (attr->code attr)
59 (cond
60 ((number? attr) attr)
61 ((or (color? attr)
62 (and (pair? attr)
63 (eq? (car attr) 'fg)
64 (or (color? (cdr attr))
65 (color? (cadr attr)))))
66 => (lambda (clr)
67 (cadr (assq clr %colors-alist))))
68 ((and (pair? attr)
69 (eq? (car attr) 'bg)
70 (or (color? (cdr attr))
71 (color? (cadr attr))))
72 => (lambda (clr)
73 (cddr (assq clr %colors-alist))))
74 ((or (prop? attr)
75 (and (pair? attr)
76 (eq? (car attr) 'set)
77 (or (prop? (cdr attr))
78 (prop? (cadr attr)))))
79 => (lambda (prop)
80 (cadr (assq prop %props-alist))))
81 ((and (pair? attr)
82 (eq? (car attr) 'reset)
83 (or (prop? (cdr attr))
84 (prop? (cadr attr))))
85 => (lambda (prop)
86 (cddr (assq prop %props-alist))))
87 (else (error "Bad attribute" attr))))