@@ -32,7 +32,10 @@ struct Info {
32
32
impl fmt:: Display for Info {
33
33
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
34
34
let mut buffer = String :: new ( ) ;
35
- let color = self . color ( ) ;
35
+ let color = match self . colors ( ) . get ( 0 ) {
36
+ Some ( & c) => c,
37
+ None => Color :: White ,
38
+ } ;
36
39
37
40
writeln ! (
38
41
buffer,
@@ -79,27 +82,85 @@ impl fmt::Display for Info {
79
82
80
83
let logo = self . get_ascii ( ) ;
81
84
let mut lines = buffer. lines ( ) ;
82
- let left_pad = logo. lines ( ) . map ( |l| l . len ( ) ) . max ( ) . unwrap_or ( 0 ) ;
85
+ let left_pad = logo. lines ( ) . map ( |l| true_len ( l ) ) . max ( ) . unwrap_or ( 0 ) ;
83
86
84
- for a in logo. lines ( ) {
85
- let b = match lines. next ( ) {
87
+ for logo_line in logo. lines ( ) {
88
+ let info_line = match lines. next ( ) {
86
89
Some ( line) => line,
87
90
None => "" ,
88
91
} ;
89
92
90
- writeln ! (
91
- f,
92
- "{:width$} {}" ,
93
- a. color( color) . bold( ) ,
94
- b,
95
- width = left_pad
96
- ) ?;
93
+ let ( logo_line, extra_pad) = colorize_str ( logo_line, self . colors ( ) ) ;
94
+ // If the string is empty the extra padding should not be added
95
+ let pad = if logo_line. is_empty ( ) {
96
+ left_pad
97
+ } else {
98
+ left_pad + extra_pad
99
+ } ;
100
+ writeln ! ( f, "{:<width$} {:^}" , logo_line, info_line, width = pad, ) ?;
97
101
}
98
102
99
103
Ok ( ( ) )
100
104
}
101
105
}
102
106
107
+ /// Transforms a string with color format into one with proper
108
+ /// escape characters for color display.
109
+ ///
110
+ /// Colors are specified with {0}, {1}... where the number represents
111
+ /// the nth element in the colors Vec provided to the function.
112
+ /// If there are more colors in the ascii than in the Vec it
113
+ /// defaults to white.
114
+ /// The usize in the tuple refers to the extra padding needed
115
+ /// which comes from the added escape characters.
116
+ fn colorize_str ( line : & str , colors : Vec < Color > ) -> ( String , usize ) {
117
+ // Extract colors from string coded with {n}
118
+ let mut colors_in_str: Vec < Color > = line. split ( "{" ) . fold ( Vec :: new ( ) , |mut acc, s| {
119
+ if s. len ( ) > 2 {
120
+ let i = s. chars ( ) . nth ( 0 ) . unwrap_or ( '0' ) . to_digit ( 10 ) . unwrap_or ( 0 ) ;
121
+ acc. push ( * colors. iter ( ) . nth ( i as usize ) . unwrap_or ( & Color :: White ) ) ;
122
+ }
123
+ acc
124
+ } ) ;
125
+
126
+ if colors_in_str. is_empty ( ) {
127
+ colors_in_str. push ( match colors. get ( 0 ) {
128
+ Some ( & c) => c,
129
+ None => Color :: White ,
130
+ } ) ;
131
+ }
132
+
133
+ let mut colors_iter = colors_in_str. iter ( ) ;
134
+
135
+ let out_str = line. split ( "{" ) . fold ( String :: new ( ) , |mut acc, s| {
136
+ if s. len ( ) > 2 {
137
+ let s: String = s. chars ( ) . skip ( 2 ) . collect ( ) ;
138
+ let c = match colors_iter. next ( ) {
139
+ Some ( & c) => c,
140
+ None => Color :: White ,
141
+ } ;
142
+ acc. push_str ( & format ! ( "{}" , s. color( c) ) ) ;
143
+ }
144
+ acc
145
+ } ) ;
146
+ ( out_str, colors_in_str. len ( ) * 9 )
147
+ }
148
+
149
+ /// Returns the true length of a string after substracting the {n}
150
+ /// color declarations.
151
+ fn true_len ( line : & str ) -> usize {
152
+ line. split ( "{" )
153
+ . fold ( String :: new ( ) , |mut acc, s| {
154
+ if s. len ( ) > 2 {
155
+ acc. push_str ( & s. chars ( ) . skip ( 2 ) . collect :: < String > ( ) ) ;
156
+ } else {
157
+ acc. push_str ( s) ;
158
+ }
159
+ acc
160
+ } )
161
+ . len ( )
162
+ }
163
+
103
164
enum Language {
104
165
C ,
105
166
Clojure ,
@@ -378,24 +439,24 @@ impl Info {
378
439
}
379
440
}
380
441
381
- fn color ( & self ) -> Color {
442
+ fn colors ( & self ) -> Vec < Color > {
382
443
match self . language {
383
- Language :: C => Color :: Cyan ,
384
- Language :: Clojure => Color :: Cyan ,
385
- Language :: Cpp => Color :: Yellow ,
386
- Language :: Csharp => Color :: White ,
387
- Language :: Go => Color :: White ,
388
- Language :: Haskell => Color :: Cyan ,
389
- Language :: Java => Color :: Green ,
390
- Language :: Lisp => Color :: Yellow ,
391
- Language :: Lua => Color :: Blue ,
392
- Language :: Python => Color :: Magenta ,
393
- Language :: R => Color :: Blue ,
394
- Language :: Ruby => Color :: Magenta ,
395
- Language :: Rust => Color :: Cyan ,
396
- Language :: Scala => Color :: Blue ,
397
- Language :: Shell => Color :: Green ,
398
- Language :: TypeScript => Color :: Cyan ,
444
+ Language :: C => vec ! [ Color :: BrightBlue , Color :: Blue ] ,
445
+ Language :: Clojure => vec ! [ Color :: Cyan ] ,
446
+ Language :: Cpp => vec ! [ Color :: Yellow ] ,
447
+ Language :: Csharp => vec ! [ Color :: White ] ,
448
+ Language :: Go => vec ! [ Color :: White ] ,
449
+ Language :: Haskell => vec ! [ Color :: Cyan ] ,
450
+ Language :: Java => vec ! [ Color :: Green ] ,
451
+ Language :: Lisp => vec ! [ Color :: Yellow ] ,
452
+ Language :: Lua => vec ! [ Color :: Blue ] ,
453
+ Language :: Python => vec ! [ Color :: Magenta ] ,
454
+ Language :: R => vec ! [ Color :: Blue ] ,
455
+ Language :: Ruby => vec ! [ Color :: Magenta ] ,
456
+ Language :: Rust => vec ! [ Color :: White , Color :: Red ] ,
457
+ Language :: Scala => vec ! [ Color :: Blue ] ,
458
+ Language :: Shell => vec ! [ Color :: Green ] ,
459
+ Language :: TypeScript => vec ! [ Color :: Cyan ] ,
399
460
}
400
461
}
401
462
}
0 commit comments