Skip to content

Commit

Permalink
Merge pull request #149 from epage/error
Browse files Browse the repository at this point in the history
feat(query): Expose wincon errors
  • Loading branch information
epage authored Dec 8, 2023
2 parents feeec1f + 1b0b59a commit ff38a89
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions crates/anstyle-query/examples/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ fn main() {
"enable_ansi_colors: {:?}",
anstyle_query::windows::enable_ansi_colors()
);
#[cfg(windows)]
println!(
" enable_virtual_terminal_processing: {:?}",
anstyle_query::windows::enable_virtual_terminal_processing()
);
println!("is_ci: {:?}", anstyle_query::is_ci());
}
27 changes: 22 additions & 5 deletions crates/anstyle-query/src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Windows-specific style queries
#[cfg(windows)]
mod windows_console {
use std::os::windows::io::AsRawHandle;
Expand Down Expand Up @@ -30,7 +32,7 @@ mod windows_console {
}
}

fn enable_ansi_colors_raw() -> std::io::Result<()> {
pub fn enable_virtual_terminal_processing() -> std::io::Result<()> {
let stdout = std::io::stdout();
let stdout_handle = stdout.as_raw_handle();
let stderr = std::io::stderr();
Expand All @@ -45,17 +47,32 @@ mod windows_console {
}

#[inline]
pub fn enable_ansi_colors() -> Option<bool> {
Some(enable_ansi_colors_raw().map(|_| true).unwrap_or(false))
pub(crate) fn enable_ansi_colors() -> Option<bool> {
Some(
enable_virtual_terminal_processing()
.map(|_| true)
.unwrap_or(false),
)
}
}

#[cfg(not(windows))]
mod windows_console {
#[inline]
pub fn enable_ansi_colors() -> Option<bool> {
pub(crate) fn enable_ansi_colors() -> Option<bool> {
None
}
}

pub use self::windows_console::enable_ansi_colors;
/// Enable ANSI escape codes (ENABLE_VIRTUAL_TERMINAL_PROCESSING)
///
/// For non-windows systems, returns `None`
pub fn enable_ansi_colors() -> Option<bool> {
windows_console::enable_ansi_colors()
}

/// Raw ENABLE_VIRTUAL_TERMINAL_PROCESSING on stdout/stderr
#[cfg(windows)]
pub fn enable_virtual_terminal_processing() -> std::io::Result<()> {
windows_console::enable_virtual_terminal_processing()
}

0 comments on commit ff38a89

Please sign in to comment.