diff --git a/src/libterm/win.rs b/src/libterm/win.rs index 60b811db8fc..7ce6fb658b5 100644 --- a/src/libterm/win.rs +++ b/src/libterm/win.rs @@ -91,7 +91,7 @@ fn bits_to_color(bits: u16) -> color::Color { } } -impl WinConsole { +impl WinConsole { fn apply(&mut self) { let _unused = self.buf.flush(); let mut accum: libc::WORD = 0; @@ -112,6 +112,26 @@ impl WinConsole { SetConsoleTextAttribute(out, accum); } } + + /// Returns `None` whenever the terminal cannot be created for some + /// reason. + pub fn new(out: T) -> Option+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 } as Box+Send>) + } } impl Writer for WinConsole { @@ -124,7 +144,7 @@ impl Writer for WinConsole { } } -impl Terminal for WinConsole { +impl Terminal for WinConsole { fn fg(&mut self, color: color::Color) -> IoResult { self.foreground = color; self.apply(); @@ -177,26 +197,6 @@ impl Terminal for WinConsole { fn get_mut<'a>(&'a mut self) -> &'a mut T { &mut self.buf } } -impl WinConsole { - fn new(out: T) -> Option+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 UnwrappableTerminal for WinConsole { +impl UnwrappableTerminal for WinConsole { fn unwrap(self) -> T { self.buf } }