1818//! match enable_ansi_support::enable_ansi_support() {
1919//! Ok(()) => {
2020//! // ANSI escape codes were successfully enabled, or this is a non-Windows platform.
21+ //! println!("\x1b[31mHello, world\x1b[0m");
2122//! }
2223//! Err(_) => {
2324//! // The operation was unsuccessful, typically because it's running on an older
4445
4546/// Enables ANSI code support on Windows 10.
4647///
47- /// Returns the Windows error code if unsuccessful.
48+ /// Returns an [`io::Error`](std::io::Error) with the Windows error code in it if unsuccessful.
4849///
4950/// On non-Windows platforms, this is a no-op that always returns `Ok(())`.
5051///
5152/// # Examples
5253///
5354/// See the [crate documentation](crate).
5455#[ cfg( windows) ]
55- pub fn enable_ansi_support ( ) -> Result < ( ) , u32 > {
56+ pub fn enable_ansi_support ( ) -> Result < ( ) , std :: io :: Error > {
5657 // ref: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#EXAMPLE_OF_ENABLING_VIRTUAL_TERMINAL_PROCESSING @@ https://archive.is/L7wRJ#76%
5758
58- use std:: { ffi:: OsStr , iter:: once, os:: windows:: ffi:: OsStrExt , ptr:: null_mut} ;
59- use winapi:: um:: {
60- consoleapi:: { GetConsoleMode , SetConsoleMode } ,
61- errhandlingapi:: GetLastError ,
62- fileapi:: { CreateFileW , OPEN_EXISTING } ,
63- handleapi:: INVALID_HANDLE_VALUE ,
64- winnt:: { FILE_SHARE_WRITE , GENERIC_READ , GENERIC_WRITE } ,
65- } ;
59+ use std:: { ffi:: OsStr , iter:: once, os:: windows:: ffi:: OsStrExt } ;
6660
67- const ENABLE_VIRTUAL_TERMINAL_PROCESSING : u32 = 0x0004 ;
61+ use windows_sys:: Win32 :: {
62+ Foundation :: INVALID_HANDLE_VALUE ,
63+ Storage :: FileSystem :: {
64+ CreateFileW , FILE_GENERIC_READ , FILE_GENERIC_WRITE , FILE_SHARE_WRITE , OPEN_EXISTING ,
65+ } ,
66+ System :: Console :: { GetConsoleMode , SetConsoleMode , ENABLE_VIRTUAL_TERMINAL_PROCESSING } ,
67+ } ;
6868
6969 unsafe {
7070 // ref: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
@@ -73,21 +73,21 @@ pub fn enable_ansi_support() -> Result<(), u32> {
7373 OsStr :: new ( "CONOUT$" ) . encode_wide ( ) . chain ( once ( 0 ) ) . collect ( ) ;
7474 let console_handle = CreateFileW (
7575 console_out_name. as_ptr ( ) ,
76- GENERIC_READ | GENERIC_WRITE ,
76+ FILE_GENERIC_READ | FILE_GENERIC_WRITE ,
7777 FILE_SHARE_WRITE ,
78- null_mut ( ) ,
78+ std :: ptr :: null ( ) ,
7979 OPEN_EXISTING ,
8080 0 ,
81- null_mut ( ) ,
81+ 0 ,
8282 ) ;
8383 if console_handle == INVALID_HANDLE_VALUE {
84- return Err ( GetLastError ( ) ) ;
84+ return Err ( std :: io :: Error :: last_os_error ( ) ) ;
8585 }
8686
8787 // ref: https://docs.microsoft.com/en-us/windows/console/getconsolemode
88- let mut console_mode: u32 = 0 ;
88+ let mut console_mode = 0 ;
8989 if 0 == GetConsoleMode ( console_handle, & mut console_mode) {
90- return Err ( GetLastError ( ) ) ;
90+ return Err ( std :: io :: Error :: last_os_error ( ) ) ;
9191 }
9292
9393 // VT processing not already enabled?
@@ -97,7 +97,7 @@ pub fn enable_ansi_support() -> Result<(), u32> {
9797 console_handle,
9898 console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING ,
9999 ) {
100- return Err ( GetLastError ( ) ) ;
100+ return Err ( std :: io :: Error :: last_os_error ( ) ) ;
101101 }
102102 }
103103 }
@@ -107,7 +107,7 @@ pub fn enable_ansi_support() -> Result<(), u32> {
107107
108108/// Enables ANSI code support on Windows 10.
109109///
110- /// Returns the Windows error code if unsuccessful.
110+ /// Returns an [`io::Error`](std::io::Error) with the Windows error code in it if unsuccessful.
111111///
112112/// On non-Windows platforms, this is a no-op that always returns `Ok(())`.
113113///
@@ -116,6 +116,6 @@ pub fn enable_ansi_support() -> Result<(), u32> {
116116/// See the [crate documentation](crate).
117117#[ cfg( not( windows) ) ]
118118#[ inline]
119- pub fn enable_ansi_support ( ) -> Result < ( ) , u32 > {
119+ pub fn enable_ansi_support ( ) -> Result < ( ) , std :: io :: Error > {
120120 Ok ( ( ) )
121121}
0 commit comments