Skip to content

Commit

Permalink
fix: fix PF_COLOR=0 stripping too much
Browse files Browse the repository at this point in the history
closes #60
  • Loading branch information
Gobidev committed Aug 2, 2024
1 parent 96facd5 commit d3ab51b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
13 changes: 7 additions & 6 deletions pfetch-logo-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ impl Display for Logo {
self.logo_parts
.iter()
.fold("".to_string(), |a, LogoPart { color, content }| a
+ &format!("{color}{content}"))
+ &if !f.alternate() {
format!("{color}{content}")
} else {
format!("{content}")
})
)
}
}
Expand All @@ -107,7 +111,7 @@ pub fn parse_logo(input: &str) -> Option<(bool, Logo)> {
if input.is_empty() {
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");

Expand All @@ -116,10 +120,7 @@ pub fn parse_logo(input: &str) -> Option<(bool, Logo)> {
Some(color) => color.as_str().parse::<u8>().unwrap(),
None => 7,
};
let secondary_color = match groups.get(3) {
Some(color) => color.as_str().parse::<u8>().unwrap(),
None => (primary_color + 1) % 8,
};
let secondary_color = (primary_color + 1) % 8;
let logo = input
.split_once("EOF\n")
.expect("Could not find start of logo, make sure to include the `<<- EOF` and to use tabs for indentation")
Expand Down
30 changes: 19 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {
} else {
"".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 raw_logo_lines: Vec<_> = raw_logo.lines().collect();
let logo_width = raw_logo_lines
Expand All @@ -78,6 +83,7 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {
.skip(1)
.map(|(_, line, _)| {
if line.starts_with("\x1b[4") {
// exclude palette from info1 width
0
} else {
line.len()
Expand All @@ -103,8 +109,9 @@ fn pfetch(info: Vec<(Color, String, String)>, logo: Logo, logo_enabled: bool) {

for l in 0..line_amount {
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),
bold = if color_enabled {"\x1b[1m"} else {""},
logo = if logo_enabled {
logo_lines.next().unwrap_or("")
} else {
Expand All @@ -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())
+ 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),
nobold = if color_enabled {"\x1b[0m"} else {""},
separator = info.get(l).map_or("".to_string(), |line|
if ! &line.2.is_empty() {
dotenvy::var("PF_SEP").unwrap_or_default()
Expand All @@ -125,26 +133,26 @@ 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()))
+ padding3
),
color2 = match dotenvy::var("PF_COL2") {
color2 = if color_enabled {match dotenvy::var("PF_COL2") {
Ok(newcolor) => {
match Color::from_str(&newcolor) {
Ok(newcolor) => format!("{newcolor}"),
Err(_) => "".to_string(),
}
},
Err(_) => "".to_string()
},
}} else {"".to_string()},
info2 = info.get(l).map_or("", |line| &line.2)
)
}

// if colors are disabled, remove them from string
if dotenvy::var("PF_COLOR").unwrap_or_default() == "0" {
pfetch_str = pfetch_str
.split("\x1b[")
.map(|chunk| chunk.chars().skip(3).collect::<String>())
.collect();
}
// if dotenvy::var("PF_COLOR").unwrap_or_default() == "0" {
// pfetch_str = pfetch_str
// .split("\x1b[")
// .map(|chunk| chunk.chars().skip(3).collect::<String>())
// .collect();
// }

// disable line wrap
crossterm::execute!(std::io::stdout(), crossterm::terminal::DisableLineWrap)
Expand Down

0 comments on commit d3ab51b

Please sign in to comment.