Skip to content

Commit

Permalink
Revert "Handle possible errors in set_virtual_terminal (#105)" (#160)
Browse files Browse the repository at this point in the history
This reverts commit a9aa7d8.
  • Loading branch information
kurtlawrence authored Dec 12, 2023
1 parent 949f601 commit 933b254
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Unreleased
- Document crate MSRV of `1.70`.
- Handle errors in `set_virtual_terminal`.

- Updated top-level docs to include a note about `ColoredString`\'s role in the `Colorize` pipeline as well as link to it to suggest learning more about how to manipulate existing `ColoredString`\'s.
- Changes to `ColoredString`:
Expand Down
15 changes: 4 additions & 11 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use std::sync::atomic::{AtomicBool, Ordering};
/// This is primarily used for Windows 10 environments which will not correctly colorize
/// the outputs based on ANSI escape codes.
///
/// The returned `Result` is _always_ `Ok(())`, the return type was kept to ensure backwards
/// compatibility.
///
/// # Notes
/// > Only available to `Windows` build targets.
///
Expand All @@ -25,25 +28,15 @@ use std::sync::atomic::{AtomicBool, Ordering};
#[allow(clippy::result_unit_err)]
#[cfg(windows)]
pub fn set_virtual_terminal(use_virtual: bool) -> Result<(), ()> {
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows_sys::Win32::System::Console::{
GetConsoleMode, GetStdHandle, SetConsoleMode, ENABLE_VIRTUAL_TERMINAL_PROCESSING,
STD_OUTPUT_HANDLE,
};

unsafe {
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
if handle == INVALID_HANDLE_VALUE {
return Err(());
}

let mut original_mode = 0;
// Return value of 0 means that the function failed:
// https://learn.microsoft.com/en-us/windows/console/getconsolemode#return-value
if GetConsoleMode(handle, &mut original_mode) == 0 {
// TODO: It would be prudent to get the error using `GetLastError` here.
return Err(());
}
GetConsoleMode(handle, &mut original_mode);

let enabled = original_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING
== ENABLE_VIRTUAL_TERMINAL_PROCESSING;
Expand Down

0 comments on commit 933b254

Please sign in to comment.