Use STATUSLINE_WIDTH env var for reliable width detection
term.GetSize returns unreliable values when running inside Claude Code over SSH. Replace the hardcoded statuslineWidthOffset with STATUSLINE_WIDTH env var (set in Claude Code settings), falling back to terminal detection, then 80 columns.
This commit is contained in:
26
main.go
26
main.go
@@ -15,7 +15,7 @@ import (
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
const statuslineWidthOffset = 7
|
||||
const defaultStatuslineWidth = 80
|
||||
|
||||
// TokenUsage tracks context window token consumption.
|
||||
type TokenUsage struct {
|
||||
@@ -117,7 +117,7 @@ func run(r *bufio.Reader, w *strings.Builder) error {
|
||||
rightVisible := stripANSI(right)
|
||||
|
||||
// Get terminal width
|
||||
termWidth := getTerminalWidth() - statuslineWidthOffset
|
||||
termWidth := getTerminalWidth()
|
||||
|
||||
// Calculate and apply padding
|
||||
padding := calculatePadding(leftVisible, rightVisible, termWidth)
|
||||
@@ -212,30 +212,30 @@ func getGitInfo(cwd string) string {
|
||||
return fmt.Sprintf(" git:(%s)", branch)
|
||||
}
|
||||
|
||||
// termWidthFunc returns the terminal width.
|
||||
// Tries stderr and stdin as fallbacks since stdout is typically piped
|
||||
// when Claude Code captures the status line output.
|
||||
// termWidthFunc returns the usable status line width.
|
||||
// Checks STATUSLINE_WIDTH env var first (set in Claude Code settings),
|
||||
// then tries terminal detection on stderr/stdin/stdout, then falls back
|
||||
// to defaultStatuslineWidth.
|
||||
// Replaced in tests to avoid depending on a real TTY.
|
||||
var termWidthFunc = func() (int, error) {
|
||||
if sw := os.Getenv("STATUSLINE_WIDTH"); sw != "" {
|
||||
var w int
|
||||
if _, err := fmt.Sscanf(sw, "%d", &w); err == nil && w > 0 {
|
||||
return w, nil
|
||||
}
|
||||
}
|
||||
for _, fd := range []uintptr{os.Stderr.Fd(), os.Stdin.Fd(), os.Stdout.Fd()} {
|
||||
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 {
|
||||
width, err := termWidthFunc()
|
||||
if err != nil {
|
||||
return 80
|
||||
return defaultStatuslineWidth
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user