diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-16 03:54:28 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-16 03:54:28 +0200 |
| commit | 93dfe3798e03e74766b229418cde364a5ef29ae9 (patch) | |
| tree | 4438058be9bee9a9b8682148a1f8417a530fac2f /internal | |
| parent | 8d9d903852a2cd28034e00e116a7bfcc14783108 (diff) | |
Replace custom stringsTrim with strings.TrimSpace
Remove duplicate stringsTrim implementations in stats and tmux packages,
replacing all call sites with the standard library strings.TrimSpace.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/stats/stats.go | 19 | ||||
| -rw-r--r-- | internal/stats/stats_test.go | 11 | ||||
| -rw-r--r-- | internal/tmux/status.go | 21 | ||||
| -rw-r--r-- | internal/tmux/status_coverage_test.go | 6 |
4 files changed, 14 insertions, 43 deletions
diff --git a/internal/stats/stats.go b/internal/stats/stats.go index 4b05617..939e1aa 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -13,6 +13,7 @@ import ( "os" "path/filepath" "strconv" + "strings" "sync/atomic" "time" ) @@ -264,7 +265,7 @@ func TakeSnapshot() (Snapshot, error) { // CacheDir resolves the cache directory for stats. func CacheDir() (string, error) { - if x := os.Getenv("XDG_CACHE_HOME"); stringsTrim(x) != "" { + if x := os.Getenv("XDG_CACHE_HOME"); strings.TrimSpace(x) != "" { return filepath.Join(x, "hexai"), nil } home, err := os.UserHomeDir() @@ -274,22 +275,6 @@ func CacheDir() (string, error) { return filepath.Join(home, ".local", "hexai", "cache"), nil } -// stringsTrim is a tiny helper to avoid importing strings everywhere here. -func stringsTrim(s string) string { - i := 0 - j := len(s) - for i < j && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r') { - i++ - } - for j > i && (s[j-1] == ' ' || s[j-1] == '\t' || s[j-1] == '\n' || s[j-1] == '\r') { - j-- - } - if i == 0 && j == len(s) { - return s - } - return s[i:j] -} - // DebugString returns a compact single-line view of a snapshot (useful for logs). func (s Snapshot) DebugString() string { return "Σ reqs=" + strconv.FormatInt(s.Global.Reqs, 10) + " rpm=" + fmt.Sprintf("%.2f", s.RPM) diff --git a/internal/stats/stats_test.go b/internal/stats/stats_test.go index 75c1c5b..45f9e2a 100644 --- a/internal/stats/stats_test.go +++ b/internal/stats/stats_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "os" "path/filepath" + "strings" "sync" "testing" "time" @@ -102,7 +103,7 @@ func TestCacheDir_FallbackHome(t *testing.T) { } // TestCacheDir_WhitespaceXDG covers the branch where XDG_CACHE_HOME contains -// only whitespace, which stringsTrim reduces to "" so the fallback is used. +// only whitespace, which strings.TrimSpace reduces to "" so the fallback is used. func TestCacheDir_WhitespaceXDG(t *testing.T) { t.Setenv("XDG_CACHE_HOME", " \t\n ") got, err := CacheDir() @@ -140,7 +141,7 @@ func TestSetWindow_ClampHigh(t *testing.T) { // has no leading or trailing whitespace, so the original string is returned. func TestStringsTrim_NoTrimNeeded(t *testing.T) { in := "hello" - got := stringsTrim(in) + got := strings.TrimSpace(in) if got != "hello" { t.Fatalf("expected %q, got %q", "hello", got) } @@ -148,7 +149,7 @@ func TestStringsTrim_NoTrimNeeded(t *testing.T) { // TestStringsTrim_AllWhitespace covers trimming a string that is entirely whitespace. func TestStringsTrim_AllWhitespace(t *testing.T) { - got := stringsTrim(" \t\r\n ") + got := strings.TrimSpace(" \t\r\n ") if got != "" { t.Fatalf("expected empty, got %q", got) } @@ -156,7 +157,7 @@ func TestStringsTrim_AllWhitespace(t *testing.T) { // TestStringsTrim_LeadingAndTrailing covers trimming from both ends. func TestStringsTrim_LeadingAndTrailing(t *testing.T) { - got := stringsTrim(" abc ") + got := strings.TrimSpace(" abc ") if got != "abc" { t.Fatalf("expected %q, got %q", "abc", got) } @@ -164,7 +165,7 @@ func TestStringsTrim_LeadingAndTrailing(t *testing.T) { // TestStringsTrim_Empty covers the empty string edge case. func TestStringsTrim_Empty(t *testing.T) { - got := stringsTrim("") + got := strings.TrimSpace("") if got != "" { t.Fatalf("expected empty, got %q", got) } diff --git a/internal/tmux/status.go b/internal/tmux/status.go index dcc0714..2f8cff6 100644 --- a/internal/tmux/status.go +++ b/internal/tmux/status.go @@ -76,7 +76,7 @@ func FormatGlobalStatusColored(globalReqs int64, globalRPM float64, globalIn, gl gout := textutil.HumanBytes(globalOut) head := fmt.Sprintf("%sΣ@%s %s↑%s%s %s↓%s%s %.1frpm", baseFGToken, humanWindow(window), arrowUpToken, baseFGToken, gin, arrowDownToken, baseFGToken, gout, globalRPM) // Narrow modes: only show Σ head - if narrowEnabled() || stringsTrim(scopeProvider) == "" || stringsTrim(scopeModel) == "" { + if narrowEnabled() || strings.TrimSpace(scopeProvider) == "" || strings.TrimSpace(scopeModel) == "" { return head } tail := fmt.Sprintf(" | %s:%s %.1frpm %dr", scopeProvider, scopeModel, scopeRPM, scopeReqs) @@ -108,7 +108,7 @@ func humanWindow(d time.Duration) string { // narrowEnabled returns true when HEXAI_TMUX_STATUS_NARROW is truthy (1/true/yes/on). func narrowEnabled() bool { - v := strings.ToLower(stringsTrim(os.Getenv("HEXAI_TMUX_STATUS_NARROW"))) + v := strings.ToLower(strings.TrimSpace(os.Getenv("HEXAI_TMUX_STATUS_NARROW"))) if v == "" { return false } @@ -122,7 +122,7 @@ func narrowEnabled() bool { // maxStatusLen returns HEXAI_TMUX_STATUS_MAXLEN parsed as int; 0 disables. func maxStatusLen() int { - v := stringsTrim(os.Getenv("HEXAI_TMUX_STATUS_MAXLEN")) + v := strings.TrimSpace(os.Getenv("HEXAI_TMUX_STATUS_MAXLEN")) if v == "" { return 0 } @@ -146,21 +146,6 @@ func truncateStatus(s string, n int) string { return s[:n-1] + "…" } -func stringsTrim(s string) string { - i := 0 - j := len(s) - for i < j && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r') { - i++ - } - for j > i && (s[j-1] == ' ' || s[j-1] == '\t' || s[j-1] == '\n' || s[j-1] == '\r') { - j-- - } - if i == 0 && j == len(s) { - return s - } - return s[i:j] -} - // FormatLLMStartStatus renders a short colored heartbeat at start/initialize time. // Example: "LLM:openai:gpt-4.1 ⏳" func FormatLLMStartStatus(provider, model string) string { diff --git a/internal/tmux/status_coverage_test.go b/internal/tmux/status_coverage_test.go index 8f7c034..0c76ee6 100644 --- a/internal/tmux/status_coverage_test.go +++ b/internal/tmux/status_coverage_test.go @@ -141,7 +141,7 @@ func TestTruncateStatus(t *testing.T) { } } -// --- stringsTrim --- +// --- strings.TrimSpace --- func TestStringsTrim(t *testing.T) { tests := []struct { @@ -163,9 +163,9 @@ func TestStringsTrim(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := stringsTrim(tt.s) + got := strings.TrimSpace(tt.s) if got != tt.want { - t.Errorf("stringsTrim(%q) = %q, want %q", tt.s, got, tt.want) + t.Errorf("strings.TrimSpace(%q) = %q, want %q", tt.s, got, tt.want) } }) } |
