@@ -18,7 +18,6 @@ use git2::{Repository, Oid};
18
18
use license:: License ;
19
19
use clap:: { App , Arg } ;
20
20
use std:: {
21
- cmp,
22
21
collections:: HashMap ,
23
22
convert:: From ,
24
23
ffi:: OsStr ,
@@ -31,6 +30,9 @@ use std::{
31
30
} ;
32
31
use strum:: { IntoEnumIterator , EnumCount } ;
33
32
33
+ mod parse;
34
+ use parse:: AsciiArt ;
35
+
34
36
type Result < T > = result:: Result < T , Error > ;
35
37
36
38
struct Info {
@@ -207,97 +209,26 @@ impl fmt::Display for Info {
207
209
" " . on_bright_white( ) ,
208
210
) ?;
209
211
210
- let logo = self . get_ascii ( ) ;
211
- let mut logo_lines = logo. lines ( ) ;
212
- let mut info_lines = buffer. lines ( ) ;
213
- let left_pad = logo. lines ( ) . map ( |l| true_len ( l) ) . max ( ) . unwrap_or ( 0 ) ;
214
-
215
- for _ in 0 ..cmp:: max ( count_newlines ( logo) , count_newlines ( & buffer) ) {
216
- let logo_line = match logo_lines. next ( ) {
217
- Some ( line) => line,
218
- None => "" ,
219
- } ;
220
212
221
- let info_line = match info_lines. next ( ) {
222
- Some ( line) => line,
223
- None => "" ,
224
- } ;
213
+ let mut logo_lines = AsciiArt :: new ( self . get_ascii ( ) , self . colors ( ) ) ;
214
+ let mut info_lines = buffer. lines ( ) ;
225
215
226
- let ( logo_line, extra_pad) = colorize_str ( logo_line, self . colors ( ) ) ;
227
- // If the string is empty the extra padding should not be added
228
- let pad = if logo_line. is_empty ( ) {
229
- left_pad
230
- } else {
231
- left_pad + extra_pad
232
- } ;
233
- writeln ! ( f, "{:<width$} {:^}" , logo_line, info_line, width = pad, ) ?;
234
- }
216
+ loop {
217
+ match ( logo_lines. next ( ) , info_lines. next ( ) ) {
218
+ ( Some ( logo_line) , Some ( info_line) ) =>
219
+ writeln ! ( f, "{} {:^}" , logo_line, info_line) ?,
220
+ ( Some ( logo_line) , None ) =>
221
+ writeln ! ( f, "{}" , logo_line) ?,
222
+ ( None , Some ( info_line) ) =>
223
+ writeln ! ( f, "{:<width$} {:^}" , "" , info_line, width = logo_lines. width( ) ) ?,
224
+ ( None , None ) => break ,
225
+ }
226
+ } ;
235
227
236
228
Ok ( ( ) )
237
229
}
238
230
}
239
231
240
- fn count_newlines ( s : & str ) -> usize {
241
- bytecount:: count ( s. as_bytes ( ) , b'\n' )
242
- }
243
-
244
- /// Transforms a string with color format into one with proper
245
- /// escape characters for color display.
246
- ///
247
- /// Colors are specified with {0}, {1}... where the number represents
248
- /// the nth element in the colors Vec provided to the function.
249
- /// If there are more colors in the ascii than in the Vec it
250
- /// defaults to white.
251
- /// The usize in the tuple refers to the extra padding needed
252
- /// which comes from the added escape characters.
253
- fn colorize_str ( line : & str , colors : Vec < Color > ) -> ( String , usize ) {
254
- // Extract colors from string coded with {n}
255
- let mut colors_in_str: Vec < Color > = line. split ( '{' ) . fold ( Vec :: new ( ) , |mut acc, s| {
256
- if s. len ( ) > 2 {
257
- let i = s. chars ( ) . nth ( 0 ) . unwrap_or ( '0' ) . to_digit ( 10 ) . unwrap_or ( 0 ) ;
258
- acc. push ( * colors. get ( i as usize ) . unwrap_or ( & Color :: White ) ) ;
259
- }
260
- acc
261
- } ) ;
262
-
263
- if colors_in_str. is_empty ( ) {
264
- colors_in_str. push ( match colors. get ( 0 ) {
265
- Some ( & c) => c,
266
- None => Color :: White ,
267
- } ) ;
268
- }
269
-
270
- let mut colors_iter = colors_in_str. iter ( ) ;
271
-
272
- let out_str = line. split ( '{' ) . fold ( String :: new ( ) , |mut acc, s| {
273
- if s. len ( ) > 2 {
274
- let s: String = s. chars ( ) . skip ( 2 ) . collect ( ) ;
275
- let c = match colors_iter. next ( ) {
276
- Some ( & c) => c,
277
- None => Color :: White ,
278
- } ;
279
- acc. push_str ( & format ! ( "{}" , s. color( c) ) ) ;
280
- }
281
- acc
282
- } ) ;
283
- ( out_str, colors_in_str. len ( ) * 9 )
284
- }
285
-
286
- /// Returns the true length of a string after substracting the {n}
287
- /// color declarations.
288
- fn true_len ( line : & str ) -> usize {
289
- line. split ( '{' )
290
- . fold ( String :: new ( ) , |mut acc, s| {
291
- if s. len ( ) > 2 {
292
- acc. push_str ( & s. chars ( ) . skip ( 2 ) . collect :: < String > ( ) ) ;
293
- } else {
294
- acc. push_str ( s) ;
295
- }
296
- acc
297
- } )
298
- . len ( )
299
- }
300
-
301
232
struct CommitInfo {
302
233
commit : Oid ,
303
234
refs : Vec < String > ,
0 commit comments