Skip to content

Commit 5dd1bc3

Browse files
committed
Changes for Windows terminal
1 parent dd2a1e3 commit 5dd1bc3

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

src/libterm/lib.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,16 @@ pub fn stdout() -> Option<Box<Terminal<WriterWrapper> + Send>> {
9898
/// Return a Terminal wrapping stdout, or None if a terminal couldn't be
9999
/// opened.
100100
pub fn stdout() -> Option<Box<Terminal<WriterWrapper> + Send>> {
101-
let ti: Option<TerminfoTerminal<WriterWrapper>>
102-
= Terminal::new(WriterWrapper {
103-
wrapped: box std::io::stdout() as Box<Writer + Send>,
104-
});
101+
let ti = TerminfoTerminal::new(WriterWrapper {
102+
wrapped: box std::io::stdout() as Box<Writer + Send>,
103+
});
105104

106105
match ti {
107-
Some(t) => Some(box t as Box<Terminal<WriterWrapper> + Send>),
106+
Some(t) => Some(t),
108107
None => {
109-
let wc: Option<WinConsole<WriterWrapper>>
110-
= Terminal::new(WriterWrapper {
111-
wrapped: box std::io::stdout() as Box<Writer + Send>,
112-
});
113-
wc.map(|w| box w as Box<Terminal<WriterWrapper> + Send>)
108+
WinConsole::new(WriterWrapper {
109+
wrapped: box std::io::stdout() as Box<Writer + Send>,
110+
})
114111
}
115112
}
116113
}
@@ -128,19 +125,16 @@ pub fn stderr() -> Option<Box<Terminal<WriterWrapper> + Send> + Send> {
128125
/// Return a Terminal wrapping stderr, or None if a terminal couldn't be
129126
/// opened.
130127
pub fn stderr() -> Option<Box<Terminal<WriterWrapper> + Send> + Send> {
131-
let ti: Option<TerminfoTerminal<WriterWrapper>>
132-
= Terminal::new(WriterWrapper {
133-
wrapped: box std::io::stderr() as Box<Writer + Send>,
134-
});
128+
let ti = TerminfoTerminal::new(WriterWrapper {
129+
wrapped: box std::io::stderr() as Box<Writer + Send>,
130+
});
135131

136132
match ti {
137-
Some(t) => Some(box t as Box<Terminal<WriterWrapper> + Send>),
133+
Some(t) => Some(t),
138134
None => {
139-
let wc: Option<WinConsole<WriterWrapper>>
140-
= Terminal::new(WriterWrapper {
141-
wrapped: box std::io::stderr() as Box<Writer + Send>,
142-
});
143-
wc.map(|w| box w as Box<Terminal<WriterWrapper> + Send>)
135+
WinConsole::new(WriterWrapper {
136+
wrapped: box std::io::stderr() as Box<Writer + Send>,
137+
})
144138
}
145139
}
146140
}

src/libterm/win.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::io::IoResult;
1818

1919
use attr;
2020
use color;
21-
use Terminal;
21+
use {Terminal,UnwrappableTerminal};
2222

2323
/// A Terminal implementation which uses the Win32 Console API.
2424
pub struct WinConsole<T> {
@@ -125,24 +125,6 @@ impl<T: Writer> Writer for WinConsole<T> {
125125
}
126126

127127
impl<T: Writer> Terminal<T> for WinConsole<T> {
128-
fn new(out: T) -> Option<WinConsole<T>> {
129-
let fg;
130-
let bg;
131-
unsafe {
132-
let mut buffer_info = ::std::mem::uninitialized();
133-
if GetConsoleScreenBufferInfo(GetStdHandle(-11), &mut buffer_info) != 0 {
134-
fg = bits_to_color(buffer_info.wAttributes);
135-
bg = bits_to_color(buffer_info.wAttributes >> 4);
136-
} else {
137-
fg = color::WHITE;
138-
bg = color::BLACK;
139-
}
140-
}
141-
Some(WinConsole { buf: out,
142-
def_foreground: fg, def_background: bg,
143-
foreground: fg, background: bg } )
144-
}
145-
146128
fn fg(&mut self, color: color::Color) -> IoResult<bool> {
147129
self.foreground = color;
148130
self.apply();
@@ -190,9 +172,31 @@ impl<T: Writer> Terminal<T> for WinConsole<T> {
190172
Ok(())
191173
}
192174

193-
fn unwrap(self) -> T { self.buf }
194-
195175
fn get_ref<'a>(&'a self) -> &'a T { &self.buf }
196176

197177
fn get_mut<'a>(&'a mut self) -> &'a mut T { &mut self.buf }
198178
}
179+
180+
impl<T: Writer> WinConsole<T> {
181+
fn new(out: T) -> Option<Box<WinConsole<T>+Send+'static>> {
182+
let fg;
183+
let bg;
184+
unsafe {
185+
let mut buffer_info = ::std::mem::uninitialized();
186+
if GetConsoleScreenBufferInfo(GetStdHandle(-11), &mut buffer_info) != 0 {
187+
fg = bits_to_color(buffer_info.wAttributes);
188+
bg = bits_to_color(buffer_info.wAttributes >> 4);
189+
} else {
190+
fg = color::WHITE;
191+
bg = color::BLACK;
192+
}
193+
}
194+
Some(box WinConsole { buf: out,
195+
def_foreground: fg, def_background: bg,
196+
foreground: fg, background: bg } )
197+
}
198+
}
199+
200+
impl<T: Writer> UnwrappableTerminal<T> for WinConsole<T> {
201+
fn unwrap(self) -> T { self.buf }
202+
}

0 commit comments

Comments
 (0)