Skip to content

Commit cc3cba1

Browse files
authored
Merge pull request #65 from alexwlchan/detect-tty
Don't print terminal colours when not running in a tty
2 parents 7f653ec + 148b3b3 commit cc3cba1

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v1.4.0 - 2024-10-05
4+
5+
* `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.
6+
37
## v1.3.0 - 2024-09-04
48

59
* Add support for animated WebP images.

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dominant_colours"
3-
version = "1.3.0"
3+
version = "1.4.0"
44
edition = "2018"
55

66
[dependencies]

src/main.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![deny(warnings)]
22

3+
use std::io::IsTerminal;
34
use std::path::PathBuf;
45

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

55+
// Should we print with colours in the terminal, or just sent text?
56+
//
57+
// When I created this tool, I had a `--no-palette` flag to suppress the
58+
// terminal colours, but I've since realised that I can look for the
59+
// presence of a TTY and disable colours if we're not in a terminal,
60+
// even if the user hasn't passed `--no-palette`.
61+
//
62+
// I'm keeping the old flag for backwards compatibility, but I might
63+
// retire it in a future v2 update.
64+
//
65+
// Note: because of the difficulty of simulating a TTY in automated tests,
66+
// this isn't tested properly -- but I'll notice quickly if this breaks!
67+
let include_bg_color = if cli.no_palette {
68+
false
69+
} else if std::io::stdout().is_terminal() {
70+
true
71+
} else {
72+
false
73+
};
74+
5475
for c in rgb_colors {
55-
printing::print_color(c, &cli.background, cli.no_palette);
76+
printing::print_color(c, &cli.background, include_bg_color);
5677
}
5778
}
5879

@@ -68,14 +89,13 @@ mod tests {
6889
// provided by the external library.
6990

7091
#[test]
71-
fn it_prints_the_color_with_ansi_escape_codes() {
92+
fn it_prints_the_colour() {
7293
let output = get_success(&["./src/tests/red.png", "--max-colours=1"]);
7394

7495
assert_eq!(output.exit_code, 0);
7596

7697
assert!(
77-
output.stdout == "\u{1b}[38;2;255;0;0m▇ #ff0000\u{1b}[0m\n"
78-
|| output.stdout == "\u{1b}[38;2;254;0;0m▇ #fe0000\u{1b}[0m\n",
98+
output.stdout == "#ff0000\n" || output.stdout == "#fe0000\n",
7999
"stdout = {:?}",
80100
output.stdout
81101
);

src/printing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ use palette::Srgb;
55
//
66
// See https://alexwlchan.net/2021/04/coloured-squares/
77
// See: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797?permalink_comment_id=3857871
8-
pub fn print_color(c: Srgb<u8>, background: &Option<Srgb<u8>>, no_palette: bool) {
8+
pub fn print_color(c: Srgb<u8>, background: &Option<Srgb<u8>>, include_bg_colo: bool) {
99
let display_value = format!("#{:02x}{:02x}{:02x}", c.red, c.green, c.blue);
1010

11-
if no_palette {
12-
println!("{}", display_value);
13-
} else {
11+
if include_bg_colo {
1412
// If a background colour is specified, print it behind the
1513
// hex strings.
1614
match background {
@@ -22,5 +20,7 @@ pub fn print_color(c: Srgb<u8>, background: &Option<Srgb<u8>>, no_palette: bool)
2220
"\x1B[38;2;{};{};{}m▇ {}\x1B[0m",
2321
c.red, c.green, c.blue, display_value
2422
);
23+
} else {
24+
println!("{}", display_value);
2525
}
2626
}

0 commit comments

Comments
 (0)