yansi/
windows.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#[cfg(windows)]
#[allow(non_camel_case_types, non_snake_case)]
mod windows_console {
    use core::ffi::c_void;

    type c_ulong = u32;
    type c_int = i32;
    type wchar_t = u16;

    type DWORD = c_ulong;
    type LPDWORD = *mut DWORD;
    type HANDLE = *mut c_void;
    type BOOL = c_int;
    type LPCWSTR = *const WCHAR;
    type WCHAR = wchar_t;
    type LPSECURITY_ATTRIBUTES = *mut SECURITY_ATTRIBUTES;
    type LPVOID = *mut c_void;

    #[repr(C)]
    pub struct SECURITY_ATTRIBUTES {
        pub nLength: DWORD,
        pub lpSecurityDescriptor: LPVOID,
        pub bInheritHandle: BOOL,
    }

    const ENABLE_VIRTUAL_TERMINAL_PROCESSING: DWORD = 0x0004;
    const INVALID_HANDLE_VALUE: HANDLE = -1isize as HANDLE;
    const FALSE: BOOL = 0;
    const TRUE: BOOL = 1;

    const GENERIC_READ: DWORD = 0x80000000;
    const GENERIC_WRITE: DWORD = 0x40000000;

    const FILE_SHARE_READ: DWORD = 0x00000001;
    const FILE_SHARE_WRITE: DWORD = 0x00000002;
    const OPEN_EXISTING: DWORD = 3;

    // This is the win32 console API, taken from the 'winapi' crate.
    extern "system" {
        fn CreateFileW(
            lpFileName: LPCWSTR,
            dwDesiredAccess: DWORD,
            dwShareMode: DWORD,
            lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
            dwCreationDisposition: DWORD,
            dwFlagsAndAttributes: DWORD,
            hTemplateFile: HANDLE
        ) -> HANDLE;

        fn GetLastError() -> DWORD;
        fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL;
        fn SetConsoleMode(hConsoleHandle: HANDLE, dwMode: DWORD) -> BOOL;
    }

    fn get_output_handle() -> Result<HANDLE, DWORD> {
        // This is "CONOUT$\0" UTF-16 encoded.
        const CONOUT: &[u16] = &[0x43, 0x4F, 0x4E, 0x4F, 0x55, 0x54, 0x24, 0x00];

        let raw_handle = unsafe {
            CreateFileW(
                CONOUT.as_ptr(),
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                core::ptr::null_mut(),
                OPEN_EXISTING,
                0,
                core::ptr::null_mut(),
            )
        };

        if raw_handle == INVALID_HANDLE_VALUE {
            return Err(6);
        }

        Ok(raw_handle)
    }

    unsafe fn enable_vt(handle: HANDLE) -> Result<(), DWORD> {
        let mut dw_mode: DWORD = 0;
        if GetConsoleMode(handle, &mut dw_mode) == FALSE {
            return Err(GetLastError());
        }

        dw_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
        match SetConsoleMode(handle, dw_mode) {
            result if result == TRUE => Ok(()),
            _ => Err(GetLastError())
        }
    }

    unsafe fn enable_ansi_colors_raw() -> Result<bool, DWORD> {
        enable_vt(get_output_handle()?)?;
        Ok(true)
    }

    #[inline(always)]
    pub fn enable() -> bool {
        unsafe { enable_ansi_colors_raw().unwrap_or(false) }
    }

    // Try to enable colors on Windows, and try to do it at most once.
    pub fn cache_enable() -> bool {
        use crate::condition::CachedBool;

        static ENABLED: CachedBool = CachedBool::new();
        ENABLED.get_or_init(enable)
    }
}

#[cfg(not(windows))]
mod windows_console {
    #[inline(always)]
    #[allow(dead_code)]
    pub fn enable() -> bool { true }

    #[inline(always)]
    pub fn cache_enable() -> bool { true }
}

// pub use self::windows_console::enable;
pub use self::windows_console::cache_enable;