@@ -12,6 +12,7 @@ import (
1212 "unsafe"
1313
1414 "github.com/grisu48/gopenqa"
15+ "github.com/os-autoinst/openqa-mon/internal"
1516 "golang.org/x/crypto/ssh/terminal"
1617)
1718
@@ -38,11 +39,14 @@ type TUI struct {
3839
3940 Keypress KeyPressCallback
4041
41- header string
42- status string // Additional status text
43- showStatus bool // Show status line
44- showHelp bool // Show help line
45- hideEnable bool // If hideStates will be considered
42+ header string // program version, remote servers and current/total page
43+ status string // Additional status text
44+ remotes string // address of the openQA server or number of monitored instances
45+ showStatus bool // Show status line
46+ showHelp bool // Show help line
47+ hideEnable bool // If hideStates will be considered
48+ currentPage int // the page we are displaying (0=first)
49+ totalPages int // how many pages are there to display
4650}
4751
4852/* The model that will be displayed in the TUI */
@@ -97,6 +101,8 @@ func CreateTUI() TUI {
97101 tui .status = ""
98102 tui .showStatus = true
99103 tui .hideEnable = true
104+ tui .currentPage = 0
105+ tui .totalPages = 1
100106 return tui
101107}
102108
@@ -263,6 +269,24 @@ func (tui *TUI) SetHideStates(enabled bool) {
263269 tui .Update ()
264270}
265271
272+ func (tui * TUI ) NextPage () {
273+ tui .Model .mutex .Lock ()
274+ defer tui .Model .mutex .Unlock ()
275+ if tui .currentPage < tui .totalPages - 1 {
276+ tui .currentPage ++
277+ tui .header = fmt .Sprintf ("openqa-mon v%s - Monitoring %s - Page %d/%d" , internal .VERSION , tui .remotes , 1 + tui .currentPage , tui .totalPages )
278+ }
279+ }
280+
281+ func (tui * TUI ) PrevPage () {
282+ tui .Model .mutex .Lock ()
283+ defer tui .Model .mutex .Unlock ()
284+ if tui .currentPage > 0 {
285+ tui .currentPage --
286+ tui .header = fmt .Sprintf ("openqa-mon v%s - Monitoring %s - Page %d/%d" , internal .VERSION , tui .remotes , 1 + tui .currentPage , tui .totalPages )
287+ }
288+ }
289+
266290func (tui * TUI ) DoHideStates () bool {
267291 return tui .hideEnable
268292}
@@ -295,6 +319,10 @@ func (tui *TUI) doHideJob(j gopenqa.Job) bool {
295319
296320// Redraw tui
297321func (tui * TUI ) Update () {
322+ if len (tui .Model .jobs ) == 0 {
323+ // no jobs fetched yet, nothing to display
324+ return
325+ }
298326 width , height := terminalSize ()
299327
300328 tui .Clear ()
@@ -304,48 +332,48 @@ func (tui *TUI) Update() {
304332 lines ++
305333 }
306334 if tui .showHelp {
307- help := "?:Toggle help r: Refresh d:Toggle notifications b:Toggle bell +/-:Modify refresh time p:Toggle pause"
335+ help := "?:Toggle help r: Refresh d:Toggle notifications b:Toggle bell +/-:Modify refresh time p:Toggle pause <>:Page "
308336 if len (tui .Model .HideStates ) > 0 {
309- help += " h:Toggle hide"
337+ help += " h:Toggle hide"
310338 }
311- help += " q:Quit"
339+ help += " q:Quit"
312340 PrintLine (help , width )
313341 lines ++
314342 }
315- fmt .Println ()
316- lines ++
317-
318- offset := 0
319- maxHeight := height
343+ pageHeight := height - 1
320344 if tui .showStatus {
321- maxHeight -= 2
345+ pageHeight --
346+ }
347+ // ensure to always have something to display without exceeding the slice limits
348+ startIdx := min (tui .currentPage * pageHeight , len (tui .Model .jobs ))
349+ endIdx := min (startIdx + pageHeight , len (tui .Model .jobs ))
350+ tui .totalPages = len (tui .Model .jobs ) / pageHeight
351+ if len (tui .Model .jobs )% pageHeight > 0 {
352+ tui .totalPages ++ // one more page for any partial-page leftover
322353 }
323- for _ , job := range tui .Model .jobs {
354+ for _ , job := range tui .Model .jobs [ startIdx : endIdx ] {
324355 if tui .hideEnable && tui .doHideJob (job ) {
325356 continue
326357 }
327- // Ignore offset jobs (for scrolling)
328- if offset > 0 {
329- offset --
330- continue
331- }
332358 PrintJob (job , true , width )
333359 lines ++
334- if lines >= maxHeight {
335- return
336- }
360+ }
361+
362+ // print some empty lines if needed to fill last page and make footer always on last line
363+ for lines <= pageHeight {
364+ fmt .Println ()
365+ lines ++
337366 }
338367
339368 // Status line
340369 if tui .showStatus {
341370 // Add footer, if possible
342371 status := tui .status
343- footer := "openqa-mon (https://github.com/grisu48 /openqa-mon)"
372+ footer := "openqa-mon (https://github.com/os-autoinst /openqa-mon)"
344373 if width >= len (status )+ len (footer )+ 5 {
345- spaces := strings .Repeat (" " , width - len (status )- len (footer ))
374+ spaces := strings .Repeat (" " , width - len (status )- len (footer )- 1 )
346375 status += spaces + footer
347376 }
348- fmt .Println ("" )
349- fmt .Println (status )
377+ fmt .Print (status )
350378 }
351379}
0 commit comments