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

Auto disable colored output for unsupported Windows shells #137

Merged
merged 2 commits into from Oct 20, 2017
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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ regex-syntax = "0.4"
[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
windows = { path = "win" }

[dev-dependencies]
diff = "0.1"
tempdir = "0.3"
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ extern crate libc;
extern crate num_cpus;
extern crate regex;
extern crate regex_syntax;
#[cfg(windows)]
extern crate windows;

pub mod fshelper;
pub mod lscolors;
Expand Down Expand Up @@ -79,6 +81,8 @@ fn main() {
Some("never") => false,
_ => atty::is(Stream::Stdout),
};
#[cfg(windows)]
let colored_output = colored_output && windows::enable_colored_output();

let ls_colors = if colored_output {
Some(
Expand Down
31 changes: 31 additions & 0 deletions win/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions win/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "windows"
version = "0.0.0"

[dependencies]
kernel32-sys = "0.2"
winapi = "0.2"
31 changes: 31 additions & 0 deletions win/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extern crate kernel32;
extern crate winapi;

use kernel32::{GetStdHandle, GetConsoleMode, SetConsoleMode};
use winapi::{STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE};

const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;

// https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example
pub fn enable_colored_output() -> bool {
unsafe {
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
if handle == INVALID_HANDLE_VALUE {
return false;
}

// https://docs.microsoft.com/en-us/windows/console/getconsolemode
let mut mode = 0;
if GetConsoleMode(handle, &mut mode) == 0 {
return false;
}
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;

// https://docs.microsoft.com/en-us/windows/console/setconsolemode
//
// A console consists of an input buffer and one or more screen buffers. ... Setting the
// output modes of one screen buffer does not affect the output modes of other screen
// buffers.
SetConsoleMode(handle, mode) != 0
}
}