-
Notifications
You must be signed in to change notification settings - Fork 0
/
color-print.jai
208 lines (175 loc) · 6.39 KB
/
color-print.jai
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/// Colored console output
// @Todo(Judah): Windows shouldn't rely on ANSI sequences sadly...
// @Todo(Judah): Use new consume/view API
// @Todo(Judah): Support unicode?
// @Note(Judah): Maybe make 'format_string' $$ so we can do search/replace at compile-time
color_print :: (format_string: string, args: ..Any, stderr := false) -> int {
builder: String_Builder;
builder.allocator = temporary_allocator;
print_to_builder(*builder, format_string, ..args);
pre_expanded := builder_to_string(*builder, allocator = temporary_allocator, do_reset = true);
cursor := 0;
while cursor < pre_expanded.count {
chr := pre_expanded.data[cursor];
if chr == {
case #char "@";
cursor += 1;
// Escaped '@'
if cursor < pre_expanded.count && pre_expanded[cursor] == #char "@" {
cursor += 1;
append(*builder, #char "@");
continue;
}
if cursor >= pre_expanded.count && context.print_style.log_runtime_errors {
log_error("Expected style declaration after @ at character %", cursor);
return 0;
}
fore: Foreground_Color;
back: Background_Color;
styles: Text_Style;
style := pre_expanded;
style.data += cursor;
style.count = 0;
// Get all styles for the next string
set_background := false;
while cursor < pre_expanded.count {
chr := pre_expanded.data[cursor];
if chr == #char "," || chr == #char "{" {
if style == {
case "black";
if set_background then back = .Black; else fore = .Black;
set_background = !set_background;
case "red";
if set_background then back = .Red; else fore = .Red;
set_background = !set_background;
case "green";
if set_background then back = .Green; else fore = .Green;
set_background = !set_background;
case "blue";
if set_background then back = .Blue; else fore = .Blue;
set_background = !set_background;
case "yellow"; #through;
case "orange";
if set_background then back = .Orange; else fore = .Orange;
set_background = !set_background;
case "bold";
styles = .Bold;
case "dim";
styles = .Dim;
case "italic";
styles = .Italic;
case "ul"; #through;
case "underline";
styles = .Underline;
case "blink";
styles = .Blink;
case "invert";
styles = .Invert;
case "strike";
styles = .Strike;
case;
if (!style.count || style == "_") && styles == 0 {
set_background = true;
}
else if context.print_style.log_runtime_errors {
log_error("Unknown style directive '%' at character %", style, cursor);
return -1;
}
}
if chr == #char "{" {
cursor += 1;
break;
}
cursor += 1;
style.data += style.count + 1;
style.count = 0;
continue;
}
cursor += 1;
style.count += 1;
}
// Push the style and inner string
append(*builder, "\e[");
if fore then print_to_builder(*builder, "%", cast(u8)fore);
if back then print_to_builder(*builder, ";%", cast(u8)back);
if styles then print_to_builder(*builder, ";%", cast(u8)styles);
append(*builder, "m");
while cursor < pre_expanded.count {
chr := pre_expanded.data[cursor];
if chr == {
case #char "\\";
cursor += 1;
if cursor >= pre_expanded.count && context.print_style.log_runtime_errors {
log_error("Empty escape sequence at character %", cursor);
return -1;
}
if pre_expanded.data[cursor] == {
case #char "{"; #through;
case #char "}";
append(*builder, pre_expanded.data[cursor]);
cursor += 1;
continue;
}
case #char "}";
break;
}
append(*builder, chr);
cursor += 1;
}
append(*builder, "\e[0;m");
case;
append(*builder, chr);
}
cursor += 1;
}
return write_builder(*builder, to_standard_error = stderr);
} @PrintLike
/// This allows the context.logger to be routed through 'color_print'
color_logger :: (message: string, data: *void, info: Log_Info) {
stderr: bool;
prefix: string;
if info.common_flags & .ERROR {
stderr = true;
prefix = "@bold,red{.. error}";
}
else if info.common_flags & .WARNING {
prefix = "@bold,yellow{.. warn}";
}
else {
prefix = "@bold{.. info}";
}
msg := message;
if msg.count && msg[msg.count - 1] == #char "\n" {
msg.count -= 1;
}
color_print(tprint("% %\n", prefix, msg), stderr = stderr);
}
Foreground_Color :: enum u8 {
Black :: 30;
Red :: 31;
Green :: 32;
Orange :: 33;
Blue :: 34;
}
Background_Color :: enum u8 {
Black :: 40;
Red :: 41;
Green :: 42;
Orange :: 43;
Blue :: 44;
}
Text_Style :: enum u8 {
Bold :: 1;
Dim :: 2;
Italic :: 3;
Underline :: 4;
Blink :: 5;
Invert :: 7;
Strike :: 9;
}
#scope_file
is_letter :: (chr: u8) -> bool {
return chr >= #char "a" && chr <= #char "z" ||
chr >= #char "A" && chr <= #char "Z";
}
#import "Basic";