Implement neofetch_util functions
Co-authored-by: Chiew Yan Wei <chiewyanwei@gmail.com>
This commit is contained in:
parent
4f1efbc699
commit
d1d99daa87
3 changed files with 50 additions and 6 deletions
|
@ -16,6 +16,7 @@ bpaf = { workspace = true, features = [] }
|
||||||
chrono = { workspace = true, features = ["clock", "std"] }
|
chrono = { workspace = true, features = ["clock", "std"] }
|
||||||
# derive_more = { workspace = true, features = ["std"] }
|
# derive_more = { workspace = true, features = ["std"] }
|
||||||
indexmap = { workspace = true, features = ["std"] }
|
indexmap = { workspace = true, features = ["std"] }
|
||||||
|
regex = { workspace = true, features = ["perf", "std", "unicode"] }
|
||||||
rgb = { workspace = true, features = [] }
|
rgb = { workspace = true, features = [] }
|
||||||
shell-words = { workspace = true, features = ["std"] }
|
shell-words = { workspace = true, features = ["std"] }
|
||||||
strum = { workspace = true, features = ["derive", "std"] }
|
strum = { workspace = true, features = ["derive", "std"] }
|
||||||
|
|
|
@ -87,7 +87,7 @@ impl Distro {
|
||||||
if stripped.contains(['*', '"']) {
|
if stripped.contains(['*', '"']) {
|
||||||
if let Some((prefix, suffix)) = stripped.split_once(r#""*""#) {
|
if let Some((prefix, suffix)) = stripped.split_once(r#""*""#) {
|
||||||
conds.push(format!(
|
conds.push(format!(
|
||||||
r###"name.starts_with("{prefix}") && name.ends_with("{suffix}")"###
|
r#"name.starts_with("{prefix}") && name.ends_with("{suffix}")"#
|
||||||
));
|
));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -96,27 +96,27 @@ impl Distro {
|
||||||
|
|
||||||
// Exact matches
|
// Exact matches
|
||||||
if m.trim_matches('*') == m {
|
if m.trim_matches('*') == m {
|
||||||
conds.push(format!(r###"name == "{stripped}""###));
|
conds.push(format!(r#"name == "{stripped}""#));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Both sides are *
|
// Both sides are *
|
||||||
if m.starts_with('*') && m.ends_with('*') {
|
if m.starts_with('*') && m.ends_with('*') {
|
||||||
conds.push(format!(
|
conds.push(format!(
|
||||||
r###"name.starts_with("{stripped}") || name.ends_with("{stripped}")"###
|
r#"name.starts_with("{stripped}") || name.ends_with("{stripped}")"#
|
||||||
));
|
));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ends with *
|
// Ends with *
|
||||||
if m.ends_with('*') {
|
if m.ends_with('*') {
|
||||||
conds.push(format!(r###"name.starts_with("{stripped}")"###));
|
conds.push(format!(r#"name.starts_with("{stripped}")"#));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starts with *
|
// Starts with *
|
||||||
if m.starts_with('*') {
|
if m.starts_with('*') {
|
||||||
conds.push(format!(r###"name.ends_with("{stripped}")"###));
|
conds.push(format!(r#"name.ends_with("{stripped}")"#));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ use std::process::Command;
|
||||||
use std::{env, fmt};
|
use std::{env, fmt};
|
||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
use regex::Regex;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
use crate::distros::Distro;
|
use crate::distros::Distro;
|
||||||
|
@ -64,12 +65,54 @@ where
|
||||||
debug!(%distro, "distro name");
|
debug!(%distro, "distro name");
|
||||||
|
|
||||||
if let Some(distro) = Distro::detect(&distro) {
|
if let Some(distro) = Distro::detect(&distro) {
|
||||||
return Ok(distro.ascii_art().to_owned());
|
return Ok(normalize_ascii(distro.ascii_art()));
|
||||||
}
|
}
|
||||||
|
|
||||||
todo!()
|
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.
|
/// Runs neofetch command, returning the piped stdout output.
|
||||||
#[tracing::instrument(level = "debug")]
|
#[tracing::instrument(level = "debug")]
|
||||||
fn run_neofetch_command_piped<S>(args: &[S]) -> Result<String>
|
fn run_neofetch_command_piped<S>(args: &[S]) -> Result<String>
|
||||||
|
|
Loading…
Reference in a new issue