Skip to content

Commit b1cf029

Browse files
authored
Right align authorship percentages (#1207)
This patch right-aligns the percentages for authorship to make them easier to visually compare. Before: Authors: 53% Ossama Hjaji 1212 21% dependabot[bot] 485 4% Spenser Black 88 After: Authors: 53% Ossama Hjaji 1212 21% dependabot[bot] 485 4% Spenser Black 88 Tested: Cargo test all passes. Also added a new test that checks alignment in a simple case, and tested that the output looks as intended locally. Signed-off-by: Luke Hsiao <luke.hsiao@numbersstation.ai>
1 parent 5b1ef67 commit b1cf029

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

src/info/author.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,37 @@ impl AuthorsInfo {
7070
let authors = git_metrics.authors_to_display.clone();
7171
Self { authors }
7272
}
73+
74+
fn top_contribution(&self) -> usize {
75+
if let Some(top_contributor) = self.authors.get(0) {
76+
return top_contributor.contribution;
77+
}
78+
0
79+
}
80+
}
81+
82+
fn digit_difference(num1: usize, num2: usize) -> usize {
83+
let count_digits = |num: usize| (num.checked_ilog10().unwrap_or(0) + 1) as usize;
84+
85+
count_digits(num1) - count_digits(num2)
7386
}
7487

7588
impl std::fmt::Display for AuthorsInfo {
7689
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
7790
let mut authors_info = String::new();
7891

7992
let pad = self.title().len() + 2;
80-
8193
for (i, author) in self.authors.iter().enumerate() {
8294
if i == 0 {
8395
write!(authors_info, "{author}")?;
8496
} else {
85-
write!(authors_info, "\n{:<width$}{}", "", author, width = pad)?;
97+
write!(
98+
authors_info,
99+
"\n{:<width$}{}",
100+
"",
101+
author,
102+
width = pad + digit_difference(self.top_contribution(), author.contribution)
103+
)?;
86104
}
87105
}
88106

@@ -221,4 +239,39 @@ mod test {
221239
.value()
222240
.contains(&"80% Roberto Berto 240".to_string()));
223241
}
242+
#[test]
243+
fn test_author_info_value_alignment_with_three_authors() {
244+
let author = Author::new(
245+
"John Doe".into(),
246+
Some("john.doe@email.com".into()),
247+
1500,
248+
2000,
249+
NumberSeparator::Plain,
250+
);
251+
252+
let author_2 = Author::new(
253+
"Roberto Berto".into(),
254+
None,
255+
240,
256+
300,
257+
NumberSeparator::Plain,
258+
);
259+
260+
let author_3 = Author::new("Jane Doe".into(), None, 1, 100, NumberSeparator::Plain);
261+
262+
let authors_info = AuthorsInfo {
263+
authors: vec![author, author_2, author_3],
264+
};
265+
266+
assert!(authors_info
267+
.value()
268+
.contains(&"75% John Doe <john.doe@email.com> 1500".to_string()));
269+
270+
assert!(authors_info
271+
.value()
272+
.contains(&"80% Roberto Berto 240".to_string()));
273+
274+
// Note the extra leading space to right-align the percentages
275+
assert!(authors_info.value().contains(&" 1% Jane Doe 1".to_string()));
276+
}
224277
}

0 commit comments

Comments
 (0)