Skip to content

Commit 4f497a4

Browse files
author
Roman Stingler
committed
Add coloring to repo names and version diff
closes Morganamilo#1291
1 parent 6527db3 commit 4f497a4

File tree

1 file changed

+83
-12
lines changed

1 file changed

+83
-12
lines changed

src/fmt.rs

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,11 @@ pub fn color_repo(enabled: bool, name: &str) -> String {
114114
return name.to_string();
115115
}
116116

117-
let mut col: u32 = 5;
118-
119-
for &b in name.as_bytes() {
120-
col = (b as u32).wrapping_add((col << 4).wrapping_add(col));
121-
}
122-
123-
col = (col % 6) + 9;
124-
let col = Style::from(Color::Fixed(col as u8)).bold();
125-
col.paint(name).to_string()
117+
let color = 9 + (name.len() % 6) as u8;
118+
Style::from(Color::Fixed(color))
119+
.bold()
120+
.paint(name)
121+
.to_string()
126122
}
127123

128124
pub fn print_target(targ: &str, quiet: bool) {
@@ -443,13 +439,38 @@ pub fn print_install_verbose(config: &Config, actions: &Actions, devel: &HashSet
443439
});
444440

445441
for pkg in &install {
446-
println!(
447-
"{:<package_len$} {:<old_len$} {:<new_len$} {}",
448-
format!("{}/{}", pkg.pkg.db().unwrap().name(), pkg.pkg.name()),
442+
let repo_name = pkg.pkg.db().unwrap().name();
443+
let colored_repo = color_repo(config.color.enabled, repo_name);
444+
let pkg_str = format!("{}/{}", colored_repo, pkg.pkg.name());
445+
let visible_width = repo_name.len() + 1 + pkg.pkg.name().len(); // Calculate visible width without ANSI codes
446+
let padding = " ".repeat(package_len.saturating_sub(visible_width));
447+
448+
let (old_colored, new_colored) = colorize_version_diff(
449449
db.pkg(pkg.pkg.name())
450450
.map(|pkg| pkg.version().as_str())
451451
.unwrap_or(""),
452452
pkg.pkg.version().as_str(),
453+
);
454+
455+
// Calculate visible width of the colored versions
456+
let old_visible_width = db
457+
.pkg(pkg.pkg.name())
458+
.map(|pkg| pkg.version().as_str().len())
459+
.unwrap_or(0);
460+
let new_visible_width = pkg.pkg.version().as_str().len();
461+
462+
// Add padding to maintain alignment
463+
let old_padding = " ".repeat(old_len.saturating_sub(old_visible_width));
464+
let new_padding = " ".repeat(new_len.saturating_sub(new_visible_width));
465+
466+
println!(
467+
"{}{} {}{} {}{} {}",
468+
pkg_str,
469+
padding,
470+
old_colored,
471+
old_padding,
472+
new_colored,
473+
new_padding,
453474
if pkg.make { &yes } else { &no }
454475
);
455476
}
@@ -516,3 +537,53 @@ pub fn print_install_verbose(config: &Config, actions: &Actions, devel: &HashSet
516537

517538
println!();
518539
}
540+
541+
fn colorize_version_diff(old_ver: &str, new_ver: &str) -> (String, String) {
542+
if old_ver.is_empty() {
543+
return (
544+
String::new(),
545+
Style::new().fg(Color::Green).paint(new_ver).to_string(), // all green for new version
546+
);
547+
}
548+
549+
let mut old_colored = String::new();
550+
let mut new_colored = String::new();
551+
552+
// Split versions into characters
553+
let old_chars: Vec<char> = old_ver.chars().collect();
554+
let new_chars: Vec<char> = new_ver.chars().collect();
555+
556+
// Find common prefix
557+
let mut common_len = 0;
558+
for (a, b) in old_chars.iter().zip(new_chars.iter()) {
559+
if a == b {
560+
common_len += 1;
561+
} else {
562+
break;
563+
}
564+
}
565+
566+
// Color the old version (red for different parts)
567+
old_colored.push_str(&old_ver[..common_len]);
568+
if common_len < old_ver.len() {
569+
old_colored.push_str(
570+
&Style::new()
571+
.fg(Color::Red)
572+
.paint(&old_ver[common_len..])
573+
.to_string(),
574+
);
575+
}
576+
577+
// Color the new version (green for different parts)
578+
new_colored.push_str(&new_ver[..common_len]);
579+
if common_len < new_ver.len() {
580+
new_colored.push_str(
581+
&Style::new()
582+
.fg(Color::Green)
583+
.paint(&new_ver[common_len..])
584+
.to_string(),
585+
);
586+
}
587+
588+
(old_colored, new_colored)
589+
}

0 commit comments

Comments
 (0)