Skip to content

Commit

Permalink
new: use winapi-util instead of winapi
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Apr 30, 2024
1 parent 4a78024 commit 6875e91
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [".", "crate/encstr"]
[workspace.package]
repository = "https://github.com/pamburus/hl"
authors = ["Pavel Ivanov <mr.pavel.ivanov@gmail.com>"]
version = "0.28.1"
version = "0.28.2-alpha.1"
edition = "2021"
license = "MIT"

Expand Down Expand Up @@ -77,7 +77,7 @@ snap = "1"
strum = { version = "0", features = ["derive"] }
thiserror = "1"
wildflower = { git = "https://github.com/cassaundra/wildflower.git" }
winapi = { version = "0", features = ["handleapi"] }
winapi-util = { version = "0" }
wyhash = "0"
encstr = { path = "crate/encstr" }

Expand Down
40 changes: 9 additions & 31 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,23 @@ use thiserror::Error;
/// ConsoleError is an error which may occur in initialization of windows console.
#[derive(Error, Debug)]
pub enum ConsoleError {
#[error("failed to get standard output handle: error {0}")]
FailedToGetStandardOutputHandle(u32),
#[error("failed to get console mode: error {0}")]
FailedToGetConsoleMode(u32),
#[error("failed to set console mode: error {0}")]
FailedToSetConsoleMode(u32),
#[error("failed to get standard output handle: {0}")]
FailedToGetStandardOutputHandle(std::io::Error),
#[error("failed to set console mode: {0}")]
FailedToSetConsoleMode(std::io::Error),
}

#[cfg(windows)]
pub fn enable_ansi_support() -> Result<(), ConsoleError> {
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::processenv::GetStdHandle;
use winapi::um::winbase::STD_OUTPUT_HANDLE;
use winapi_util::console::Console;

const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
let mut console = Console::stdout().map_err(ConsoleError::FailedToGetStandardOutputHandle)?;
console.set_virtual_terminal_processing(true).map_err(ConsoleError::FailedToSetConsoleMode)?;

unsafe {
let std_out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
if std_out_handle == INVALID_HANDLE_VALUE {
return Err(ConsoleError::FailedToGetStandardOutputHandle(GetLastError()));
}
let mut console_mode: u32 = 0;
if GetConsoleMode(std_out_handle, &mut console_mode) == 0 {
return Err(ConsoleError::FailedToGetConsoleMode(GetLastError()));
}

if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 {
if SetConsoleMode(std_out_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0 {
return Err(ConsoleError::FailedToSetConsoleMode(GetLastError()));
}
}
}

return Ok(());
Ok(())
}

#[cfg(not(windows))]
pub fn enable_ansi_support() -> Result<(), ConsoleError> {
return Ok(());
Ok(())

Check warning on line 24 in src/console.rs

View check run for this annotation

Codecov / codecov/patch

src/console.rs#L24

Added line #L24 was not covered by tests
}

0 comments on commit 6875e91

Please sign in to comment.