From 166beed381755d12199d95b23ca2b4aae9cee31f 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 | 165 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 24 deletions(-) diff --git a/src/fmt.rs b/src/fmt.rs index 1ef86803..8097e308 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!( - "{: { for pkg in &base.pkgs { + let repo_name = repo(config, &pkg.pkg.name); + let colored_repo = color_repo(config.color.enabled, &repo_name); + let pkg_str = format!("{}/{}", colored_repo, pkg.pkg.name); + let visible_width = repo_name.len() + 1 + pkg.pkg.name.len(); + let padding = " ".repeat(package_len.saturating_sub(visible_width)); + let ver = if devel.contains(&pkg.pkg.name) { "latest-commit" } else { &pkg.pkg.version }; + + let old_version = old_ver(config, &pkg.pkg.name) + .map(|v| v.as_str()) + .unwrap_or_default(); + + // Colorize the versions + let (old_colored, new_colored) = colorize_version_diff(old_version, ver); + + // Calculate visible width of the colored versions + let old_visible_width = old_version.len(); + let new_visible_width = ver.len(); + + // Add padding to maintain alignment + let old_padding = " ".repeat(old_len.saturating_sub(old_visible_width)); + let new_padding = " ".repeat(new_len.saturating_sub(new_visible_width)); + println!( - "{: { for pkg in &base.pkgs { + let repo_name = &base.repo; + let colored_repo = color_repo(config.color.enabled, repo_name); + let pkg_str = format!("{}/{}", colored_repo, pkg.pkg.pkgname); + let visible_width = repo_name.len() + 1 + pkg.pkg.pkgname.len(); + let padding = " ".repeat(package_len.saturating_sub(visible_width)); + let ver = base.srcinfo.version(); let ver = if devel.contains(&pkg.pkg.pkgname) { "latest-commit" } else { &ver }; + + let old_version = old_ver(config, &pkg.pkg.pkgname) + .map(|v| v.as_str()) + .unwrap_or_default(); + + // Colorize the versions + let (old_colored, new_colored) = colorize_version_diff(old_version, ver); + + // Calculate visible width of the colored versions + let old_visible_width = old_version.len(); + let new_visible_width = ver.len(); + + // Add padding to maintain alignment + let old_padding = " ".repeat(old_len.saturating_sub(old_visible_width)); + let new_padding = " ".repeat(new_len.saturating_sub(new_visible_width)); + 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) +}