@@ -17,6 +17,8 @@ var Agreed = true
1717var Disagreed = false
1818var LicenseAgreed * bool
1919var Extract = false
20+ var terminalWidth , terminalHeight int
21+ var minTerminalWidth int
2022
2123// LicenseWindowType defines the struct to handle UI
2224type LicenseWindowType struct {
@@ -89,8 +91,10 @@ func DisplayAndWaitForEULA(licenseTitle, licenseContents string) (bool, error) {
8991
9092func NewLicenseWindow (licenseTitle , licenseContents , promptText string ) * LicenseWindowType {
9193 licenseWindow := & LicenseWindowType {}
94+ licenseContents = strings .ReplaceAll (licenseContents , "\r " , "" )
9295 licenseHeight := utils .CountLines (licenseContents )
9396 licenseMarginBottom := 10
97+ minTerminalWidth = len (promptText ) + 3 // add margins and one space behind "(E)xtract"
9498
9599 // The LayoutManager is called on events, like key press or window resize
96100 // and it is going to look more or less like
@@ -104,46 +108,33 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
104108 // | promptText |
105109 // +----------------------------+
106110 licenseWindow .LayoutManager = func (g * gocui.Gui ) error {
107- terminalWidth , terminalHeight := g .Size ()
108-
109- marginSize := 1
110- promptWindowHeight := 3
111-
112- // License window dimensions
113- licenseWindowBeginX := marginSize
114- licenseWindowBeginY := marginSize
115- licenseWindowEndX := terminalWidth - marginSize
116- licenseWindowEndY := terminalHeight - marginSize - promptWindowHeight
117- if licenseWindowBeginX >= licenseWindowEndX || licenseWindowBeginY >= licenseWindowEndY {
118- return fmt .Errorf ("dimensions for license window are invalid (%d, %d, %d, %d)" , licenseWindowBeginX , licenseWindowBeginY , licenseWindowEndX , licenseWindowEndY )
111+ terminalWidth , terminalHeight = g .Size ()
112+
113+ // Compute and validate rectangles
114+ lbx , lby , lex , ley , pbx , pby , pex , pey , err := computeLayoutRects (terminalWidth , terminalHeight )
115+ if err != nil {
116+ return err
119117 }
120- if v , err := g .SetView ("license" , licenseWindowBeginX , licenseWindowBeginY , licenseWindowEndX , licenseWindowEndY ); err != nil {
118+ if v , err := g .SetView ("license" , lbx , lby , lex , ley ); err != nil {
121119 if err != gocui .ErrUnknownView {
122120 log .Error ("Cannot modify license window: " , err )
123121 return err
124122 }
125123 v .Wrap = true
126124 v .Title = licenseTitle
127- fmt .Fprint (v , strings .ReplaceAll (licenseContents , "\r " , "" ))
125+ // fmt.Fprintf(v, "tW: %d, tH: %d, lbx: %d, lby: %d, lex: %d, ley: %d, pbx: %d, pby: %d, pex: %d, pey: %d\n", terminalWidth, terminalHeight, lbx, lby, lex, ley, pbx, pby, pex, pey)
126+ fmt .Fprint (v , licenseContents )
128127 }
129128
130- // Prompt window dimensions
131- promptWindowBeginX := licenseWindowBeginX
132- promptWindowBeginY := licenseWindowEndY + marginSize
133- promptWindowEndX := licenseWindowEndX
134- promptWindowEndY := terminalHeight - marginSize
135- if promptWindowBeginX >= promptWindowEndX || promptWindowBeginY >= promptWindowEndY {
136- return fmt .Errorf ("dimensions for prompt window are invalid (%d, %d, %d, %d)" , promptWindowBeginX , promptWindowBeginY , promptWindowEndX , promptWindowEndY )
137- }
138- if v , err := g .SetView ("prompt" , promptWindowBeginX , promptWindowBeginY , promptWindowEndX , promptWindowEndY ); err != nil {
129+ if v , err := g .SetView ("prompt" , pbx , pby , pex , pey ); err != nil {
139130 if err != gocui .ErrUnknownView {
140131 log .Error ("Cannot modify prompt window: " , err )
141132 return err
142133 }
143134 fmt .Fprint (v , promptText )
144135 }
145136
146- _ , err : = g .SetCurrentView ("license" )
137+ _ , err = g .SetCurrentView ("license" )
147138 if err != nil && err != gocui .ErrUnknownView {
148139 return err
149140 }
@@ -201,14 +192,51 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
201192 return licenseWindow
202193}
203194
195+ // Helper to compute rectangles for license and prompt windows based on terminal size.
196+ // Uses the same constants as LayoutManager (marginSize=1, promptWindowHeight=3).
197+ // computeLayoutRects consolidates rectangle computation and validation.
198+ // Returns rectangles for license and prompt windows or an error when the
199+ // terminal is too small.
200+ func computeLayoutRects (terminalWidth , terminalHeight int ) (lbx , lby , lex , ley , pbx , pby , pex , pey int , err error ) {
201+ marginSize := 1
202+ promptWindowHeight := 3
203+ const minTerminalHeight = 8
204+
205+ if terminalWidth < minTerminalWidth || terminalHeight < minTerminalHeight {
206+ err = fmt .Errorf ("increase window size to display license information and obtain user response to at least %dx%d" , minTerminalWidth , minTerminalHeight )
207+ return
208+ }
209+
210+ // License window dimensions
211+ lbx = 0
212+ lby = 0
213+ lex = terminalWidth - marginSize
214+ ley = terminalHeight - marginSize - promptWindowHeight
215+
216+ // Validate license rect (fallback guard – should not trigger if min constraints above are correct)
217+ if lbx >= lex || lby >= ley {
218+ err = fmt .Errorf ("increase window size to display license information and obtain user response to at least %dx%d" , minTerminalWidth , minTerminalHeight )
219+ return
220+ }
221+
222+ // Prompt window dimensions
223+ pbx = lbx
224+ pby = ley + marginSize
225+ pex = lex
226+ pey = terminalHeight - marginSize
227+ return
228+ }
229+
204230func (l * LicenseWindowType ) Setup () error {
205231 log .Debug ("Setting up UI to display license" )
232+
206233 g , err := gocui .NewGui (gocui .OutputNormal )
207234 if err != nil {
208235 log .Error ("Cannot initialize UI: " , err )
209236 return err
210237 }
211238
239+ terminalWidth , terminalHeight = g .Size ()
212240 g .SetManagerFunc (l .LayoutManager )
213241
214242 bindings := []struct {
@@ -255,7 +283,6 @@ func (l *LicenseWindowType) PromptUser() (bool, error) {
255283 log .Debug ("Prompting user for license agreement" )
256284 err := l .Gui .MainLoop ()
257285 if err != nil && err != gocui .ErrQuit && err != errs .ErrExtractEula {
258- log .Error ("Cannot obtain user response: " , err )
259286 return false , err
260287 }
261288
0 commit comments