-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromatic.gleam
300 lines (252 loc) · 6.62 KB
/
chromatic.gleam
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//// # Chromatic
//// Composable text formatting for CLI in Gleam.
////
//// ## Usage
////
//// The best way to use this is through Gleam's pipelines.
////
//// Check it out!
////
//// ```gleam
//// import chromatic.{bold, italic, magenta, rainbow}
////
//// pub fn main() {
//// io.println(
//// "Hello "
//// <> "Gleam"
//// |> magenta
//// |> italic
//// |> bold
//// <> ", this is "
//// <> "Chromatic"
//// |> rainbow
//// |> bold,
//// )
//// }
////```
////
import gleam/string
import gleam/list
const ansi_escape = "\u{001b}["
const ansi_clear = "\u{001b}[0m"
/// ANSI escape helper, adds a ANSI code to the provided string.
/// Unlike the `decorate_ansi` function not terminate with the clear sequence.
pub fn decorate_ansi_raw(string: String, code: String) {
ansi_escape <> code <> "m" <> string
}
/// ANSI escape helper, adds a ANSI code to the provided string.
/// This also terminates the string with the clear sequence so it doesn't affect
/// other strings.
pub fn decorate_ansi(string: String, code: String) {
decorate_ansi_raw(string, code)
|> auto_clear
}
fn auto_clear(string: String) {
let result = case string.ends_with(string, ansi_clear) {
True -> string
False -> string <> ansi_clear
}
result
}
/// Set text color to black.
pub fn black(string: String) {
decorate_ansi(string, "30")
}
/// Set text color to red.
pub fn red(string: String) {
decorate_ansi(string, "31")
}
/// Set text color to green.
pub fn green(string: String) {
decorate_ansi(string, "32")
}
/// Set text color to yellow.
pub fn yellow(string: String) {
decorate_ansi(string, "33")
}
/// Set text color to blue.
pub fn blue(string: String) {
decorate_ansi(string, "34")
}
/// Set text color to magenta.
pub fn magenta(string: String) {
decorate_ansi(string, "35")
}
/// Set text color to cyan.
pub fn cyan(string: String) {
decorate_ansi(string, "36")
}
/// Set text color to an off-white color,
/// for a lighter white try `bright_white`
pub fn white(string: String) {
decorate_ansi(string, "37")
}
/// Set text color to gray.
pub fn gray(string: String) {
decorate_ansi(string, "90")
}
/// Set text color to a lighter red.
pub fn bright_red(string: String) {
decorate_ansi(string, "91")
}
/// Set text color to a lighter green.
pub fn bright_green(string: String) {
decorate_ansi(string, "92")
}
/// Set text color to a lighter yellow.
pub fn bright_yellow(string: String) {
decorate_ansi(string, "93")
}
/// Set text color to a lighter blue.
pub fn bright_blue(string: String) {
decorate_ansi(string, "94")
}
/// Set text color to a lighter magenta.
pub fn bright_magenta(string: String) {
decorate_ansi(string, "95")
}
/// Set text color to a lighter cyan.
pub fn bright_cyan(string: String) {
decorate_ansi(string, "96")
}
/// Set text color to a bright white color
pub fn bright_white(string: String) {
decorate_ansi(string, "97")
}
/// Set background color to black.
pub fn bg_black(string: String) {
decorate_ansi(string, "40")
}
/// Set background color to red.
pub fn bg_red(string: String) {
decorate_ansi(string, "41")
}
/// Set background color to green.
pub fn bg_green(string: String) {
decorate_ansi(string, "42")
}
/// Set background color to yellow.
pub fn bg_yellow(string: String) {
decorate_ansi(string, "43")
}
/// Set background color to blue.
pub fn bg_blue(string: String) {
decorate_ansi(string, "44")
}
/// Set background color to magenta.
pub fn bg_magenta(string: String) {
decorate_ansi(string, "45")
}
/// Set background color to cyan.
pub fn bg_cyan(string: String) {
decorate_ansi(string, "46")
}
/// Set's background to an off-white color,
/// for a lighter white try `bg_bright_white`
pub fn bg_white(string: String) {
decorate_ansi(string, "47")
}
/// Set background color to gray.
pub fn bg_gray(string: String) {
decorate_ansi(string, "100")
}
/// Set background color to a lighter red.
pub fn bg_bright_red(string: String) {
decorate_ansi(string, "101")
}
/// Set background color to a lighter green.
pub fn bg_bright_green(string: String) {
decorate_ansi(string, "102")
}
/// Set background color to a lighter yellow.
pub fn bg_bright_yellow(string: String) {
decorate_ansi(string, "103")
}
/// Set background color to a lighter blue.
pub fn bg_bright_blue(string: String) {
decorate_ansi(string, "104")
}
/// Set background color to a lighter magenta.
pub fn bg_bright_magenta(string: String) {
decorate_ansi(string, "105")
}
/// Set background color to a lighter cyan.
pub fn bg_bright_cyan(string: String) {
decorate_ansi(string, "106")
}
/// Set background color to a bright white color
pub fn bg_bright_white(string: String) {
decorate_ansi(string, "107")
}
/// Set text style to bold
pub fn bold(string: String) {
decorate_ansi(string, "1")
}
/// Set text style to italic
pub fn italic(string: String) {
decorate_ansi(string, "3")
}
/// Set text style to underlined
pub fn underline(string: String) {
decorate_ansi(string, "4")
}
/// Propagate style from this string until next reset.
/// Must be the last function in the pipeline.
pub fn bleed(string: String) {
let result = case string.ends_with(string, ansi_clear) {
True -> string.drop_right(from: string, up_to: string.length(ansi_clear))
False -> string
}
result
}
/// Print this to clear any styles that are bleeding through.
pub fn clear_str() {
ansi_clear
}
/// Clears any previously added styles that would have bled into it.
///
/// Only necessary if you've used `bleed`.
///
/// Like `bleed` needs to be the last function in the pipeline
pub fn clear(string: String) {
ansi_clear <> string
}
/// Rainbow text with an integer offset
pub fn offset_rainbow(string: String, offset: Int) {
let #(str, _, _) =
string.to_graphemes(string)
|> list.fold(#("", False, offset), fn(acc, char) {
let #(str, in, color) = acc
let new_in = case in {
True -> char != "m"
False -> char == "\u{001b}"
}
let maybe_color = case in || new_in {
True -> #(str <> char, new_in, color)
False -> {
let colored = case color % 6 {
// Red
0 -> decorate_ansi_raw(char, "31")
// Yellow
1 -> decorate_ansi_raw(char, "33")
// Green
2 -> decorate_ansi_raw(char, "32")
// Cyan
3 -> decorate_ansi_raw(char, "36")
// Blue
4 -> decorate_ansi_raw(char, "34")
// Magenta
5 -> decorate_ansi_raw(char, "35")
_ -> ""
}
#(str <> colored, new_in, color + 1)
}
}
maybe_color
})
str
}
// Rainbow text!
pub fn rainbow(string: String) {
offset_rainbow(string, 0)
}