Skip to content

Commit 5cadfcf

Browse files
committed
Fix: use DARK_GRAY for unmapped content type groups (fixes #1243)
Root cause: color_by_group in magika_client.py did not have entries for "text", "unknown", "application", "font", and "inode" groups, so they all fell back to colors.WHITE (\033[1;37m — bright white). On light-background terminals this produces nearly invisible output. Fix: add explicit "text" and "unknown" mappings to DARK_GRAY, and change the dict's default fallback from colors.WHITE to colors.DARK_GRAY. DARK_GRAY (\033[1;30m) is visible on both dark and light terminals. Regression test added: verifies that plain-text file output uses DARK_GRAY and not the bright-white code that breaks on white backgrounds.
1 parent 7056c1f commit 5cadfcf

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

python/src/magika/cli/magika_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ def main(
255255
"image": colors.YELLOW,
256256
"video": colors.YELLOW,
257257
"code": colors.LIGHT_BLUE,
258+
"text": colors.DARK_GRAY,
259+
"unknown": colors.DARK_GRAY,
258260
}
259261

260262
# updated only when we need to output in JSON format
@@ -309,7 +311,7 @@ def main(
309311

310312
if with_colors:
311313
start_color = color_by_group.get(
312-
result.prediction.output.group, colors.WHITE
314+
result.prediction.output.group, colors.DARK_GRAY
313315
)
314316
end_color = colors.RESET
315317
else:

python/tests/test_python_magika_client.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
# limitations under the License.
1414

1515
import subprocess
16+
import tempfile
1617
from pathlib import Path
1718

19+
from magika import colors
20+
1821

1922
def test_python_magika_client() -> None:
2023
python_root_dir = Path(__file__).parent.parent
@@ -29,3 +32,46 @@ def test_python_magika_client() -> None:
2932
# quick test to check there are no crashes
3033
cmd = [str(python_magika_client_path), str(python_magika_client_path)]
3134
subprocess.run(cmd, capture_output=True, check=True)
35+
36+
37+
def test_colored_output_visible_on_light_background_terminals() -> None:
38+
"""Regression test for https://github.com/google/magika/issues/1243.
39+
40+
The fallback color for content type groups not explicitly mapped in
41+
color_by_group must not be bright white (\033[1;37m), which is nearly
42+
invisible on light-background terminals. DARK_GRAY (\033[1;30m) is
43+
visible on both dark and light backgrounds.
44+
"""
45+
python_root_dir = Path(__file__).parent.parent
46+
python_magika_client_path = (
47+
python_root_dir / "src" / "magika" / "cli" / "magika_client.py"
48+
).resolve()
49+
50+
# Write a plain text file — classified as group="text", which previously
51+
# fell through to the bright-white fallback color.
52+
with tempfile.NamedTemporaryFile(suffix=".txt", mode="w", delete=False) as tmp:
53+
tmp.write("Hello, world!\n")
54+
tmp_path = Path(tmp.name)
55+
56+
try:
57+
result = subprocess.run(
58+
[str(python_magika_client_path), "--colors", str(tmp_path)],
59+
capture_output=True,
60+
text=True,
61+
check=True,
62+
)
63+
output = result.stdout
64+
# The output must not contain the bright-white ANSI code, which is
65+
# invisible on white/light-background terminals.
66+
assert colors.WHITE not in output, (
67+
f"Output used bright-white ({repr(colors.WHITE)}), which is "
68+
"invisible on light-background terminals. Use DARK_GRAY instead."
69+
)
70+
# DARK_GRAY must be present so that content types in the "text" group
71+
# (and any other unmapped group) are readable on light terminals.
72+
assert colors.DARK_GRAY in output, (
73+
f"Expected DARK_GRAY ({repr(colors.DARK_GRAY)}) in colored output "
74+
"for a plain-text file, but it was not found."
75+
)
76+
finally:
77+
tmp_path.unlink(missing_ok=True)

0 commit comments

Comments
 (0)