1#[cfg(unix)]
7use std::os::unix::io::AsRawFd;
8#[cfg(windows)]
9use std::os::windows::io::AsRawHandle;
10
11#[cfg(windows)]
12use winapi::um::consoleapi::GetConsoleMode;
13
14pub trait IsTty {
23 fn is_tty(&self) -> bool;
25}
26
27#[cfg(all(unix, feature = "libc"))]
30impl<S: AsRawFd> IsTty for S {
31 fn is_tty(&self) -> bool {
32 let fd = self.as_raw_fd();
33 unsafe { libc::isatty(fd) == 1 }
34 }
35}
36
37#[cfg(all(unix, not(feature = "libc")))]
38impl<S: AsRawFd> IsTty for S {
39 fn is_tty(&self) -> bool {
40 let fd = self.as_raw_fd();
41 rustix::termios::isatty(unsafe { std::os::unix::io::BorrowedFd::borrow_raw(fd) })
42 }
43}
44
45#[cfg(windows)]
48impl<S: AsRawHandle> IsTty for S {
49 fn is_tty(&self) -> bool {
50 let mut mode = 0;
51 let ok = unsafe { GetConsoleMode(self.as_raw_handle() as *mut _, &mut mode) };
52 ok == 1
53 }
54}