xonecas.io
60255
Zn

Zoea-Nova / commit 50f12e5

fix: prevent input box overflow with minimum width guard

Adds minimum width check (10 chars) to ViewAlways to prevent overflow
when width is 0 (before WindowSizeMsg) or very small. Includes
comprehensive tests for all input modes at various widths.

Fixes issue where placeholder text would overflow terminal borders on
first render or narrow terminals.

xonecas2026-02-05 50f12e5af99d0ff928080b958a7d6a1073b380bd parent 73ed61b

2 files changed

internal/tui/input.go+5−0
@@ -181,6 +181,11 @@ func (m InputModel) View() string {
181181
182182 // ViewAlways renders the input bar, showing placeholder when not active.
183183 func (m InputModel) ViewAlways(width int) string {
184+ // Ensure minimum width to prevent negative or zero width
185+ if width < 10 {
186+ width = 10
187+ }
188+
184189 if m.mode != InputModeNone {
185190 // Active - show the actual input
186191 return inputStyle.Width(width - 2).Render(m.textInput.View())
internal/tui/input_test.go+142−0
@@ -0,0 +1,142 @@
1+package tui
2+
3+import (
4+ "strings"
5+ "testing"
6+
7+ "github.com/charmbracelet/lipgloss"
8+)
9+
10+func TestInputModelViewAlwaysNoOverflow(t *testing.T) {
11+ tests := []struct {
12+ name string
13+ width int
14+ mode InputMode
15+ target string
16+ }{
17+ {"inactive small", 40, InputModeNone, ""},
18+ {"inactive medium", 80, InputModeNone, ""},
19+ {"inactive large", 120, InputModeNone, ""},
20+ {"broadcast small", 40, InputModeBroadcast, ""},
21+ {"broadcast medium", 80, InputModeBroadcast, ""},
22+ {"message small", 40, InputModeMessage, "mysis-1"},
23+ {"message medium", 80, InputModeMessage, "mysis-1"},
24+ {"new mysis small", 40, InputModeNewMysis, ""},
25+ {"new mysis medium", 80, InputModeNewMysis, ""},
26+ {"config provider small", 40, InputModeConfigProvider, ""},
27+ {"config provider medium", 80, InputModeConfigProvider, ""},
28+ {"config model small", 40, InputModeConfigModel, ""},
29+ {"config model medium", 80, InputModeConfigModel, ""},
30+ }
31+
32+ for _, tt := range tests {
33+ t.Run(tt.name, func(t *testing.T) {
34+ input := NewInputModel()
35+ input.SetWidth(tt.width - 4) // Simulate WindowSizeMsg handling
36+
37+ if tt.mode != InputModeNone {
38+ input.SetMode(tt.mode, tt.target)
39+ }
40+
41+ view := input.ViewAlways(tt.width)
42+ if view == "" {
43+ t.Error("ViewAlways should return non-empty view")
44+ }
45+
46+ renderedWidth := lipgloss.Width(view)
47+ if renderedWidth != tt.width {
48+ t.Errorf("rendered width = %d, want %d (overflow: %d)", renderedWidth, tt.width, renderedWidth-tt.width)
49+ }
50+
51+ // Check that no line exceeds terminal width
52+ lines := strings.Split(view, "\n")
53+ for i, line := range lines {
54+ lineWidth := lipgloss.Width(line)
55+ if lineWidth > tt.width {
56+ t.Errorf("line %d width = %d, exceeds terminal width %d", i, lineWidth, tt.width)
57+ t.Logf("Line content: %q", line)
58+ }
59+ }
60+ })
61+ }
62+}
63+
64+func TestInputModelSetWidthUpdatesTextInput(t *testing.T) {
65+ input := NewInputModel()
66+
67+ widths := []int{40, 60, 80, 100, 120}
68+ for _, width := range widths {
69+ input.SetWidth(width - 4)
70+ expected := (width - 4) - 4 // SetWidth subtracts 4
71+ if input.textInput.Width != expected {
72+ t.Errorf("SetWidth(%d): textInput.Width = %d, want %d", width-4, input.textInput.Width, expected)
73+ }
74+ }
75+}
76+
77+func TestInputModelAllModesRender(t *testing.T) {
78+ modes := []struct {
79+ mode InputMode
80+ name string
81+ }{
82+ {InputModeNone, "none"},
83+ {InputModeBroadcast, "broadcast"},
84+ {InputModeMessage, "message"},
85+ {InputModeNewMysis, "new_mysis"},
86+ {InputModeConfigProvider, "config_provider"},
87+ {InputModeConfigModel, "config_model"},
88+ }
89+
90+ width := 80
91+ input := NewInputModel()
92+ input.SetWidth(width - 4)
93+
94+ for _, tt := range modes {
95+ t.Run(tt.name, func(t *testing.T) {
96+ input.SetMode(tt.mode, "test-target")
97+ view := input.ViewAlways(width)
98+
99+ if view == "" {
100+ t.Error("ViewAlways should return non-empty view")
101+ }
102+
103+ renderedWidth := lipgloss.Width(view)
104+ if renderedWidth != width {
105+ t.Errorf("rendered width = %d, want %d", renderedWidth, width)
106+ }
107+ })
108+ }
109+}
110+
111+func TestInputModelViewAlwaysZeroWidth(t *testing.T) {
112+ input := NewInputModel()
113+
114+ // Test with zero width (before WindowSizeMsg)
115+ view := input.ViewAlways(0)
116+ if view == "" {
117+ t.Error("ViewAlways should return non-empty view even with zero width")
118+ }
119+
120+ // Should use minimum width of 10
121+ renderedWidth := lipgloss.Width(view)
122+ if renderedWidth != 10 {
123+ t.Errorf("rendered width = %d, want 10 (minimum width)", renderedWidth)
124+ }
125+}
126+
127+func TestInputModelViewAlwaysSmallWidth(t *testing.T) {
128+ input := NewInputModel()
129+ input.SetWidth(5)
130+
131+ // Test with very small width
132+ view := input.ViewAlways(5)
133+ if view == "" {
134+ t.Error("ViewAlways should return non-empty view")
135+ }
136+
137+ // Should use minimum width of 10
138+ renderedWidth := lipgloss.Width(view)
139+ if renderedWidth != 10 {
140+ t.Errorf("rendered width = %d, want 10 (minimum width)", renderedWidth)
141+ }
142+}