diff --git a/rust/cli/CHANGELOG.md b/rust/cli/CHANGELOG.md index 3f514475..5c68caee 100644 --- a/rust/cli/CHANGELOG.md +++ b/rust/cli/CHANGELOG.md @@ -11,6 +11,7 @@ ### Patch +- Use the terminal palette for low-confidence groups so they stay readable on light backgrounds (fixes #1243) - Fix panic with JSON output when a file name is not UTF-8 - Remove deprecated `package.authors` field in `Cargo.toml` - Update dependencies diff --git a/rust/cli/src/main.rs b/rust/cli/src/main.rs index 9c6de0b0..65929ba9 100644 --- a/rust/cli/src/main.rs +++ b/rust/cli/src/main.rs @@ -910,8 +910,8 @@ impl Response { fn color(&self, result: ColoredString) -> ColoredString { use colored::Colorize as _; - // We only use true colors (except for errors). If the terminal doesn't support true colors, - // the colored crate will automatically choose the closest one. + // We only use true colors for the highlighted groups (and red for errors). If the terminal + // doesn't support true colors, the colored crate will automatically choose the closest one. match &self.result { Err(_) => result.bold().red(), Ok(x) => match x.info().group { @@ -924,7 +924,11 @@ impl Response { "executable" => result.truecolor(0xec, 0x48, 0x99), // Pink 500 "image" => result.truecolor(0x06, 0xb6, 0xd4), // Cyan 500 "video" => result.truecolor(0x10, 0xb9, 0x81), // Emerald 500 - _ => result.bold().truecolor(0xcc, 0xcc, 0xcc), + // Use the terminal palette for the remaining groups (e.g. "text" and "unknown", + // used for low-confidence reports) instead of a fixed light gray. The palette + // adapts to the user's terminal theme: bright black renders as a dark gray on + // light backgrounds and as a light gray on dark backgrounds (fixes #1243). + _ => result.bold().bright_black(), }, } } diff --git a/rust/cli/test.sh b/rust/cli/test.sh index 3c4cfdf4..9ff712ec 100755 --- a/rust/cli/test.sh +++ b/rust/cli/test.sh @@ -53,6 +53,21 @@ info "Test symlinks" [ "$(magika --format=%l LICENSE)" = txt ] || error "does not follow symlinks" [ "$(magika --format=%l --no-dereference LICENSE)" = symlink ] || error "follows symlinks" +info "Test low-confidence groups are readable on light backgrounds" +( dir=$(mktemp -d) + trap "rm -rf $dir" EXIT + file=$dir/text.txt + printf 'hello, world\n' > $file + output=$(magika --colors $file) + # The "text" group falls back to the terminal palette (bright black) instead of the fixed + # light gray #cccccc that is invisible on light-background terminals (fixes #1243). + printf '%s' "$output" | grep -qF $'\033[1;90m' \ + || error "low-confidence groups do not use the terminal palette" + if printf '%s' "$output" | grep -qF $'\033[1;38;2;204;204;204m'; then + error "low-confidence groups still use the light gray #cccccc" + fi +) + test_error() { files="$1" expected="$2"