Skip to content

Commit 2123334

Browse files
committed
Fix progress output for dumb terminals
1 parent 0ad9f05 commit 2123334

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎internal/app/app.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ func (a App) Run(ctx context.Context, args []string) int {
170170
lookup = os.LookupEnv
171171
}
172172
termName, _ := lookup("TERM")
173-
if cfg.LogFormat == "human" && terminal.IsTerminalWriter(stdout) && termName != "" && termName != "dumb" {
173+
interactiveTerminal := cfg.LogFormat == "human" && a.isTerminal(stdout) && termName != "" && termName != "dumb"
174+
if interactiveTerminal && terminal.IsTerminalWriter(stdout) {
174175
candidate := terminal.New(stdout, stdin)
175176
candidate.Interrupt = cancelRun
176177
if candidate.Eligible() && candidate.Start() == nil {
@@ -180,7 +181,11 @@ func (a App) Run(ctx context.Context, args []string) int {
180181
}
181182
logger := event.Logger{
182183
Out: stdout, Err: stderr, Now: a.Now, Format: cfg.LogFormat, Heartbeat: cfg.Heartbeat,
183-
Interactive: a.isTerminal(stdout), ColorDepth: a.colorDepth(cfg, stdout), View: view,
184+
// A TTY alone is insufficient: TERM=dumb (and an absent TERM) does not
185+
// promise to interpret carriage-return/ANSI transient frames. Keep all
186+
// output permanent in that environment so captured session output cannot
187+
// be corrupted by terminal control sequences.
188+
Interactive: interactiveTerminal, ColorDepth: a.colorDepth(cfg, stdout), View: view,
184189
}
185190
if logger.Interactive && cfg.LogFormat == "human" && cfg.Heartbeat == 0 {
186191
logger.TerminalWidth = func() (int, error) { return a.terminalWidth(stdout) }

‎internal/app/app_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,29 @@ func TestAppHumanDevNullWorkflow(t *testing.T) {
502502
t.Fatalf("code=%d stderr=%q", code, stderr.String())
503503
}
504504
}
505+
506+
func TestAppHumanDumbTerminalUsesPermanentProgress(t *testing.T) {
507+
root, home := testRepo(t)
508+
var stdout, stderr bytes.Buffer
509+
fake := &appFakeRunner{
510+
t: t,
511+
review: runner.Result{Stdout: "No findings.\n"},
512+
reviewMsg: cleanReviewJSONForApp,
513+
finalizeMsg: `{"verdict":"SUCCESS","commit":"success","push":"success","change_request":"skipped","ci":"skipped"}`,
514+
}
515+
code := (App{
516+
Stdout: &stdout, Stderr: &stderr, Cwd: root, Home: home, Runner: fake,
517+
IsTerminal: func(io.Writer) bool { return true },
518+
LookupEnv: func(key string) (string, bool) { return "dumb", key == "TERM" },
519+
}).Run(context.Background(), nil)
520+
if code != workflow.ExitSuccess || stderr.Len() != 0 {
521+
t.Fatalf("code=%d stderr=%q stdout=%q", code, stderr.String(), stdout.String())
522+
}
523+
got := stdout.String()
524+
if !strings.Contains(got, "Review started\n") {
525+
t.Fatalf("permanent progress records missing: %q", got)
526+
}
527+
if strings.ContainsAny(got, "\r\x1b") {
528+
t.Fatalf("dumb-terminal output contains control sequences: %q", got)
529+
}
530+
}

0 commit comments

Comments
 (0)