Skip to content

Commit

Permalink
Add pagination on TUI
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmanzo committed Aug 18, 2024
1 parent 3468b60 commit 1947b62
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 33 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ uninstall:

test:
go test ./...

clean:
go clean
rm openqa-mon openqa-mq openqa-revtui
27 changes: 16 additions & 11 deletions cmd/openqa-mon/openqa-mon.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ func main() {
if len(remotes) == 1 {
remotesString = remotes[0].URI
}
tui.remotes=remotesString
tui.SetHeader(fmt.Sprintf("openqa-mon v%s - Monitoring %s", internal.VERSION, remotesString))
tui.Model.HideStates = config.HideStates
tui.Update()
Expand Down Expand Up @@ -861,6 +862,7 @@ func singleCall(remotes []Remote) {
}
}


func continuousMonitoring(remotes []Remote) {
var err error
// Ensure cursor is visible after termination
Expand All @@ -880,47 +882,50 @@ func continuousMonitoring(remotes []Remote) {

// Keybress callback
tui.Keypress = func(b byte) {
if b == 'q' {
switch b {
case 'q':
tui.LeaveAltScreen()
os.Exit(0)
} else if b == 'r' {
case 'r':
// Refresh
refreshSignal <- 1
return
} else if b == '?' {
case '?':
tui.SetShowHelp(!tui.DoShowHelp())
} else if b == 'h' {
case 'h':
tui.SetHideStates(!tui.DoHideStates())
} else if b == 'p' {
case 'p':
config.Paused = !config.Paused
if config.Paused {
SetStatus()
} else {
refreshSignal <- 1
}
return
} else if b == 'd' || b == 'n' {
case 'd','n':
config.Notify = !config.Notify
SetStatus()
} else if b == 'b' {
case 'b':
config.Bell = !config.Bell
SetStatus()
} else if b == 'm' {
case 'm':
config.Notify = false
config.Bell = false
SetStatus()
} else if b == 'l' {
case 'l':
config.Notify = true
config.Bell = true
SetStatus()
} else if b == '+' {
case '+':
config.Continuous++
SetStatus()
} else if b == '-' {
case '-':
if config.Continuous > 1 {
config.Continuous--
}
SetStatus()
case '>': tui.NextPage()
case '<': tui.PrevPage()
}
tui.Update()
}
Expand Down
71 changes: 50 additions & 21 deletions cmd/openqa-mon/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"unsafe"

"github.com/grisu48/gopenqa"
"github.com/grisu48/openqa-mon/internal"
"golang.org/x/crypto/ssh/terminal"
)

Expand All @@ -38,11 +39,14 @@ type TUI struct {

Keypress KeyPressCallback

header string
status string // Additional status text
showStatus bool // Show status line
showHelp bool // Show help line
hideEnable bool // If hideStates will be considered
header string
status string // Additional status text
remotes string //
showStatus bool // Show status line
showHelp bool // Show help line
hideEnable bool // If hideStates will be considered
currentPage int // the page we are displaying (0=first)
totalPages int // how many pages are there to display
}

/* The model that will be displayed in the TUI */
Expand Down Expand Up @@ -97,6 +101,8 @@ func CreateTUI() TUI {
tui.status = ""
tui.showStatus = true
tui.hideEnable = true
tui.currentPage = 0
tui.totalPages = 1
return tui
}

Expand Down Expand Up @@ -263,6 +269,27 @@ func (tui *TUI) SetHideStates(enabled bool) {
tui.Update()
}

func (tui *TUI) NextPage() {
tui.Model.mutex.Lock()
defer tui.Model.mutex.Unlock()
if tui.currentPage <= tui.totalPages-1 {
tui.currentPage++
tui.currentPage = tui.currentPage % tui.totalPages
tui.header = fmt.Sprintf("openqa-mon v%s - Monitoring %s - Page %d/%d", internal.VERSION, tui.remotes, 1+tui.currentPage, tui.totalPages)
}
}

func (tui *TUI) PrevPage() {
tui.Model.mutex.Lock()
defer tui.Model.mutex.Unlock()
if tui.currentPage > 0 {
tui.currentPage--
} else {
tui.currentPage = tui.totalPages - 1
}
tui.header = fmt.Sprintf("openqa-mon v%s - Monitoring %s - Page %d/%d", internal.VERSION, tui.remotes, 1+tui.currentPage, tui.totalPages)
}

func (tui *TUI) DoHideStates() bool {
return tui.hideEnable
}
Expand Down Expand Up @@ -304,23 +331,30 @@ func (tui *TUI) Update() {
lines++
}
if tui.showHelp {
help := "?:Toggle help r: Refresh d:Toggle notifications b:Toggle bell +/-:Modify refresh time p:Toggle pause"
help := "?:Toggle help r:Refresh d:Toggle notifications b:Toggle bell +/-:Modify refresh time p:Toggle pause <>:Page"
if len(tui.Model.HideStates) > 0 {
help += " h:Toggle hide"
help += " h:Toggle hide"
}
help += " q:Quit"
help += " q:Quit"
PrintLine(help, width)
lines++
}
fmt.Println()
lines++

offset := 0
maxHeight := height
maxHeight := height - 2
if tui.showStatus {
maxHeight -= 2
maxHeight -= 1
}
for _, job := range tui.Model.jobs {
startIdx := tui.currentPage * maxHeight
endIdx := min(startIdx+maxHeight, len(tui.Model.jobs))
tui.totalPages = len(tui.Model.jobs) / maxHeight
if len(tui.Model.jobs)%maxHeight > 0 {
tui.totalPages++
}
// safety check
if startIdx >= len(tui.Model.jobs) || endIdx > len(tui.Model.jobs) {
return
}
for _, job := range tui.Model.jobs[startIdx:endIdx] {
if tui.hideEnable && tui.doHideJob(job) {
continue
}
Expand All @@ -330,22 +364,17 @@ func (tui *TUI) Update() {
continue
}
PrintJob(job, true, width)
lines++
if lines >= maxHeight {
return
}
}

// Status line
if tui.showStatus {
// Add footer, if possible
status := tui.status
footer := "openqa-mon (https://github.com/grisu48/openqa-mon)"
footer := "openqa-mon (https://github.com/os-autoinst/openqa-mon)"
if width >= len(status)+len(footer)+5 {
spaces := strings.Repeat(" ", width-len(status)-len(footer))
status += spaces + footer
}
fmt.Println("")
fmt.Println(status)
fmt.Print(status)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/grisu48/openqa-mon

go 1.20
go 1.21

require (
github.com/BurntSushi/toml v1.4.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/grisu48/gopenqa v0.7.4 h1:E2IXKid1SJ2ndtzs3KNmYhFGcl8IX8eh2A2pYWD1CgU=
github.com/grisu48/gopenqa v0.7.4/go.mod h1:+gXgBF7HsC/JjqQM5iSTEy9Ws9OlvJivALVYJRtXH+8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

0 comments on commit 1947b62

Please sign in to comment.