Skip to content

[Code Quality] Centralize ANSI escape sequences in pkg/console/terminal.go #14336

@github-actions

Description

@github-actions

Description

The codebase currently has 8 instances of manual ANSI escape sequences scattered across different files. These should be centralized into helper functions in a new pkg/console/terminal.go file to improve maintainability, consistency, and prevent TTY detection bugs.

Current Problem

Manual ANSI codes are duplicated in multiple locations:

  • pkg/cli/add_interactive_orchestrator.go: \033[H\033[2J (clear screen)
  • pkg/console/console.go: \033[2J\033[H (clear screen)
  • pkg/console/console.go: \r\033[K (clear line)
  • pkg/console/spinner.go: Multiple instances of \r\033[K

This leads to:

  • Code duplication and inconsistency
  • Missing TTY detection in some cases
  • Harder to maintain and update
  • Potential bugs if ANSI codes need changes

Suggested Changes

Create pkg/console/terminal.go with helper functions:

package console

import (
    "fmt"
    "os"
    "github.com/github/gh-aw/pkg/tty"
)

// ClearScreen clears the terminal screen if stderr is a TTY
func ClearScreen() {
    if tty.IsStderrTerminal() {
        fmt.Fprint(os.Stderr, "\033[H\033[2J")
    }
}

// ClearLine clears the current line if stderr is a TTY
func ClearLine() {
    if tty.IsStderrTerminal() {
        fmt.Fprint(os.Stderr, "\r\033[K")
    }
}

// MoveCursorUp moves cursor up n lines if stderr is a TTY
func MoveCursorUp(n int) {
    if tty.IsStderrTerminal() {
        fmt.Fprintf(os.Stderr, "\033[%dA", n)
    }
}

// MoveCursorDown moves cursor down n lines if stderr is a TTY
func MoveCursorDown(n int) {
    if tty.IsStderrTerminal() {
        fmt.Fprintf(os.Stderr, "\033[%dB", n)
    }
}

Then replace all manual ANSI code usage with these helpers.

Files Affected

  • New file: pkg/console/terminal.go
  • pkg/cli/add_interactive_orchestrator.go
  • pkg/console/console.go
  • pkg/console/spinner.go

Success Criteria

  • All manual ANSI escape sequences replaced with helper functions
  • TTY detection consistently applied across all terminal control operations
  • No regressions in terminal output behavior
  • Code is more maintainable and easier to understand
  • All existing tests pass

Source

Extracted from Terminal Stylist Report: Console Output Analysis (Discussion #12889)

Priority

High - Improves code organization and prevents potential TTY detection bugs. Quick win with small effort (1-2 hours).

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 8, 2026, 1:18 PM UTC

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions