Add io_streams_t::out_is_terminal()

This encapsulates a "is our output going to the terminal" check we do
in a few places - functions, type, set_color, possibly test
This commit is contained in:
Fabian Boehm 2023-08-11 15:26:19 +02:00
parent b75f901376
commit 5e78cf8c41

View File

@ -7,6 +7,9 @@ use crate::wchar_ffi::{c_str, empty_wstring, ToCppWString, WCharFromFFI};
use crate::wgetopt::{wgetopter_t, wopt, woption, woption_argument_t};
use cxx::{type_id, ExternType};
use libc::c_int;
use libc::isatty;
use libc::STDOUT_FILENO;
use std::borrow::Cow;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
@ -164,6 +167,10 @@ impl io_streams_t {
unsafe { &*self.streams }
}
pub fn out_is_terminal(&self) -> bool {
!self.out_is_redirected && unsafe { isatty(STDOUT_FILENO) == 1 }
}
pub fn stdin_is_directly_redirected(&self) -> bool {
self.ffi_ref().ffi_stdin_is_directly_redirected()
}