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
//! The glyphs the dashboard draws with, and the guess behind the default.
use std::borrow::Cow;
/// The marks the dashboard draws that not every font can render.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Glyphs {
/// Prefixes the charging state in the table.
pub charging: Cow<'static, str>,
}
impl Glyphs {
/// What every terminal can draw, and what blubat falls back to.
pub const ASCII: Self = Self {
charging: Cow::Borrowed("+"),
};
/// The Nerd Fonts bolt, single width so it cannot shift a column.
pub const NERD_FONT: Self = Self {
charging: Cow::Borrowed("\u{f0e7}"),
};
/// The glyphs to draw with, guessed from the environment.
pub fn detected() -> Self {
Self::from_env(|name| std::env::var(name).ok())
}
/// The charging mark the config named, in place of the guessed one.
///
/// The guess is best effort and the file is not, so anything written wins.
/// Blank is nothing written: a mark of no characters would leave the state
/// column reading as a stray space.
pub fn overridden(self, charging: Option<&str>) -> Self {
charging
.map(str::trim)
.filter(|glyph| !glyph.is_empty())
.map_or(self, |glyph| Self {
charging: Cow::Owned(glyph.to_string()),
})
}
/// Guesses whether the terminal can draw the Nerd Fonts private use area.
///
/// Best effort and deliberately shy: no terminal reports the font it draws
/// with, so this reads the variables some terminals and shells set and
/// answers ascii whenever it is not sure, since a wrong yes prints tofu
/// while a wrong no only prints a plainer glyph. Only blubat's own variable
/// is an answer, taken at its word either way, which is the escape hatch
/// for a terminal the guess reads wrong. Every other name is a hint: they
/// belong to whoever set them, and `NERD_FONT` holding a font name is as
/// likely as it holding a yes.
fn from_env(var: impl Fn(&str) -> Option<String>) -> Self {
const ANSWER: &str = "BLUBAT_NERD_FONT";
const FONTS: [&str; 4] = ["NERD_FONT", "TERM_PROGRAM", "TERMINAL_FONT", "FONT"];
if let Some(answer) = var(ANSWER) {
return Self::of(said_yes(&answer));
}
Self::of(
FONTS
.iter()
.filter_map(|name| var(name))
.any(|value| said_yes(&value) || value.to_lowercase().contains("nerd")),
)
}
fn of(nerd_font: bool) -> Self {
if nerd_font {
Self::NERD_FONT
} else {
Self::ASCII
}
}
}
impl Default for Glyphs {
/// What every terminal can draw, since a wrong yes prints tofu.
fn default() -> Self {
Self::ASCII
}
}
/// Whether a variable's value reads as yes rather than as a font name.
fn said_yes(value: &str) -> bool {
matches!(value.trim().to_lowercase().as_str(), "1" | "true" | "yes")
}
#[cfg(test)]
mod tests {
use super::*;
/// An environment holding exactly the variables a case sets.
fn env(set: Vec<(&'static str, &'static str)>) -> impl Fn(&str) -> Option<String> {
move |name| {
set.iter()
.find(|(key, _)| *key == name)
.map(|(_, value)| (*value).to_string())
}
}
#[test]
fn nothing_known_about_the_font_means_ascii() {
assert_eq!(Glyphs::from_env(env(Vec::new())), Glyphs::ASCII);
assert_eq!(
Glyphs::from_env(env(vec![("TERM_PROGRAM", "Apple_Terminal")])),
Glyphs::ASCII
);
}
#[test]
fn a_font_naming_itself_nerd_is_taken_at_its_word() {
for hint in [
("TERMINAL_FONT", "JetBrainsMono Nerd Font Mono"),
("FONT", "hack nerd font"),
("NERD_FONT", "1"),
("NERD_FONT", "JetBrainsMono Nerd Font"),
("TERM_PROGRAM", "WezTerm-NerdFont"),
] {
assert_eq!(
Glyphs::from_env(env(vec![hint])),
Glyphs::NERD_FONT,
"{hint:?}"
);
}
}
#[test]
fn the_override_settles_it_in_both_directions() {
assert_eq!(
Glyphs::from_env(env(vec![("BLUBAT_NERD_FONT", "1")])),
Glyphs::NERD_FONT
);
assert_eq!(
Glyphs::from_env(env(vec![
("BLUBAT_NERD_FONT", "0"),
("FONT", "Hack Nerd Font Mono"),
])),
Glyphs::ASCII,
"an explicit no beats a hint"
);
assert_eq!(
Glyphs::from_env(env(vec![("NERD_FONT", "0")])),
Glyphs::ASCII,
"a hint that is not blubat's own says no by saying nothing"
);
}
#[test]
fn the_bolt_is_one_cell_so_it_cannot_shift_a_column() {
assert_eq!(Glyphs::NERD_FONT.charging.chars().count(), 1);
assert_eq!(Glyphs::ASCII.charging.chars().count(), 1);
}
#[test]
fn a_configured_mark_wins_over_whatever_was_guessed() {
assert_eq!(
Glyphs::NERD_FONT.overridden(Some("^")).charging,
"^",
"the escape hatch for a terminal the guess reads wrong"
);
assert_eq!(
Glyphs::ASCII.overridden(Some(" \u{26a1} ")).charging,
"\u{26a1}"
);
}
#[test]
fn nothing_written_leaves_the_guess_standing() {
for written in [None, Some(""), Some(" ")] {
assert_eq!(
Glyphs::NERD_FONT.overridden(written),
Glyphs::NERD_FONT,
"{written:?}"
);
}
}
}