@@ -24,35 +24,38 @@ use core::io;
24
24
25
25
// FIXME (#2807): Windows support.
26
26
27
- pub static color_black: u8 = 0u8 ;
28
- pub static color_red: u8 = 1u8 ;
29
- pub static color_green: u8 = 2u8 ;
30
- pub static color_yellow: u8 = 3u8 ;
31
- pub static color_blue: u8 = 4u8 ;
32
- pub static color_magenta: u8 = 5u8 ;
33
- pub static color_cyan: u8 = 6u8 ;
34
- pub static color_light_gray: u8 = 7u8 ;
35
- pub static color_light_grey: u8 = 7u8 ;
36
- pub static color_dark_gray: u8 = 8u8 ;
37
- pub static color_dark_grey: u8 = 8u8 ;
38
- pub static color_bright_red: u8 = 9u8 ;
39
- pub static color_bright_green: u8 = 10u8 ;
40
- pub static color_bright_yellow: u8 = 11u8 ;
41
- pub static color_bright_blue: u8 = 12u8 ;
42
- pub static color_bright_magenta: u8 = 13u8 ;
43
- pub static color_bright_cyan: u8 = 14u8 ;
44
- pub static color_bright_white: u8 = 15u8 ;
27
+ pub mod color {
28
+ pub type Color = u16 ;
29
+
30
+ pub static black: Color = 0u16 ;
31
+ pub static red: Color = 1u16 ;
32
+ pub static green: Color = 2u16 ;
33
+ pub static yellow: Color = 3u16 ;
34
+ pub static blue: Color = 4u16 ;
35
+ pub static magenta: Color = 5u16 ;
36
+ pub static cyan: Color = 6u16 ;
37
+ pub static white: Color = 7u16 ;
38
+
39
+ pub static bright_black: Color = 8u16 ;
40
+ pub static bright_red: Color = 9u16 ;
41
+ pub static bright_green: Color = 10u16 ;
42
+ pub static bright_yellow: Color = 11u16 ;
43
+ pub static bright_blue: Color = 12u16 ;
44
+ pub static bright_magenta: Color = 13u16 ;
45
+ pub static bright_cyan: Color = 14u16 ;
46
+ pub static bright_white: Color = 15u16 ;
47
+ }
45
48
46
49
#[ cfg( not( target_os = "win32" ) ) ]
47
50
pub struct Terminal {
48
- color_supported : bool ,
51
+ num_colors : u16 ,
49
52
priv out: @io:: Writer ,
50
53
priv ti: ~TermInfo
51
54
}
52
55
53
56
#[ cfg( target_os = "win32" ) ]
54
57
pub struct Terminal {
55
- color_supported : bool ,
58
+ num_colors : u16 ,
56
59
priv out: @io:: Writer ,
57
60
}
58
61
@@ -66,66 +69,81 @@ impl Terminal {
66
69
67
70
let entry = open ( term. unwrap ( ) ) ;
68
71
if entry. is_err ( ) {
69
- return Err ( entry. get_err ( ) ) ;
72
+ return Err ( entry. unwrap_err ( ) ) ;
70
73
}
71
74
72
- let ti = parse ( entry. get ( ) , false ) ;
75
+ let ti = parse ( entry. unwrap ( ) , false ) ;
73
76
if ti. is_err ( ) {
74
- return Err ( entry . get_err ( ) ) ;
77
+ return Err ( ti . unwrap_err ( ) ) ;
75
78
}
76
79
77
- let mut inf = ti. get ( ) ;
78
- let cs = * inf. numbers . find_or_insert ( ~"colors", 0 ) >= 16
79
- && inf. strings . find ( & ~"setaf") . is_some ( )
80
- && inf. strings . find_equiv ( & ( "setab" ) ) . is_some ( ) ;
80
+ let inf = ti. unwrap ( ) ;
81
+ let nc = if inf. strings . find_equiv ( & ( "setaf" ) ) . is_some ( )
82
+ && inf. strings . find_equiv ( & ( "setab" ) ) . is_some ( ) {
83
+ inf. numbers . find_equiv ( & ( "colors" ) ) . map_consume_default ( 0 , |& n| n)
84
+ } else { 0 } ;
81
85
82
- return Ok ( Terminal { out : out, ti : inf, color_supported : cs } ) ;
86
+ return Ok ( Terminal { out : out, ti : inf, num_colors : nc } ) ;
83
87
}
84
- pub fn fg ( & self , color : u8 ) {
85
- if self . color_supported {
88
+ /// Sets the foreground color to the given color.
89
+ ///
90
+ /// If the color is a bright color, but the terminal only supports 8 colors,
91
+ /// the corresponding normal color will be used instead.
92
+ pub fn fg ( & self , color : color:: Color ) {
93
+ let color = self . dim_if_necessary ( color) ;
94
+ if self . num_colors > color {
86
95
let s = expand ( * self . ti . strings . find_equiv ( & ( "setaf" ) ) . unwrap ( ) ,
87
96
[ Number ( color as int ) ] , & mut Variables :: new ( ) ) ;
88
97
if s. is_ok ( ) {
89
- self . out . write ( s. get ( ) ) ;
98
+ self . out . write ( s. unwrap ( ) ) ;
90
99
} else {
91
- warn ! ( s. get_err ( ) ) ;
100
+ warn ! ( s. unwrap_err ( ) ) ;
92
101
}
93
102
}
94
103
}
95
- pub fn bg ( & self , color : u8 ) {
96
- if self . color_supported {
104
+ /// Sets the background color to the given color.
105
+ ///
106
+ /// If the color is a bright color, but the terminal only supports 8 colors,
107
+ /// the corresponding normal color will be used instead.
108
+ pub fn bg ( & self , color : color:: Color ) {
109
+ let color = self . dim_if_necessary ( color) ;
110
+ if self . num_colors > color {
97
111
let s = expand ( * self . ti . strings . find_equiv ( & ( "setab" ) ) . unwrap ( ) ,
98
112
[ Number ( color as int ) ] , & mut Variables :: new ( ) ) ;
99
113
if s. is_ok ( ) {
100
- self . out . write ( s. get ( ) ) ;
114
+ self . out . write ( s. unwrap ( ) ) ;
101
115
} else {
102
- warn ! ( s. get_err ( ) ) ;
116
+ warn ! ( s. unwrap_err ( ) ) ;
103
117
}
104
118
}
105
119
}
106
120
pub fn reset ( & self ) {
107
- if self . color_supported {
108
- let mut vars = Variables :: new ( ) ;
109
- let s = expand ( * self . ti . strings . find_equiv ( & ( "op" ) ) . unwrap ( ) , [ ] , & mut vars) ;
110
- if s. is_ok ( ) {
111
- self . out . write ( s. get ( ) ) ;
112
- } else {
113
- warn ! ( s. get_err( ) ) ;
114
- }
121
+ let mut vars = Variables :: new ( ) ;
122
+ let s = expand ( * self . ti . strings . find_equiv ( & ( "op" ) ) . unwrap ( ) , [ ] , & mut vars) ;
123
+ if s. is_ok ( ) {
124
+ self . out . write ( s. unwrap ( ) ) ;
125
+ } else {
126
+ warn ! ( s. unwrap_err( ) ) ;
115
127
}
116
128
}
129
+
130
+ priv fn dim_if_necessary ( & self , color : color:: Color ) -> color:: Color {
131
+ if color >= self . num_colors && color >= 8 && color < 16 {
132
+ color-8
133
+ } else { color }
134
+ }
117
135
}
118
136
119
137
#[ cfg( target_os = "win32" ) ]
120
138
impl Terminal {
121
139
pub fn new ( out : @io:: Writer ) -> Result < Terminal , ~str > {
122
- return Ok ( Terminal { out : out, color_supported : false } ) ;
140
+ return Ok ( Terminal { out : out, num_colors : 0 } ) ;
123
141
}
124
142
125
- pub fn fg ( & self , _color : u8 ) {
143
+ pub fn fg ( & self , _color : color :: Color ) {
126
144
}
127
145
128
- pub fn bg ( & self , _color : u8 ) {
146
+ pub fn bg ( & self , _color : color :: Color ) {
129
147
}
130
148
131
149
pub fn reset ( & self ) {
0 commit comments