Merge pull request #31 from teohhanhui/riir

Fix stray newlines
This commit is contained in:
Teoh Han Hui 2024-07-19 01:26:30 +08:00 committed by GitHub
commit 0c7c43d71c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,6 +11,7 @@ use std::{env, fmt};
use aho_corasick::AhoCorasick; use aho_corasick::AhoCorasick;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use indexmap::IndexMap; use indexmap::IndexMap;
use itertools::Itertools;
#[cfg(windows)] #[cfg(windows)]
use normpath::PathExt as _; use normpath::PathExt as _;
#[cfg(windows)] #[cfg(windows)]
@ -133,21 +134,21 @@ impl ColorAlignment {
.with_length(length) .with_length(length)
.context("failed to spread color profile to length")? .context("failed to spread color profile to length")?
}; };
let mut asc = String::new(); lines
for (i, line) in lines.into_iter().enumerate() { .into_iter()
let line = line.replace( .enumerate()
&format!("${{c{back}}}", back = u8::from(back)), .map(|(i, line)| {
&colors[i].to_ansi_string(color_mode, { let line = line.replace(
// note: this is "background" in the ascii art, but foreground &format!("${{c{back}}}", back = u8::from(back)),
// text in terminal &colors[i].to_ansi_string(color_mode, {
ForegroundBackground::Foreground // note: this is "background" in the ascii art, but
}), // foreground text in terminal
); ForegroundBackground::Foreground
asc.push_str(&line); }),
asc.push_str(&reset); );
asc.push('\n'); format!("{line}{reset}")
} })
asc .join("\n")
}, },
Self::Vertical { .. } => { Self::Vertical { .. } => {
unimplemented!( unimplemented!(
@ -193,21 +194,19 @@ impl ColorAlignment {
.with_length(length) .with_length(length)
.context("failed to spread color profile to length")? .context("failed to spread color profile to length")?
}; };
let mut asc = String::new(); lines
for (i, line) in lines.into_iter().enumerate() { .into_iter()
asc.push_str( .enumerate()
&colors[i] .map(|(i, line)| {
.to_ansi_string(color_mode, ForegroundBackground::Foreground), let fore = colors[i]
); .to_ansi_string(color_mode, ForegroundBackground::Foreground);
asc.push_str(line); format!("{fore}{line}{reset}")
asc.push_str(&reset); })
asc.push('\n'); .join("\n")
}
asc
}, },
Self::Vertical { .. } => { Self::Vertical { .. } => lines
let mut asc = String::new(); .into_iter()
for line in lines { .map(|line| {
let line = color_profile let line = color_profile
.color_text( .color_text(
line, line,
@ -216,12 +215,10 @@ impl ColorAlignment {
false, false,
) )
.context("failed to color text using color profile")?; .context("failed to color text using color profile")?;
asc.push_str(&line); Ok(format!("{line}{reset}"))
asc.push_str(&reset); })
asc.push('\n'); .collect::<Result<Vec<_>>>()?
} .join("\n"),
asc
},
_ => { _ => {
unreachable!(); unreachable!();
}, },
@ -542,16 +539,13 @@ where
let (w, _) = ascii_size(asc); let (w, _) = ascii_size(asc);
let mut buf = String::new(); asc.split('\n')
for line in asc.split('\n') { .map(|line| {
let (line_w, _) = ascii_size(line); let (line_w, _) = ascii_size(line);
buf.push_str(line); let pad = " ".repeat(usize::from(w - line_w));
let pad = " ".repeat(usize::from(w - line_w)); format!("{line}{pad}")
buf.push_str(&pad); })
buf.push('\n'); .join("\n")
}
buf
} }
/// Fills the missing starting placeholders. /// Fills the missing starting placeholders.
@ -565,31 +559,36 @@ where
let ac = NEOFETCH_COLORS_AC.get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap()); let ac = NEOFETCH_COLORS_AC.get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap());
let mut new = String::new();
let mut last = None; let mut last = None;
for line in asc.split('\n') { Ok(asc
let mut matches = ac.find_iter(line).peekable(); .split('\n')
.map(|line| {
let mut new = String::new();
let mut matches = ac.find_iter(line).peekable();
match matches.peek() { match matches.peek() {
Some(m) if m.start() == 0 || line[0..m.start()].trim_end_matches(' ').is_empty() => { Some(m)
// line starts with neofetch color code, do nothing if m.start() == 0 || line[0..m.start()].trim_end_matches(' ').is_empty() =>
}, {
_ => { // line starts with neofetch color code, do nothing
new.push_str( },
last.context("failed to find neofetch color code from a previous line")?, _ => {
); new.push_str(
}, last.context("failed to find neofetch color code from a previous line")?,
} );
new.push_str(line); },
new.push('\n'); }
new.push_str(line);
// Get the last placeholder for the next line // Get the last placeholder for the next line
if let Some(m) = matches.last() { if let Some(m) = matches.last() {
last = Some(&line[m.span()]) last = Some(&line[m.span()])
} }
}
Ok(new) Ok(new)
})
.collect::<Result<Vec<_>>>()?
.join("\n"))
} }
/// Runs neofetch command, returning the piped stdout output. /// Runs neofetch command, returning the piped stdout output.