Drop padding and terminal width detection
Output left + right with a single space separator instead of trying to right-align to a detected terminal width. Terminal detection is unreliable when all fds are piped by Claude Code, and hardcoding a width defeats the purpose. Also removes the x/term dependency.
This commit is contained in:
1
go.mod
1
go.mod
@@ -5,7 +5,6 @@ go 1.25.5
|
|||||||
require (
|
require (
|
||||||
github.com/go-git/go-git/v6 v6.0.0-20260206150416-f623c7555599
|
github.com/go-git/go-git/v6 v6.0.0-20260206150416-f623c7555599
|
||||||
github.com/shirou/gopsutil/v4 v4.26.1
|
github.com/shirou/gopsutil/v4 v4.26.1
|
||||||
golang.org/x/term v0.39.0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
67
main.go
67
main.go
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -12,11 +11,8 @@ import (
|
|||||||
|
|
||||||
"github.com/go-git/go-git/v6"
|
"github.com/go-git/go-git/v6"
|
||||||
"github.com/shirou/gopsutil/v4/process"
|
"github.com/shirou/gopsutil/v4/process"
|
||||||
"golang.org/x/term"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultStatuslineWidth = 80
|
|
||||||
|
|
||||||
// TokenUsage tracks context window token consumption.
|
// TokenUsage tracks context window token consumption.
|
||||||
type TokenUsage struct {
|
type TokenUsage struct {
|
||||||
InputTokens int `json:"input_tokens"`
|
InputTokens int `json:"input_tokens"`
|
||||||
@@ -72,36 +68,26 @@ func parseStatusInput(jsonStr string) (*StatusInput, error) {
|
|||||||
return &data, nil
|
return &data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildStatusLine constructs the left and right parts of the status line.
|
// buildStatusLine constructs the formatted status line.
|
||||||
func buildStatusLine(data *StatusInput) (left, right string) {
|
func buildStatusLine(data *StatusInput) string {
|
||||||
contextInfo := formatContextInfo(data.ContextWindow.ContextWindowSize, data.ContextWindow.CurrentUsage)
|
contextInfo := formatContextInfo(data.ContextWindow.ContextWindowSize, data.ContextWindow.CurrentUsage)
|
||||||
dirName := filepath.Base(data.Workspace.CurrentDir)
|
dirName := filepath.Base(data.Workspace.CurrentDir)
|
||||||
giteaStatus := getGiteaStatus()
|
giteaStatus := getGiteaStatus()
|
||||||
gitInfo := getGitInfo(data.Workspace.CurrentDir)
|
gitInfo := getGitInfo(data.Workspace.CurrentDir)
|
||||||
|
|
||||||
left = fmt.Sprintf("%s %s%s%s %s➜%s %s%s%s%s",
|
left := fmt.Sprintf("%s %s%s%s %s➜%s %s%s%s%s",
|
||||||
giteaStatus,
|
giteaStatus,
|
||||||
magenta, data.Model.DisplayName, reset,
|
magenta, data.Model.DisplayName, reset,
|
||||||
boldGreen, reset,
|
boldGreen, reset,
|
||||||
cyan, dirName, reset,
|
cyan, dirName, reset,
|
||||||
gitInfo)
|
gitInfo)
|
||||||
|
|
||||||
right = fmt.Sprintf("%s%s%s", yellow, contextInfo, reset)
|
right := fmt.Sprintf("%s%s%s", yellow, contextInfo, reset)
|
||||||
return left, right
|
|
||||||
}
|
|
||||||
|
|
||||||
// calculatePadding returns the number of spaces needed for padding.
|
return left + " " + right
|
||||||
func calculatePadding(leftVisible, rightVisible string, termWidth int) int {
|
|
||||||
return max(termWidth-len(leftVisible)-len(rightVisible), 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// formatOutput combines left, right, and padding into final output.
|
|
||||||
func formatOutput(left, right string, padding int) string {
|
|
||||||
return fmt.Sprintf("%s%s%s", left, strings.Repeat(" ", padding), right)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// run reads JSON from r, builds the statusline, and writes it to w.
|
// run reads JSON from r, builds the statusline, and writes it to w.
|
||||||
// Returns an error if the input cannot be parsed.
|
|
||||||
func run(r *bufio.Reader, w *strings.Builder) error {
|
func run(r *bufio.Reader, w *strings.Builder) error {
|
||||||
jsonStr := readInputFromStdin(r)
|
jsonStr := readInputFromStdin(r)
|
||||||
|
|
||||||
@@ -110,20 +96,7 @@ func run(r *bufio.Reader, w *strings.Builder) error {
|
|||||||
return fmt.Errorf("error parsing JSON: %w", err)
|
return fmt.Errorf("error parsing JSON: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
left, right := buildStatusLine(data)
|
w.WriteString(buildStatusLine(data))
|
||||||
|
|
||||||
// Calculate visible lengths (strip ANSI)
|
|
||||||
leftVisible := stripANSI(left)
|
|
||||||
rightVisible := stripANSI(right)
|
|
||||||
|
|
||||||
// Get terminal width
|
|
||||||
termWidth := getTerminalWidth()
|
|
||||||
|
|
||||||
// Calculate and apply padding
|
|
||||||
padding := calculatePadding(leftVisible, rightVisible, termWidth)
|
|
||||||
output := formatOutput(left, right, padding)
|
|
||||||
|
|
||||||
w.WriteString(output)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,34 +185,6 @@ func getGitInfo(cwd string) string {
|
|||||||
return fmt.Sprintf(" git:(%s)", branch)
|
return fmt.Sprintf(" git:(%s)", branch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0, errors.New("no terminal detected")
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTerminalWidth() int {
|
|
||||||
width, err := termWidthFunc()
|
|
||||||
if err != nil {
|
|
||||||
return defaultStatuslineWidth
|
|
||||||
}
|
|
||||||
return width
|
|
||||||
}
|
|
||||||
|
|
||||||
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
||||||
|
|
||||||
func stripANSI(s string) string {
|
func stripANSI(s string) string {
|
||||||
|
|||||||
1021
main_test.go
1021
main_test.go
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user