Merge pull request #11 from teohhanhui/riir
Implement neofetch_util functions
This commit is contained in:
commit
a0856aa4f5
3 changed files with 50 additions and 6 deletions
|
@ -16,6 +16,7 @@ bpaf = { workspace = true, features = [] }
|
|||
chrono = { workspace = true, features = ["clock", "std"] }
|
||||
# derive_more = { workspace = true, features = ["std"] }
|
||||
indexmap = { workspace = true, features = ["std"] }
|
||||
regex = { workspace = true, features = ["perf", "std", "unicode"] }
|
||||
rgb = { workspace = true, features = [] }
|
||||
shell-words = { workspace = true, features = ["std"] }
|
||||
strum = { workspace = true, features = ["derive", "std"] }
|
||||
|
|
|
@ -87,7 +87,7 @@ impl Distro {
|
|||
if stripped.contains(['*', '"']) {
|
||||
if let Some((prefix, suffix)) = stripped.split_once(r#""*""#) {
|
||||
conds.push(format!(
|
||||
r###"name.starts_with("{prefix}") && name.ends_with("{suffix}")"###
|
||||
r#"name.starts_with("{prefix}") && name.ends_with("{suffix}")"#
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
@ -96,27 +96,27 @@ impl Distro {
|
|||
|
||||
// Exact matches
|
||||
if m.trim_matches('*') == m {
|
||||
conds.push(format!(r###"name == "{stripped}""###));
|
||||
conds.push(format!(r#"name == "{stripped}""#));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Both sides are *
|
||||
if m.starts_with('*') && m.ends_with('*') {
|
||||
conds.push(format!(
|
||||
r###"name.starts_with("{stripped}") || name.ends_with("{stripped}")"###
|
||||
r#"name.starts_with("{stripped}") || name.ends_with("{stripped}")"#
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Ends with *
|
||||
if m.ends_with('*') {
|
||||
conds.push(format!(r###"name.starts_with("{stripped}")"###));
|
||||
conds.push(format!(r#"name.starts_with("{stripped}")"#));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Starts with *
|
||||
if m.starts_with('*') {
|
||||
conds.push(format!(r###"name.ends_with("{stripped}")"###));
|
||||
conds.push(format!(r#"name.ends_with("{stripped}")"#));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ use std::process::Command;
|
|||
use std::{env, fmt};
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use regex::Regex;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::distros::Distro;
|
||||
|
@ -64,12 +65,54 @@ where
|
|||
debug!(%distro, "distro name");
|
||||
|
||||
if let Some(distro) = Distro::detect(&distro) {
|
||||
return Ok(distro.ascii_art().to_owned());
|
||||
return Ok(normalize_ascii(distro.ascii_art()));
|
||||
}
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Gets distro ascii width and height, ignoring color code.
|
||||
pub fn ascii_size<S>(asc: S, neofetch_color_re: &Regex) -> (u8, u8)
|
||||
where
|
||||
S: AsRef<str>,
|
||||
{
|
||||
let asc = asc.as_ref();
|
||||
|
||||
let Some(width) = neofetch_color_re
|
||||
.replace_all(asc, "")
|
||||
.split('\n')
|
||||
.map(|line| line.len())
|
||||
.max()
|
||||
else {
|
||||
unreachable!();
|
||||
};
|
||||
let height = asc.split('\n').count();
|
||||
|
||||
(width as u8, height as u8)
|
||||
}
|
||||
|
||||
/// Makes sure every line are the same width.
|
||||
fn normalize_ascii<S>(asc: S) -> String
|
||||
where
|
||||
S: AsRef<str>,
|
||||
{
|
||||
let asc = asc.as_ref();
|
||||
|
||||
let neofetch_color_re =
|
||||
Regex::new(r"\$\{c[0-9]\}").expect("neofetch color regex should not be invalid");
|
||||
|
||||
let (w, _) = ascii_size(asc, &neofetch_color_re);
|
||||
|
||||
let mut buf = "".to_owned();
|
||||
for line in asc.split('\n') {
|
||||
let (line_w, _) = ascii_size(line, &neofetch_color_re);
|
||||
let pad = " ".repeat((w - line_w) as usize);
|
||||
buf.push_str(&format!("{line}{pad}\n"))
|
||||
}
|
||||
|
||||
buf
|
||||
}
|
||||
|
||||
/// Runs neofetch command, returning the piped stdout output.
|
||||
#[tracing::instrument(level = "debug")]
|
||||
fn run_neofetch_command_piped<S>(args: &[S]) -> Result<String>
|
||||
|
|
Loading…
Reference in a new issue