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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#![cfg_attr(unix, no_std)]
#[cfg(windows)]
use winapi::shared::minwindef::DWORD;
#[cfg(windows)]
use winapi::shared::ntdef::WCHAR;
#[derive(Clone, Copy, Debug)]
pub enum Stream {
Stdout,
Stderr,
Stdin,
}
#[cfg(target_family = "unix")]
pub fn is(stream: Stream) -> bool {
let fd = match stream {
Stream::Stdout => libc::STDOUT_FILENO,
Stream::Stderr => libc::STDERR_FILENO,
Stream::Stdin => libc::STDIN_FILENO,
};
unsafe { libc::isatty(fd) != 0 }
}
#[cfg(target_os = "hermit")]
pub fn is(stream: Stream) -> bool {
let fd = match stream {
Stream::Stdout => hermit_abi::STDOUT_FILENO,
Stream::Stderr => hermit_abi::STDERR_FILENO,
Stream::Stdin => hermit_abi::STDIN_FILENO,
};
hermit_abi::isatty(fd)
}
#[cfg(windows)]
pub fn is(stream: Stream) -> bool {
use winapi::um::winbase::{
STD_ERROR_HANDLE as STD_ERROR, STD_INPUT_HANDLE as STD_INPUT,
STD_OUTPUT_HANDLE as STD_OUTPUT,
};
let (fd, others) = match stream {
Stream::Stdin => (STD_INPUT, [STD_ERROR, STD_OUTPUT]),
Stream::Stderr => (STD_ERROR, [STD_INPUT, STD_OUTPUT]),
Stream::Stdout => (STD_OUTPUT, [STD_INPUT, STD_ERROR]),
};
if unsafe { console_on_any(&[fd]) } {
return true;
}
if unsafe { console_on_any(&others) } {
return false;
}
unsafe { msys_tty_on(fd) }
}
pub fn isnt(stream: Stream) -> bool {
!is(stream)
}
#[cfg(windows)]
unsafe fn console_on_any(fds: &[DWORD]) -> bool {
use winapi::um::{consoleapi::GetConsoleMode, processenv::GetStdHandle};
for &fd in fds {
let mut out = 0;
let handle = GetStdHandle(fd);
if GetConsoleMode(handle, &mut out) != 0 {
return true;
}
}
false
}
#[cfg(windows)]
unsafe fn msys_tty_on(fd: DWORD) -> bool {
use std::{mem, slice};
use winapi::{
ctypes::c_void,
shared::minwindef::MAX_PATH,
um::{
fileapi::FILE_NAME_INFO, minwinbase::FileNameInfo, processenv::GetStdHandle,
winbase::GetFileInformationByHandleEx,
},
};
let size = mem::size_of::<FILE_NAME_INFO>();
let mut name_info_bytes = vec![0u8; size + MAX_PATH * mem::size_of::<WCHAR>()];
let res = GetFileInformationByHandleEx(
GetStdHandle(fd),
FileNameInfo,
&mut *name_info_bytes as *mut _ as *mut c_void,
name_info_bytes.len() as u32,
);
if res == 0 {
return false;
}
let name_info: &FILE_NAME_INFO = &*(name_info_bytes.as_ptr() as *const FILE_NAME_INFO);
let s = slice::from_raw_parts(
name_info.FileName.as_ptr(),
name_info.FileNameLength as usize / 2,
);
let name = String::from_utf16_lossy(s);
let is_msys = name.contains("msys-") || name.contains("cygwin-");
let is_pty = name.contains("-pty");
is_msys && is_pty
}
#[cfg(target_family = "wasm")]
pub fn is(_stream: Stream) -> bool {
false
}