jakwings/fd@a44e677 -> subdirectory "win"

This commit is contained in:
J.W 2017-10-20 16:26:56 +08:00 committed by David Peter
parent ce73c086b6
commit 570ea78d24
6 changed files with 85 additions and 2 deletions

9
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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(

31
win/Cargo.lock generated Normal file
View File

@ -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"

7
win/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "windows"
version = "0.0.0"
[dependencies]
kernel32-sys = "0.2"
winapi = "0.2"

31
win/src/lib.rs Normal file
View File

@ -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
}
}