Skip to content

Commit 850d697

Browse files
committed
FEAT: improved FORMAT dialect so one can specify escape color codes (using money! value)
Simple usage example: ``` print format [$31.47 "some text" $0] "" ``` would print red text on gray background (depending on color palette of the terminal) The string returned by format function would look like: "^[[32;47msome text^[[0m"
1 parent 1e36d12 commit 850d697

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/mezz/mezz-banner.r

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,27 @@ make-banner: func [
1717
][
1818
if string? fmt [return fmt] ; aleady built
1919
str: make string! 200
20-
star: append/dup make string! 74 #"*" 74
21-
spc: format ["**" 70 "**"] ""
22-
append str "^[[7m"
20+
star: format/pad [$30.107 74 $0] "" #"*"
21+
22+
spc: format [$30.107 "**" 70 "**" $0] ""
2323
parse fmt [
2424
some [
2525
[
26-
set a string! (s: format ["** " 68 "**"] a)
26+
set a string! (s: format [$30.107 "** " $35 68 $30.107 "**" $0] a)
2727
| '= set a [string! | word! | set-word!] [
2828
b:
2929
path! (b: get b/1)
3030
| word! (b: get b/1)
3131
| block! (b: reform b/1)
3232
| string! (b: b/1)
3333
]
34-
(s: either none? b [none][format ["** " 11 55 "**"] reduce [a b]])
34+
(s: either none? b [none][format [$30.107 "** " $32 11 $31 55 $30 "**" $0] reduce [a b]])
3535
| '* (s: star)
3636
| '- (s: spc)
3737
]
3838
(unless none? s [append append str s newline])
3939
]
4040
]
41-
append str "^[[0m"
4241
str
4342
]
4443

src/mezz/mezz-series.r

+5-3
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ collect: func [
362362

363363
format: function [
364364
"Format a string according to the format dialect."
365-
rules {A block in the format dialect. E.g. [10 -10 #"-" 4]}
365+
rules {A block in the format dialect. E.g. [10 -10 #"-" 4 $32 "green" $0]}
366366
values
367-
/pad p
367+
/pad p {Pattern to use instead of spaces}
368368
][
369369
p: any [p #" "]
370370
unless block? :rules [rules: reduce [:rules]]
@@ -376,8 +376,9 @@ format: function [
376376
if word? :rule [rule: get rule]
377377
val: val + switch/default type?/word :rule [
378378
integer! [abs rule]
379-
string! [length? rule]
379+
string! [length? rule]
380380
char! [1]
381+
money! [2 + length? form rule]
381382
][0]
382383
]
383384

@@ -402,6 +403,7 @@ format: function [
402403
]
403404
string! [out: change out rule]
404405
char! [out: change out rule]
406+
money! [out: change out replace rejoin ["^[[" next form rule #"m"] #"." #";"]
405407
]
406408
]
407409

0 commit comments

Comments
 (0)