From 859c97b90a2947bd16b010c5db01c37932bb21e8 Mon Sep 17 00:00:00 2001 From: Blair Noctis Date: Tue, 5 Nov 2024 19:17:58 +0800 Subject: [PATCH 1/2] Upgrade yansi: 0.5.1 -> 1.0.1 - Adapt to `thing.paint(style)` API; was `style.paint(thing)` - Remove `yansi::Paint::enable_windows_ascii()` in style usage decision; removed in yansi commit b186eb5bfb, which introduced "automatic" support for Windows: "If support is not available, styling is disabled and no styling sequences are emitted", fitting the `Auto` option --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/config.rs | 2 +- src/main.rs | 9 +-------- src/output.rs | 11 ++++++----- src/utils.rs | 4 ++-- 6 files changed, 13 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a1c940d..0208435e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2009,9 +2009,9 @@ checksum = "213b7324336b53d2414b2db8537e56544d981803139155afa84f76eeebb7a546" [[package]] name = "yansi" -version = "0.5.1" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" [[package]] name = "zerocopy" diff --git a/Cargo.toml b/Cargo.toml index 954a52c6..d567831f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ serde = "1.0.21" serde_derive = "1.0.21" toml = "0.8.19" walkdir = "2.0.1" -yansi = "0.5" +yansi = "1" zip = { version = "2.1.6", default-features = false, features = ["deflate"] } [target.'cfg(not(windows))'.dependencies] diff --git a/src/config.rs b/src/config.rs index 98d4e465..454fbf58 100644 --- a/src/config.rs +++ b/src/config.rs @@ -57,7 +57,7 @@ impl From for Color { RawColor::Cyan => Self::Cyan, RawColor::White => Self::White, RawColor::Ansi(num) => Self::Fixed(num), - RawColor::Rgb { r, g, b } => Self::RGB(r, g, b), + RawColor::Rgb { r, g, b } => Self::Rgb(r, g, b), } } } diff --git a/src/main.rs b/src/main.rs index 9dede44d..c6a00d01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -249,20 +249,13 @@ fn main() { let args = Cli::parse(); // Determine the usage of styles - #[cfg(target_os = "windows")] - let ansi_support = yansi::Paint::enable_windows_ascii(); - #[cfg(not(target_os = "windows"))] - let ansi_support = true; let enable_styles = match args.color.unwrap_or_default() { // Attempt to use styling if instructed ColorOptions::Always => true, // Enable styling if: - // * There is `ansi_support` // * NO_COLOR env var isn't set: https://no-color.org/ // * The output stream is stdout (not being piped) - ColorOptions::Auto => { - ansi_support && env::var_os("NO_COLOR").is_none() && io::stdout().is_terminal() - } + ColorOptions::Auto => env::var_os("NO_COLOR").is_none() && io::stdout().is_terminal(), // Disable styling ColorOptions::Never => false, }; diff --git a/src/output.rs b/src/output.rs index 357b988d..abef1dbe 100644 --- a/src/output.rs +++ b/src/output.rs @@ -3,6 +3,7 @@ use std::io::{self, BufRead, Write}; use anyhow::{Context, Result}; +use yansi::Paint; use crate::{ cache::PageLookupResult, @@ -86,11 +87,11 @@ fn print_snippet( use PageSnippet::*; match snip { - CommandName(s) => write!(writer, "{}", style.command_name.paint(s)), - Variable(s) => write!(writer, "{}", style.example_variable.paint(s)), - NormalCode(s) => write!(writer, "{}", style.example_code.paint(s)), - Description(s) => writeln!(writer, " {}", style.description.paint(s)), - Text(s) => writeln!(writer, " {}", style.example_text.paint(s)), + CommandName(s) => write!(writer, "{}", s.paint(style.command_name)), + Variable(s) => write!(writer, "{}", s.paint(style.example_variable)), + NormalCode(s) => write!(writer, "{}", s.paint(style.example_code)), + Description(s) => writeln!(writer, " {}", s.paint(style.description)), + Text(s) => writeln!(writer, " {}", s.paint(style.example_text)), Linebreak => writeln!(writer), } } diff --git a/src/utils.rs b/src/utils.rs index 1649281d..f4d825a8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use yansi::Color; +use yansi::{Color, Paint}; /// Print a warning to stderr. If `enable_styles` is true, then a yellow /// message will be printed. @@ -14,7 +14,7 @@ pub fn print_error(enable_styles: bool, error: &anyhow::Error) { fn print_msg(enable_styles: bool, message: &str, prefix: &'static str, color: Color) { if enable_styles { - eprintln!("{}{}", color.paint(prefix), color.paint(message)); + eprintln!("{}{}", prefix.paint(color), message.paint(color)); } else { eprintln!("{message}"); } From 8a8253a093fef143bac3b7788ae2ae01c75def70 Mon Sep 17 00:00:00 2001 From: Niklas Mohrin Date: Sun, 10 Nov 2024 22:24:21 +0100 Subject: [PATCH 2/2] Respect `--color=always` even if we know it won't work --- src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index c6a00d01..5e6e7397 100644 --- a/src/main.rs +++ b/src/main.rs @@ -251,7 +251,10 @@ fn main() { // Determine the usage of styles let enable_styles = match args.color.unwrap_or_default() { // Attempt to use styling if instructed - ColorOptions::Always => true, + ColorOptions::Always => { + yansi::enable(); // disable yansi's automatic detection for ANSI support on Windows + true + } // Enable styling if: // * NO_COLOR env var isn't set: https://no-color.org/ // * The output stream is stdout (not being piped)