Changes for Windows terminal

This commit is contained in:
Nick Cameron 2014-10-21 10:59:00 +13:00
parent dd2a1e3469
commit 5dd1bc3d14
2 changed files with 39 additions and 41 deletions

View file

@ -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 {
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 {
WinConsole::new(WriterWrapper {
wrapped: box std::io::stdout() as Box<Writer + Send>,
});
wc.map(|w| box w as Box<Terminal<WriterWrapper> + 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 {
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 {
WinConsole::new(WriterWrapper {
wrapped: box std::io::stderr() as Box<Writer + Send>,
});
wc.map(|w| box w as Box<Terminal<WriterWrapper> + Send>)
})
}
}
}

View file

@ -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 }
}