| 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 |
(define nl (displayed "\n")) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
(define fl |
| 69 |
(fn (col) (if (zero? col) nothing nl))) |
| 70 |
|
| 71 |
|
| 72 |
(define (tab-to . o) |
| 73 |
(fn (col pad-char) |
| 74 |
(let* ((tab-width (if (pair? o) (car o) 8)) |
| 75 |
(rem (modulo col tab-width))) |
| 76 |
(if (positive? rem) |
| 77 |
(displayed (make-string (- tab-width rem) pad-char)) |
| 78 |
nothing)))) |
| 79 |
|
| 80 |
|
| 81 |
(define (space-to where) |
| 82 |
(fn (col pad-char) |
| 83 |
(displayed (make-string (max 0 (- where col)) pad-char)))) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
(define (padded/both width . ls) |
| 93 |
(call-with-output |
| 94 |
(each-in-list ls) |
| 95 |
(lambda (str) |
| 96 |
(fn (string-width pad-char) |
| 97 |
(let ((diff (- width (string-width str)))) |
| 98 |
(if (positive? diff) |
| 99 |
(let* ((diff/2 (quotient diff 2)) |
| 100 |
(left (make-string diff/2 pad-char)) |
| 101 |
(right (if (even? diff) |
| 102 |
left |
| 103 |
(make-string (+ 1 diff/2) pad-char)))) |
| 104 |
(each left str right)) |
| 105 |
(displayed str))))))) |
| 106 |
|
| 107 |
|
| 108 |
(define (padded/right width . ls) |
| 109 |
(fn ((col1 col)) |
| 110 |
(each (each-in-list ls) |
| 111 |
(fn ((col2 col) pad-char) |
| 112 |
(displayed (make-string (max 0 (- width (- col2 col1))) |
| 113 |
pad-char)))))) |
| 114 |
|
| 115 |
|
| 116 |
(define (padded/left width . ls) |
| 117 |
(call-with-output |
| 118 |
(each-in-list ls) |
| 119 |
(lambda (str) |
| 120 |
(fn (string-width pad-char) |
| 121 |
(let ((diff (- width (string-width str)))) |
| 122 |
(each (make-string (max 0 diff) pad-char) str)))))) |
| 123 |
|
| 124 |
|
| 125 |
(define padded padded/left) |
| 126 |
|
| 127 |
|
| 128 |
(define (trimmed/buffered width producer proc) |
| 129 |
(call-with-output |
| 130 |
producer |
| 131 |
(lambda (str) |
| 132 |
(fn (string-width) |
| 133 |
(let* ((str-width (string-width str)) |
| 134 |
(diff (- str-width width))) |
| 135 |
(displayed (if (positive? diff) |
| 136 |
(proc str str-width diff) |
| 137 |
str))))))) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
(define (trimmed/right width . ls) |
| 145 |
(trimmed/buffered |
| 146 |
width |
| 147 |
(each-in-list ls) |
| 148 |
(lambda (str str-width diff) |
| 149 |
(fn (ellipsis string-width substring/width substring/preserve) |
| 150 |
(let* ((ell (if (char? ellipsis) (string ellipsis) (or ellipsis ""))) |
| 151 |
(ell-len (string-width ell)) |
| 152 |
(diff (- (+ str-width ell-len) width)) |
| 153 |
(end (- width ell-len))) |
| 154 |
(each (if substring/preserve |
| 155 |
(substring/preserve (substring/width str -1 0)) |
| 156 |
nothing) |
| 157 |
(if (negative? diff) |
| 158 |
nothing |
| 159 |
(substring/width str 0 end)) |
| 160 |
ell |
| 161 |
(if (and substring/preserve (< end str-width)) |
| 162 |
(substring/preserve (substring/width str end str-width)) |
| 163 |
nothing))))))) |
| 164 |
|
| 165 |
|
| 166 |
(define (trimmed/left width . ls) |
| 167 |
(trimmed/buffered |
| 168 |
width |
| 169 |
(each-in-list ls) |
| 170 |
(lambda (str str-width diff) |
| 171 |
(fn (ellipsis string-width substring/width substring/preserve) |
| 172 |
(let* ((ell (if (char? ellipsis) (string ellipsis) (or ellipsis ""))) |
| 173 |
(ell-len (string-width ell)) |
| 174 |
(diff (- (+ str-width ell-len) width))) |
| 175 |
(each (if (and substring/preserve (positive? diff)) |
| 176 |
(substring/preserve (substring/width str 0 diff)) |
| 177 |
nothing) |
| 178 |
ell |
| 179 |
(if (negative? diff) |
| 180 |
nothing |
| 181 |
(substring/width str diff str-width)))))))) |
| 182 |
|
| 183 |
|
| 184 |
(define trimmed trimmed/left) |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
(define (trimmed/both width . ls) |
| 190 |
(trimmed/buffered |
| 191 |
width |
| 192 |
(each-in-list ls) |
| 193 |
(lambda (str str-width diff) |
| 194 |
(fn (ellipsis string-width substring/width substring/preserve) |
| 195 |
(let* ((ell (if (char? ellipsis) (string ellipsis) (or ellipsis ""))) |
| 196 |
(ell-len (string-width ell)) |
| 197 |
(diff (- (+ str-width ell-len ell-len) width)) |
| 198 |
(left (quotient diff 2)) |
| 199 |
(right (- str-width (quotient (+ diff 1) 2)))) |
| 200 |
(each |
| 201 |
(if substring/preserve |
| 202 |
(substring/preserve (substring/width str 0 left)) |
| 203 |
nothing) |
| 204 |
(if (negative? diff) |
| 205 |
ell |
| 206 |
(each ell (substring/width str left right) ell)) |
| 207 |
(if substring/preserve |
| 208 |
(substring/preserve (substring/width str right str-width)) |
| 209 |
nothing))))))) |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
(define (trimmed/lazy width . ls) |
| 217 |
(fn ((orig-output output) string-width substring/width) |
| 218 |
(call-with-current-continuation |
| 219 |
(lambda (return) |
| 220 |
(let ((chars-written 0) |
| 221 |
(orig-output (or orig-output output-default))) |
| 222 |
(define (output* str) |
| 223 |
(let ((len (string-width str))) |
| 224 |
(set! chars-written (+ chars-written len)) |
| 225 |
(if (> chars-written width) |
| 226 |
(let* ((end (max 0 (- len (- chars-written width)))) |
| 227 |
(s (substring/width str 0 end))) |
| 228 |
(each (orig-output s) |
| 229 |
(with! (output orig-output)) |
| 230 |
(fn () (return nothing)))) |
| 231 |
(orig-output str)))) |
| 232 |
(with ((output output*)) |
| 233 |
(each-in-list ls))))))) |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
(define (fitted/right width . ls) |
| 239 |
(padded/right width (trimmed/right width (each-in-list ls)))) |
| 240 |
|
| 241 |
|
| 242 |
(define (fitted/left width . ls) |
| 243 |
(padded/left width (trimmed/left width (each-in-list ls)))) |
| 244 |
|
| 245 |
|
| 246 |
(define fitted fitted/left) |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
(define (fitted/both width . ls) |
| 251 |
(padded/both width (trimmed/both width (each-in-list ls)))) |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
(define (joined/general elt-f last-f dot-f init-ls sep) |
| 257 |
(fn () |
| 258 |
(let lp ((ls init-ls)) |
| 259 |
(cond |
| 260 |
((pair? ls) |
| 261 |
(each (if (eq? ls init-ls) nothing sep) |
| 262 |
((if (and last-f (null? (cdr ls))) last-f elt-f) (car ls)) |
| 263 |
(lp (cdr ls)))) |
| 264 |
((and dot-f (not (null? ls))) |
| 265 |
(each (if (eq? ls init-ls) nothing sep) (dot-f ls))) |
| 266 |
(else |
| 267 |
nothing))))) |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
(define (joined elt-f ls . o) |
| 275 |
(joined/general elt-f #f #f ls (if (pair? o) (car o) ""))) |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
(define (joined/prefix elt-f ls . o) |
| 280 |
(if (null? ls) |
| 281 |
nothing |
| 282 |
(let ((sep (if (pair? o) (car o) ""))) |
| 283 |
(each sep (joined elt-f ls sep))))) |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
(define (joined/suffix elt-f ls . o) |
| 288 |
(if (null? ls) |
| 289 |
nothing |
| 290 |
(let ((sep (if (pair? o) (car o) ""))) |
| 291 |
(each (joined elt-f ls sep) sep)))) |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
(define (joined/last elt-f last-f ls . o) |
| 297 |
(joined/general elt-f last-f #f ls (if (pair? o) (car o) ""))) |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
(define (joined/dot elt-f dot-f ls . o) |
| 302 |
(joined/general elt-f #f dot-f ls (if (pair? o) (car o) ""))) |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
(define (joined/range elt-f start . o) |
| 309 |
(let ((end (and (pair? o) (car o))) |
| 310 |
(sep (if (and (pair? o) (pair? (cdr o))) (cadr o) ""))) |
| 311 |
(let lp ((i start)) |
| 312 |
(if (and end (>= i end)) |
| 313 |
nothing |
| 314 |
(each (if (> i start) sep nothing) |
| 315 |
(elt-f i) |
| 316 |
(fn () (lp (+ i 1)))))))) |