fix(homebrew): 修复 Homebrew 伪终端输入和输出处理问题

- 在 colorize_line 函数中处理光标控制序列,避免它们破坏版本着色
- 添加 run_brew_in_pseudoterminal 函数,从控制终端读取输入而不是关闭的管道
- 将主脚本通过 curl 管道启动时的输入重定向到 /dev/tty
- 修复启动器中 sudo 密码读取时的输入源问题
- 更新文档说明启动器如何处理 curl 管道输入和确认提示
This commit is contained in:
2026-07-24 08:54:53 +08:00
parent 8124810b6e
commit 15d18f6123
3 changed files with 43 additions and 5 deletions
+30 -2
View File
@@ -639,6 +639,14 @@ sub print_heading {
sub colorize_line {
my ($line) = @_;
# Homebrew 的进度队列会在普通输出前留下光标控制序列(例如显示光标)。
# 先原样输出这些序列,再匹配文本,避免它们成为包名的一部分而破坏版本着色。
my $control_prefix = "";
while ($line =~ s/\A(\e\[[0-?]*[ -\/]*[@-~])//) {
$control_prefix .= $1;
}
print $control_prefix;
return if print_version_transition($line);
if ($line =~ /^==> /) {
@@ -706,13 +714,33 @@ if (length $buffer) {
'
}
run_brew_in_pseudoterminal() {
local log_file="$1"
shift
# brewup 通常通过 curl 管道启动,因而主脚本的 stdin 可能是已关闭的管道。
# script(1) 若继承它,会向伪终端传入 ^D,使 Homebrew 的 y/n 提示直接失败。
# 应从控制终端读取输入,继续支持升级确认与实时进度。
if [[ -n "$log_file" ]]; then
if [[ -r /dev/tty ]]; then
/usr/bin/script -qF "$log_file" env -u HOMEBREW_COLOR HOMEBREW_NO_COLOR=1 brew "$@" < /dev/tty
else
/usr/bin/script -qF "$log_file" env -u HOMEBREW_COLOR HOMEBREW_NO_COLOR=1 brew "$@"
fi
elif [[ -r /dev/tty ]]; then
/usr/bin/script -q /dev/null env -u HOMEBREW_COLOR HOMEBREW_NO_COLOR=1 brew "$@" < /dev/tty
else
/usr/bin/script -q /dev/null env -u HOMEBREW_COLOR HOMEBREW_NO_COLOR=1 brew "$@"
fi
}
run_brew_colored() {
local pipeline_status=()
# 伪终端保留 Homebrew 的实时进度,流式着色器只处理完整文本行,
# 不会缓冲或改写用 CR 刷新的进度输出。
if can_stream_colorize_tty_output; then
if /usr/bin/script -q /dev/null env -u HOMEBREW_COLOR HOMEBREW_NO_COLOR=1 brew "$@" | colorize_brew_tty_stream; then
if run_brew_in_pseudoterminal "" "$@" | colorize_brew_tty_stream; then
return 0
fi
pipeline_status=("${PIPESTATUS[@]}")
@@ -851,7 +879,7 @@ run_brew_with_retries() {
: > "$ACTIVE_LOG_FILE"
if can_stream_colorize_tty_output; then
# 为错误分类保留转录,同时通过伪终端和流式着色兼顾进度条与版本配色。
if /usr/bin/script -qF "$ACTIVE_LOG_FILE" env -u HOMEBREW_COLOR HOMEBREW_NO_COLOR=1 brew "$@" | colorize_brew_tty_stream; then
if run_brew_in_pseudoterminal "$ACTIVE_LOG_FILE" "$@" | colorize_brew_tty_stream; then
status=0
else
pipeline_status=("${PIPESTATUS[@]}")