blob: 70b821bafff5f659966a5b48082eb5ade9ca61d8 (
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
82
83
84
85
86
87
88
89
|
;;; (yolk colors) --- ansi color escapes
;; "Colors" in this context refers both to actual /colors/, and to text
;; properties like bold, italic, etc.
(import (scheme base)
(scheme write)
(chicken string)
(matchable) ; is this something i want?
(yolk common))
(define (%colors . codes)
(let ((codestr (string-intersperse (map ->string codes) ";")))
(csi codestr "m")))
(define %props-alist
'((reset . "0")
;; name . (on . off)
(bold . ("1" . "22"))
(dim . ("2" . "22"))
(italic . ("3" . "23"))
(underline . ("4" . "24"))
(blink . ("5" . "25"))
(inverse . ("7" . "27"))
(hidden . ("8" . "28"))
(strikethrough . ("9" . "29"))))
(define %colors-alist
'((reset . "0")
;; color . (fg . bg)
(black . ("30" . "40"))
(red . ("31" . "41"))
(green . ("32" . "42"))
(yellow . ("33" . "43"))
(blue . ("34" . "44"))
(magenta . ("35" . "45"))
(cyan . ("36" . "46"))
(white . ("37" . "47"))
(default . ("39" . "49"))))
(define (atom? x)
(and (not (pair? x))
(not (null? x))))
(define (colors-reset . codes)
(cond
((null? codes)
(%colors 0))
(else (apply string-append
(map (lambda (code)
(match code
('fg (%colors (cadr (assq 'default %colors-alist))))
('bg (%colors (cddr (assq 'default %colors-alist))))
(x (%colors (cddr (assq x %props-alist))))))
codes)))))
(define (color? x)
(memq x '(default black red green yellow blue magenta cyan white)))
(define (colors codes)
(let loop ((codes codes)
(acc '()))
(if (null? codes)
(apply %colors (reverse acc))
(loop (cdr codes)
(match (car codes)
((? number? x)
(cons x acc))
((or `(fg ,color)
(? color? color))
(cons (cadr (assq color %colors-alist))
acc))
(`(bg ,color)
(cons (cddr (assq color %colors-alist))
acc))
((or `(set ,prop)
`(,prop on)
(? atom? prop))
(cons (cadr (assq prop %props-alist))
acc))
((or `(reset ,prop)
`(,prop off))
(cons (cddr (assq prop %props-alist)))))))))
(define (with-colors codes thunk)
(dynamic-wind
(lambda () (display (colors codes)))
thunk
(lambda () (display (colors-reset)))))
|