From 8a909697f892187fc5a90d2cd63befb8b9d04c57 Mon Sep 17 00:00:00 2001 From: WindFade <17927678+a322655@users.noreply.github.com> Date: Fri, 16 Jan 2026 14:03:27 +0800 Subject: [PATCH] feat: disable ANSI escape sequences in `ya pkg` when stdout is not a TTY (#3566) Co-authored-by: sxyazi --- CHANGELOG.md | 2 ++ Cargo.lock | 1 + yazi-cli/Cargo.toml | 1 + yazi-cli/src/package/dependency.rs | 18 +++++++++++------- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb7529ea..904ef262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/): - Tree view for the preset archive previewer ([#3525]) - Support compressed tarballs (`.tar.gz`, `.tar.bz2`, etc.) in the preset archive previewer ([#3518]) - Check and refresh the file list when the terminal gains focus ([#3561]) +- Disable ANSI escape sequences in `ya pkg` when stdout is not a TTY ([#3566]) - New `Path.os()` API creates an OS-native `Path` ([#3541]) ### Fixed @@ -1603,3 +1604,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/): [#3540]: https://github.com/sxyazi/yazi/pull/3540 [#3541]: https://github.com/sxyazi/yazi/pull/3541 [#3561]: https://github.com/sxyazi/yazi/pull/3561 +[#3566]: https://github.com/sxyazi/yazi/pull/3566 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..37698ba0 100644 --- a/yazi-cli/src/package/dependency.rs +++ b/yazi-cli/src/package/dependency.rs @@ -1,4 +1,4 @@ -use std::{io::BufWriter, path::{Path, PathBuf}, str::FromStr}; +use std::{env, io::{self, BufWriter}, path::{Path, PathBuf}, str::FromStr}; use anyhow::{Result, bail}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -46,23 +46,27 @@ impl Dependency { } pub(super) fn header(&self, s: &str) -> Result<()> { - use crossterm::style::{Attribute, Print, SetAttributes}; + use std::io::IsTerminal; + use crossterm::style::{Attribute, Print, SetAttributes}; + use yazi_term::If; + + let ansi = env::var_os("YA_FORCE_ANSI").is_some_and(|v| v == "1") || io::stdout().is_terminal(); crossterm::execute!( - BufWriter::new(std::io::stdout()), + BufWriter::new(io::stdout()), Print("\n"), - SetAttributes(Attribute::Reverse.into()), - SetAttributes(Attribute::Bold.into()), + If(ansi, SetAttributes(Attribute::Reverse.into())), + If(ansi, SetAttributes(Attribute::Bold.into())), Print(" "), Print(s.replacen("{name}", &self.name, 1)), Print(" "), - SetAttributes(Attribute::Reset.into()), + If(ansi, SetAttributes(Attribute::Reset.into())), Print("\n\n"), )?; Ok(()) } - pub(super) async fn plugin_files(dir: &Path) -> std::io::Result> { + pub(super) async fn plugin_files(dir: &Path) -> io::Result> { let mut it = ok_or_not_found!(tokio::fs::read_dir(dir).await, return Ok(vec![])); let mut files: Vec = ["LICENSE", "README.md", "main.lua"].into_iter().map(Into::into).collect();