From 4f497a4146f93ba1cb3ff9c605e9a25c62950f2e Mon Sep 17 00:00:00 2001 From: Roman Stingler Date: Sun, 15 Dec 2024 22:36:46 +0100 Subject: [PATCH] Add coloring to repo names and version diff closes #1291 --- src/fmt.rs | 95 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 12 deletions(-) diff --git a/src/fmt.rs b/src/fmt.rs index 1ef86803..fe1a6943 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -114,15 +114,11 @@ pub fn color_repo(enabled: bool, name: &str) -> String { return name.to_string(); } - let mut col: u32 = 5; - - for &b in name.as_bytes() { - col = (b as u32).wrapping_add((col << 4).wrapping_add(col)); - } - - col = (col % 6) + 9; - let col = Style::from(Color::Fixed(col as u8)).bold(); - col.paint(name).to_string() + let color = 9 + (name.len() % 6) as u8; + Style::from(Color::Fixed(color)) + .bold() + .paint(name) + .to_string() } pub fn print_target(targ: &str, quiet: bool) { @@ -443,13 +439,38 @@ pub fn print_install_verbose(config: &Config, actions: &Actions, devel: &HashSet }); for pkg in &install { - println!( - "{: (String, String) { + if old_ver.is_empty() { + return ( + String::new(), + Style::new().fg(Color::Green).paint(new_ver).to_string(), // all green for new version + ); + } + + let mut old_colored = String::new(); + let mut new_colored = String::new(); + + // Split versions into characters + let old_chars: Vec = old_ver.chars().collect(); + let new_chars: Vec = new_ver.chars().collect(); + + // Find common prefix + let mut common_len = 0; + for (a, b) in old_chars.iter().zip(new_chars.iter()) { + if a == b { + common_len += 1; + } else { + break; + } + } + + // Color the old version (red for different parts) + old_colored.push_str(&old_ver[..common_len]); + if common_len < old_ver.len() { + old_colored.push_str( + &Style::new() + .fg(Color::Red) + .paint(&old_ver[common_len..]) + .to_string(), + ); + } + + // Color the new version (green for different parts) + new_colored.push_str(&new_ver[..common_len]); + if common_len < new_ver.len() { + new_colored.push_str( + &Style::new() + .fg(Color::Green) + .paint(&new_ver[common_len..]) + .to_string(), + ); + } + + (old_colored, new_colored) +}