From 27128e2e3b63a74454fcf099bfb74531a31cab5b Mon Sep 17 00:00:00 2001 From: Kaj Kowalski Date: Sun, 8 Feb 2026 23:27:26 +0100 Subject: [PATCH] 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. --- main.go | 26 +++++++++++++------------- main_test.go | 7 +++---- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 75ee1d7..2b08946 100644 --- a/main.go +++ b/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 } diff --git a/main_test.go b/main_test.go index 736c9ea..6970c9a 100644 --- a/main_test.go +++ b/main_test.go @@ -756,10 +756,9 @@ func TestTokenUsage_ZeroValues(t *testing.T) { } } -func TestStatuslineWidthOffset_Constant(t *testing.T) { - // Verify the constant is defined and reasonable - if statuslineWidthOffset <= 0 || statuslineWidthOffset > 20 { - t.Errorf("statuslineWidthOffset = %d, expected between 1 and 20", statuslineWidthOffset) +func TestDefaultStatuslineWidth_Constant(t *testing.T) { + if defaultStatuslineWidth < 40 || defaultStatuslineWidth > 300 { + t.Errorf("defaultStatuslineWidth = %d, expected between 40 and 300", defaultStatuslineWidth) } }