]> git.lizzy.rs Git - rust.git/blob - library/test/src/helpers/isatty.rs
Rollup merge of #89869 - kpreid:from-doc, r=yaahc
[rust.git] / library / test / src / helpers / isatty.rs
1 //! Helper module which provides a function to test
2 //! if stdout is a tty.
3
4 cfg_if::cfg_if! {
5     if #[cfg(unix)] {
6         pub fn stdout_isatty() -> bool {
7             unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
8         }
9     } else if #[cfg(windows)] {
10         pub fn stdout_isatty() -> bool {
11             type DWORD = u32;
12             type BOOL = i32;
13             type HANDLE = *mut u8;
14             type LPDWORD = *mut u32;
15             const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
16             extern "system" {
17                 fn GetStdHandle(which: DWORD) -> HANDLE;
18                 fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL;
19             }
20             unsafe {
21                 let handle = GetStdHandle(STD_OUTPUT_HANDLE);
22                 let mut out = 0;
23                 GetConsoleMode(handle, &mut out) != 0
24             }
25         }
26     } else {
27         // FIXME: Implement isatty on SGX
28         pub fn stdout_isatty() -> bool {
29             false
30         }
31     }
32 }