fix(replay): 过滤 Unicode 格式控制符并截断紧凑报告源主机名

This commit is contained in:
tsosunchia committed 2026-09-08 14:48:38 +08:00
1 parent cb02ef92d1
commit 84a0df2097
4 files changed
+33 -2

No files matched your search

+8
View File
@@ -414,3 +414,11 @@ func TestMTRReplayLongSessionStreamingSeek(t *testing.T) {
}
t.Logf("400s / 4000 probes, bytes=%d generation=%s load=%s seek-to-10s=%s", r.Size(), generated, loaded, sought)
}
func TestMTRReplayStripsUnicodeFormatControls(t *testing.T) {
input := "中文\u202ehost\u202c\u2066name\u2069\u200b"
want := "中文 host name "
if got := sanitizeMTRReplayText(input); got != want {
t.Fatalf("got %q want %q", got, want)
}
}
+1 -1
View File
@@ -189,7 +189,7 @@ func runMTRReplayTUI(parent context.Context, reader *mtrsession.Reader, current
func sanitizeMTRReplayText(value string) string {
return strings.Map(func(r rune) rune {
if unicode.IsControl(r) {
if unicode.IsControl(r) || unicode.Is(unicode.Cf, r) {
return ' '
}
return r
+5 -1
View File
@@ -78,7 +78,11 @@ func WriteMTRReplayReport(w io.Writer, stats []trace.MTRHopStat, opts MTRReportO
columns = DefaultMTRColumns()
}
widths := mtrColumnWidths(columns, stats)
fmt.Fprintf(&b, "HOST: %s %s\n", reportPadRight(opts.SrcHost, hostWidth), mtrSelectedMetrics(columns, widths, nil, false))
hostHeader := opts.SrcHost
if !opts.Wide {
hostHeader = reportTruncateToWidth(hostHeader, hostWidth)
}
fmt.Fprintf(&b, "HOST: %s %s\n", reportPadRight(hostHeader, hostWidth), mtrSelectedMetrics(columns, widths, nil, false))
previous := 0
for i, stat := range stats {
fmt.Fprintf(&b, "%s%s %s\n", mtrReportPrefix(stat.TTL, previous), reportPadRight(hosts[i], hostWidth), mtrSelectedMetrics(columns, widths, &stat, false))
+19
View File
@@ -54,3 +54,22 @@ func TestMTRReplayReportPlainSink(t *testing.T) {
t.Fatal("ignored write error")
}
}
func TestMTRReplayReportLongSourceAlignment(t *testing.T) {
stats := []trace.MTRHopStat{{TTL: 1, IP: "192.0.2.1", Snt: 2, Received: 1, Loss: 50}}
for _, wide := range []bool{false, true} {
var b bytes.Buffer
opts := MTRReportOptions{Columns: []MTRColumn{MTRColumnSnt}, Wide: wide, SrcHost: strings.Repeat("源host", 12)}
if err := WriteMTRReplayReport(&b, stats, opts); err != nil {
t.Fatal(err)
}
lines := strings.Split(b.String(), "\n")
// HOST: has one more prefix cell than the live report's TTL prefix.
if reportDisplayWidth(lines[1]) != reportDisplayWidth(lines[2])+1 {
t.Fatalf("misaligned: %s", b.String())
}
if wide && !strings.Contains(lines[1], opts.SrcHost) {
t.Fatal("wide source truncated")
}
}
}