@@ -14,12 +14,18 @@ pub struct LanguageWithPercentage {
14
14
15
15
pub struct LanguagesInfo {
16
16
pub languages_with_percentage : Vec < LanguageWithPercentage > ,
17
- pub true_color : bool ,
18
- pub info_color : DynColors ,
17
+ true_color : bool ,
18
+ number_of_languages : usize ,
19
+ info_color : DynColors ,
19
20
}
20
21
21
22
impl LanguagesInfo {
22
- pub fn new ( languages : Vec < ( Language , f64 ) > , true_color : bool , info_color : DynColors ) -> Self {
23
+ pub fn new (
24
+ languages : Vec < ( Language , f64 ) > ,
25
+ true_color : bool ,
26
+ number_of_languages : usize ,
27
+ info_color : DynColors ,
28
+ ) -> Self {
23
29
let languages_with_percentage = languages
24
30
. into_iter ( )
25
31
. map ( |( language, percentage) | LanguageWithPercentage {
@@ -30,6 +36,7 @@ impl LanguagesInfo {
30
36
Self {
31
37
languages_with_percentage,
32
38
true_color,
39
+ number_of_languages,
33
40
info_color,
34
41
}
35
42
}
@@ -65,8 +72,11 @@ impl std::fmt::Display for LanguagesInfo {
65
72
( language. to_string ( ) , percentage, circle_color)
66
73
} ,
67
74
) ;
68
- if self . languages_with_percentage . len ( ) > 6 {
69
- let mut languages = iter. by_ref ( ) . take ( 6 ) . collect :: < Vec < _ > > ( ) ;
75
+ if self . languages_with_percentage . len ( ) > self . number_of_languages {
76
+ let mut languages = iter
77
+ . by_ref ( )
78
+ . take ( self . number_of_languages )
79
+ . collect :: < Vec < _ > > ( ) ;
70
80
let other_perc = iter. fold ( 0.0 , |acc, x| acc + x. 1 ) ;
71
81
languages. push ( (
72
82
"Other" . to_string ( ) ,
@@ -144,6 +154,7 @@ mod test {
144
154
percentage: 100_f64 ,
145
155
} ] ,
146
156
true_color : false ,
157
+ number_of_languages : 6 ,
147
158
info_color : DynColors :: Ansi ( AnsiColors :: White ) ,
148
159
} ;
149
160
let expected_languages_info = format ! (
@@ -158,4 +169,47 @@ mod test {
158
169
159
170
assert_eq ! ( languages_info. value( ) , expected_languages_info) ;
160
171
}
172
+
173
+ #[ test]
174
+ fn should_display_correct_number_of_languages ( ) {
175
+ let languages_info = LanguagesInfo {
176
+ languages_with_percentage : vec ! [
177
+ LanguageWithPercentage {
178
+ language: Language :: Go ,
179
+ percentage: 30_f64 ,
180
+ } ,
181
+ LanguageWithPercentage {
182
+ language: Language :: Erlang ,
183
+ percentage: 40_f64 ,
184
+ } ,
185
+ LanguageWithPercentage {
186
+ language: Language :: Java ,
187
+ percentage: 20_f64 ,
188
+ } ,
189
+ LanguageWithPercentage {
190
+ language: Language :: Rust ,
191
+ percentage: 10_f64 ,
192
+ } ,
193
+ ] ,
194
+ true_color : false ,
195
+ number_of_languages : 2 ,
196
+ info_color : DynColors :: Ansi ( AnsiColors :: White ) ,
197
+ } ;
198
+
199
+ assert ! ( languages_info. value( ) . contains(
200
+ & "Go (30.0 %)"
201
+ . color( DynColors :: Ansi ( AnsiColors :: White ) )
202
+ . to_string( )
203
+ ) ) ;
204
+ assert ! ( languages_info. value( ) . contains(
205
+ & "Erlang (40.0 %)"
206
+ . color( DynColors :: Ansi ( AnsiColors :: White ) )
207
+ . to_string( )
208
+ ) ) ;
209
+ assert ! ( languages_info. value( ) . contains(
210
+ & "Other (30.0 %)"
211
+ . color( DynColors :: Ansi ( AnsiColors :: White ) )
212
+ . to_string( )
213
+ ) ) ;
214
+ }
161
215
}
0 commit comments