| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
(define (color->ansi x) |
| 6 |
(case x |
| 7 |
((bold) "1") |
| 8 |
((dark) "2") |
| 9 |
((italic) "3") |
| 10 |
((underline) "4") |
| 11 |
|
| 12 |
((bold-off) "22") |
| 13 |
((italic-off) "23") |
| 14 |
((underline-off) "24") |
| 15 |
|
| 16 |
((black) "30") |
| 17 |
((red) "31") |
| 18 |
((green) "32") |
| 19 |
((yellow) "33") |
| 20 |
((blue) "34") |
| 21 |
((magenta) "35") |
| 22 |
((cyan) "36") |
| 23 |
((white) "37") |
| 24 |
((default-fg) "39") |
| 25 |
|
| 26 |
((on-black) "40") |
| 27 |
((on-red) "41") |
| 28 |
((on-green) "42") |
| 29 |
((on-yellow) "43") |
| 30 |
((on-blue) "44") |
| 31 |
((on-magenta) "45") |
| 32 |
((on-cyan) "46") |
| 33 |
((on-white) "47") |
| 34 |
((default-bg) "49"))) |
| 35 |
|
| 36 |
(define (ansi-escape color) |
| 37 |
(if (string? color) |
| 38 |
color |
| 39 |
(string-append "\x1B;[" (color->ansi color) "m"))) |
| 40 |
|
| 41 |
(define color (make-state-variable 'color 'default-fg #f)) |
| 42 |
(define background (make-state-variable 'background 'default-bg #f)) |
| 43 |
(define bold (make-state-variable 'bold 'bold-off #f)) |
| 44 |
(define italic (make-state-variable 'bold 'italic-off #f)) |
| 45 |
(define underline (make-state-variable 'bold 'underline-off #f)) |
| 46 |
|
| 47 |
(define (with-attr var new-attr . args) |
| 48 |
(fn ((orig-attr var)) |
| 49 |
(with ((var new-attr)) |
| 50 |
(each (ansi-escape new-attr) |
| 51 |
(each-in-list args) |
| 52 |
(ansi-escape orig-attr))))) |
| 53 |
|
| 54 |
(define (as-bold . args) (with-attr bold 'bold (each-in-list args))) |
| 55 |
(define (as-italic . args) (with-attr italic 'italic (each-in-list args))) |
| 56 |
(define (as-underline . args) (with-attr underline 'underline (each-in-list args))) |
| 57 |
|
| 58 |
(define (as-red . args) (with-attr color 'red (each-in-list args))) |
| 59 |
(define (as-blue . args) (with-attr color 'blue (each-in-list args))) |
| 60 |
(define (as-green . args) (with-attr color 'green (each-in-list args))) |
| 61 |
(define (as-cyan . args) (with-attr color 'cyan (each-in-list args))) |
| 62 |
(define (as-yellow . args) (with-attr color 'yellow (each-in-list args))) |
| 63 |
(define (as-magenta . args) (with-attr color 'magenta (each-in-list args))) |
| 64 |
(define (as-white . args) (with-attr color 'white (each-in-list args))) |
| 65 |
(define (as-black . args) (with-attr color 'black (each-in-list args))) |
| 66 |
|
| 67 |
(define (on-red . args) (with-attr background 'on-red (each-in-list args))) |
| 68 |
(define (on-blue . args) (with-attr background 'on-blue (each-in-list args))) |
| 69 |
(define (on-green . args) (with-attr background 'on-green (each-in-list args))) |
| 70 |
(define (on-cyan . args) (with-attr background 'on-cyan (each-in-list args))) |
| 71 |
(define (on-yellow . args) (with-attr background 'on-yellow (each-in-list args))) |
| 72 |
(define (on-magenta . args) (with-attr background 'on-magenta (each-in-list args))) |
| 73 |
(define (on-white . args) (with-attr background 'on-white (each-in-list args))) |
| 74 |
(define (on-black . args) (with-attr background 'on-black (each-in-list args))) |
| 75 |
|
| 76 |
(define (rgb-escape red-level green-level blue-level bg?) |
| 77 |
(when (not (and (exact-integer? red-level) (<= 0 red-level 5))) |
| 78 |
(error "invalid red-level value" red-level)) |
| 79 |
(when (not (and (exact-integer? green-level) (<= 0 green-level 5))) |
| 80 |
(error "invalid green-level value" green-level)) |
| 81 |
(when (not (and (exact-integer? blue-level) (<= 0 blue-level 5))) |
| 82 |
(error "invalid blue-level value" blue-level)) |
| 83 |
(string-append |
| 84 |
(if bg? "\x1B;[48;5;" "\x1B;[38;5;") |
| 85 |
(number->string (+ (* 36 red-level) (* 6 green-level) blue-level 16)) |
| 86 |
"m")) |
| 87 |
|
| 88 |
(define (rgb24-escape red-level green-level blue-level bg?) |
| 89 |
(when (not (and (exact-integer? red-level) (<= 0 red-level 255))) |
| 90 |
(error "invalid red-level value" red-level)) |
| 91 |
(when (not (and (exact-integer? green-level) (<= 0 green-level 255))) |
| 92 |
(error "invalid green-level value" green-level)) |
| 93 |
(when (not (and (exact-integer? blue-level) (<= 0 blue-level 255))) |
| 94 |
(error "invalid blue-level value" blue-level)) |
| 95 |
(string-append |
| 96 |
(if bg? "\x1B;[48;2;" "\x1B;[38;2;") |
| 97 |
(number->string red-level) ";" |
| 98 |
(number->string green-level) ";" |
| 99 |
(number->string blue-level) |
| 100 |
"m")) |
| 101 |
|
| 102 |
(define (as-color red green blue . fmt) |
| 103 |
(with-attr color (rgb-escape red green blue #f) (each-in-list fmt))) |
| 104 |
|
| 105 |
(define (as-true-color red green blue . fmt) |
| 106 |
(with-attr color (rgb24-escape red green blue #f) (each-in-list fmt))) |
| 107 |
|
| 108 |
(define (on-color red green blue . fmt) |
| 109 |
(with-attr background (rgb-escape red green blue #t) (each-in-list fmt))) |
| 110 |
|
| 111 |
(define (on-true-color red green blue . fmt) |
| 112 |
(with-attr background (rgb24-escape red green blue #t) (each-in-list fmt))) |