Fix right-alignment by detecting terminal width from stderr/stdin

When Claude Code captures stdout, term.GetSize on stdout fails and
falls back to 80 columns. Now tries stderr and stdin first (which
remain connected to the TTY), then COLUMNS env var, then 80.
This commit is contained in:
2026-02-08 23:14:13 +01:00
parent 29350032ba
commit a06aa4c549

18
main.go
View File

@@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@@ -212,10 +213,23 @@ func getGitInfo(cwd string) string {
} }
// termWidthFunc returns the terminal width. // termWidthFunc returns the terminal width.
// Tries stderr and stdin as fallbacks since stdout is typically piped
// when Claude Code captures the status line output.
// Replaced in tests to avoid depending on a real TTY. // Replaced in tests to avoid depending on a real TTY.
var termWidthFunc = func() (int, error) { var termWidthFunc = func() (int, error) {
width, _, err := term.GetSize(int(os.Stdout.Fd())) for _, fd := range []uintptr{os.Stderr.Fd(), os.Stdin.Fd(), os.Stdout.Fd()} {
return width, err if width, _, err := term.GetSize(int(fd)); err == nil {
return width, nil
}
}
// Fall back to COLUMNS env var
if cols := os.Getenv("COLUMNS"); cols != "" {
var w int
if _, err := fmt.Sscanf(cols, "%d", &w); err == nil && w > 0 {
return w, nil
}
}
return 0, errors.New("no terminal detected")
} }
func getTerminalWidth() int { func getTerminalWidth() int {