@@ -10,9 +10,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
10
10
/// This is primarily used for Windows 10 environments which will not correctly colorize
11
11
/// the outputs based on ANSI escape codes.
12
12
///
13
- /// The returned `Result` is _always_ `Ok(())`, the return type was kept to ensure backwards
14
- /// compatibility.
15
- ///
16
13
/// # Notes
17
14
/// > Only available to `Windows` build targets.
18
15
///
@@ -28,15 +25,25 @@ use std::sync::atomic::{AtomicBool, Ordering};
28
25
#[ allow( clippy:: result_unit_err) ]
29
26
#[ cfg( windows) ]
30
27
pub fn set_virtual_terminal ( use_virtual : bool ) -> Result < ( ) , ( ) > {
28
+ use windows_sys:: Win32 :: Foundation :: INVALID_HANDLE_VALUE ;
31
29
use windows_sys:: Win32 :: System :: Console :: {
32
30
GetConsoleMode , GetStdHandle , SetConsoleMode , ENABLE_VIRTUAL_TERMINAL_PROCESSING ,
33
31
STD_OUTPUT_HANDLE ,
34
32
} ;
35
33
36
34
unsafe {
37
35
let handle = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
36
+ if handle == INVALID_HANDLE_VALUE {
37
+ return Err ( ( ) ) ;
38
+ }
39
+
38
40
let mut original_mode = 0 ;
39
- GetConsoleMode ( handle, & mut original_mode) ;
41
+ // Return value of 0 means that the function failed:
42
+ // https://learn.microsoft.com/en-us/windows/console/getconsolemode#return-value
43
+ if GetConsoleMode ( handle, & mut original_mode) == 0 {
44
+ // TODO: It would be prudent to get the error using `GetLastError` here.
45
+ return Err ( ( ) ) ;
46
+ }
40
47
41
48
let enabled = original_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING
42
49
== ENABLE_VIRTUAL_TERMINAL_PROCESSING ;
0 commit comments