diff options
| -rw-r--r-- | src/libterm/lib.rs | 34 | ||||
| -rw-r--r-- | src/libterm/win.rs | 46 |
2 files changed, 39 insertions, 41 deletions
diff --git a/src/libterm/lib.rs b/src/libterm/lib.rs index 37c7162333b..2e93f6badf2 100644 --- a/src/libterm/lib.rs +++ b/src/libterm/lib.rs @@ -98,19 +98,16 @@ pub fn stdout() -> Option<Box<Terminal<WriterWrapper> + Send>> { /// Return a Terminal wrapping stdout, or None if a terminal couldn't be /// opened. pub fn stdout() -> Option<Box<Terminal<WriterWrapper> + Send>> { - let ti: Option<TerminfoTerminal<WriterWrapper>> - = Terminal::new(WriterWrapper { - wrapped: box std::io::stdout() as Box<Writer + Send>, - }); + let ti = TerminfoTerminal::new(WriterWrapper { + wrapped: box std::io::stdout() as Box<Writer + Send>, + }); match ti { - Some(t) => Some(box t as Box<Terminal<WriterWrapper> + Send>), + Some(t) => Some(t), None => { - let wc: Option<WinConsole<WriterWrapper>> - = Terminal::new(WriterWrapper { - wrapped: box std::io::stdout() as Box<Writer + Send>, - }); - wc.map(|w| box w as Box<Terminal<WriterWrapper> + Send>) + WinConsole::new(WriterWrapper { + wrapped: box std::io::stdout() as Box<Writer + Send>, + }) } } } @@ -128,19 +125,16 @@ pub fn stderr() -> Option<Box<Terminal<WriterWrapper> + Send> + Send> { /// Return a Terminal wrapping stderr, or None if a terminal couldn't be /// opened. pub fn stderr() -> Option<Box<Terminal<WriterWrapper> + Send> + Send> { - let ti: Option<TerminfoTerminal<WriterWrapper>> - = Terminal::new(WriterWrapper { - wrapped: box std::io::stderr() as Box<Writer + Send>, - }); + let ti = TerminfoTerminal::new(WriterWrapper { + wrapped: box std::io::stderr() as Box<Writer + Send>, + }); match ti { - Some(t) => Some(box t as Box<Terminal<WriterWrapper> + Send>), + Some(t) => Some(t), None => { - let wc: Option<WinConsole<WriterWrapper>> - = Terminal::new(WriterWrapper { - wrapped: box std::io::stderr() as Box<Writer + Send>, - }); - wc.map(|w| box w as Box<Terminal<WriterWrapper> + Send>) + WinConsole::new(WriterWrapper { + wrapped: box std::io::stderr() as Box<Writer + Send>, + }) } } } diff --git a/src/libterm/win.rs b/src/libterm/win.rs index d4f06403c1a..60b811db8fc 100644 --- a/src/libterm/win.rs +++ b/src/libterm/win.rs @@ -18,7 +18,7 @@ use std::io::IoResult; use attr; use color; -use Terminal; +use {Terminal,UnwrappableTerminal}; /// A Terminal implementation which uses the Win32 Console API. pub struct WinConsole<T> { @@ -125,24 +125,6 @@ impl<T: Writer> Writer for WinConsole<T> { } impl<T: Writer> Terminal<T> for WinConsole<T> { - fn new(out: T) -> Option<WinConsole<T>> { - let fg; - let bg; - unsafe { - let mut buffer_info = ::std::mem::uninitialized(); - if GetConsoleScreenBufferInfo(GetStdHandle(-11), &mut buffer_info) != 0 { - fg = bits_to_color(buffer_info.wAttributes); - bg = bits_to_color(buffer_info.wAttributes >> 4); - } else { - fg = color::WHITE; - bg = color::BLACK; - } - } - Some(WinConsole { buf: out, - def_foreground: fg, def_background: bg, - foreground: fg, background: bg } ) - } - fn fg(&mut self, color: color::Color) -> IoResult<bool> { self.foreground = color; self.apply(); @@ -190,9 +172,31 @@ impl<T: Writer> Terminal<T> for WinConsole<T> { Ok(()) } - fn unwrap(self) -> T { self.buf } - fn get_ref<'a>(&'a self) -> &'a T { &self.buf } fn get_mut<'a>(&'a mut self) -> &'a mut T { &mut self.buf } } + +impl<T: Writer> WinConsole<T> { + fn new(out: T) -> Option<Box<WinConsole<T>+Send+'static>> { + let fg; + let bg; + unsafe { + let mut buffer_info = ::std::mem::uninitialized(); + if GetConsoleScreenBufferInfo(GetStdHandle(-11), &mut buffer_info) != 0 { + fg = bits_to_color(buffer_info.wAttributes); + bg = bits_to_color(buffer_info.wAttributes >> 4); + } else { + fg = color::WHITE; + bg = color::BLACK; + } + } + Some(box WinConsole { buf: out, + def_foreground: fg, def_background: bg, + foreground: fg, background: bg } ) + } +} + +impl<T: Writer> UnwrappableTerminal<T> for WinConsole<T> { + fn unwrap(self) -> T { self.buf } +} |
