13
13
#[ allow( missing_doc) ] ;
14
14
15
15
16
- use std:: io;
16
+ use std:: io:: { Decorator , Writer } ;
17
17
18
18
#[ cfg( not( target_os = "win32" ) ) ] use std:: os;
19
19
#[ cfg( not( target_os = "win32" ) ) ] use terminfo:: * ;
@@ -94,21 +94,21 @@ fn cap_for_attr(attr: attr::Attr) -> &'static str {
94
94
}
95
95
96
96
#[ cfg( not( target_os = "win32" ) ) ]
97
- pub struct Terminal {
97
+ pub struct Terminal < T > {
98
98
priv num_colors : u16 ,
99
- priv out: @ mut io :: Writer ,
99
+ priv out: T ,
100
100
priv ti: ~TermInfo
101
101
}
102
102
103
103
#[ cfg( target_os = "win32" ) ]
104
- pub struct Terminal {
104
+ pub struct Terminal < T > {
105
105
priv num_colors : u16 ,
106
- priv out: @ mut io :: Writer ,
106
+ priv out: T ,
107
107
}
108
108
109
109
#[ cfg( not( target_os = "win32" ) ) ]
110
- impl Terminal {
111
- pub fn new ( out : @ mut io :: Writer ) -> Result < Terminal , ~str > {
110
+ impl < T : Writer > Terminal < T > {
111
+ pub fn new ( out : T ) -> Result < Terminal < T > , ~str > {
112
112
let term = os:: getenv ( "TERM" ) ;
113
113
if term. is_none ( ) {
114
114
return Err ( ~"TERM environment variable undefined") ;
@@ -138,7 +138,7 @@ impl Terminal {
138
138
/// the corresponding normal color will be used instead.
139
139
///
140
140
/// Returns true if the color was set, false otherwise.
141
- pub fn fg ( & self , color : color:: Color ) -> bool {
141
+ pub fn fg ( & mut self , color : color:: Color ) -> bool {
142
142
let color = self . dim_if_necessary ( color) ;
143
143
if self . num_colors > color {
144
144
let s = expand ( * self . ti . strings . find_equiv ( & ( "setaf" ) ) . unwrap ( ) ,
@@ -158,7 +158,7 @@ impl Terminal {
158
158
/// the corresponding normal color will be used instead.
159
159
///
160
160
/// Returns true if the color was set, false otherwise.
161
- pub fn bg ( & self , color : color:: Color ) -> bool {
161
+ pub fn bg ( & mut self , color : color:: Color ) -> bool {
162
162
let color = self . dim_if_necessary ( color) ;
163
163
if self . num_colors > color {
164
164
let s = expand ( * self . ti . strings . find_equiv ( & ( "setab" ) ) . unwrap ( ) ,
@@ -175,7 +175,7 @@ impl Terminal {
175
175
176
176
/// Sets the given terminal attribute, if supported.
177
177
/// Returns true if the attribute was supported, false otherwise.
178
- pub fn attr ( & self , attr : attr:: Attr ) -> bool {
178
+ pub fn attr ( & mut self , attr : attr:: Attr ) -> bool {
179
179
match attr {
180
180
attr:: ForegroundColor ( c) => self . fg ( c) ,
181
181
attr:: BackgroundColor ( c) => self . bg ( c) ,
@@ -210,7 +210,7 @@ impl Terminal {
210
210
}
211
211
212
212
/// Resets all terminal attributes and color to the default.
213
- pub fn reset ( & self ) {
213
+ pub fn reset ( & mut self ) {
214
214
let mut cap = self . ti . strings . find_equiv ( & ( "sgr0" ) ) ;
215
215
if cap. is_none ( ) {
216
216
// are there any terminals that have color/attrs and not sgr0?
@@ -242,20 +242,20 @@ impl Terminal {
242
242
}
243
243
244
244
#[ cfg( target_os = "win32" ) ]
245
- impl Terminal {
246
- pub fn new ( out : @ mut io :: Writer ) -> Result < Terminal , ~str > {
245
+ impl < T : Writer > Terminal < T > {
246
+ pub fn new ( out : T ) -> Result < Terminal < T > , ~str > {
247
247
return Ok ( Terminal { out : out, num_colors : 0 } ) ;
248
248
}
249
249
250
- pub fn fg ( & self , _color : color:: Color ) -> bool {
250
+ pub fn fg ( & mut self , _color : color:: Color ) -> bool {
251
251
false
252
252
}
253
253
254
- pub fn bg ( & self , _color : color:: Color ) -> bool {
254
+ pub fn bg ( & mut self , _color : color:: Color ) -> bool {
255
255
false
256
256
}
257
257
258
- pub fn attr ( & self , _attr : attr:: Attr ) -> bool {
258
+ pub fn attr ( & mut self , _attr : attr:: Attr ) -> bool {
259
259
false
260
260
}
261
261
@@ -266,3 +266,27 @@ impl Terminal {
266
266
pub fn reset ( & self ) {
267
267
}
268
268
}
269
+
270
+ impl < T : Writer > Decorator < T > for Terminal < T > {
271
+ fn inner ( self ) -> T {
272
+ self . out
273
+ }
274
+
275
+ fn inner_ref < ' a > ( & ' a self ) -> & ' a T {
276
+ & self . out
277
+ }
278
+
279
+ fn inner_mut_ref < ' a > ( & ' a mut self ) -> & ' a mut T {
280
+ & mut self . out
281
+ }
282
+ }
283
+
284
+ impl < T : Writer > Writer for Terminal < T > {
285
+ fn write ( & mut self , buf : & [ u8 ] ) {
286
+ self . out . write ( buf) ;
287
+ }
288
+
289
+ fn flush ( & mut self ) {
290
+ self . out . flush ( ) ;
291
+ }
292
+ }
0 commit comments