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

Fix windows compilation errors #52

Merged
merged 6 commits into from Sep 2, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .appveyor.yml
Expand Up @@ -26,7 +26,7 @@ environment:

matrix:
# minimum version
- CHANNEL: 1.17.0
- CHANNEL: 1.28.0
ARCH: i686
ABI: msvc
# "msvc" ABI
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -23,10 +23,11 @@ optional = true

[target.'cfg(target_os="windows")'.dependencies.winapi]
version = "0.3.4"
features = ["errhandlingapi", "consoleapi", "processenv", "handleapi"]
features = ["consoleapi", "errhandlingapi", "fileapi", "handleapi", "processenv"]

[dev-dependencies]
doc-comment = "0.3"
regex = "1.1.9"

[dev-dependencies.serde_json]
version = "1.0.39"
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
# rust-ansi-term [![ansi-term on crates.io](http://meritbadge.herokuapp.com/ansi-term)](https://crates.io/crates/ansi_term) [![Build status](https://travis-ci.org/ogham/rust-ansi-term.svg?branch=master)](https://travis-ci.org/ogham/rust-ansi-term) [![Coverage status](https://coveralls.io/repos/ogham/rust-ansi-term/badge.svg?branch=master&service=github)](https://coveralls.io/github/ogham/rust-ansi-term?branch=master)
# rust-ansi-term [![ansi-term on crates.io](http://meritbadge.herokuapp.com/ansi-term)](https://crates.io/crates/ansi_term) [![Build status](https://img.shields.io/travis/ogham/rust-ansi-term/master.svg?style=flat)](https://travis-ci.org/ogham/rust-ansi-term) [![Build status](https://img.shields.io/appveyor/ci/ogham/rust-ansi-term/master.svg?style=flat&logo=AppVeyor&logoColor=silver)](https://ci.appveyor.com/project/ogham/rust-ansi-term) [![Coverage status](https://coveralls.io/repos/ogham/rust-ansi-term/badge.svg?branch=master&service=github)](https://coveralls.io/github/ogham/rust-ansi-term?branch=master)

This is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals.

Expand Down
44 changes: 28 additions & 16 deletions src/debug.rs
Expand Up @@ -2,7 +2,6 @@ use std::fmt;

use style::Style;


/// Styles have a special `Debug` implementation that only shows the fields that
/// are set. Fields that haven’t been touched aren’t included in the output.
///
Expand Down Expand Up @@ -103,20 +102,33 @@ mod test {

#[test]
fn long_and_detailed() {
let debug = r##"Style {
foreground: Some(
Blue,
),
background: None,
blink: false,
bold: true,
dimmed: false,
hidden: false,
italic: false,
reverse: false,
strikethrough: false,
underline: false,
}"##;
assert_eq!(debug, format!("{:#?}", Blue.bold()));
extern crate regex;
let expected_debug = "Style { fg(Blue), bold }";
let expected_pretty_repat = r##"(?x)
Style\s+\{\s+
foreground:\s+Some\(\s+
Blue,?\s+
\),\s+
background:\s+None,\s+
blink:\s+false,\s+
bold:\s+true,\s+
dimmed:\s+false,\s+
hidden:\s+false,\s+
italic:\s+false,\s+
reverse:\s+false,\s+
strikethrough:\s+
false,\s+
underline:\s+false,?\s+
\}"##;
let re = regex::Regex::new(expected_pretty_repat).unwrap();

let style = Blue.bold();
let style_fmt_debug = format!("{:?}", style);
let style_fmt_pretty = format!("{:#?}", style);
println!("style_fmt_debug:\n{}", style_fmt_debug);
println!("style_fmt_pretty:\n{}", style_fmt_pretty);

assert_eq!(expected_debug, style_fmt_debug);
assert!(re.is_match(&style_fmt_pretty));
}
}
17 changes: 10 additions & 7 deletions src/windows.rs
Expand Up @@ -16,20 +16,23 @@ pub fn enable_ansi_support() -> Result<(), u32> {
use std::ptr::null_mut;
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::fileapi::CreateFile2;
use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING};
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::winnt::{FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE};

const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;

unsafe {
// ref: https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfile2
// Using `CreateFile2("CONOUT$", ...)` to retrieve the console handle works correctly even if STDOUT and/or STDERR are redirected
// ref: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
// Using `CreateFileW("CONOUT$", ...)` to retrieve the console handle works correctly even if STDOUT and/or STDERR are redirected
let console_out_name: Vec<u16> = OsStr::new("CONOUT$").encode_wide().chain(once(0)).collect();
let console_handle = CreateFile2(
let console_handle = CreateFileW(
console_out_name.as_ptr(),
winapi::um::winnt::GENERIC_READ | winapi::um::winnt::GENERIC_WRITE,
winapi::um::winnt::FILE_SHARE_WRITE,
winapi::um::fileapi::OPEN_EXISTING,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
null_mut(),
OPEN_EXISTING,
0,
null_mut(),
);
if console_handle == INVALID_HANDLE_VALUE
Expand Down