Files
ligent/tools/check-colors.sh
T
2026-10-03 14:13:21 +08:00

39 lines
1.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# check-colors.sh - 审计所有 ANSI 颜色码,确保都在 16 色内
#
# 为什么要单独一个脚本:颜色越界这件事**看代码看不出来** —— \033[94m 在人眼里
# 也是"一个蓝色",只有拿尺子量才知道它属于 aixterm 的亮色段,不在最窄的
# 16 色 ANSI 里。而这个项目是硬编码 SGR、不查 terminfo 的,既然不探测终端
# 能力,就该待在所有终端都认的那一档。
#
# 允许的 SGR 参数:
# 0 重置
# 1/2/3/4 加粗/暗淡/斜体/下划线(属性,不是颜色)
# 30-37 标准前景 8 色
# 39 默认前景色
# 不允许:90-97(亮色)、38;5/48;5(256 色)、38;2/48;2(真彩)
set -eu
ROOT=$(cd "$(dirname "$0")/.." && pwd)
cd "$ROOT"
# 只扫源码,不扫文档 —— README 里要举反例讲这件事,出现在那里是对的
bad=$(grep -rhoE '\\033\[[0-9;]+m' src/*.c src/*.h 2>/dev/null \
| grep -oE '[0-9;]+' \
| tr ';' '\n' \
| sort -n -u \
| awk '$1 != 0 && $1 != 1 && $1 != 2 && $1 != 3 && $1 != 4 \
&& $1 != 39 && !($1 >= 30 && $1 <= 37) { print }' || true)
if [ -n "$bad" ]; then
echo "发现 16 色范围外的 SGR 参数:" >&2
printf ' %s\n' $bad >&2
echo "" >&2
echo "允许的:0 1 2 3 4 30-37 39" >&2
echo "越界的:90-97(aixterm 亮色)、38;5/48;5(256 色)、38;2/48;2(真彩)" >&2
exit 1
fi
n=$(grep -rhoE '\\033\[[0-9;]+m' src/*.c src/*.h 2>/dev/null | sort -u | wc -l)
echo "颜色审计通过:$n 个不同的 SGR 序列,全部在 16 色内。"