1/// Enables ANSI code support on Windows 10.
2///
3/// This uses Windows API calls to alter the properties of the console that
4/// the program is running in.
5///
6/// https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
7///
8/// Returns a `Result` with the Windows error code if unsuccessful.
9#[cfg(windows)]
10pub fn enable_ansi_support() -> Result<(), u32> {
11 // ref: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#EXAMPLE_OF_ENABLING_VIRTUAL_TERMINAL_PROCESSING @@ https://archive.is/L7wRJ#76%
12
13 use std::{ffi::OsStr, iter::once, os::windows::ffi::OsStrExt, ptr::null_mut};
14 use winapi::um::{
15 consoleapi::{GetConsoleMode, SetConsoleMode},
16 errhandlingapi::GetLastError,
17 fileapi::{CreateFileW, OPEN_EXISTING},
18 handleapi::INVALID_HANDLE_VALUE,
19 winnt::{FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE},
20 };
21
22 const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
23
24 unsafe {
25 // ref: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
26 // Using `CreateFileW("CONOUT$", ...)` to retrieve the console handle works correctly even if STDOUT and/or STDERR are redirected
27 let console_out_name: Vec<u16> =
28 OsStr::new("CONOUT$").encode_wide().chain(once(0)).collect();
29 let console_handle = CreateFileW(
30 console_out_name.as_ptr(),
31 GENERIC_READ | GENERIC_WRITE,
32 FILE_SHARE_WRITE,
33 null_mut(),
34 OPEN_EXISTING,
35 0,
36 null_mut(),
37 );
38 if console_handle == INVALID_HANDLE_VALUE {
39 return Err(GetLastError());
40 }
41
42 // ref: https://docs.microsoft.com/en-us/windows/console/getconsolemode
43 let mut console_mode: u32 = 0;
44 if 0 == GetConsoleMode(console_handle, &mut console_mode) {
45 return Err(GetLastError());
46 }
47
48 // VT processing not already enabled?
49 if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 {
50 // https://docs.microsoft.com/en-us/windows/console/setconsolemode
51 if 0 == SetConsoleMode(
52 console_handle,
53 console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING,
54 ) {
55 return Err(GetLastError());
56 }
57 }
58 }
59
60 return Ok(());
61}
62