Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't print terminal colours when not running in a tty #65

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dominant_colours"
version = "1.3.0"
version = "1.4.0"
edition = "2018"

[dependencies]
Expand Down
28 changes: 24 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(warnings)]

use std::io::IsTerminal;
use std::path::PathBuf;

use clap::Parser;
Expand Down Expand Up @@ -51,8 +52,28 @@ fn main() {
.map(|c| Srgb::from_color(*c).into_format())
.collect::<Vec<Srgb<u8>>>();

// 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);
}
}

Expand All @@ -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
);
Expand Down
8 changes: 4 additions & 4 deletions src/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>, background: &Option<Srgb<u8>>, no_palette: bool) {
pub fn print_color(c: Srgb<u8>, background: &Option<Srgb<u8>>, 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 {
Expand All @@ -22,5 +20,7 @@ pub fn print_color(c: Srgb<u8>, background: &Option<Srgb<u8>>, no_palette: bool)
"\x1B[38;2;{};{};{}m▇ {}\x1B[0m",
c.red, c.green, c.blue, display_value
);
} else {
println!("{}", display_value);
}
}