feat: disable ANSI escape sequences in ya pkg when stdout is not a TTY

This commit is contained in:
WindFade 2026-01-14 08:18:37 +00:00 committed by sxyazi
parent 798d38e494
commit 423dc8b344
No known key found for this signature in database
3 changed files with 18 additions and 3 deletions

1
Cargo.lock generated
View file

@ -5528,6 +5528,7 @@ dependencies = [
"yazi-fs", "yazi-fs",
"yazi-macro", "yazi-macro",
"yazi-shared", "yazi-shared",
"yazi-term",
] ]
[[package]] [[package]]

View file

@ -27,6 +27,7 @@ yazi-dds = { path = "../yazi-dds", version = "26.1.4" }
yazi-fs = { path = "../yazi-fs", version = "26.1.4" } yazi-fs = { path = "../yazi-fs", version = "26.1.4" }
yazi-macro = { path = "../yazi-macro", version = "26.1.4" } yazi-macro = { path = "../yazi-macro", version = "26.1.4" }
yazi-shared = { path = "../yazi-shared", version = "26.1.4" } yazi-shared = { path = "../yazi-shared", version = "26.1.4" }
yazi-term = { path = "../yazi-term", version = "26.1.4" }
# External dependencies # External dependencies
anyhow = { workspace = true } anyhow = { workspace = true }

View file

@ -47,16 +47,29 @@ impl Dependency {
pub(super) fn header(&self, s: &str) -> Result<()> { pub(super) fn header(&self, s: &str) -> Result<()> {
use crossterm::style::{Attribute, Print, SetAttributes}; use crossterm::style::{Attribute, Print, SetAttributes};
use std::io::IsTerminal;
use yazi_term::If;
fn styles_enabled() -> bool {
if let Ok(v) = std::env::var("YA_FORCE_ANSI")
&& !v.is_empty()
&& v != "0"
{
return true;
}
std::io::stdout().is_terminal()
}
let styled = styles_enabled();
crossterm::execute!( crossterm::execute!(
BufWriter::new(std::io::stdout()), BufWriter::new(std::io::stdout()),
Print("\n"), Print("\n"),
SetAttributes(Attribute::Reverse.into()), If(styled, SetAttributes(Attribute::Reverse.into())),
SetAttributes(Attribute::Bold.into()), If(styled, SetAttributes(Attribute::Bold.into())),
Print(" "), Print(" "),
Print(s.replacen("{name}", &self.name, 1)), Print(s.replacen("{name}", &self.name, 1)),
Print(" "), Print(" "),
SetAttributes(Attribute::Reset.into()), If(styled, SetAttributes(Attribute::Reset.into())),
Print("\n\n"), Print("\n\n"),
)?; )?;
Ok(()) Ok(())