From 4ee96aa291b0d457af36958cee1b446f0bc1bc03 Mon Sep 17 00:00:00 2001 From: "J.W" Date: Fri, 20 Oct 2017 15:15:32 +0800 Subject: [PATCH 1/2] Auto disable colored output for unsupported Windows shells --- Cargo.toml | 2 +- src/main.rs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 44b46ece8..89c218798 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ repository = "sharkdp/fd" clap = "2.26.0" [dependencies] -ansi_term = "0.9" +ansi_term = "0.10" atty = "0.2" clap = "2.26.0" ignore = "0.2" diff --git a/src/main.rs b/src/main.rs index 8b3b78c55..c064290cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,6 +79,8 @@ fn main() { Some("never") => false, _ => atty::is(Stream::Stdout), }; + #[cfg(windows)] + let colored_output = colored_output && ansi_term::enable_ansi_support().is_ok(); let ls_colors = if colored_output { Some( From 2b858908b18c2e62b3ed6df2403a3b732c3571d3 Mon Sep 17 00:00:00 2001 From: "J.W" Date: Fri, 20 Oct 2017 16:26:56 +0800 Subject: [PATCH 2/2] jakwings/fd@a44e677 -> subdirectory "win" --- Cargo.lock | 9 +++++++++ Cargo.toml | 5 ++++- src/main.rs | 4 +++- win/Cargo.lock | 31 +++++++++++++++++++++++++++++++ win/Cargo.toml | 7 +++++++ win/src/lib.rs | 31 +++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 win/Cargo.lock create mode 100644 win/Cargo.toml create mode 100644 win/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 811623cef..0205dd522 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,7 @@ dependencies = [ "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "windows 0.0.0", ] [[package]] @@ -323,6 +324,14 @@ name = "winapi-build" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "windows" +version = "0.0.0" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [metadata] "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" diff --git a/Cargo.toml b/Cargo.toml index 89c218798..d35f3978c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ repository = "sharkdp/fd" clap = "2.26.0" [dependencies] -ansi_term = "0.10" +ansi_term = "0.9" atty = "0.2" clap = "2.26.0" ignore = "0.2" @@ -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" diff --git a/src/main.rs b/src/main.rs index c064290cc..84680ab8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -80,7 +82,7 @@ fn main() { _ => atty::is(Stream::Stdout), }; #[cfg(windows)] - let colored_output = colored_output && ansi_term::enable_ansi_support().is_ok(); + let colored_output = colored_output && windows::enable_colored_output(); let ls_colors = if colored_output { Some( diff --git a/win/Cargo.lock b/win/Cargo.lock new file mode 100644 index 000000000..62ef489a1 --- /dev/null +++ b/win/Cargo.lock @@ -0,0 +1,31 @@ +[root] +name = "windows" +version = "0.0.0" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/win/Cargo.toml b/win/Cargo.toml new file mode 100644 index 000000000..c1d95a215 --- /dev/null +++ b/win/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "windows" +version = "0.0.0" + +[dependencies] +kernel32-sys = "0.2" +winapi = "0.2" diff --git a/win/src/lib.rs b/win/src/lib.rs new file mode 100644 index 000000000..46b5496c4 --- /dev/null +++ b/win/src/lib.rs @@ -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 + } +}