Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,40 @@ pub fn enable_ansi_support() -> Result<(), u32> {

const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;

unsafe {

// ref: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
// Using `CreateFileW("CONOUT$", ...)` to retrieve the console handle works correctly even if STDOUT and/or STDERR are redirected
let console_out_name: Vec<u16> = OsStr::new("CONOUT$").encode_wide().chain(once(0)).collect();
let console_handle = CreateFileW(
let console_handle = unsafe { CreateFileW(
console_out_name.as_ptr(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
null_mut(),
OPEN_EXISTING,
0,
null_mut(),
);
) };
if console_handle == INVALID_HANDLE_VALUE
{
return Err(GetLastError());
return Err(unsafe { GetLastError() });
}

// ref: https://docs.microsoft.com/en-us/windows/console/getconsolemode
let mut console_mode: u32 = 0;
if 0 == GetConsoleMode(console_handle, &mut console_mode)
if 0 == unsafe { GetConsoleMode(console_handle, &mut console_mode) }
{
return Err(GetLastError());
return Err(unsafe { GetLastError() });
}

// VT processing not already enabled?
if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 {
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
if 0 == SetConsoleMode(console_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
if 0 == unsafe { SetConsoleMode(console_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) }
{
return Err(GetLastError());
return Err(unsafe { GetLastError() });
}
}
}


return Ok(());
}