@@ -72,10 +72,45 @@ fn print_top_helper<T: Music>(music_dict: HashMap<T, u32>, num: usize) {
72
72
let mus = music_vec. get ( i) . unwrap ( ) ;
73
73
let m = mus. 0 ;
74
74
let n = mus. 1 ;
75
- println ! ( "#{} \t {} | {} plays" , i + 1 , m, n)
75
+ println ! ( "{}: {} | {} plays" , leading_whitespace ( i + 1 , ind ) , m, n)
76
76
}
77
77
}
78
78
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
+
79
114
fn gather_songs ( entries : & Vec < SongEntry > ) -> HashMap < Song , u32 > {
80
115
let mut songs: HashMap < Song , u32 > = HashMap :: new ( ) ;
81
116
@@ -311,6 +346,11 @@ fn print_album(album: HashMap<Song, u32>) {
311
346
let mus = album_vec. get ( i) . unwrap ( ) ;
312
347
let m = mus. 0 ;
313
348
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
+ )
315
355
}
316
356
}
0 commit comments