1 //! Windows console handling
3 // FIXME (#13400): this is only a tiny fraction of the Windows console api
6 use std::io::prelude::*;
12 /// A Terminal implementation that uses the Win32 Console API.
13 pub struct WinConsole<T> {
15 def_foreground: color::Color,
16 def_background: color::Color,
17 foreground: color::Color,
18 background: color::Color,
25 type HANDLE = *mut u8;
27 #[allow(non_snake_case)]
36 #[allow(non_snake_case)]
43 #[allow(non_snake_case)]
45 struct CONSOLE_SCREEN_BUFFER_INFO {
47 dwCursorPosition: COORD,
50 dwMaximumWindowSize: COORD,
53 #[allow(non_snake_case)]
54 #[link(name = "kernel32")]
56 fn SetConsoleTextAttribute(handle: HANDLE, attr: WORD) -> BOOL;
57 fn GetStdHandle(which: DWORD) -> HANDLE;
58 fn GetConsoleScreenBufferInfo(handle: HANDLE, info: *mut CONSOLE_SCREEN_BUFFER_INFO) -> BOOL;
61 fn color_to_bits(color: color::Color) -> u16 {
62 // magic numbers from mingw-w64's wincon.h
64 let bits = match color % 8 {
69 color::YELLOW => 0x2 | 0x4,
70 color::MAGENTA => 0x1 | 0x4,
71 color::CYAN => 0x1 | 0x2,
72 color::WHITE => 0x1 | 0x2 | 0x4,
83 fn bits_to_color(bits: u16) -> color::Color {
84 let color = match bits & 0x7 {
90 0x5 => color::MAGENTA,
96 color | (bits & 0x8) // copy the hi-intensity bit
99 impl<T: Write + Send + 'static> WinConsole<T> {
100 fn apply(&mut self) {
101 let _unused = self.buf.flush();
102 let mut accum: WORD = 0;
103 accum |= color_to_bits(self.foreground);
104 accum |= color_to_bits(self.background) << 4;
107 // Magic -11 means stdout, from
108 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
110 // You may be wondering, "but what about stderr?", and the answer
111 // to that is that setting terminal attributes on the stdout
112 // handle also sets them for stderr, since they go to the same
113 // terminal! Admittedly, this is fragile, since stderr could be
114 // redirected to a different console. This is good enough for
115 // rustc though. See #13400.
116 let out = GetStdHandle(-11i32 as DWORD);
117 SetConsoleTextAttribute(out, accum);
121 /// Returns `None` whenever the terminal cannot be created for some reason.
122 pub fn new(out: T) -> io::Result<WinConsole<T>> {
123 use std::mem::MaybeUninit;
128 let mut buffer_info = MaybeUninit::<CONSOLE_SCREEN_BUFFER_INFO>::uninit();
129 if GetConsoleScreenBufferInfo(
130 GetStdHandle(-11i32 as DWORD),
131 buffer_info.as_mut_ptr()
133 let buffer_info = buffer_info.assume_init() ;
134 fg = bits_to_color(buffer_info.wAttributes);
135 bg = bits_to_color(buffer_info.wAttributes >> 4);
151 impl<T: Write> Write for WinConsole<T> {
152 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
156 fn flush(&mut self) -> io::Result<()> {
161 impl<T: Write + Send + 'static> Terminal for WinConsole<T> {
164 fn fg(&mut self, color: color::Color) -> io::Result<bool> {
165 self.foreground = color;
171 fn bg(&mut self, color: color::Color) -> io::Result<bool> {
172 self.background = color;
178 fn attr(&mut self, attr: Attr) -> io::Result<bool> {
180 Attr::ForegroundColor(f) => {
185 Attr::BackgroundColor(b) => {
194 fn supports_attr(&self, attr: Attr) -> bool {
195 // it claims support for underscore and reverse video, but I can't get
196 // it to do anything -cmr
198 Attr::ForegroundColor(_) | Attr::BackgroundColor(_) => true,
203 fn reset(&mut self) -> io::Result<bool> {
204 self.foreground = self.def_foreground;
205 self.background = self.def_background;
211 fn get_ref(&self) -> &T {
215 fn get_mut(&mut self) -> &mut T {
219 fn into_inner(self) -> T