Skip to content

Commit

Permalink
switch to use winapi as substitute for winconsole
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtlawrence committed Oct 30, 2019
1 parent abd2e69 commit 918ec3f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ no-color = []
[dependencies]
lazy_static = "1.2.0"

[target.'cfg(windows)'.dependencies]
winconsole = "0.10.0"
[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
default-features = false
features = [
"consoleapi",
"processenv",
"winbase"
]

[dev_dependencies]
ansi_term = "^0.9"
Expand Down
26 changes: 26 additions & 0 deletions examples/control.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
extern crate colored;
use colored::*;

#[cfg(not(windows))]
fn main() {
both()
}

#[cfg(windows)]
fn main() {
both();

// additional control setting using windows set_virtual_terminal
colored::control::set_virtual_terminal(true);
println!("{}", "stdout: Virtual Terminal is in use".bright_green());
colored::control::set_virtual_terminal(false);
println!("{}", "stderr: Virtual Terminal is NOT in use, escape chars should be visible".bright_red());
colored::control::set_virtual_terminal(true);
println!("{}", "stdout: Virtual Terminal is in use AGAIN and should be green!".bright_green());
colored::control::set_virtual_terminal(true);

// again with stderr
eprintln!("{}", "stderr: Virtual Terminal is in use".bright_green());
colored::control::set_virtual_terminal(false);
eprintln!("{}", "stderr: Virtual Terminal is NOT in use, escape chars should be visible".bright_red());
colored::control::set_virtual_terminal(true);
eprintln!("{}", "stderr: Virtual Terminal is in use AGAIN and should be green!".bright_green());
}

fn both() {
// this will be yellow if your environment allow it
println!("{}", "some warning".yellow());
// now , this will be always yellow
Expand Down
33 changes: 26 additions & 7 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use std::default::Default;
use std::env;
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(windows)]
use winconsole::{console, errors::WinResult};

/// Sets a flag to the console to use a virtual terminal environment.
/// This is primarily used for Windows 10 environments which will not correctly colorize the outputs based on ansi escape codes.
Expand All @@ -22,11 +20,32 @@ use winconsole::{console, errors::WinResult};
/// println!("{}", "bright cyan".bright_cyan()); // will print correctly
/// ```
#[cfg(windows)]
pub fn set_virtual_terminal(use_virtual: bool) -> WinResult<()> {
let mut mode = console::get_output_mode()?;
mode.VirtualTerminalProcessing = use_virtual;
console::set_output_mode(mode)?;
Ok(())
pub fn set_virtual_terminal(use_virtual: bool) {
use winapi::{
shared::minwindef::DWORD,
um::{
consoleapi::{GetConsoleMode, SetConsoleMode},
processenv::GetStdHandle,
winbase::STD_OUTPUT_HANDLE,
wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING,
}
};

unsafe {
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
let mut original_mode: DWORD = 0;
GetConsoleMode(handle, &mut original_mode);

let enabled = original_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == ENABLE_VIRTUAL_TERMINAL_PROCESSING;

match (use_virtual, enabled) {
// not enabled, should be enabled
(true, false) => SetConsoleMode(handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING | original_mode),
// already enabled, should be disabled
(false, true) => SetConsoleMode(handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING ^ original_mode),
_ => 0,
};
}
}

pub struct ShouldColorize {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#[macro_use]
extern crate lazy_static;
#[cfg(windows)]
extern crate winconsole;
extern crate winapi;

#[cfg(test)]
extern crate rspec;
Expand Down

0 comments on commit 918ec3f

Please sign in to comment.