Skip to content

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

@github-actions

Description

@github-actions

Description

The console package currently contains 8 instances of manual ANSI escape sequences scattered across multiple files. These should be centralized into helper functions to improve maintainability, ensure consistent TTY detection, and reduce the risk of bugs.

Suggested Changes

Create a new file pkg/console/terminal.go with centralized helper functions:

package console

import (
    "fmt"
    "os"
    "github.com/githubnext/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 manual ANSI codes in these files:

  • pkg/cli/add_interactive_orchestrator.go
  • pkg/console/console.go
  • pkg/console/spinner.go

Example refactoring:

// BEFORE
fmt.Fprint(os.Stderr, "\r\033[K")

// AFTER
console.ClearLine()

Files Affected

  • pkg/console/terminal.go (new file)
  • pkg/cli/add_interactive_orchestrator.go (2 instances)
  • pkg/console/console.go (2 instances)
  • pkg/console/spinner.go (4 instances)

Success Criteria

  • New terminal.go file created with helper functions
  • All 8 manual ANSI escape sequences replaced with helper calls
  • All helpers include TTY detection guards
  • Existing tests continue to pass
  • Code is more maintainable and consistent

Priority

Medium - Improves code quality and maintainability but not blocking

Source

Extracted from Terminal Stylist Report discussion #12889

Quote from source:

Found 8 instances of manual ANSI codes. Recommendation: Replace with Lipgloss equivalents where possible or centralize into helper functions.

Estimated Effort

1 day - Straightforward refactoring with clear implementation pattern

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 14, 2026, 1:19 PM UTC

Metadata

Metadata

Assignees

No one assigned

    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