From 7b5131b613a6f41404a1528d822682767be223d1 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 9 Apr 2024 15:59:30 +0200 Subject: [PATCH] Use std IsTerminal interface --- src/currentprocess.rs | 8 +++----- src/utils/mod.rs | 1 - src/utils/tty.rs | 37 ------------------------------------- 3 files changed, 3 insertions(+), 43 deletions(-) delete mode 100644 src/utils/tty.rs diff --git a/src/currentprocess.rs b/src/currentprocess.rs index 1e51db3e18..5400bbabcb 100644 --- a/src/currentprocess.rs +++ b/src/currentprocess.rs @@ -2,7 +2,7 @@ use std::cell::RefCell; use std::env; use std::ffi::OsString; use std::fmt::Debug; -use std::io; +use std::io::{self, IsTerminal}; use std::panic; use std::path::PathBuf; use std::sync::Once; @@ -31,8 +31,6 @@ use cwdsource::*; use filesource::*; use varsource::*; -use crate::utils::tty::{stderr_isatty, stdout_isatty}; - /// An abstraction for the current process. /// /// This acts as a clonable proxy to the global state provided by some key OS @@ -186,8 +184,8 @@ pub struct OSProcess { impl OSProcess { pub fn new() -> Self { OSProcess { - stderr_is_a_tty: stderr_isatty(), - stdout_is_a_tty: stdout_isatty(), + stderr_is_a_tty: io::stderr().is_terminal(), + stdout_is_a_tty: io::stdout().is_terminal(), } } } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 13c965cccb..7aaea2544a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,7 +2,6 @@ pub(crate) mod notifications; pub mod raw; pub(crate) mod toml_utils; -pub(crate) mod tty; pub(crate) mod units; #[allow(clippy::module_inception)] pub mod utils; diff --git a/src/utils/tty.rs b/src/utils/tty.rs deleted file mode 100644 index 9f185ad4dc..0000000000 --- a/src/utils/tty.rs +++ /dev/null @@ -1,37 +0,0 @@ -// Copied from rustc. isatty crate did not work as expected. -#[cfg(unix)] -pub(crate) fn stderr_isatty() -> bool { - isatty(libc::STDERR_FILENO) -} - -#[cfg(windows)] -pub(crate) fn stderr_isatty() -> bool { - isatty(winapi::um::winbase::STD_ERROR_HANDLE) -} - -#[cfg(unix)] -pub(crate) fn stdout_isatty() -> bool { - isatty(libc::STDOUT_FILENO) -} - -#[cfg(windows)] -pub(crate) fn stdout_isatty() -> bool { - isatty(winapi::um::winbase::STD_OUTPUT_HANDLE) -} - -#[inline] -#[cfg(unix)] -fn isatty(fd: libc::c_int) -> bool { - unsafe { libc::isatty(fd) == 1 } -} - -#[inline] -#[cfg(windows)] -fn isatty(fd: winapi::shared::minwindef::DWORD) -> bool { - use winapi::um::{consoleapi::GetConsoleMode, processenv::GetStdHandle}; - unsafe { - let handle = GetStdHandle(fd); - let mut out = 0; - GetConsoleMode(handle, &mut out) != 0 - } -}