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)] enable_ansi_support() -> Result<(), u32>10pub fn enable_ansi_support() -> Result<(), u32> { 11 use winapi::um::processenv::GetStdHandle; 12 use winapi::um::errhandlingapi::GetLastError; 13 use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode}; 14 15 const STD_OUT_HANDLE: u32 = -11i32 as u32; 16 const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004; 17 18 unsafe { 19 // https://docs.microsoft.com/en-us/windows/console/getstdhandle 20 let std_out_handle = GetStdHandle(STD_OUT_HANDLE); 21 let error_code = GetLastError(); 22 if error_code != 0 { return Err(error_code); } 23 24 // https://docs.microsoft.com/en-us/windows/console/getconsolemode 25 let mut console_mode: u32 = 0; 26 GetConsoleMode(std_out_handle, &mut console_mode); 27 let error_code = GetLastError(); 28 if error_code != 0 { return Err(error_code); } 29 30 // VT processing not already enabled? 31 if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 { 32 // https://docs.microsoft.com/en-us/windows/console/setconsolemode 33 SetConsoleMode(std_out_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); 34 let error_code = GetLastError(); 35 if error_code != 0 { return Err(error_code); } 36 } 37 } 38 39 return Ok(()); 40 } 41