diff options
author | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
commit | 1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch) | |
tree | 7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/anstyle-query/src/windows.rs | |
parent | 5ecd8cf2cba827454317368b68571df0d13d7842 (diff) | |
download | fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip |
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/anstyle-query/src/windows.rs')
-rw-r--r-- | vendor/anstyle-query/src/windows.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/vendor/anstyle-query/src/windows.rs b/vendor/anstyle-query/src/windows.rs new file mode 100644 index 0000000..a8ce404 --- /dev/null +++ b/vendor/anstyle-query/src/windows.rs @@ -0,0 +1,78 @@ +//! Windows-specific style queries + +#[cfg(windows)] +mod windows_console { + use std::os::windows::io::AsRawHandle; + use std::os::windows::io::RawHandle; + + use windows_sys::Win32::System::Console::CONSOLE_MODE; + use windows_sys::Win32::System::Console::ENABLE_VIRTUAL_TERMINAL_PROCESSING; + + fn enable_vt(handle: RawHandle) -> std::io::Result<()> { + unsafe { + let handle = std::mem::transmute(handle); + if handle == 0 { + return Err(std::io::Error::new( + std::io::ErrorKind::BrokenPipe, + "console is detached", + )); + } + + let mut dwmode: CONSOLE_MODE = 0; + if windows_sys::Win32::System::Console::GetConsoleMode(handle, &mut dwmode) == 0 { + return Err(std::io::Error::last_os_error()); + } + + dwmode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if windows_sys::Win32::System::Console::SetConsoleMode(handle, dwmode) == 0 { + return Err(std::io::Error::last_os_error()); + } + + Ok(()) + } + } + + 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(); + let stderr_handle = stderr.as_raw_handle(); + + enable_vt(stdout_handle)?; + if stdout_handle != stderr_handle { + enable_vt(stderr_handle)?; + } + + Ok(()) + } + + #[inline] + 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(crate) fn enable_ansi_colors() -> Option<bool> { + None + } +} + +/// 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() +} |