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;
14 use std::iter::once;
15 use std::os::windows::ffi::OsStrExt;
16 use std::ptr::null_mut;
17 use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
18 use winapi::um::errhandlingapi::GetLastError;
19 use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING};
20 use winapi::um::handleapi::INVALID_HANDLE_VALUE;
21 use winapi::um::winnt::{FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE};
22
23 const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
24
25 unsafe {
26 // ref: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
27 // Using `CreateFileW("CONOUT$", ...)` to retrieve the console handle works correctly even if STDOUT and/or STDERR are redirected
28 let console_out_name: Vec<u16> =
29 OsStr::new("CONOUT$").encode_wide().chain(once(0)).collect();
30 let console_handle = CreateFileW(
31 console_out_name.as_ptr(),
32 GENERIC_READ | GENERIC_WRITE,
33 FILE_SHARE_WRITE,
34 null_mut(),
35 OPEN_EXISTING,
36 0,
37 null_mut(),
38 );
39 if console_handle == INVALID_HANDLE_VALUE {
40 return Err(GetLastError());
41 }
42
43 // ref: https://docs.microsoft.com/en-us/windows/console/getconsolemode
44 let mut console_mode: u32 = 0;
45 if 0 == GetConsoleMode(console_handle, &mut console_mode) {
46 return Err(GetLastError());
47 }
48
49 // VT processing not already enabled?
50 if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 {
51 // https://docs.microsoft.com/en-us/windows/console/setconsolemode
52 if 0 == SetConsoleMode(
53 console_handle,
54 console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING,
55 ) {
56 return Err(GetLastError());
57 }
58 }
59 }
60
61 Ok(())
62}
63