1#![cfg_attr(unix, no_std)]
19
20#[cfg(unix)]
21extern crate libc;
22#[cfg(windows)]
23extern crate winapi;
24
25#[cfg(windows)]
26use winapi::shared::minwindef::DWORD;
27#[cfg(windows)]
28use winapi::shared::ntdef::WCHAR;
29
30#[derive(Clone, Copy, Debug)]
32pub enum Stream {
33 Stdout,
34 Stderr,
35 Stdin,
36}
37
38#[cfg(all(unix, not(target_arch = "wasm32")))]
40pub fn is(stream: Stream) -> bool {
41 extern crate libc;
42
43 let fd = match stream {
44 Stream::Stdout => libc::STDOUT_FILENO,
45 Stream::Stderr => libc::STDERR_FILENO,
46 Stream::Stdin => libc::STDIN_FILENO,
47 };
48 unsafe { libc::isatty(fd) != 0 }
49}
50
51#[cfg(target_os = "hermit")]
53pub fn is(stream: Stream) -> bool {
54 extern crate hermit_abi;
55
56 let fd = match stream {
57 Stream::Stdout => hermit_abi::STDOUT_FILENO,
58 Stream::Stderr => hermit_abi::STDERR_FILENO,
59 Stream::Stdin => hermit_abi::STDIN_FILENO,
60 };
61 hermit_abi::isatty(fd)
62}
63
64#[cfg(windows)]
66pub fn is(stream: Stream) -> bool {
67 use winapi::um::winbase::{
68 STD_ERROR_HANDLE as STD_ERROR, STD_INPUT_HANDLE as STD_INPUT,
69 STD_OUTPUT_HANDLE as STD_OUTPUT,
70 };
71
72 let (fd, others) = match stream {
73 Stream::Stdin => (STD_INPUT, [STD_ERROR, STD_OUTPUT]),
74 Stream::Stderr => (STD_ERROR, [STD_INPUT, STD_OUTPUT]),
75 Stream::Stdout => (STD_OUTPUT, [STD_INPUT, STD_ERROR]),
76 };
77 if unsafe { console_on_any(&[fd]) } {
78 return true;
81 }
82
83 if unsafe { console_on_any(&others) } {
88 return false;
89 }
90
91 unsafe { msys_tty_on(fd) }
94}
95
96pub fn isnt(stream: Stream) -> bool {
98 !is(stream)
99}
100
101#[cfg(windows)]
103unsafe fn console_on_any(fds: &[DWORD]) -> bool {
104 use winapi::um::{consoleapi::GetConsoleMode, processenv::GetStdHandle};
105
106 for &fd in fds {
107 let mut out = 0;
108 let handle = GetStdHandle(fd);
109 if GetConsoleMode(handle, &mut out) != 0 {
110 return true;
111 }
112 }
113 false
114}
115
116#[cfg(windows)]
118unsafe fn msys_tty_on(fd: DWORD) -> bool {
119 use std::{mem, slice};
120
121 use winapi::{
122 ctypes::c_void,
123 shared::minwindef::MAX_PATH,
124 um::{
125 fileapi::FILE_NAME_INFO, minwinbase::FileNameInfo, processenv::GetStdHandle,
126 winbase::GetFileInformationByHandleEx,
127 },
128 };
129
130 let size = mem::size_of::<FILE_NAME_INFO>();
131 let mut name_info_bytes = vec![0u8; size + MAX_PATH * mem::size_of::<WCHAR>()];
132 let res = GetFileInformationByHandleEx(
133 GetStdHandle(fd),
134 FileNameInfo,
135 &mut *name_info_bytes as *mut _ as *mut c_void,
136 name_info_bytes.len() as u32,
137 );
138 if res == 0 {
139 return false;
140 }
141 let name_info: &FILE_NAME_INFO = &*(name_info_bytes.as_ptr() as *const FILE_NAME_INFO);
142 let s = slice::from_raw_parts(
143 name_info.FileName.as_ptr(),
144 name_info.FileNameLength as usize / 2,
145 );
146 let name = String::from_utf16_lossy(s);
147 let is_msys = name.contains("msys-") || name.contains("cygwin-");
152 let is_pty = name.contains("-pty");
153 is_msys && is_pty
154}
155
156#[cfg(target_arch = "wasm32")]
158pub fn is(_stream: Stream) -> bool {
159 false
160}
161
162#[cfg(test)]
163mod tests {
164 use super::{is, Stream};
165
166 #[test]
167 #[cfg(windows)]
168 fn is_err() {
169 assert!(!is(Stream::Stderr))
171 }
172
173 #[test]
174 #[cfg(windows)]
175 fn is_out() {
176 assert!(!is(Stream::Stdout))
178 }
179
180 #[test]
181 #[cfg(windows)]
182 fn is_in() {
183 assert!(is(Stream::Stdin))
184 }
185
186 #[test]
187 #[cfg(unix)]
188 fn is_err() {
189 assert!(is(Stream::Stderr))
190 }
191
192 #[test]
193 #[cfg(unix)]
194 fn is_out() {
195 assert!(is(Stream::Stdout))
196 }
197
198 #[test]
199 #[cfg(target_os = "macos")]
200 fn is_in() {
201 assert!(is(Stream::Stdin))
203 }
204
205 #[test]
206 #[cfg(all(not(target_os = "macos"), unix))]
207 fn is_in() {
208 assert!(is(Stream::Stdin))
209 }
210}