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)] |
10 | pub 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> = 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 | { |
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 | { |
47 | return Err(GetLastError()); |
48 | } |
49 | |
50 | // VT processing not already enabled? |
51 | if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 { |
52 | // https://docs.microsoft.com/en-us/windows/console/setconsolemode |
53 | if 0 == SetConsoleMode(console_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
54 | { |
55 | return Err(GetLastError()); |
56 | } |
57 | } |
58 | } |
59 | |
60 | return Ok(()); |
61 | } |
62 | |