| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
(define (unicode-char-width ch ambiguous-is-wide?) |
| 6 |
(let ((ci (char->integer ch))) |
| 7 |
(cond |
| 8 |
((char-set:zero-width? ci) |
| 9 |
0) |
| 10 |
((char-set:full-width? ci) |
| 11 |
2) |
| 12 |
((and ambiguous-is-wide? (char-set:ambiguous-width? ci)) |
| 13 |
2) |
| 14 |
(else |
| 15 |
1)))) |
| 16 |
|
| 17 |
(define (string-terminal-width/aux str start end ambiguous-is-wide?) |
| 18 |
(let lp1 ((sc start) (width 0)) |
| 19 |
(if (string-cursor>=? sc end) |
| 20 |
width |
| 21 |
(let ((c (string-ref/cursor str sc)) |
| 22 |
(sc2 (string-cursor-next str sc))) |
| 23 |
(cond |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
((and (= 27 (char->integer c)) |
| 29 |
(string-cursor<? sc2 end) |
| 30 |
(eqv? #\[ (string-ref/cursor str sc2))) |
| 31 |
(let lp2 ((sc (string-cursor-forward str sc 2))) |
| 32 |
(cond ((string-cursor>=? sc end) width) |
| 33 |
((memv (string-ref/cursor str sc) '(#\m #\newline)) |
| 34 |
(lp1 (string-cursor-next str sc) width)) |
| 35 |
(else (lp2 (string-cursor-next str sc)))))) |
| 36 |
|
| 37 |
((char<=? c #\~) |
| 38 |
(lp1 sc2 (+ width 1))) |
| 39 |
|
| 40 |
(else |
| 41 |
(lp1 sc2 (+ width (unicode-char-width c ambiguous-is-wide?))) |
| 42 |
)))))) |
| 43 |
|
| 44 |
(define (cursor-arg str x) |
| 45 |
(if (string-cursor? x) x (string-index->cursor str x))) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
(define (string-terminal-width str . o) |
| 50 |
(let ((start (cursor-arg str (if (pair? o) |
| 51 |
(car o) |
| 52 |
(string-cursor-start str)))) |
| 53 |
(end (cursor-arg str (if (and (pair? o) (pair? (cdr o))) |
| 54 |
(cadr o) |
| 55 |
(string-cursor-end str))))) |
| 56 |
(string-terminal-width/aux str start end #f))) |
| 57 |
|
| 58 |
(define (string-terminal-width/wide str . o) |
| 59 |
(let ((start (cursor-arg str (if (pair? o) |
| 60 |
(car o) |
| 61 |
(string-cursor-start str)))) |
| 62 |
(end (cursor-arg str (if (and (pair? o) (pair? (cdr o))) |
| 63 |
(cadr o) |
| 64 |
(string-cursor-end str))))) |
| 65 |
(string-terminal-width/aux str start end #t))) |
| 66 |
|
| 67 |
(define (substring-terminal-width/aux str lo hi ambiguous-is-wide?) |
| 68 |
(let ((start (string-cursor-start str)) |
| 69 |
(end (string-cursor-end str))) |
| 70 |
(let lp1 ((sc start) |
| 71 |
(from (and (negative? lo) start)) |
| 72 |
(width 0)) |
| 73 |
(if (string-cursor>=? sc end) |
| 74 |
(if from (substring/cursors str from end) str) |
| 75 |
(let ((c (string-ref/cursor str sc))) |
| 76 |
(cond |
| 77 |
((and (= 27 (char->integer c)) |
| 78 |
(string-cursor<? (string-cursor-next str sc) end) |
| 79 |
(eqv? #\[ (string-ref/cursor |
| 80 |
str |
| 81 |
(string-cursor-next str sc)))) |
| 82 |
(let lp2 ((sc2 (string-cursor-forward str sc 2))) |
| 83 |
(cond ((string-cursor>=? sc2 end) |
| 84 |
(lp1 sc2 from width)) |
| 85 |
((memv (string-ref/cursor str sc2) '(#\m #\newline)) |
| 86 |
(lp1 (string-cursor-next str sc2) from width)) |
| 87 |
(else (lp2 (string-cursor-next str sc2)))))) |
| 88 |
(else |
| 89 |
(let ((width2 (+ width |
| 90 |
(unicode-char-width c ambiguous-is-wide?)))) |
| 91 |
(cond |
| 92 |
((> width2 hi) |
| 93 |
(if from |
| 94 |
(substring/cursors str from sc) |
| 95 |
"")) |
| 96 |
((and (not from) (> width2 lo)) |
| 97 |
(lp1 (string-cursor-next str sc) sc width2)) |
| 98 |
(else |
| 99 |
(lp1 (string-cursor-next str sc) from width2) |
| 100 |
)))))))))) |
| 101 |
|
| 102 |
(define (substring-terminal-width str lo hi) |
| 103 |
(substring-terminal-width/aux str lo hi #f)) |
| 104 |
|
| 105 |
(define (substring-terminal-width/wide str lo hi) |
| 106 |
(substring-terminal-width/aux str lo hi #t)) |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
(define non-local-controls |
| 116 |
'(#\x061C #\x200E #\x200F #\x202A #\x202B #\x202C |
| 117 |
#\x202D #\x202E #\x2066 #\x2067 #\x2068 #\x2069)) |
| 118 |
|
| 119 |
(define (substring-terminal-preserve str) |
| 120 |
(let ((start (string-cursor-start str)) |
| 121 |
(end (string-cursor-end str))) |
| 122 |
(let lp1 ((sc start) (escapes '())) |
| 123 |
(if (string-cursor>=? sc end) |
| 124 |
(string-concatenate-reverse escapes) |
| 125 |
(let ((c (string-ref/cursor str sc)) |
| 126 |
(sc2 (string-cursor-next str sc))) |
| 127 |
(cond |
| 128 |
((and (= 27 (char->integer c)) |
| 129 |
(string-cursor<? sc2 end) |
| 130 |
(eqv? #\[ (string-ref/cursor str sc2))) |
| 131 |
(let lp2 ((sc2 (string-cursor-next str sc2))) |
| 132 |
(if (string-cursor>=? sc2 end) |
| 133 |
(string-concatenate-reverse escapes) |
| 134 |
(let ((c2 (string-ref/cursor str sc2)) |
| 135 |
(sc3 (string-cursor-next str sc2))) |
| 136 |
(if (eqv? #\m c2) |
| 137 |
(lp1 sc3 |
| 138 |
(cons (substring/cursors str sc sc3) |
| 139 |
escapes)) |
| 140 |
(lp2 sc3)))))) |
| 141 |
((and (memv c non-local-controls)) |
| 142 |
(lp1 sc2 (cons (string c) escapes))) |
| 143 |
(else |
| 144 |
(lp1 sc2 escapes)))))))) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
(define (terminal-aware . args) |
| 149 |
(fn (ambiguous-is-wide?) |
| 150 |
(with ((string-width (if ambiguous-is-wide? |
| 151 |
string-terminal-width/wide |
| 152 |
string-terminal-width)) |
| 153 |
(substring/width (if ambiguous-is-wide? |
| 154 |
substring-terminal-width/wide |
| 155 |
substring-terminal-width)) |
| 156 |
(substring/preserve substring-terminal-preserve)) |
| 157 |
(each-in-list args)))) |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
(define (with-string-transformer proc . ls) |
| 163 |
(fn ((orig-output output)) |
| 164 |
(let ((output* (lambda (str) (orig-output (proc str))))) |
| 165 |
(with ((output output*)) |
| 166 |
(each-in-list ls))))) |
| 167 |
|
| 168 |
|
| 169 |
(define (upcased . ls) (apply with-string-transformer string-upcase ls)) |
| 170 |
|
| 171 |
|
| 172 |
(define (downcased . ls) (apply with-string-transformer string-downcase ls)) |