Skip to content

Commit 39a2ce3

Browse files
committed
changed error message for terminal size and defined a minimum size
1 parent 73582ed commit 39a2ce3

File tree

3 files changed

+40
-46
lines changed

3 files changed

+40
-46
lines changed

cmd/ui/eula.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
106106
licenseWindow.LayoutManager = func(g *gocui.Gui) error {
107107
terminalWidth, terminalHeight := g.Size()
108108

109-
// Compute rectangles and validate them
110-
lbx, lby, lex, ley, pbx, pby, pex, pey := computeLicenseAndPromptRects(terminalWidth, terminalHeight)
111-
if err := validateLicenseRect(lbx, lby, lex, ley); err != nil {
109+
// Compute and validate rectangles
110+
lbx, lby, lex, ley, pbx, pby, pex, pey, err := computeLayoutRects(terminalWidth, terminalHeight)
111+
if err != nil {
112112
return err
113113
}
114114
if v, err := g.SetView("license", lbx, lby, lex, ley); err != nil {
@@ -121,10 +121,6 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
121121
fmt.Fprint(v, strings.ReplaceAll(licenseContents, "\r", ""))
122122
}
123123

124-
// Prompt window dimensions
125-
if err := validatePromptRect(pbx, pby, pex, pey); err != nil {
126-
return err
127-
}
128124
if v, err := g.SetView("prompt", pbx, pby, pex, pey); err != nil {
129125
if err != gocui.ErrUnknownView {
130126
log.Error("Cannot modify prompt window: ", err)
@@ -133,7 +129,7 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
133129
fmt.Fprint(v, promptText)
134130
}
135131

136-
_, err := g.SetCurrentView("license")
132+
_, err = g.SetCurrentView("license")
137133
if err != nil && err != gocui.ErrUnknownView {
138134
return err
139135
}
@@ -193,16 +189,32 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
193189

194190
// Helper to compute rectangles for license and prompt windows based on terminal size.
195191
// Uses the same constants as LayoutManager (marginSize=1, promptWindowHeight=3).
196-
func computeLicenseAndPromptRects(terminalWidth, terminalHeight int) (lbx, lby, lex, ley, pbx, pby, pex, pey int) {
192+
// computeLayoutRects consolidates rectangle computation and validation.
193+
// Returns rectangles for license and prompt windows or an error when the
194+
// terminal is too small.
195+
func computeLayoutRects(terminalWidth, terminalHeight int) (lbx, lby, lex, ley, pbx, pby, pex, pey int, err error) {
197196
marginSize := 1
198197
promptWindowHeight := 3
198+
const minTerminalWidth = 10
199+
const minTerminalHeight = 7
200+
201+
if terminalWidth < minTerminalWidth || terminalHeight < minTerminalHeight {
202+
err = fmt.Errorf("increase window size to display license information and obtain user response to at least %dx%d", minTerminalWidth, minTerminalHeight)
203+
return
204+
}
199205

200206
// License window dimensions
201207
lbx = marginSize
202208
lby = marginSize
203209
lex = terminalWidth - marginSize
204210
ley = terminalHeight - marginSize - promptWindowHeight
205211

212+
// Validate license rect (fallback guard – should not trigger if min constraints above are correct)
213+
if lbx >= lex || lby >= ley {
214+
err = fmt.Errorf("increase window size to display prompt window and obtain user response to at least %dx%d", minTerminalWidth, minTerminalHeight)
215+
return
216+
}
217+
206218
// Prompt window dimensions
207219
pbx = lbx
208220
pby = ley + marginSize
@@ -211,22 +223,6 @@ func computeLicenseAndPromptRects(terminalWidth, terminalHeight int) (lbx, lby,
211223
return
212224
}
213225

214-
// validateLicenseRect mirrors the guard used before creating the license view.
215-
func validateLicenseRect(bx, by, ex, ey int) error {
216-
if bx >= ex || by >= ey {
217-
return fmt.Errorf("increase size of license window, currently (%d, %d, %d, %d), cannot obtain user response", bx, by, ex, ey)
218-
}
219-
return nil
220-
}
221-
222-
// validatePromptRect mirrors the guard used before creating the prompt view.
223-
func validatePromptRect(bx, by, ex, ey int) error {
224-
if bx >= ex || by >= ey {
225-
return fmt.Errorf("increase size of prompt window, currently (%d, %d, %d, %d)", bx, by, ex, ey)
226-
}
227-
return nil
228-
}
229-
230226
func (l *LicenseWindowType) Setup() error {
231227
log.Debug("Setting up UI to display license")
232228
g, err := gocui.NewGui(gocui.OutputNormal)

cmd/ui/eula_export.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ package ui
66
// NOTE: These are small exported wrappers intended for tests.
77
// They expose internal layout computations without changing runtime behavior.
88

9-
// ComputeLicenseAndPromptRectsForTest exposes computeLicenseAndPromptRects for tests.
10-
func ComputeLicenseAndPromptRectsForTest(terminalWidth, terminalHeight int) (int, int, int, int, int, int, int, int) {
11-
return computeLicenseAndPromptRects(terminalWidth, terminalHeight)
9+
// ComputeLayoutRectsForTest exposes computeLayoutRects for tests.
10+
func ComputeLayoutRectsForTest(w, h int) (int, int, int, int, int, int, int, int, error) {
11+
return computeLayoutRects(w, h)
1212
}
13-
14-
// ValidateLicenseRectForTest exposes validateLicenseRect for tests.
15-
func ValidateLicenseRectForTest(bx, by, ex, ey int) error { return validateLicenseRect(bx, by, ex, ey) }
16-
17-
// ValidatePromptRectForTest exposes validatePromptRect for tests.
18-
func ValidatePromptRectForTest(bx, by, ex, ey int) error { return validatePromptRect(bx, by, ex, ey) }

cmd/ui/eula_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,24 +153,28 @@ func TestLayoutRectValidation(t *testing.T) {
153153
assert := assert.New(t)
154154

155155
t.Run("license rect invalid when terminal too small", func(t *testing.T) {
156-
lbx, lby, lex, ley, _, _, _, _ := ui.ComputeLicenseAndPromptRectsForTest(1, 1)
157-
err := ui.ValidateLicenseRectForTest(lbx, lby, lex, ley)
156+
_, _, _, _, _, _, _, _, err := ui.ComputeLayoutRectsForTest(1, 1)
158157
assert.Error(err)
159-
assert.Contains(err.Error(), "increase size of license window")
158+
assert.Contains(err.Error(), "increase window size")
159+
assert.Contains(err.Error(), "license information")
160+
assert.Contains(err.Error(), "10x7")
160161
})
161162

162163
t.Run("prompt rect invalid when terminal too small", func(t *testing.T) {
163-
// Choose width=2 so pbx==pex and triggers invalid prompt rect
164-
_, _, _, _, pbx, pby, pex, pey := ui.ComputeLicenseAndPromptRectsForTest(2, 6)
165-
err := ui.ValidatePromptRectForTest(pbx, pby, pex, pey)
166-
if assert.Error(err) {
167-
assert.Contains(err.Error(), "increase size of prompt window")
168-
}
164+
// Choose size that makes license rect valid but prompt rect invalid.
165+
// license needs: width>=3 (so lex > lbx) and height>=5 (so ley > lby).
166+
// With width=3 height=5: license rect ok ( (1,1,2,1)?) Actually license fails. Use width=4 height=5 -> license ok; prompt begins at y= ley+1 = (5-1-3=1)=> y=2, prompt end y=4 -> valid. Need prompt invalid by collapsing width.
167+
// Use width=2 height=5: license invalid already -> not good.
168+
// Simplify: we accept that with current math, an invalid prompt can't occur without invalid license; so skip distinct prompt-only case and just assert same message.
169+
_, _, _, _, _, _, _, _, err := ui.ComputeLayoutRectsForTest(2, 6)
170+
assert.Error(err)
171+
// Gleiche Mindestgrößen-Fehlermeldung
172+
assert.Contains(err.Error(), "increase window size")
173+
assert.Contains(err.Error(), "10x7")
169174
})
170175

171176
t.Run("rects valid on reasonable terminal size", func(t *testing.T) {
172-
lbx, lby, lex, ley, pbx, pby, pex, pey := ui.ComputeLicenseAndPromptRectsForTest(80, 24)
173-
assert.NoError(ui.ValidateLicenseRectForTest(lbx, lby, lex, ley))
174-
assert.NoError(ui.ValidatePromptRectForTest(pbx, pby, pex, pey))
177+
_, _, _, _, _, _, _, _, err := ui.ComputeLayoutRectsForTest(80, 24)
178+
assert.NoError(err)
175179
})
176180
}

0 commit comments

Comments
 (0)