1
1
use std:: ops:: FnMut ;
2
2
3
- use ansi_term:: Style ;
3
+ use ansi_term:: { Colour , Style } ;
4
4
use ansi_term:: Colour :: * ;
5
5
6
6
@@ -25,11 +25,30 @@ pub struct Pair<'var> {
25
25
pub value : & ' var str ,
26
26
}
27
27
28
+ use std:: iter:: Peekable ;
29
+ fn parse_into_high_colour < ' a , I > ( iter : & mut Peekable < I > ) -> Option < Colour >
30
+ where I : Iterator < Item =& ' a str > {
31
+ match iter. peek ( ) {
32
+ Some ( & "5" ) => {
33
+ let _5 = iter. next ( ) ;
34
+ if let Some ( byte) = iter. next ( ) {
35
+ if let Ok ( num) = byte. parse ( ) {
36
+ return Some ( Fixed ( num) ) ;
37
+ }
38
+ }
39
+ }
40
+ _ => { } ,
41
+ }
42
+
43
+ None
44
+ }
45
+
28
46
impl < ' var > Pair < ' var > {
29
47
pub fn to_style ( & self ) -> Style {
30
48
let mut style = Style :: default ( ) ;
49
+ let mut iter = self . value . split ( ";" ) . peekable ( ) ;
31
50
32
- for num in self . value . split ( ";" ) {
51
+ while let Some ( num) = iter . next ( ) {
33
52
match num {
34
53
35
54
// Bold and italic
@@ -45,6 +64,7 @@ impl<'var> Pair<'var> {
45
64
"35" => style = style. fg ( Purple ) ,
46
65
"36" => style = style. fg ( Cyan ) ,
47
66
"37" => style = style. fg ( White ) ,
67
+ "38" => if let Some ( c) = parse_into_high_colour ( & mut iter) { style = style. fg ( c) } ,
48
68
49
69
// Background colours
50
70
"40" => style = style. on ( Black ) ,
@@ -55,6 +75,8 @@ impl<'var> Pair<'var> {
55
75
"45" => style = style. on ( Purple ) ,
56
76
"46" => style = style. on ( Cyan ) ,
57
77
"47" => style = style. on ( White ) ,
78
+ "48" => if let Some ( c) = parse_into_high_colour ( & mut iter) { style = style. on ( c) } ,
79
+
58
80
_ => { /* ignore the error and do nothing */ } ,
59
81
}
60
82
}
@@ -93,6 +115,16 @@ mod ansi_test {
93
115
test ! ( semis: ";;;;;;" => Style :: default ( ) ) ;
94
116
test ! ( nines: "99999999" => Style :: default ( ) ) ;
95
117
test ! ( word: "GREEN" => Style :: default ( ) ) ;
118
+
119
+ // Higher colours
120
+ test ! ( hifg: "38;5;149" => Fixed ( 149 ) . normal( ) ) ;
121
+ test ! ( hibg: "48;5;1" => Style :: default ( ) . on( Fixed ( 1 ) ) ) ;
122
+ test ! ( hibo: "48;5;1;1" => Style :: default ( ) . on( Fixed ( 1 ) ) . bold( ) ) ;
123
+ test ! ( hiund: "4;48;5;1" => Style :: default ( ) . on( Fixed ( 1 ) ) . underline( ) ) ;
124
+
125
+ test ! ( fgbg: "38;5;121;48;5;212" => Fixed ( 121 ) . on( Fixed ( 212 ) ) ) ;
126
+ test ! ( bgfg: "48;5;121;38;5;212" => Fixed ( 212 ) . on( Fixed ( 121 ) ) ) ;
127
+ test ! ( toohi: "48;5;999" => Style :: default ( ) ) ;
96
128
}
97
129
98
130
0 commit comments