1313#[ allow( missing_doc) ] ;
1414
1515
16- use std:: io;
16+ use std:: io:: { Decorator , Writer } ;
1717
1818#[ cfg( not( target_os = "win32" ) ) ] use std:: os;
1919#[ cfg( not( target_os = "win32" ) ) ] use terminfo:: * ;
@@ -94,21 +94,21 @@ fn cap_for_attr(attr: attr::Attr) -> &'static str {
9494}
9595
9696#[ cfg( not( target_os = "win32" ) ) ]
97- pub struct Terminal {
97+ pub struct Terminal < T > {
9898 priv num_colors : u16 ,
99- priv out: @ mut io :: Writer ,
99+ priv out: T ,
100100 priv ti: ~TermInfo
101101}
102102
103103#[ cfg( target_os = "win32" ) ]
104- pub struct Terminal {
104+ pub struct Terminal < T > {
105105 priv num_colors : u16 ,
106- priv out: @ mut io :: Writer ,
106+ priv out: T ,
107107}
108108
109109#[ 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 > {
112112 let term = os:: getenv ( "TERM" ) ;
113113 if term. is_none ( ) {
114114 return Err ( ~"TERM environment variable undefined") ;
@@ -138,7 +138,7 @@ impl Terminal {
138138 /// the corresponding normal color will be used instead.
139139 ///
140140 /// 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 {
142142 let color = self . dim_if_necessary ( color) ;
143143 if self . num_colors > color {
144144 let s = expand ( * self . ti . strings . find_equiv ( & ( "setaf" ) ) . unwrap ( ) ,
@@ -158,7 +158,7 @@ impl Terminal {
158158 /// the corresponding normal color will be used instead.
159159 ///
160160 /// 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 {
162162 let color = self . dim_if_necessary ( color) ;
163163 if self . num_colors > color {
164164 let s = expand ( * self . ti . strings . find_equiv ( & ( "setab" ) ) . unwrap ( ) ,
@@ -175,7 +175,7 @@ impl Terminal {
175175
176176 /// Sets the given terminal attribute, if supported.
177177 /// 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 {
179179 match attr {
180180 attr:: ForegroundColor ( c) => self . fg ( c) ,
181181 attr:: BackgroundColor ( c) => self . bg ( c) ,
@@ -210,7 +210,7 @@ impl Terminal {
210210 }
211211
212212 /// Resets all terminal attributes and color to the default.
213- pub fn reset ( & self ) {
213+ pub fn reset ( & mut self ) {
214214 let mut cap = self . ti . strings . find_equiv ( & ( "sgr0" ) ) ;
215215 if cap. is_none ( ) {
216216 // are there any terminals that have color/attrs and not sgr0?
@@ -242,20 +242,20 @@ impl Terminal {
242242}
243243
244244#[ 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 > {
247247 return Ok ( Terminal { out : out, num_colors : 0 } ) ;
248248 }
249249
250- pub fn fg ( & self , _color : color:: Color ) -> bool {
250+ pub fn fg ( & mut self , _color : color:: Color ) -> bool {
251251 false
252252 }
253253
254- pub fn bg ( & self , _color : color:: Color ) -> bool {
254+ pub fn bg ( & mut self , _color : color:: Color ) -> bool {
255255 false
256256 }
257257
258- pub fn attr ( & self , _attr : attr:: Attr ) -> bool {
258+ pub fn attr ( & mut self , _attr : attr:: Attr ) -> bool {
259259 false
260260 }
261261
@@ -266,3 +266,27 @@ impl Terminal {
266266 pub fn reset ( & self ) {
267267 }
268268}
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