-
Notifications
You must be signed in to change notification settings - Fork 31
/
term_color.rs
168 lines (158 loc) Β· 4.9 KB
/
term_color.rs
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
use std::env;
use term::terminfo::TermInfo;
#[derive(PartialEq, Clone, Copy)]
pub enum Color {
Reset,
Red,
Green,
Gray,
Yellow,
Orange,
Blue,
Purple,
Cyan,
RedBg,
YellowBg,
OrangeBg,
NonText,
Invert,
}
impl Color {
pub fn has_bg_color(self) -> bool {
use Color::*;
matches!(self, YellowBg | RedBg | OrangeBg)
}
}
#[inline]
fn true_colors_sequence(color: Color) -> &'static [u8] {
macro_rules! rgb_color {
(fg, $r:expr, $g:expr, $b:expr) => {
concat!("\x1b[38;2;", $r, ';', $g, ';', $b, "m")
};
(bg, $r:expr, $g:expr, $b:expr) => {
concat!("\x1b[48;2;", $r, ';', $g, ';', $b, "m")
};
}
use Color::*;
match color {
Reset => concat!(
"\x1b[39;0m",
rgb_color!(fg, 0xfb, 0xf1, 0xc7),
rgb_color!(bg, 0x28, 0x28, 0x28),
)
.as_bytes(),
Red => rgb_color!(fg, 0xfb, 0x49, 0x34).as_bytes(),
Green => rgb_color!(fg, 0xb8, 0xbb, 0x26).as_bytes(),
Gray => rgb_color!(fg, 0xa8, 0x99, 0x84).as_bytes(),
Yellow => rgb_color!(fg, 0xfa, 0xbd, 0x2f).as_bytes(),
Orange => rgb_color!(fg, 0xfe, 0x80, 0x19).as_bytes(),
Blue => rgb_color!(fg, 0x83, 0xa5, 0x98).as_bytes(),
Purple => rgb_color!(fg, 0xd3, 0x86, 0x9b).as_bytes(),
Cyan => rgb_color!(fg, 0x8e, 0xc0, 0x7c).as_bytes(),
RedBg => concat!(
rgb_color!(fg, 0xfb, 0xf1, 0xc7),
rgb_color!(bg, 0xcc, 0x24, 0x1d),
)
.as_bytes(),
YellowBg => concat!(
rgb_color!(fg, 0x28, 0x28, 0x28),
rgb_color!(bg, 0xd7, 0x99, 0x21),
)
.as_bytes(),
OrangeBg => concat!(
rgb_color!(fg, 0x28, 0x28, 0x28),
rgb_color!(bg, 0xd6, 0x5d, 0x0e),
)
.as_bytes(),
NonText => rgb_color!(fg, 0x66, 0x5c, 0x54).as_bytes(),
Invert => b"\x1b[7m",
}
}
// From color palette of gruvbox: https://github.com/morhetz/gruvbox#palette
//
// 'm' sets attributes to text printed after: https://vt100.net/docs/vt100-ug/chapter3.html#SGR
// Color table: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
//
// 256 colors sequences are '\x1b[38;5;<n>m' (for fg) or '\x1b[48;5;<n>m (for bg)
// https://www.xfree86.org/current/ctlseqs.html
//
// 24bit colors sequences are '\x1b[38;2;<r>;<g>;<b>m' (for fg) or '\x1b[48;2;<r>;<g>;<b>m' (for fg)
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
#[inline]
fn colors_256_sequence(color: Color) -> &'static [u8] {
use Color::*;
match color {
Reset => b"\x1b[39;0m\x1b[38;5;230m\x1b[48;5;235m",
Red => b"\x1b[38;5;167m",
Green => b"\x1b[38;5;142m",
Gray => b"\x1b[38;5;246m",
Yellow => b"\x1b[38;5;214m",
Orange => b"\x1b[38;5;208m",
Blue => b"\x1b[38;5;109m",
Purple => b"\x1b[38;5;175m",
Cyan => b"\x1b[38;5;108m",
RedBg => b"\x1b[38;5;230m\x1b[48;5;124m",
YellowBg => b"\x1b[38;5;235m\x1b[48;5;214m",
OrangeBg => b"\x1b[38;5;235m\x1b[48;5;166m",
NonText => b"\x1b[38;5;241m",
Invert => b"\x1b[7m",
}
}
#[inline]
fn colors_16_sequence(color: Color) -> &'static [u8] {
use Color::*;
match color {
Reset => b"\x1b[39;0m",
Red => b"\x1b[91m",
Green => b"\x1b[32m",
Gray => b"\x1b[90m",
Yellow => b"\x1b[93m",
Orange => b"\x1b[33m", // No orange color in 16 colors. Use darker yellow instead
Blue => b"\x1b[94m",
Purple => b"\x1b[95m",
Cyan => b"\x1b[96m",
RedBg => b"\x1b[97m\x1b[41m",
YellowBg => b"\x1b[103m\x1b[30m",
OrangeBg => b"\x1b[107m\x1b[30m", // White BG color is used instead of orange
NonText => b"\x1b[37m",
Invert => b"\x1b[7m",
}
}
#[derive(Clone, Copy)]
pub enum TermColor {
TrueColors,
Colors256,
Colors16,
}
impl TermColor {
pub fn from_env() -> TermColor {
env::var("COLORTERM")
.ok()
.and_then(|v| {
if v == "truecolor" {
Some(TermColor::TrueColors)
} else {
None
}
})
.or_else(|| {
TermInfo::from_env().ok().and_then(|info| {
info.numbers.get("colors").map(|colors| {
if *colors == 256 {
TermColor::Colors256
} else {
TermColor::Colors16
}
})
})
})
.unwrap_or(TermColor::Colors16)
}
pub fn sequence(self, color: Color) -> &'static [u8] {
match self {
TermColor::TrueColors => true_colors_sequence(color),
TermColor::Colors256 => colors_256_sequence(color),
TermColor::Colors16 => colors_16_sequence(color),
}
}
}