feat(homebrew): 优化交互式终端中的 Homebrew 进度显示

- 在交互式终端中保留 Homebrew 6 的原生实时下载进度条和动态状态
- 使用 -t 检查判断终端类型,决定是否启用原生进度条
- 在 macOS 上使用 script(1) 创建伪终端以记录输出到日志
- 重定向到文件或 CI 时自动切换为稳定逐行输出模式
- 通过 HOMEBREW_COLOR 环境变量控制配色方案
- 在 Darwin 系统上检查 script 工具可用性以决定处理方式
This commit is contained in:
2026-07-24 00:59:02 +08:00
parent d090edd5c6
commit 972e7a142d
2 changed files with 21 additions and 3 deletions
+18 -3
View File
@@ -573,8 +573,14 @@ colorize_brew_output() {
run_brew_colored() {
local pipeline_status=()
# 禁用 Homebrew 自带的不完整配色,统一按语义着色。这里不能在
# 交互式终端直接运行 brew,否则版本变化行会绕过着色器。
# Homebrew 6 的并行下载队列只有在 stdout 是 TTY 时才会绘制
# 实时进度。交互式终端直接运行 brew保留它的原生进度条与
# 动态下载状态;重定向到文件或 CI 时再使用按行语义着色。
if [[ -t 1 ]]; then
env -u HOMEBREW_COLOR brew "$@"
return $?
fi
if env -u HOMEBREW_COLOR brew "$@" 2>&1 | colorize_brew_output; then
return 0
fi
@@ -698,7 +704,16 @@ run_brew_with_retries() {
fi
: > "$ACTIVE_LOG_FILE"
if env -u HOMEBREW_COLOR brew "$@" 2>&1 | tee "$ACTIVE_LOG_FILE" | colorize_brew_output; then
if [[ -t 1 && "$(uname -s 2>/dev/null || true)" == "Darwin" && -x /usr/bin/script ]]; then
# tee 会把 brew 的 stdout 变成管道,导致 Homebrew 关闭实时进度。
# macOS script(1) 为 brew 分配伪终端,同时把完整输出记录到
# 日志,供后续错误分类与重试决策使用。-F 确保记录实时刷新。
if /usr/bin/script -qF "$ACTIVE_LOG_FILE" env -u HOMEBREW_COLOR brew "$@"; then
status=0
else
status=$?
fi
elif env -u HOMEBREW_COLOR brew "$@" 2>&1 | tee "$ACTIVE_LOG_FILE" | colorize_brew_output; then
status=0
else
pipeline_status=("${PIPESTATUS[@]}")