Skip to content

Commit 5ac4d77

Browse files
committed
Implement function for leading whitespaces for top listings
1 parent 4582ca9 commit 5ac4d77

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/display.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,45 @@ fn print_top_helper<T: Music>(music_dict: HashMap<T, u32>, num: usize) {
7272
let mus = music_vec.get(i).unwrap();
7373
let m = mus.0;
7474
let n = mus.1;
75-
println!("#{}\t{} | {} plays", i + 1, m, n)
75+
println!("{}: {} | {} plays", leading_whitespace(i + 1, ind), m, n)
7676
}
7777
}
7878

79+
/// Formats `1` to ` #1` if user wishes for Top 10
80+
/// or to ` #1` if Top 100 etc.
81+
/// # Examples
82+
///
83+
/// ```
84+
/// let num = 6;
85+
/// let max = 100;
86+
///
87+
/// assert_eq!(leading_whitespace(num, max), String::from(" #7"))
88+
/// ```
89+
fn leading_whitespace(num: usize, max_num: usize) -> String {
90+
// https://github.com/Filip-Tomasko/endsong-parser-python/blob/main/src/endsong_parser.py#L551-L578
91+
let mut order_format = String::from("");
92+
93+
// bc as of Rust 1.62 it doesn't support log10 on usize
94+
// https://doc.rust-lang.org/std/primitive.usize.html#method.log10
95+
let num = num as f64;
96+
let max_num = max_num as f64;
97+
98+
let mut num_of_zero = max_num.log10().floor() as usize;
99+
let digits = num.log10() as usize + 1;
100+
101+
loop {
102+
if num_of_zero == 0 {
103+
break;
104+
}
105+
if digits <= num_of_zero {
106+
order_format += " ";
107+
}
108+
num_of_zero -= 1;
109+
}
110+
111+
format!("{}#{}", order_format, num)
112+
}
113+
79114
fn gather_songs(entries: &Vec<SongEntry>) -> HashMap<Song, u32> {
80115
let mut songs: HashMap<Song, u32> = HashMap::new();
81116

@@ -311,6 +346,11 @@ fn print_album(album: HashMap<Song, u32>) {
311346
let mus = album_vec.get(i).unwrap();
312347
let m = mus.0;
313348
let n = mus.1;
314-
println!("#{}\t{} | {} plays", i + 1, m, n)
349+
println!(
350+
"{}: {} | {} plays",
351+
leading_whitespace(i + 1, album_vec.len()),
352+
m,
353+
n
354+
)
315355
}
316356
}

0 commit comments

Comments
 (0)