Skip to content

[Code Quality] Centralize ANSI escape sequences in console package #14319

@github-actions

Description

@github-actions

Description

Manual ANSI escape sequences are scattered across 8 locations in the codebase. Centralizing these in a dedicated module will reduce duplication, improve consistency, and make it easier to maintain TTY detection logic.

Current State

8 instances of manual ANSI codes found:

pkg/cli/add_interactive_orchestrator.go:  fmt.Fprint(os.Stderr, "\033[H\033[2J")
pkg/console/console.go:                   var clearScreenSequence = "\033[2J\033[H"
pkg/console/console.go:                   fmt.Fprint(os.Stderr, "\r\033[K")
pkg/console/spinner.go:                   fmt.Fprintf(m.output, "\r\033[K%s %s", ...)
pkg/console/spinner.go:                   fmt.Fprint(os.Stderr, "\r\033[K")
pkg/console/spinner.go:                   fmt.Fprintf(os.Stderr, "\r\033[K%s\n", msg)

Current issues:

  • ANSI codes duplicated in multiple files
  • No centralized TTY detection for these operations
  • Harder to maintain and update

Recommended Solution

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)
    }
}

Files Affected

  • Create: pkg/console/terminal.go (new helper functions)
  • Update: pkg/cli/add_interactive_orchestrator.go
  • Update: pkg/console/console.go
  • Update: pkg/console/spinner.go
  • Create: pkg/console/terminal_test.go (tests for new helpers)

Success Criteria

  • ✅ All ANSI escape sequences centralized in pkg/console/terminal.go
  • ✅ Helper functions respect TTY detection (no ANSI in pipes)
  • ✅ All 8 instances updated to use new helpers
  • ✅ Test coverage for new helper functions
  • ✅ All existing tests pass after refactoring
  • ✅ No functional changes (pure refactoring)
  • ✅ Build and lint checks pass (make build, make lint)

Source

Extracted from Terminal Stylist Report discussion #12889

Finding: "Manual ANSI Escape Sequences" - 8 instances found
Recommendation: "Replace with Lipgloss equivalents or centralize"
Impact: Reduces code duplication, improves consistency

Priority

Medium - This is a code quality improvement that will make the codebase more maintainable and consistent. Not urgent, but valuable for reducing technical debt.

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 8, 2026, 9:05 AM UTC

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions