2014-04-08 10:18:10 -05:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Windows console handling
|
|
|
|
|
2014-08-23 03:12:59 -05:00
|
|
|
// FIXME (#13400): this is only a tiny fraction of the Windows console api
|
2014-04-08 10:18:10 -05:00
|
|
|
|
|
|
|
extern crate libc;
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
use std::io;
|
|
|
|
use std::io::prelude::*;
|
2014-04-08 10:18:10 -05:00
|
|
|
|
2015-11-23 03:03:52 -06:00
|
|
|
use Attr;
|
2014-04-08 10:18:10 -05:00
|
|
|
use color;
|
2015-11-23 03:03:52 -06:00
|
|
|
use Terminal;
|
2014-04-08 10:18:10 -05:00
|
|
|
|
2014-04-14 09:20:24 -05:00
|
|
|
/// A Terminal implementation which uses the Win32 Console API.
|
2014-04-08 10:18:10 -05:00
|
|
|
pub struct WinConsole<T> {
|
|
|
|
buf: T,
|
2014-09-20 17:22:11 -05:00
|
|
|
def_foreground: color::Color,
|
|
|
|
def_background: color::Color,
|
2014-04-08 10:18:10 -05:00
|
|
|
foreground: color::Color,
|
|
|
|
background: color::Color,
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:23:22 -06:00
|
|
|
type WORD = u16;
|
|
|
|
type DWORD = u32;
|
|
|
|
type BOOL = i32;
|
|
|
|
type HANDLE = *mut u8;
|
|
|
|
|
2014-09-20 17:22:11 -05:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct CONSOLE_SCREEN_BUFFER_INFO {
|
2014-12-30 02:19:41 -06:00
|
|
|
dwSize: [libc::c_short; 2],
|
|
|
|
dwCursorPosition: [libc::c_short; 2],
|
2015-11-02 18:23:22 -06:00
|
|
|
wAttributes: WORD,
|
2014-12-30 02:19:41 -06:00
|
|
|
srWindow: [libc::c_short; 4],
|
|
|
|
dwMaximumWindowSize: [libc::c_short; 2],
|
2014-09-20 17:22:11 -05:00
|
|
|
}
|
|
|
|
|
2014-07-18 07:45:17 -05:00
|
|
|
#[allow(non_snake_case)]
|
2014-04-08 10:18:10 -05:00
|
|
|
#[link(name = "kernel32")]
|
|
|
|
extern "system" {
|
2015-11-02 18:23:22 -06:00
|
|
|
fn SetConsoleTextAttribute(handle: HANDLE, attr: WORD) -> BOOL;
|
|
|
|
fn GetStdHandle(which: DWORD) -> HANDLE;
|
2015-11-21 18:24:56 -06:00
|
|
|
fn GetConsoleScreenBufferInfo(handle: HANDLE, info: *mut CONSOLE_SCREEN_BUFFER_INFO) -> BOOL;
|
2014-04-08 10:18:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn color_to_bits(color: color::Color) -> u16 {
|
|
|
|
// magic numbers from mingw-w64's wincon.h
|
|
|
|
|
|
|
|
let bits = match color % 8 {
|
2015-11-21 18:24:56 -06:00
|
|
|
color::BLACK => 0,
|
|
|
|
color::BLUE => 0x1,
|
|
|
|
color::GREEN => 0x2,
|
|
|
|
color::RED => 0x4,
|
|
|
|
color::YELLOW => 0x2 | 0x4,
|
2014-04-08 10:18:10 -05:00
|
|
|
color::MAGENTA => 0x1 | 0x4,
|
2015-11-21 18:24:56 -06:00
|
|
|
color::CYAN => 0x1 | 0x2,
|
|
|
|
color::WHITE => 0x1 | 0x2 | 0x4,
|
|
|
|
_ => unreachable!(),
|
2014-04-08 10:18:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if color >= 8 {
|
|
|
|
bits | 0x8
|
|
|
|
} else {
|
|
|
|
bits
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 17:22:11 -05:00
|
|
|
fn bits_to_color(bits: u16) -> color::Color {
|
2014-10-30 19:26:07 -05:00
|
|
|
let color = match bits & 0x7 {
|
2014-09-20 17:22:11 -05:00
|
|
|
0 => color::BLACK,
|
|
|
|
0x1 => color::BLUE,
|
|
|
|
0x2 => color::GREEN,
|
|
|
|
0x4 => color::RED,
|
|
|
|
0x6 => color::YELLOW,
|
|
|
|
0x5 => color::MAGENTA,
|
|
|
|
0x3 => color::CYAN,
|
|
|
|
0x7 => color::WHITE,
|
2015-11-21 18:24:56 -06:00
|
|
|
_ => unreachable!(),
|
2014-09-20 17:22:11 -05:00
|
|
|
};
|
|
|
|
|
2014-10-30 19:26:07 -05:00
|
|
|
color | (bits & 0x8) // copy the hi-intensity bit
|
2014-09-20 17:22:11 -05:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:51:22 -06:00
|
|
|
impl<T: Write + Send + 'static> WinConsole<T> {
|
2014-04-08 10:18:10 -05:00
|
|
|
fn apply(&mut self) {
|
2014-05-11 19:08:49 -05:00
|
|
|
let _unused = self.buf.flush();
|
2015-11-02 18:23:22 -06:00
|
|
|
let mut accum: WORD = 0;
|
2014-04-08 10:18:10 -05:00
|
|
|
accum |= color_to_bits(self.foreground);
|
|
|
|
accum |= color_to_bits(self.background) << 4;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// Magic -11 means stdout, from
|
|
|
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
|
|
|
|
//
|
|
|
|
// You may be wondering, "but what about stderr?", and the answer
|
|
|
|
// to that is that setting terminal attributes on the stdout
|
|
|
|
// handle also sets them for stderr, since they go to the same
|
|
|
|
// terminal! Admittedly, this is fragile, since stderr could be
|
|
|
|
// redirected to a different console. This is good enough for
|
|
|
|
// rustc though. See #13400.
|
2015-11-02 18:23:22 -06:00
|
|
|
let out = GetStdHandle(-11i32 as DWORD);
|
2014-04-08 10:18:10 -05:00
|
|
|
SetConsoleTextAttribute(out, accum);
|
|
|
|
}
|
|
|
|
}
|
2014-10-21 23:42:13 -05:00
|
|
|
|
|
|
|
/// Returns `None` whenever the terminal cannot be created for some
|
|
|
|
/// reason.
|
2015-11-23 03:03:52 -06:00
|
|
|
pub fn new(out: T) -> io::Result<WinConsole<T>> {
|
2014-10-21 23:42:13 -05:00
|
|
|
let fg;
|
|
|
|
let bg;
|
|
|
|
unsafe {
|
|
|
|
let mut buffer_info = ::std::mem::uninitialized();
|
2015-11-21 18:24:56 -06:00
|
|
|
if GetConsoleScreenBufferInfo(GetStdHandle(-11i32 as DWORD), &mut buffer_info) != 0 {
|
2014-10-21 23:42:13 -05:00
|
|
|
fg = bits_to_color(buffer_info.wAttributes);
|
|
|
|
bg = bits_to_color(buffer_info.wAttributes >> 4);
|
|
|
|
} else {
|
|
|
|
fg = color::WHITE;
|
|
|
|
bg = color::BLACK;
|
|
|
|
}
|
|
|
|
}
|
2015-11-23 03:03:52 -06:00
|
|
|
Ok(WinConsole {
|
2015-11-21 18:24:56 -06:00
|
|
|
buf: out,
|
|
|
|
def_foreground: fg,
|
|
|
|
def_background: bg,
|
|
|
|
foreground: fg,
|
|
|
|
background: bg,
|
|
|
|
})
|
2014-10-21 23:42:13 -05:00
|
|
|
}
|
2014-04-08 10:18:10 -05:00
|
|
|
}
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
impl<T: Write> Write for WinConsole<T> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.buf.write(buf)
|
2014-04-08 10:18:10 -05:00
|
|
|
}
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
2014-04-08 10:18:10 -05:00
|
|
|
self.buf.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 19:51:22 -06:00
|
|
|
impl<T: Write + Send + 'static> Terminal for WinConsole<T> {
|
2015-11-23 03:03:52 -06:00
|
|
|
type Output = T;
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
fn fg(&mut self, color: color::Color) -> io::Result<bool> {
|
2014-04-08 10:18:10 -05:00
|
|
|
self.foreground = color;
|
|
|
|
self.apply();
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
fn bg(&mut self, color: color::Color) -> io::Result<bool> {
|
2014-04-08 10:18:10 -05:00
|
|
|
self.background = color;
|
|
|
|
self.apply();
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2015-11-23 03:03:52 -06:00
|
|
|
fn attr(&mut self, attr: Attr) -> io::Result<bool> {
|
2014-04-08 10:18:10 -05:00
|
|
|
match attr {
|
2015-11-23 03:03:52 -06:00
|
|
|
Attr::ForegroundColor(f) => {
|
2014-04-08 10:18:10 -05:00
|
|
|
self.foreground = f;
|
|
|
|
self.apply();
|
|
|
|
Ok(true)
|
2015-11-21 18:24:56 -06:00
|
|
|
}
|
2015-11-23 03:03:52 -06:00
|
|
|
Attr::BackgroundColor(b) => {
|
2014-04-08 10:18:10 -05:00
|
|
|
self.background = b;
|
|
|
|
self.apply();
|
|
|
|
Ok(true)
|
2015-11-21 18:24:56 -06:00
|
|
|
}
|
|
|
|
_ => Ok(false),
|
2014-04-08 10:18:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 03:03:52 -06:00
|
|
|
fn supports_attr(&self, attr: Attr) -> bool {
|
2014-04-08 10:18:10 -05:00
|
|
|
// it claims support for underscore and reverse video, but I can't get
|
|
|
|
// it to do anything -cmr
|
|
|
|
match attr {
|
2015-11-23 03:03:52 -06:00
|
|
|
Attr::ForegroundColor(_) | Attr::BackgroundColor(_) => true,
|
2015-11-21 18:24:56 -06:00
|
|
|
_ => false,
|
2014-04-08 10:18:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 03:03:52 -06:00
|
|
|
fn reset(&mut self) -> io::Result<bool> {
|
2014-09-20 17:22:11 -05:00
|
|
|
self.foreground = self.def_foreground;
|
|
|
|
self.background = self.def_background;
|
2014-04-08 10:18:10 -05:00
|
|
|
self.apply();
|
|
|
|
|
2015-11-23 03:03:52 -06:00
|
|
|
Ok(true)
|
2014-04-08 10:18:10 -05:00
|
|
|
}
|
|
|
|
|
2015-11-21 18:24:56 -06:00
|
|
|
fn get_ref<'a>(&'a self) -> &'a T {
|
|
|
|
&self.buf
|
|
|
|
}
|
2014-04-08 10:18:10 -05:00
|
|
|
|
2015-11-21 18:24:56 -06:00
|
|
|
fn get_mut<'a>(&'a mut self) -> &'a mut T {
|
|
|
|
&mut self.buf
|
|
|
|
}
|
2014-10-20 16:59:00 -05:00
|
|
|
|
2015-11-23 03:03:52 -06:00
|
|
|
fn into_inner(self) -> T
|
|
|
|
where Self: Sized
|
|
|
|
{
|
2015-11-21 18:24:56 -06:00
|
|
|
self.buf
|
|
|
|
}
|
2014-10-20 16:59:00 -05:00
|
|
|
}
|