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
|
/******************************************************************************
__ ____ __
/ / ___ ____ _/ __/_____________ _/ /__
/ / / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \
/ /___/ __/ /_/ / __(__ ) /__/ /_/ / / __/
/_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/
(C)opyright 2025, Leafscale, LLC - https://www.leafscale.com
Project: Zygaena
Filename: color.reef
Authors: Chris Tusa <chris.tusa@leafscale.com>
License: <see LICENSE file included with this source code>
Description: Terminal color utilities for Coral
******************************************************************************/
module util.color
import sys.args
import sys.env
import sys.platform.runtime as runtime
export
// Color functions - wrap text in ANSI color codes
fn bold(s: string): string
fn dim(s: string): string
fn red(s: string): string
fn green(s: string): string
fn yellow(s: string): string
fn blue(s: string): string
fn cyan(s: string): string
fn magenta(s: string): string
// Formatted output helpers
proc print_error(msg: string)
proc print_success(msg: string)
proc print_warning(msg: string)
proc print_info(msg: string)
proc print_action(msg: string)
end export
// ANSI escape code prefix: ESC[ (ASCII 27)
// Reset code: ESC[0m
// Helper to convert int to char (for ESC character)
fn int_to_char(n: int): char
unsafe
let result: char = n
return result
end unsafe
end int_to_char
// Get ESC character (ASCII 27) as a single-character string
fn get_esc(): string
unsafe
let result: string = runtime.reef_alloc_string_buffer(2)
result[0] = int_to_char(27)
return result
end unsafe
end get_esc
// Check if color output should be disabled
// Respects --no-color flag and NO_COLOR environment variable
fn no_color(): bool
if args.has_flag("no-color")
return true
end if
if env.has_env("NO_COLOR")
return true
end if
return false
end no_color
// Helper to wrap text with ANSI codes
fn wrap_color(s: string, code: string): string
if no_color()
return s
end if
// ESC[ + code + m + text + ESC[0m
let esc = get_esc()
return esc + "[" + code + "m" + s + esc + "[0m"
end wrap_color
// Bold text (code 1)
fn bold(s: string): string
return wrap_color(s, "1")
end bold
// Dim text (code 2)
fn dim(s: string): string
return wrap_color(s, "2")
end dim
// Red text (code 31)
fn red(s: string): string
return wrap_color(s, "31")
end red
// Green text (code 32)
fn green(s: string): string
return wrap_color(s, "32")
end green
// Yellow text (code 33)
fn yellow(s: string): string
return wrap_color(s, "33")
end yellow
// Blue text (code 34)
fn blue(s: string): string
return wrap_color(s, "34")
end blue
// Magenta text (code 35)
fn magenta(s: string): string
return wrap_color(s, "35")
end magenta
// Cyan text (code 36)
fn cyan(s: string): string
return wrap_color(s, "36")
end cyan
// Print error message with red X prefix
proc print_error(msg: string)
if no_color()
println("ERROR: " + msg)
else
println(red("error:") + " " + msg)
end if
end print_error
// Print success message with green checkmark prefix
proc print_success(msg: string)
if no_color()
println("OK: " + msg)
else
println(green("ok:") + " " + msg)
end if
end print_success
// Print warning message with yellow prefix
proc print_warning(msg: string)
if no_color()
println("WARNING: " + msg)
else
println(yellow("warning:") + " " + msg)
end if
end print_warning
// Print info message with blue arrow prefix
proc print_info(msg: string)
if no_color()
println("-> " + msg)
else
println(blue("->") + " " + msg)
end if
end print_info
// Print action message with bold arrow prefix
proc print_action(msg: string)
if no_color()
println("=> " + msg)
else
println(bold("=>") + " " + msg)
end if
end print_action
end module
|