fix: fix PF_COLOR=0 stripping too much

closes #60
This commit is contained in:
Adrian Groh 2024-08-02 19:24:05 +02:00
parent 96facd5b5a
commit d3ab51b0c3
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771
2 changed files with 26 additions and 17 deletions

View File

@ -96,7 +96,11 @@ impl Display for Logo {
self.logo_parts self.logo_parts
.iter() .iter()
.fold("".to_string(), |a, LogoPart { color, content }| a .fold("".to_string(), |a, LogoPart { color, content }| a
+ &format!("{color}{content}")) + &if !f.alternate() {
format!("{color}{content}")
} else {
format!("{content}")
})
) )
} }
} }
@ -107,7 +111,7 @@ pub fn parse_logo(input: &str) -> Option<(bool, Logo)> {
if input.is_empty() { if input.is_empty() {
return None; return None;
} }
let regex = Regex::new(r"^\(?(.*)\)[\s\S]*read_ascii *(\d)? *(\d)?").unwrap(); let regex = Regex::new(r"^\(?(.*)\)[\s\S]*read_ascii *(\d)?").unwrap();
let groups = regex.captures(&input).expect("Error while parsing logo"); let groups = regex.captures(&input).expect("Error while parsing logo");
@ -116,10 +120,7 @@ pub fn parse_logo(input: &str) -> Option<(bool, Logo)> {
Some(color) => color.as_str().parse::<u8>().unwrap(), Some(color) => color.as_str().parse::<u8>().unwrap(),
None => 7, None => 7,
}; };
let secondary_color = match groups.get(3) { let secondary_color = (primary_color + 1) % 8;
Some(color) => color.as_str().parse::<u8>().unwrap(),
None => (primary_color + 1) % 8,
};
let logo = input let logo = input
.split_once("EOF\n") .split_once("EOF\n")
.expect("Could not find start of logo, make sure to include the `<<- EOF` and to use tabs for indentation") .expect("Could not find start of logo, make sure to include the `<<- EOF` and to use tabs for indentation")

View File

@ -63,7 +63,12 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {
} else { } else {
"".to_string() "".to_string()
}; };
let logo = logo.to_string(); let color_enabled = dotenvy::var("PF_COLOR").unwrap_or_default() != "0";
let logo = if color_enabled {
logo.to_string()
} else {
format!("{:#}", logo)
};
let mut logo_lines = logo.lines(); let mut logo_lines = logo.lines();
let raw_logo_lines: Vec<_> = raw_logo.lines().collect(); let raw_logo_lines: Vec<_> = raw_logo.lines().collect();
let logo_width = raw_logo_lines let logo_width = raw_logo_lines
@ -78,6 +83,7 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {
.skip(1) .skip(1)
.map(|(_, line, _)| { .map(|(_, line, _)| {
if line.starts_with("\x1b[4") { if line.starts_with("\x1b[4") {
// exclude palette from info1 width
0 0
} else { } else {
line.len() line.len()
@ -103,8 +109,9 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {
for l in 0..line_amount { for l in 0..line_amount {
pfetch_str += &format!( pfetch_str += &format!(
"{padding1}\x1b[1m{logo}{padding2}{color}{info1}\x1b[0m{separator}{padding3}{color2}{info2}\n", "{padding1}{bold}{logo}{padding2}{color}{info1}{nobold}{separator}{padding3}{color2}{info2}\n",
padding1 = " ".repeat(padding1), padding1 = " ".repeat(padding1),
bold = if color_enabled {"\x1b[1m"} else {""},
logo = if logo_enabled { logo = if logo_enabled {
logo_lines.next().unwrap_or("") logo_lines.next().unwrap_or("")
} else { } else {
@ -114,8 +121,9 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {
logo_width - raw_logo_lines.get(l).map_or(0, |line| line.chars().count()) logo_width - raw_logo_lines.get(l).map_or(0, |line| line.chars().count())
+ if logo_enabled { padding2 } else { 0 } + if logo_enabled { padding2 } else { 0 }
), ),
color = info.get(l).map_or("".to_owned(), |line| line.0.to_string()), color = if color_enabled {info.get(l).map_or("".to_owned(), |line| line.0.to_string())} else {"".to_string()},
info1 = info.get(l).map_or("", |line| &line.1), info1 = info.get(l).map_or("", |line| &line.1),
nobold = if color_enabled {"\x1b[0m"} else {""},
separator = info.get(l).map_or("".to_string(), |line| separator = info.get(l).map_or("".to_string(), |line|
if ! &line.2.is_empty() { if ! &line.2.is_empty() {
dotenvy::var("PF_SEP").unwrap_or_default() dotenvy::var("PF_SEP").unwrap_or_default()
@ -125,7 +133,7 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {
info1_width.saturating_sub(info.get(l).map_or(0, |(_, line, _)| line.len())) info1_width.saturating_sub(info.get(l).map_or(0, |(_, line, _)| line.len()))
+ padding3 + padding3
), ),
color2 = match dotenvy::var("PF_COL2") { color2 = if color_enabled {match dotenvy::var("PF_COL2") {
Ok(newcolor) => { Ok(newcolor) => {
match Color::from_str(&newcolor) { match Color::from_str(&newcolor) {
Ok(newcolor) => format!("{newcolor}"), Ok(newcolor) => format!("{newcolor}"),
@ -133,18 +141,18 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {
} }
}, },
Err(_) => "".to_string() Err(_) => "".to_string()
}, }} else {"".to_string()},
info2 = info.get(l).map_or("", |line| &line.2) info2 = info.get(l).map_or("", |line| &line.2)
) )
} }
// if colors are disabled, remove them from string // if colors are disabled, remove them from string
if dotenvy::var("PF_COLOR").unwrap_or_default() == "0" { // if dotenvy::var("PF_COLOR").unwrap_or_default() == "0" {
pfetch_str = pfetch_str // pfetch_str = pfetch_str
.split("\x1b[") // .split("\x1b[")
.map(|chunk| chunk.chars().skip(3).collect::<String>()) // .map(|chunk| chunk.chars().skip(3).collect::<String>())
.collect(); // .collect();
} // }
// disable line wrap // disable line wrap
crossterm::execute!(std::io::stdout(), crossterm::terminal::DisableLineWrap) crossterm::execute!(std::io::stdout(), crossterm::terminal::DisableLineWrap)