Skip to content

Commit

Permalink
FEAT: improved FORMAT dialect so one can specify escape color codes (…
Browse files Browse the repository at this point in the history
…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"
  • Loading branch information
Oldes committed Oct 11, 2018
1 parent 1e36d12 commit 850d697
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/mezz/mezz-banner.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,27 @@ make-banner: func [
][
if string? fmt [return fmt] ; aleady built
str: make string! 200
star: append/dup make string! 74 #"*" 74
spc: format ["**" 70 "**"] ""
append str "^[[7m"
star: format/pad [$30.107 74 $0] "" #"*"

spc: format [$30.107 "**" 70 "**" $0] ""
parse fmt [
some [
[
set a string! (s: format ["** " 68 "**"] a)
set a string! (s: format [$30.107 "** " $35 68 $30.107 "**" $0] a)
| '= set a [string! | word! | set-word!] [
b:
path! (b: get b/1)
| word! (b: get b/1)
| block! (b: reform b/1)
| string! (b: b/1)
]
(s: either none? b [none][format ["** " 11 55 "**"] reduce [a b]])
(s: either none? b [none][format [$30.107 "** " $32 11 $31 55 $30 "**" $0] reduce [a b]])
| '* (s: star)
| '- (s: spc)
]
(unless none? s [append append str s newline])
]
]
append str "^[[0m"
str
]

Expand Down
8 changes: 5 additions & 3 deletions src/mezz/mezz-series.r
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ collect: func [

format: function [
"Format a string according to the format dialect."
rules {A block in the format dialect. E.g. [10 -10 #"-" 4]}
rules {A block in the format dialect. E.g. [10 -10 #"-" 4 $32 "green" $0]}
values
/pad p
/pad p {Pattern to use instead of spaces}
][
p: any [p #" "]
unless block? :rules [rules: reduce [:rules]]
Expand All @@ -376,8 +376,9 @@ format: function [
if word? :rule [rule: get rule]
val: val + switch/default type?/word :rule [
integer! [abs rule]
string! [length? rule]
string! [length? rule]
char! [1]
money! [2 + length? form rule]
][0]
]

Expand All @@ -402,6 +403,7 @@ format: function [
]
string! [out: change out rule]
char! [out: change out rule]
money! [out: change out replace rejoin ["^[[" next form rule #"m"] #"." #";"]
]
]

Expand Down

0 comments on commit 850d697

Please sign in to comment.