Skip to content

Commit

Permalink
Add coloring to repo names and version diff
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Stingler committed Dec 15, 2024
1 parent 6527db3 commit 4f497a4
Showing 1 changed file with 83 additions and 12 deletions.
95 changes: 83 additions & 12 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -443,13 +439,38 @@ pub fn print_install_verbose(config: &Config, actions: &Actions, devel: &HashSet
});

for pkg in &install {
println!(
"{:<package_len$} {:<old_len$} {:<new_len$} {}",
format!("{}/{}", pkg.pkg.db().unwrap().name(), pkg.pkg.name()),
let repo_name = pkg.pkg.db().unwrap().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(); // Calculate visible width without ANSI codes
let padding = " ".repeat(package_len.saturating_sub(visible_width));

let (old_colored, new_colored) = colorize_version_diff(
db.pkg(pkg.pkg.name())
.map(|pkg| pkg.version().as_str())
.unwrap_or(""),
pkg.pkg.version().as_str(),
);

// Calculate visible width of the colored versions
let old_visible_width = db
.pkg(pkg.pkg.name())
.map(|pkg| pkg.version().as_str().len())
.unwrap_or(0);
let new_visible_width = pkg.pkg.version().as_str().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!(
"{}{} {}{} {}{} {}",
pkg_str,
padding,
old_colored,
old_padding,
new_colored,
new_padding,
if pkg.make { &yes } else { &no }
);
}
Expand Down Expand Up @@ -516,3 +537,53 @@ pub fn print_install_verbose(config: &Config, actions: &Actions, devel: &HashSet

println!();
}

fn colorize_version_diff(old_ver: &str, new_ver: &str) -> (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<char> = old_ver.chars().collect();
let new_chars: Vec<char> = 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)
}

0 comments on commit 4f497a4

Please sign in to comment.