🐛 fix(core): 修复终端输出换行显示异常

* 针对 script(1) 环境优化输出逻辑,显式处理 CRLF 换行符
* 解决因 ONLCR 关闭导致的终端输出行对齐及回车换行问题
* 在 Perl 处理逻辑中引入 line_ending 变量确保文本行正确终止
* 优化语义配色规则在特定终端环境下的兼容性表现
This commit is contained in:
2026-07-25 12:49:29 +08:00
parent 15d18f6123
commit f0ce129244
+16 -14
View File
@@ -579,7 +579,8 @@ colorize_brew_tty_stream() {
# `script` 将 brew 的输出传递给本函数时,普通行以 CRLF 结尾,进度条则以
# CR 重绘。按行的 Bash read 无法同时处理两者,会等到下一个换行才刷新进度。
# 这个小型 Perl 流式过滤器会立即透传 CR 更新,仅对完整的普通行重复使用
# 现有的语义配色规则。
# 现有的语义配色规则。script(1) 读取控制终端时会临时关闭 ONLCR,
# 因此完整文本行必须显式输出 CRLF,不能依赖终端将 LF 自动转换为回车换行。
HB_STREAM_RED="$RED" \
HB_STREAM_GREEN="$GREEN" \
HB_STREAM_YELLOW="$YELLOW" \
@@ -594,6 +595,7 @@ use warnings;
use utf8;
$| = 1;
my $line_ending = "\r\n";
my %color = map { $_ => ($ENV{"HB_STREAM_" . uc($_)} // "") }
qw(red green yellow blue magenta cyan bold nc);
@@ -614,7 +616,7 @@ sub print_version_transition {
print $color{bold}, $color{cyan}, $package, $color{nc} if length $package;
print $color{yellow}, $old_version, $color{nc}, " ";
print $color{blue}, $arrow, $color{nc}, " ";
print $color{bold}, $color{green}, $new_version, $color{nc}, $suffix, "\n";
print $color{bold}, $color{green}, $new_version, $color{nc}, $suffix, $line_ending;
return 1;
}
@@ -625,15 +627,15 @@ sub print_heading {
if ($text =~ /^(Upgrading|Installing|Reinstalling|Fetching|Downloading|Pouring|Would)\s+(.+)$/) {
print $color{blue}, "==>", $color{nc}, " ";
print $color{cyan}, $1, $color{nc}, " ";
print $color{bold}, $color{green}, $2, $color{nc}, "\n";
print $color{bold}, $color{green}, $2, $color{nc}, $line_ending;
} elsif ($text =~ /^(?:Upgraded\s|Installed\s|Successfully\s)/) {
print $color{blue}, "==>", $color{nc}, " ", $color{bold}, $color{green}, $text, $color{nc}, "\n";
print $color{blue}, "==>", $color{nc}, " ", $color{bold}, $color{green}, $text, $color{nc}, $line_ending;
} elsif ($text =~ /^(?:Outdated\s|Disabled\s|Warning)/) {
print $color{blue}, "==>", $color{nc}, " ", $color{yellow}, $text, $color{nc}, "\n";
print $color{blue}, "==>", $color{nc}, " ", $color{yellow}, $text, $color{nc}, $line_ending;
} elsif ($text =~ /^(?:Formulae|Casks|New Formulae|New Casks|Caveats|Summary)$/) {
print $color{blue}, "==>", $color{nc}, " ", $color{bold}, $color{magenta}, $text, $color{nc}, "\n";
print $color{blue}, "==>", $color{nc}, " ", $color{bold}, $color{magenta}, $text, $color{nc}, $line_ending;
} else {
print $color{blue}, "==>", $color{nc}, " ", $color{cyan}, $text, $color{nc}, "\n";
print $color{blue}, "==>", $color{nc}, " ", $color{cyan}, $text, $color{nc}, $line_ending;
}
}
@@ -652,20 +654,20 @@ sub colorize_line {
if ($line =~ /^==> /) {
print_heading($line);
} elsif ($line =~ /^(?:Warning:|warning:)/) {
print $color{yellow}, $line, $color{nc}, "\n";
print $color{yellow}, $line, $color{nc}, $line_ending;
} elsif ($line =~ /^(?:Error:|error:|Failed:|failed:)/) {
print $color{red}, $line, $color{nc}, "\n";
print $color{red}, $line, $color{nc}, $line_ending;
} elsif ($line =~ /^Removing:/) {
print $color{yellow}, $line, $color{nc}, "\n";
print $color{yellow}, $line, $color{nc}, $line_ending;
} elsif ($line =~ /^(?:✔︎ |✔ |🍺 )/ ||
$line =~ /was successfully upgraded!|successfully installed!|All dependencies satisfied\./) {
print $color{green}, $line, $color{nc}, "\n";
print $color{green}, $line, $color{nc}, $line_ending;
} elsif ($line =~ /^(?:Already up-to-date|Updated Homebrew |Updated .*tap|Successfully rebased |Pruned |This operation has freed )/) {
print $color{green}, $line, $color{nc}, "\n";
print $color{green}, $line, $color{nc}, $line_ending;
} elsif ($line =~ /^ (?:Downloaded|Verified) /) {
print $color{green}, $line, $color{nc}, "\n";
print $color{green}, $line, $color{nc}, $line_ending;
} else {
print $line, "\n";
print $line, $line_ending;
}
}