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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use std::borrow::Cow;
use std::fmt::Write as _;
use std::ops::Range;
use aho_corasick::AhoCorasick;
use anyhow::{Context as _, Result};
use indexmap::IndexMap;
use tracing::debug;
use unicode_segmentation::UnicodeSegmentation;
use crate::color_util::{
color, ForegroundBackground, NeofetchAsciiIndexedColor, ToAnsiString as _,
};
use crate::neofetch_util::{
ascii_size, ColorAlignment, NEOFETCH_COLORS_AC, NEOFETCH_COLOR_PATTERNS,
};
use crate::presets::ColorProfile;
use crate::types::{AnsiMode, TerminalTheme};
/// Raw ascii art before any processing.
#[derive(Clone, Debug)]
pub struct RawAsciiArt {
pub asc: String,
pub fg: Vec<NeofetchAsciiIndexedColor>,
}
/// Normalized ascii art where every line has the same width.
#[derive(Clone, Debug)]
pub struct NormalizedAsciiArt {
pub lines: Vec<String>,
pub w: u8,
pub h: u8,
pub fg: Vec<NeofetchAsciiIndexedColor>,
}
/// Recolored ascii art with all color codes replaced.
#[derive(Clone, Debug)]
pub struct RecoloredAsciiArt {
pub lines: Vec<String>,
pub w: u8,
pub h: u8,
}
impl RawAsciiArt {
/// Makes sure every line is the same width.
#[tracing::instrument(level = "debug", skip(self))]
pub fn to_normalized(&self) -> Result<NormalizedAsciiArt> {
debug!("normalize ascii");
let (w, h) = ascii_size(&self.asc).context("failed to get ascii size")?;
let lines = self
.asc
.lines()
.map(|line| {
let (line_w, _) = ascii_size(line).unwrap();
let pad = " ".repeat(usize::from(w.checked_sub(line_w).unwrap()));
format!("{line}{pad}")
})
.collect();
Ok(NormalizedAsciiArt {
lines,
w: w.try_into().context("width does not fit in u8")?,
h: h.try_into().context("height does not fit in u8")?,
fg: self.fg.clone(),
})
}
}
impl NormalizedAsciiArt {
/// Uses a color alignment to recolor the ascii art.
#[tracing::instrument(level = "debug", skip(self), fields(self.w = self.w, self.h = self.h))]
pub fn to_recolored(
&self,
color_align: &ColorAlignment,
color_profile: &ColorProfile,
color_mode: AnsiMode,
theme: TerminalTheme,
) -> Result<RecoloredAsciiArt> {
debug!("recolor ascii");
if self.lines.is_empty() {
return Ok(RecoloredAsciiArt {
lines: self.lines.clone(),
w: 0,
h: 0,
});
}
let reset = color("&~&*", color_mode).expect("color reset should not be invalid");
let lines = match (color_align, self) {
(ColorAlignment::Horizontal, Self { fg, .. }) => {
let Self { lines, .. } = self
.fill_starting()
.context("failed to fill in starting neofetch color codes")?;
let ac = NEOFETCH_COLORS_AC
.get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap());
// Replace foreground colors
let asc = {
let asc = lines.join("\n");
let mut replacements = NEOFETCH_COLOR_PATTERNS;
let fg_color = color(
match theme {
TerminalTheme::Light => "&0",
TerminalTheme::Dark => "&f",
},
color_mode,
)
.expect("foreground color should not be invalid");
for &fore in fg {
replacements[usize::from(u8::from(fore)).checked_sub(1).unwrap()] =
&fg_color;
}
ac.replace_all(&asc, &replacements)
};
let lines = asc.lines();
// Add new colors
let lines = {
let ColorProfile { colors } = color_profile
.with_length(self.h.try_into().expect("`h` should not be 0"))
.with_context(|| {
format!("failed to spread color profile to length {h}", h = self.h)
})?;
lines.enumerate().map(move |(i, line)| {
let bg_color =
colors[i].to_ansi_string(color_mode, ForegroundBackground::Foreground);
const N: usize = NEOFETCH_COLOR_PATTERNS.len();
let replacements = [&bg_color; N];
ac.replace_all(line, &replacements)
})
};
// Reset colors at end of each line to prevent color bleeding
lines.map(|line| format!("{line}{reset}")).collect()
},
(ColorAlignment::Vertical, Self { fg, .. }) if !fg.is_empty() => {
if self.w == 0 {
return Ok(RecoloredAsciiArt {
lines: self.lines.clone(),
w: 0,
h: self.h,
});
}
let Self { lines, .. } = self
.fill_starting()
.context("failed to fill in starting neofetch color codes")?;
let color_profile = color_profile
.with_length(self.w.try_into().expect("`w` should not be 0"))
.with_context(|| {
format!("failed to spread color profile to length {w}", w = self.w)
})?;
// Apply colors
let ac = NEOFETCH_COLORS_AC
.get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap());
lines
.into_iter()
.map(|line| {
let line: &str = line.as_ref();
// `AhoCorasick` operates on bytes; we need to map that back to grapheme
// clusters (i.e. a character as seen on the terminal)
// See https://github.com/BurntSushi/aho-corasick/issues/72#issuecomment-821128859
let byte_idx_to_grapheme_idx: IndexMap<usize, usize> = {
let mut m: IndexMap<_, _> = line
.grapheme_indices(true)
.enumerate()
.map(|(gr_idx, (byte_idx, _))| (byte_idx, gr_idx))
.collect();
// Add an extra entry at the end, to support lookup using exclusive
// range end
m.insert(line.len(), m.len());
m
};
let mut matches = ac.find_iter(line).peekable();
let mut dst = String::new();
let mut offset: u8 = 0;
loop {
let current = matches.next();
let next = matches.peek();
let (neofetch_color_idx, span, done) = match (current, next) {
(Some(m), Some(m_next)) => {
let ai_start = m.start().checked_add(3).unwrap();
let ai_end = m.end().checked_sub(1).unwrap();
let neofetch_color_idx: NeofetchAsciiIndexedColor = line
[ai_start..ai_end]
.parse()
.expect("neofetch color index should be valid");
if offset == 0 && m.start() > 0 {
dst.push_str(&line[..m.start()]);
}
offset =
offset.checked_add(u8::try_from(m.len()).unwrap()).unwrap();
let mut span = m.span();
span.start = m.end();
span.end = m_next.start();
(neofetch_color_idx, span, false)
},
(Some(m), None) => {
// Last color code
let ai_start = m.start().checked_add(3).unwrap();
let ai_end = m.end().checked_sub(1).unwrap();
let neofetch_color_idx: NeofetchAsciiIndexedColor = line
[ai_start..ai_end]
.parse()
.expect("neofetch color index should be valid");
if offset == 0 && m.start() > 0 {
dst.push_str(&line[..m.start()]);
}
offset =
offset.checked_add(u8::try_from(m.len()).unwrap()).unwrap();
let mut span = m.span();
span.start = m.end();
span.end = line.len();
(neofetch_color_idx, span, true)
},
(None, _) => {
// No color code in the entire line
unreachable!(
"`fill_starting` ensured each line of ascii art starts \
with neofetch color code"
);
},
};
if span.is_empty() {
continue;
}
let txt = &line[span];
if fg.contains(&neofetch_color_idx) {
let fore = color(
match theme {
TerminalTheme::Light => "&0",
TerminalTheme::Dark => "&f",
},
color_mode,
)
.expect("foreground color should not be invalid");
write!(dst, "{fore}{txt}{reset}").unwrap();
} else {
let mut c_range: Range<usize> = span.into();
c_range.start = byte_idx_to_grapheme_idx
.get(&c_range.start)
.unwrap()
.checked_sub(usize::from(offset))
.unwrap();
c_range.end = byte_idx_to_grapheme_idx
.get(&c_range.end)
.unwrap()
.checked_sub(usize::from(offset))
.unwrap();
dst.push_str(
&ColorProfile::new(Vec::from(&color_profile.colors[c_range]))
.color_text(
txt,
color_mode,
ForegroundBackground::Foreground,
false,
)
.context("failed to color text using color profile")?,
);
}
if done {
break;
}
}
Ok(dst)
})
.collect::<Result<_>>()?
},
(ColorAlignment::Vertical, Self { fg, .. }) if fg.is_empty() => {
// Remove existing colors
let asc = {
let asc = self.lines.join("\n");
let ac = NEOFETCH_COLORS_AC
.get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap());
const N: usize = NEOFETCH_COLOR_PATTERNS.len();
const REPLACEMENTS: [&str; N] = [""; N];
ac.replace_all(&asc, &REPLACEMENTS)
};
let lines = asc.lines();
// Add new colors
lines
.map(|line| {
let line = color_profile
.color_text(line, color_mode, ForegroundBackground::Foreground, false)
.context("failed to color text using color profile")?;
Ok(line)
})
.collect::<Result<_>>()?
},
(
ColorAlignment::Custom {
colors: custom_colors,
},
_,
) => {
let Self { lines, .. } = self
.fill_starting()
.context("failed to fill in starting neofetch color codes")?;
let ColorProfile { colors } = color_profile.unique_colors();
// Apply colors
let asc = {
let asc = lines.join("\n");
let ac = NEOFETCH_COLORS_AC
.get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap());
const N: usize = NEOFETCH_COLOR_PATTERNS.len();
let mut replacements = vec![Cow::from(""); N];
for (&ai, &pi) in custom_colors {
let ai: u8 = ai.into();
let pi: u8 = pi.into();
replacements[usize::from(ai.checked_sub(1).unwrap())] = colors
[usize::from(pi)]
.to_ansi_string(color_mode, ForegroundBackground::Foreground)
.into();
}
ac.replace_all(&asc, &replacements)
};
let lines = asc.lines();
// Reset colors at end of each line to prevent color bleeding
lines.map(|line| format!("{line}{reset}")).collect()
},
_ => {
unreachable!()
},
};
Ok(RecoloredAsciiArt {
lines,
w: self.w,
h: self.h,
})
}
/// Fills the missing starting placeholders.
///
/// e.g. `"${c1}...\n..."` -> `"${c1}...\n${c1}..."`
fn fill_starting(&self) -> Result<Self> {
let ac =
NEOFETCH_COLORS_AC.get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap());
let mut last = None;
let lines =
self.lines
.iter()
.map(|line| {
let line: &str = line.as_ref();
let mut new = String::new();
let mut matches = ac.find_iter(line).peekable();
match matches.peek() {
Some(m)
if m.start() == 0
|| line[0..m.start()].trim_end_matches(' ').is_empty() =>
{
// Line starts with neofetch color code
last = Some(&line[m.span()]);
},
Some(_) => {
new.push_str(last.context(
"failed to find neofetch color code from a previous line",
)?);
},
None => {
new.push_str(last.unwrap_or(NEOFETCH_COLOR_PATTERNS[0]));
},
}
new.push_str(line);
// Get the last placeholder for the next line
if let Some(m) = matches.last() {
last.context("non-space character seen before first color code")?;
last = Some(&line[m.span()]);
}
Ok(new)
})
.collect::<Result<_>>()?;
Ok(Self {
lines,
fg: self.fg.clone(),
..*self
})
}
}