1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#[cfg(windows)]
pub fn enable_ansi_support() -> Result<(), u32> {
use winapi::um::processenv::GetStdHandle;
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
const STD_OUT_HANDLE: u32 = -11i32 as u32;
const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
unsafe {
let std_out_handle = GetStdHandle(STD_OUT_HANDLE);
let error_code = GetLastError();
if error_code != 0 { return Err(error_code); }
let mut console_mode: u32 = 0;
GetConsoleMode(std_out_handle, &mut console_mode);
let error_code = GetLastError();
if error_code != 0 { return Err(error_code); }
if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 {
SetConsoleMode(std_out_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
let error_code = GetLastError();
if error_code != 0 { return Err(error_code); }
}
}
return Ok(());
}