From 5e78cf8c41f09044504c7b4de55407f4ff84fd78 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Fri, 11 Aug 2023 15:26:19 +0200 Subject: [PATCH] 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 --- fish-rust/src/builtins/shared.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fish-rust/src/builtins/shared.rs b/fish-rust/src/builtins/shared.rs index 3f4a7e50c..1bfc2b6b1 100644 --- a/fish-rust/src/builtins/shared.rs +++ b/fish-rust/src/builtins/shared.rs @@ -7,6 +7,9 @@ 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 @@ pub fn ffi_ref(&self) -> &builtins_ffi::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() }