-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorify.jule
60 lines (51 loc) · 1.17 KB
/
colorify.jule
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
use "std/fmt"
struct Style {
Foreground: Foreground
Background: Background
Fonts: []Font
Graphics: []Graphic
foregroundRGB: str
backgroundRGB: str
}
impl Style {
fn ForegroundRGB(mut self, r: i16, g: i16, b: i16)!: Style {
self.foregroundRGB = rgbToStr(r, g, b, "38") else {
error(error)
}
ret self
}
fn BackgroundRGB(mut self, r: i16, g: i16, b: i16)!: Style {
self.backgroundRGB = rgbToStr(r, g, b, "48") else {
error(error)
}
ret self
}
}
fn rgbToStr(r: i16, g: i16, b: i16, t: str)!: str {
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
error("RGB value must not be less than 0 or greater than 255")
}
ret fmt::Format("\033[{};2;{};{};{}m", t, r, g, b)
}
fn Colorify(s: str, style: Style): str {
let mut new_s: str = ""
for _, font in style.Fonts {
new_s += str(font)
}
for _, graphic in style.Graphics {
new_s += str(graphic)
}
if style.foregroundRGB != "" {
new_s += str(style.foregroundRGB)
} else {
new_s += str(style.Foreground)
}
if style.backgroundRGB != "" {
new_s += str(style.backgroundRGB)
} else {
new_s += str(style.Background)
}
new_s += s
new_s += str(Graphic.Reset)
ret new_s
}