From 423dc8b3443f9ab6c391e178f5fff9851323ed50 Mon Sep 17 00:00:00 2001 From: WindFade <17927678+a322655@users.noreply.github.com> Date: Wed, 14 Jan 2026 08:18:37 +0000 Subject: [PATCH] feat: disable ANSI escape sequences in `ya pkg` when stdout is not a TTY --- Cargo.lock | 1 + yazi-cli/Cargo.toml | 1 + yazi-cli/src/package/dependency.rs | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 79182d1f..77658bb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5528,6 +5528,7 @@ dependencies = [ "yazi-fs", "yazi-macro", "yazi-shared", + "yazi-term", ] [[package]] diff --git a/yazi-cli/Cargo.toml b/yazi-cli/Cargo.toml index 7933b765..8a1bd5c5 100644 --- a/yazi-cli/Cargo.toml +++ b/yazi-cli/Cargo.toml @@ -27,6 +27,7 @@ yazi-dds = { path = "../yazi-dds", version = "26.1.4" } yazi-fs = { path = "../yazi-fs", version = "26.1.4" } yazi-macro = { path = "../yazi-macro", version = "26.1.4" } yazi-shared = { path = "../yazi-shared", version = "26.1.4" } +yazi-term = { path = "../yazi-term", version = "26.1.4" } # External dependencies anyhow = { workspace = true } diff --git a/yazi-cli/src/package/dependency.rs b/yazi-cli/src/package/dependency.rs index ccb14696..5e38a8b1 100644 --- a/yazi-cli/src/package/dependency.rs +++ b/yazi-cli/src/package/dependency.rs @@ -47,16 +47,29 @@ impl Dependency { pub(super) fn header(&self, s: &str) -> Result<()> { 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!( BufWriter::new(std::io::stdout()), Print("\n"), - SetAttributes(Attribute::Reverse.into()), - SetAttributes(Attribute::Bold.into()), + If(styled, SetAttributes(Attribute::Reverse.into())), + If(styled, SetAttributes(Attribute::Bold.into())), Print(" "), Print(s.replacen("{name}", &self.name, 1)), Print(" "), - SetAttributes(Attribute::Reset.into()), + If(styled, SetAttributes(Attribute::Reset.into())), Print("\n\n"), )?; Ok(())