Skip to content
Open
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
1 change: 1 addition & 0 deletions rust/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions rust/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
},
}
}
Expand Down
15 changes: 15 additions & 0 deletions rust/cli/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading