Skip to content

Commit

Permalink
jakwings/fd@a44e677 -> subdirectory "win"
Browse files Browse the repository at this point in the history
  • Loading branch information
J.W authored and sharkdp committed Oct 20, 2017
1 parent ce73c08 commit 570ea78
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
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.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: 3 additions & 1 deletion 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 @@ -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(
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
}
}

0 comments on commit 570ea78

Please sign in to comment.