#![cfg_attr(unix, no_std)]
#[cfg(unix)]
extern crate libc;
#[cfg(windows)]
extern crate winapi;
#[cfg(target_os = "redox")]
extern crate termion;
#[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(all(unix, not(target_arch = "wasm32")))]
pub fn is(stream: Stream) -> bool {
extern crate libc;
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(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;
use winapi::um::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;
use std::slice;
use winapi::ctypes::c_void;
use winapi::um::winbase::GetFileInformationByHandleEx;
use winapi::um::fileapi::FILE_NAME_INFO;
use winapi::um::minwinbase::FileNameInfo;
use winapi::um::processenv::GetStdHandle;
use winapi::shared::minwindef::MAX_PATH;
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_os = "redox")]
pub fn is(stream: Stream) -> bool {
use std::io;
use termion::is_tty;
match stream {
Stream::Stdin => is_tty(&io::stdin()),
Stream::Stdout => is_tty(&io::stdout()),
Stream::Stderr => is_tty(&io::stderr()),
}
}
#[cfg(target_arch = "wasm32")]
pub fn is(_stream: Stream) -> bool {
false
}
#[cfg(test)]
mod tests {
use super::{Stream, is};
#[test]
#[cfg(windows)]
fn is_err() {
assert!(!is(Stream::Stderr))
}
#[test]
#[cfg(windows)]
fn is_out() {
assert!(!is(Stream::Stdout))
}
#[test]
#[cfg(windows)]
fn is_in() {
assert!(is(Stream::Stdin))
}
#[test]
#[cfg(unix)]
fn is_err() {
assert!(is(Stream::Stderr))
}
#[test]
#[cfg(unix)]
fn is_out() {
assert!(is(Stream::Stdout))
}
#[test]
#[cfg(target_os = "macos")]
fn is_in() {
assert!(is(Stream::Stdin))
}
#[test]
#[cfg(all(not(target_os = "macos"), unix))]
fn is_in() {
assert!(is(Stream::Stdin))
}
}