1
1
use {
2
2
crate :: onefetch:: {
3
- ascii_art:: AsciiArt , cli:: Cli , commit_info:: CommitInfo , error:: * , language:: Language ,
4
- license:: Detector ,
3
+ cli:: Cli , commit_info:: CommitInfo , error:: * , language:: Language , license:: Detector ,
5
4
} ,
6
5
colored:: { Color , ColoredString , Colorize } ,
7
6
git2:: Repository ,
8
7
regex:: Regex ,
9
- std:: fmt:: Write ,
10
8
tokio:: process:: Command ,
11
9
} ;
12
10
@@ -17,7 +15,7 @@ pub struct Info {
17
15
current_commit : CommitInfo ,
18
16
version : String ,
19
17
creation_date : String ,
20
- dominant_language : Language ,
18
+ pub dominant_language : Language ,
21
19
languages : Vec < ( Language , f64 ) > ,
22
20
authors : Vec < ( String , usize , usize ) > ,
23
21
last_change : String ,
@@ -29,13 +27,13 @@ pub struct Info {
29
27
number_of_tags : usize ,
30
28
number_of_branches : usize ,
31
29
license : String ,
32
- config : Cli ,
30
+ pub colors : Vec < Color > ,
31
+ pub config : Cli ,
33
32
}
34
33
35
34
impl std:: fmt:: Display for Info {
36
35
fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
37
- let mut buf = String :: new ( ) ;
38
- let color = match self . colors ( ) . get ( 0 ) {
36
+ let color = match self . colors . get ( 0 ) {
39
37
Some ( & c) => c,
40
38
None => Color :: White ,
41
39
} ;
@@ -44,24 +42,20 @@ impl std::fmt::Display for Info {
44
42
if self . git_username != "" {
45
43
git_info_length = self . git_username . len ( ) + self . git_version . len ( ) + 3 ;
46
44
write ! (
47
- & mut buf ,
45
+ f ,
48
46
"{} ~ " ,
49
47
& self . get_formatted_info_label( & self . git_username, color)
50
48
) ?;
51
49
} else {
52
50
git_info_length = self . git_version . len ( ) ;
53
51
}
54
52
write_buf (
55
- & mut buf ,
53
+ f ,
56
54
& self . get_formatted_info_label ( & self . git_version , color) ,
57
55
"" ,
58
56
) ?;
59
57
let separator = "-" . repeat ( git_info_length) ;
60
- write_buf (
61
- & mut buf,
62
- & self . get_formatted_info_label ( "" , color) ,
63
- & separator,
64
- ) ?;
58
+ write_buf ( f, & self . get_formatted_info_label ( "" , color) , & separator) ?;
65
59
}
66
60
if !self . config . disabled_fields . project {
67
61
let branches_str = match self . number_of_branches {
@@ -87,39 +81,39 @@ impl std::fmt::Display for Info {
87
81
let project_str = & self . get_formatted_info_label ( "Project: " , color) ;
88
82
89
83
writeln ! (
90
- buf ,
84
+ f ,
91
85
"{}{} {}" ,
92
86
project_str, self . project_name, branches_tags_str
93
87
) ?;
94
88
}
95
89
96
90
if !self . config . disabled_fields . head {
97
91
write_buf (
98
- & mut buf ,
92
+ f ,
99
93
& self . get_formatted_info_label ( "HEAD: " , color) ,
100
94
& self . current_commit ,
101
95
) ?;
102
96
}
103
97
104
98
if !self . config . disabled_fields . pending && self . pending != "" {
105
99
write_buf (
106
- & mut buf ,
100
+ f ,
107
101
& self . get_formatted_info_label ( "Pending: " , color) ,
108
102
& self . pending ,
109
103
) ?;
110
104
}
111
105
112
106
if !self . config . disabled_fields . version {
113
107
write_buf (
114
- & mut buf ,
108
+ f ,
115
109
& self . get_formatted_info_label ( "Version: " , color) ,
116
110
& self . version ,
117
111
) ?;
118
112
}
119
113
120
114
if !self . config . disabled_fields . created {
121
115
write_buf (
122
- & mut buf ,
116
+ f ,
123
117
& self . get_formatted_info_label ( "Created: " , color) ,
124
118
& self . creation_date ,
125
119
) ?;
@@ -150,10 +144,10 @@ impl std::fmt::Display for Info {
150
144
s = s + & format ! ( "{} ({} %) " , language. 0 , formatted_number) ;
151
145
}
152
146
}
153
- writeln ! ( buf , "{}{}" , & self . get_formatted_info_label( title, color) , s) ?;
147
+ writeln ! ( f , "{}{}" , & self . get_formatted_info_label( title, color) , s) ?;
154
148
} else {
155
149
write_buf (
156
- & mut buf ,
150
+ f ,
157
151
& self . get_formatted_info_label ( "Language: " , color) ,
158
152
& self . dominant_language ,
159
153
) ?;
@@ -168,7 +162,7 @@ impl std::fmt::Display for Info {
168
162
} ;
169
163
170
164
writeln ! (
171
- buf ,
165
+ f ,
172
166
"{}{}% {} {}" ,
173
167
& self . get_formatted_info_label( title, color) ,
174
168
self . authors[ 0 ] . 2 ,
@@ -180,7 +174,7 @@ impl std::fmt::Display for Info {
180
174
181
175
for author in self . authors . iter ( ) . skip ( 1 ) {
182
176
writeln ! (
183
- buf ,
177
+ f ,
184
178
"{}{}% {} {}" ,
185
179
& self . get_formatted_info_label( & title, color) ,
186
180
author. 2 ,
@@ -192,55 +186,55 @@ impl std::fmt::Display for Info {
192
186
193
187
if !self . config . disabled_fields . last_change {
194
188
write_buf (
195
- & mut buf ,
189
+ f ,
196
190
& self . get_formatted_info_label ( "Last change: " , color) ,
197
191
& self . last_change ,
198
192
) ?;
199
193
}
200
194
201
195
if !self . config . disabled_fields . repo {
202
196
write_buf (
203
- & mut buf ,
197
+ f ,
204
198
& self . get_formatted_info_label ( "Repo: " , color) ,
205
199
& self . repo_url ,
206
200
) ?;
207
201
}
208
202
209
203
if !self . config . disabled_fields . commits {
210
204
write_buf (
211
- & mut buf ,
205
+ f ,
212
206
& self . get_formatted_info_label ( "Commits: " , color) ,
213
207
& self . commits ,
214
208
) ?;
215
209
}
216
210
217
211
if !self . config . disabled_fields . lines_of_code {
218
212
write_buf (
219
- & mut buf ,
213
+ f ,
220
214
& self . get_formatted_info_label ( "Lines of code: " , color) ,
221
215
& self . number_of_lines ,
222
216
) ?;
223
217
}
224
218
225
219
if !self . config . disabled_fields . size {
226
220
write_buf (
227
- & mut buf ,
221
+ f ,
228
222
& self . get_formatted_info_label ( "Size: " , color) ,
229
223
& self . repo_size ,
230
224
) ?;
231
225
}
232
226
233
227
if !self . config . disabled_fields . license {
234
228
write_buf (
235
- & mut buf ,
229
+ f ,
236
230
& self . get_formatted_info_label ( "License: " , color) ,
237
231
& self . license ,
238
232
) ?;
239
233
}
240
234
241
235
if !self . config . no_color_blocks {
242
236
writeln ! (
243
- buf ,
237
+ f ,
244
238
"\n {0}{1}{2}{3}{4}{5}{6}{7}" ,
245
239
" " . on_black( ) ,
246
240
" " . on_red( ) ,
@@ -253,53 +247,6 @@ impl std::fmt::Display for Info {
253
247
) ?;
254
248
}
255
249
256
- let center_pad = " " ;
257
- let mut info_lines = buf. lines ( ) ;
258
-
259
- if self . config . art_off {
260
- writeln ! ( f, "{}" , buf) ?;
261
- } else if let Some ( custom_image) = & self . config . image {
262
- if let Some ( image_backend) = & self . config . image_backend {
263
- writeln ! (
264
- f,
265
- "{}" ,
266
- image_backend. add_image(
267
- info_lines. map( |s| format!( "{}{}" , center_pad, s) ) . collect( ) ,
268
- custom_image
269
- )
270
- ) ?;
271
- } else {
272
- panic ! ( "No image backend found" )
273
- }
274
- } else {
275
- let mut logo_lines = if let Some ( custom_ascii) = & self . config . ascii_input {
276
- AsciiArt :: new ( custom_ascii, Vec :: new ( ) , !self . config . no_bold )
277
- } else {
278
- AsciiArt :: new ( self . get_ascii ( ) , self . colors ( ) , !self . config . no_bold )
279
- } ;
280
-
281
- loop {
282
- match ( logo_lines. next ( ) , info_lines. next ( ) ) {
283
- ( Some ( logo_line) , Some ( info_line) ) => {
284
- writeln ! ( f, "{}{}{:^}" , logo_line, center_pad, info_line) ?
285
- }
286
- ( Some ( logo_line) , None ) => writeln ! ( f, "{}" , logo_line) ?,
287
- ( None , Some ( info_line) ) => writeln ! (
288
- f,
289
- "{:<width$}{}{:^}" ,
290
- "" ,
291
- center_pad,
292
- info_line,
293
- width = logo_lines. width( )
294
- ) ?,
295
- ( None , None ) => {
296
- writeln ! ( f, "\n " ) ?;
297
- break ;
298
- }
299
- }
300
- }
301
- }
302
-
303
250
Ok ( ( ) )
304
251
}
305
252
}
@@ -340,6 +287,12 @@ impl Info {
340
287
let last_change = Info :: get_date_of_last_commit ( & git_history) ;
341
288
let project_license = Detector :: new ( ) ?. get_project_license ( workdir_str) ;
342
289
let dominant_language = Language :: get_dominant_language ( & languages_stats) ;
290
+ let colors = Info :: get_colors (
291
+ & config. ascii_language ,
292
+ & dominant_language,
293
+ & config. ascii_colors ,
294
+ config. true_color ,
295
+ ) ;
343
296
344
297
Ok ( Info {
345
298
git_version : git_v,
@@ -360,6 +313,7 @@ impl Info {
360
313
number_of_tags,
361
314
number_of_branches,
362
315
license : project_license?,
316
+ colors,
363
317
config,
364
318
} )
365
319
}
@@ -690,30 +644,25 @@ impl Info {
690
644
}
691
645
}
692
646
693
- fn get_ascii ( & self ) -> & str {
694
- let language = if let Language :: Unknown = self . config . ascii_language {
695
- & self . dominant_language
696
- } else {
697
- & self . config . ascii_language
698
- } ;
699
-
700
- language. get_ascii_art ( )
701
- }
702
-
703
- fn colors ( & self ) -> Vec < Color > {
704
- let language = if let Language :: Unknown = self . config . ascii_language {
705
- & self . dominant_language
647
+ fn get_colors (
648
+ ascii_language : & Language ,
649
+ dominant_language : & Language ,
650
+ ascii_colors : & [ String ] ,
651
+ true_color : bool ,
652
+ ) -> Vec < Color > {
653
+ let language = if let Language :: Unknown = ascii_language {
654
+ & dominant_language
706
655
} else {
707
- & self . config . ascii_language
656
+ & ascii_language
708
657
} ;
709
658
710
- let colors = language. get_colors ( self . config . true_color ) ;
659
+ let colors = language. get_colors ( true_color) ;
711
660
712
661
let colors: Vec < Color > = colors
713
662
. iter ( )
714
663
. enumerate ( )
715
664
. map ( |( index, default_color) | {
716
- if let Some ( color_num) = self . config . ascii_colors . get ( index) {
665
+ if let Some ( color_num) = ascii_colors. get ( index) {
717
666
if let Some ( color) = Info :: num_to_color ( color_num) {
718
667
return color;
719
668
}
@@ -749,7 +698,7 @@ impl Info {
749
698
}
750
699
751
700
fn write_buf < T : std:: fmt:: Display > (
752
- buffer : & mut String ,
701
+ buffer : & mut std :: fmt :: Formatter ,
753
702
title : & ColoredString ,
754
703
content : T ,
755
704
) -> std:: fmt:: Result {
0 commit comments