Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretend to be xterm if Windows console supports ansi #88

Merged
merged 3 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ byteorder = "1.2.1"
dirs = "1.0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["wincon", "handleapi", "fileapi"] }
winapi = { version = "0.3", features = ["consoleapi", "wincon", "handleapi", "fileapi"] }

[features]
default=[]
15 changes: 15 additions & 0 deletions src/terminfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use std::io;
use std::io::BufReader;
use std::path::Path;


#[cfg(windows)]
use win;

use Attr;
use color;
use Terminal;
Expand Down Expand Up @@ -67,6 +71,17 @@ impl TermInfo {
}
})
});

#[cfg(windows)]
{
if term_name.is_none() && win::supports_ansi() {
// Microsoft people seem to be fine with pretending to be xterm:
// https://github.com/Microsoft/WSL/issues/1446
// The basic ANSI fallback terminal will be uses.
return TermInfo::from_name("xterm");
}
}

if let Some(term_name) = term_name {
return TermInfo::from_name(term_name);
} else {
Expand Down
43 changes: 30 additions & 13 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@

extern crate winapi;

use std::io::prelude::*;
use color;
use std::io;
use std::io::prelude::*;
use std::ops::Deref;
use std::ptr;
use Attr;
use Error;
use Result;
use Terminal;
use color;

use win::winapi::um::wincon::{SetConsoleCursorPosition, SetConsoleTextAttribute, BACKGROUND_INTENSITY};
use win::winapi::um::wincon::{FillConsoleOutputCharacterW, GetConsoleScreenBufferInfo, COORD};
use win::winapi::um::wincon::FillConsoleOutputAttribute;
use win::winapi::shared::minwindef::{DWORD, WORD};
use win::winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
use win::winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use win::winapi::um::fileapi::{CreateFileA, OPEN_EXISTING};
use win::winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
use win::winapi::um::wincon::FillConsoleOutputAttribute;
use win::winapi::um::wincon::{FillConsoleOutputCharacterW, GetConsoleScreenBufferInfo, COORD};
use win::winapi::um::wincon::{SetConsoleCursorPosition, SetConsoleTextAttribute};
use win::winapi::um::wincon::{BACKGROUND_INTENSITY, ENABLE_VIRTUAL_TERMINAL_PROCESSING};
use win::winapi::um::winnt::{FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE, HANDLE};


/// Console info which can be used by a Terminal implementation
/// which uses the Win32 Console API.
pub struct WinConsoleInfo {
Expand Down Expand Up @@ -116,7 +117,7 @@ impl Deref for HandleWrapper {
}
}

// Just get a handle to the current console buffer whatever it is
/// Just get a handle to the current console buffer whatever it is
fn conout() -> io::Result<HandleWrapper> {
let name = b"CONOUT$\0";
let handle = unsafe {
Expand All @@ -137,6 +138,25 @@ fn conout() -> io::Result<HandleWrapper> {
}
}

unsafe fn set_flag(handle: HANDLE, flag: DWORD) -> io::Result<()> {
let mut curr_mode: DWORD = 0;
if GetConsoleMode(handle, &mut curr_mode) == 0 {
return Err(io::Error::last_os_error());
}

if SetConsoleMode(handle, curr_mode | flag) == 0 {
return Err(io::Error::last_os_error());
}
return Ok(());
}

/// Check if console supports ansi codes (should succeed on Windows 10)
pub fn supports_ansi() -> bool {
conout()
.and_then(|handle| unsafe { set_flag(*handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING) })
.is_ok()
}

// This test will only pass if it is running in an actual console, probably
#[test]
fn test_conout() {
Expand Down Expand Up @@ -183,7 +203,7 @@ impl<T: Write + Send> WinConsole<T> {
};

if self.info.secure {
fg = bg;
fg = bg;
}

let mut accum: WORD = 0;
Expand All @@ -205,10 +225,7 @@ impl<T: Write + Send> WinConsole<T> {

/// Create a new WinConsole with the given WinConsoleInfo and out
pub fn new_with_consoleinfo(out: T, info: WinConsoleInfo) -> WinConsole<T> {
WinConsole {
buf: out,
info,
}
WinConsole { buf: out, info }
}

/// Returns `Err` whenever the terminal cannot be created for some
Expand Down