@@ -36,51 +36,23 @@ pub struct Info {
36
36
impl std:: fmt:: Display for Info {
37
37
fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
38
38
if !self . config . disabled_fields . git_info {
39
- let git_info_length;
40
- if self . git_username != "" {
41
- git_info_length = self . git_username . len ( ) + self . git_version . len ( ) + 3 ;
42
- write ! (
43
- f,
44
- "{} {} " ,
45
- & self . get_formatted_info_label( & self . git_username, self . color_set. title) ,
46
- & self . get_formatted_info_label( "~" , self . color_set. tilde) ,
47
- ) ?;
48
- } else {
49
- git_info_length = self . git_version . len ( ) ;
50
- }
51
- write_buf (
39
+ let git_info_length = self . git_username . len ( ) + self . git_version . len ( ) + 3 ;
40
+
41
+ writeln ! (
52
42
f,
53
- & self . get_formatted_info_label ( & self . git_version , self . color_set . title ) ,
54
- "" ,
43
+ "{} {} {}" ,
44
+ & self . bold( & self . git_username) . color( self . color_set. title) ,
45
+ & self . bold( "~" ) . color( self . color_set. tilde) ,
46
+ & self . bold( & self . git_version) . color( self . color_set. title)
55
47
) ?;
48
+
56
49
let separator = "-" . repeat ( git_info_length) ;
57
- write_buf (
58
- f,
59
- & self . get_formatted_info_label ( "" , self . color_set . underline ) ,
60
- & self . get_formatted_info_label ( & separator, self . color_set . underline ) ,
61
- ) ?;
50
+
51
+ writeln ! ( f, "{}" , separator. color( self . color_set. underline) , ) ?;
62
52
}
63
53
64
54
if !self . config . disabled_fields . project {
65
- let branches_str = match self . number_of_branches {
66
- 0 => String :: new ( ) ,
67
- 1 => String :: from ( "1 branch" ) ,
68
- _ => format ! ( "{} branches" , self . number_of_branches) ,
69
- } ;
70
-
71
- let tags_str = match self . number_of_tags {
72
- 0 => String :: new ( ) ,
73
- 1 => String :: from ( "1 tag" ) ,
74
- _ => format ! ( "{} tags" , self . number_of_tags) ,
75
- } ;
76
-
77
- let branches_tags_str = if tags_str. is_empty ( ) && branches_str. is_empty ( ) {
78
- String :: new ( )
79
- } else if branches_str. is_empty ( ) || tags_str. is_empty ( ) {
80
- format ! ( "({})" , format!( "{}{}" , tags_str, branches_str) )
81
- } else {
82
- format ! ( "({}, {})" , branches_str, tags_str)
83
- } ;
55
+ let branches_tags_str = self . get_branches_and_tags_field ( ) ;
84
56
85
57
let project_str = & self . get_formatted_subtitle_label (
86
58
"Project" ,
@@ -98,8 +70,9 @@ impl std::fmt::Display for Info {
98
70
}
99
71
100
72
if !self . config . disabled_fields . head {
101
- write_buf (
73
+ writeln ! (
102
74
f,
75
+ "{}{}" ,
103
76
& self . get_formatted_subtitle_label(
104
77
"HEAD" ,
105
78
self . color_set. subtitle,
@@ -109,9 +82,10 @@ impl std::fmt::Display for Info {
109
82
) ?;
110
83
}
111
84
112
- if !self . config . disabled_fields . pending && self . pending != "" {
113
- write_buf (
85
+ if !self . config . disabled_fields . pending && ! self . pending . is_empty ( ) {
86
+ writeln ! (
114
87
f,
88
+ "{}{}" ,
115
89
& self . get_formatted_subtitle_label(
116
90
"Pending" ,
117
91
self . color_set. subtitle,
@@ -122,8 +96,9 @@ impl std::fmt::Display for Info {
122
96
}
123
97
124
98
if !self . config . disabled_fields . version {
125
- write_buf (
99
+ writeln ! (
126
100
f,
101
+ "{}{}" ,
127
102
& self . get_formatted_subtitle_label(
128
103
"Version" ,
129
104
self . color_set. subtitle,
@@ -134,8 +109,9 @@ impl std::fmt::Display for Info {
134
109
}
135
110
136
111
if !self . config . disabled_fields . created {
137
- write_buf (
112
+ writeln ! (
138
113
f,
114
+ "{}{}" ,
139
115
& self . get_formatted_subtitle_label(
140
116
"Created" ,
141
117
self . color_set. subtitle,
@@ -147,52 +123,22 @@ impl std::fmt::Display for Info {
147
123
148
124
if !self . config . disabled_fields . languages && !self . languages . is_empty ( ) {
149
125
if self . languages . len ( ) > 1 {
150
- let title = "Languages" ;
151
- let pad = " " . repeat ( title. len ( ) + 2 ) ;
152
- let mut s = String :: from ( "" ) ;
153
- let languages: Vec < ( String , f64 ) > = {
154
- let mut iter = self . languages . iter ( ) . map ( |x| ( format ! ( "{}" , x. 0 ) , x. 1 ) ) ;
155
- if self . languages . len ( ) > 6 {
156
- let mut languages = iter. by_ref ( ) . take ( 6 ) . collect :: < Vec < _ > > ( ) ;
157
- let other_sum = iter. fold ( 0.0 , |acc, x| acc + x. 1 ) ;
158
- languages. push ( ( "Other" . to_owned ( ) , other_sum) ) ;
159
- languages
160
- } else {
161
- iter. collect ( )
162
- }
163
- } ;
164
-
165
- for ( cnt, language) in languages. iter ( ) . enumerate ( ) {
166
- let formatted_number =
167
- format ! ( "{:.*}" , 1 , language. 1 ) . color ( self . color_set . info ) ;
168
- if cnt != 0 && cnt % 2 == 0 {
169
- s = s + & format ! (
170
- "\n {}{} ({} %) " ,
171
- pad,
172
- language. 0 . color( self . color_set. info) ,
173
- formatted_number
174
- ) ;
175
- } else {
176
- s = s + & format ! (
177
- "{} ({} %) " ,
178
- language. 0 . color( self . color_set. info) ,
179
- formatted_number
180
- ) ;
181
- }
182
- }
126
+ let languages_str = self . get_languages_field ( ) ;
127
+
183
128
writeln ! (
184
129
f,
185
130
"{}{}" ,
186
131
& self . get_formatted_subtitle_label(
187
- title ,
132
+ "Languages" ,
188
133
self . color_set. subtitle,
189
134
self . color_set. colon,
190
135
) ,
191
- s . color( self . color_set. info)
136
+ languages_str . color( self . color_set. info)
192
137
) ?;
193
138
} else {
194
- write_buf (
139
+ writeln ! (
195
140
f,
141
+ "{}{}" ,
196
142
& self . get_formatted_subtitle_label(
197
143
"Language" ,
198
144
self . color_set. subtitle,
@@ -213,38 +159,24 @@ impl std::fmt::Display for Info {
213
159
"Author"
214
160
} ;
215
161
216
- writeln ! (
162
+ let author_str = self . get_author_field ( title) ;
163
+
164
+ write ! (
217
165
f,
218
- "{}{}{} {} {} " ,
166
+ "{}{}" ,
219
167
& self . get_formatted_subtitle_label(
220
168
title,
221
169
self . color_set. subtitle,
222
- self . color_set. colon
170
+ self . color_set. colon,
223
171
) ,
224
- self . authors[ 0 ] . 2 . to_string( ) . color( self . color_set. info) ,
225
- "%" . color( self . color_set. info) ,
226
- self . authors[ 0 ] . 0 . to_string( ) . color( self . color_set. info) ,
227
- self . authors[ 0 ] . 1 . to_string( ) . color( self . color_set. info)
172
+ author_str. color( self . color_set. info) ,
228
173
) ?;
229
-
230
- let title = " " . repeat ( title. len ( ) + 2 ) ;
231
-
232
- for author in self . authors . iter ( ) . skip ( 1 ) {
233
- writeln ! (
234
- f,
235
- "{}{}{} {} {}" ,
236
- self . get_formatted_info_label( & title, self . color_set. subtitle) ,
237
- author. 2 . to_string( ) . color( self . color_set. info) ,
238
- "%" . color( self . color_set. info) ,
239
- author. 0 . to_string( ) . color( self . color_set. info) ,
240
- author. 1 . to_string( ) . color( self . color_set. info)
241
- ) ?;
242
- }
243
174
}
244
175
245
176
if !self . config . disabled_fields . last_change {
246
- write_buf (
177
+ writeln ! (
247
178
f,
179
+ "{}{}" ,
248
180
& self . get_formatted_subtitle_label(
249
181
"Last change" ,
250
182
self . color_set. subtitle,
@@ -255,8 +187,9 @@ impl std::fmt::Display for Info {
255
187
}
256
188
257
189
if !self . config . disabled_fields . repo {
258
- write_buf (
190
+ writeln ! (
259
191
f,
192
+ "{}{}" ,
260
193
& self . get_formatted_subtitle_label(
261
194
"Repo" ,
262
195
self . color_set. subtitle,
@@ -267,8 +200,9 @@ impl std::fmt::Display for Info {
267
200
}
268
201
269
202
if !self . config . disabled_fields . commits {
270
- write_buf (
203
+ writeln ! (
271
204
f,
205
+ "{}{}" ,
272
206
& self . get_formatted_subtitle_label(
273
207
"Commits" ,
274
208
self . color_set. subtitle,
@@ -279,8 +213,9 @@ impl std::fmt::Display for Info {
279
213
}
280
214
281
215
if !self . config . disabled_fields . lines_of_code {
282
- write_buf (
216
+ writeln ! (
283
217
f,
218
+ "{} {}" ,
284
219
& self . get_formatted_subtitle_label(
285
220
"Lines of code" ,
286
221
self . color_set. subtitle,
@@ -291,8 +226,9 @@ impl std::fmt::Display for Info {
291
226
}
292
227
293
228
if !self . config . disabled_fields . size {
294
- write_buf (
229
+ writeln ! (
295
230
f,
231
+ "{}{}" ,
296
232
& self . get_formatted_subtitle_label(
297
233
"Size" ,
298
234
self . color_set. subtitle,
@@ -303,8 +239,9 @@ impl std::fmt::Display for Info {
303
239
}
304
240
305
241
if !self . config . disabled_fields . license {
306
- write_buf (
242
+ writeln ! (
307
243
f,
244
+ "{}{}" ,
308
245
& self . get_formatted_subtitle_label(
309
246
"License" ,
310
247
self . color_set. subtitle,
@@ -719,15 +656,6 @@ impl Info {
719
656
Ok ( output)
720
657
}
721
658
722
- fn get_formatted_info_label ( & self , label : & str , color : Color ) -> ColoredString {
723
- let formatted_label = label. color ( color) ;
724
- if self . config . no_bold {
725
- formatted_label
726
- } else {
727
- formatted_label. bold ( )
728
- }
729
- }
730
-
731
659
fn get_colors (
732
660
ascii_language : & Language ,
733
661
dominant_language : & Language ,
@@ -787,18 +715,102 @@ impl Info {
787
715
colon_clr : Color ,
788
716
) -> ColoredString {
789
717
let formatted_label = format ! ( "{}{} " , label. color( color) , ":" . color( colon_clr) ) ;
718
+ self . bold ( & formatted_label)
719
+ }
720
+
721
+ fn bold ( & self , label : & str ) -> ColoredString {
790
722
if self . config . no_bold {
791
- formatted_label . normal ( )
723
+ label . normal ( )
792
724
} else {
793
- formatted_label . bold ( )
725
+ label . bold ( )
794
726
}
795
727
}
796
- }
797
728
798
- fn write_buf < T : std:: fmt:: Display > (
799
- buffer : & mut std:: fmt:: Formatter ,
800
- title : & ColoredString ,
801
- content : T ,
802
- ) -> std:: fmt:: Result {
803
- writeln ! ( buffer, "{}{}" , title, content)
729
+ fn get_author_field ( & self , title : & str ) -> String {
730
+ let mut authors_str = String :: from ( "" ) ;
731
+
732
+ let pad = title. len ( ) + 2 ;
733
+
734
+ for ( i, author) in self . authors . iter ( ) . enumerate ( ) {
735
+ if i == 0 {
736
+ authors_str. push_str ( & format ! (
737
+ "{}{} {} {}\n " ,
738
+ author. 2 . to_string( ) . color( self . color_set. info) ,
739
+ "%" . color( self . color_set. info) ,
740
+ author. 0 . to_string( ) . color( self . color_set. info) ,
741
+ author. 1 . to_string( ) . color( self . color_set. info) ,
742
+ ) ) ;
743
+ } else {
744
+ authors_str. push_str ( & format ! (
745
+ "{:<width$}{}{} {} {}\n " ,
746
+ "" ,
747
+ author. 2 . to_string( ) . color( self . color_set. info) ,
748
+ "%" . color( self . color_set. info) ,
749
+ author. 0 . to_string( ) . color( self . color_set. info) ,
750
+ author. 1 . to_string( ) . color( self . color_set. info) ,
751
+ width = pad
752
+ ) ) ;
753
+ }
754
+ }
755
+
756
+ authors_str
757
+ }
758
+
759
+ fn get_languages_field ( & self ) -> String {
760
+ let title = "Languages" ;
761
+ let pad = " " . repeat ( title. len ( ) + 2 ) ;
762
+ let mut languages_str = String :: from ( "" ) ;
763
+ let languages: Vec < ( String , f64 ) > = {
764
+ let mut iter = self . languages . iter ( ) . map ( |x| ( format ! ( "{}" , x. 0 ) , x. 1 ) ) ;
765
+ if self . languages . len ( ) > 6 {
766
+ let mut languages = iter. by_ref ( ) . take ( 6 ) . collect :: < Vec < _ > > ( ) ;
767
+ let other_sum = iter. fold ( 0.0 , |acc, x| acc + x. 1 ) ;
768
+ languages. push ( ( "Other" . to_owned ( ) , other_sum) ) ;
769
+ languages
770
+ } else {
771
+ iter. collect ( )
772
+ }
773
+ } ;
774
+
775
+ for ( cnt, language) in languages. iter ( ) . enumerate ( ) {
776
+ let formatted_number = format ! ( "{:.*}" , 1 , language. 1 ) . color ( self . color_set . info ) ;
777
+ if cnt != 0 && cnt % 2 == 0 {
778
+ languages_str. push_str ( & format ! (
779
+ "\n {}{} ({} %) " ,
780
+ pad,
781
+ language. 0 . color( self . color_set. info) ,
782
+ formatted_number
783
+ ) ) ;
784
+ } else {
785
+ languages_str. push_str ( & format ! (
786
+ "{} ({} %) " ,
787
+ language. 0 . color( self . color_set. info) ,
788
+ formatted_number
789
+ ) ) ;
790
+ }
791
+ }
792
+ languages_str
793
+ }
794
+
795
+ fn get_branches_and_tags_field ( & self ) -> String {
796
+ let branches_str = match self . number_of_branches {
797
+ 0 => String :: new ( ) ,
798
+ 1 => String :: from ( "1 branch" ) ,
799
+ _ => format ! ( "{} branches" , self . number_of_branches) ,
800
+ } ;
801
+
802
+ let tags_str = match self . number_of_tags {
803
+ 0 => String :: new ( ) ,
804
+ 1 => String :: from ( "1 tag" ) ,
805
+ _ => format ! ( "{} tags" , self . number_of_tags) ,
806
+ } ;
807
+
808
+ if tags_str. is_empty ( ) && branches_str. is_empty ( ) {
809
+ String :: new ( )
810
+ } else if branches_str. is_empty ( ) || tags_str. is_empty ( ) {
811
+ format ! ( "({})" , format!( "{}{}" , tags_str, branches_str) )
812
+ } else {
813
+ format ! ( "({}, {})" , branches_str, tags_str)
814
+ }
815
+ }
804
816
}
0 commit comments