From 148b3b3440236846b458ab6bb52127013a7d7091 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sat, 5 Oct 2024 16:01:18 +0100 Subject: [PATCH] Don't print terminal colours when not running in a tty --- CHANGELOG.md | 4 ++++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 28 ++++++++++++++++++++++++---- src/printing.rs | 8 ++++---- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f36415e..bf4b158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.4.0 - 2024-10-05 + +* `dominant_colours` will now skip printing terminal colours if it detects it's not running in a tty. This makes it slightly easier to use in automated environments, because you don't need to pass the `--no-palette` flag. + ## v1.3.0 - 2024-09-04 * Add support for animated WebP images. diff --git a/Cargo.lock b/Cargo.lock index 4fd851f..f20d9dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -213,7 +213,7 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" [[package]] name = "dominant_colours" -version = "1.3.0" +version = "1.4.0" dependencies = [ "assert_cmd", "clap", diff --git a/Cargo.toml b/Cargo.toml index 1324df3..a967952 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dominant_colours" -version = "1.3.0" +version = "1.4.0" edition = "2018" [dependencies] diff --git a/src/main.rs b/src/main.rs index 301c6e3..7b46eec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![deny(warnings)] +use std::io::IsTerminal; use std::path::PathBuf; use clap::Parser; @@ -51,8 +52,28 @@ fn main() { .map(|c| Srgb::from_color(*c).into_format()) .collect::>>(); + // Should we print with colours in the terminal, or just sent text? + // + // When I created this tool, I had a `--no-palette` flag to suppress the + // terminal colours, but I've since realised that I can look for the + // presence of a TTY and disable colours if we're not in a terminal, + // even if the user hasn't passed `--no-palette`. + // + // I'm keeping the old flag for backwards compatibility, but I might + // retire it in a future v2 update. + // + // Note: because of the difficulty of simulating a TTY in automated tests, + // this isn't tested properly -- but I'll notice quickly if this breaks! + let include_bg_color = if cli.no_palette { + false + } else if std::io::stdout().is_terminal() { + true + } else { + false + }; + for c in rgb_colors { - printing::print_color(c, &cli.background, cli.no_palette); + printing::print_color(c, &cli.background, include_bg_color); } } @@ -68,14 +89,13 @@ mod tests { // provided by the external library. #[test] - fn it_prints_the_color_with_ansi_escape_codes() { + fn it_prints_the_colour() { let output = get_success(&["./src/tests/red.png", "--max-colours=1"]); assert_eq!(output.exit_code, 0); assert!( - output.stdout == "\u{1b}[38;2;255;0;0m▇ #ff0000\u{1b}[0m\n" - || output.stdout == "\u{1b}[38;2;254;0;0m▇ #fe0000\u{1b}[0m\n", + output.stdout == "#ff0000\n" || output.stdout == "#fe0000\n", "stdout = {:?}", output.stdout ); diff --git a/src/printing.rs b/src/printing.rs index 8704966..e06b0d2 100644 --- a/src/printing.rs +++ b/src/printing.rs @@ -5,12 +5,10 @@ use palette::Srgb; // // See https://alexwlchan.net/2021/04/coloured-squares/ // See: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797?permalink_comment_id=3857871 -pub fn print_color(c: Srgb, background: &Option>, no_palette: bool) { +pub fn print_color(c: Srgb, background: &Option>, include_bg_colo: bool) { let display_value = format!("#{:02x}{:02x}{:02x}", c.red, c.green, c.blue); - if no_palette { - println!("{}", display_value); - } else { + if include_bg_colo { // If a background colour is specified, print it behind the // hex strings. match background { @@ -22,5 +20,7 @@ pub fn print_color(c: Srgb, background: &Option>, no_palette: bool) "\x1B[38;2;{};{};{}m▇ {}\x1B[0m", c.red, c.green, c.blue, display_value ); + } else { + println!("{}", display_value); } }