Small CLI monitoring tool to decode proprietary [...] protocol and display info in human readable representation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ycli-mon/ui/ui.go

88 lines
2.1 KiB

package ui
import (
"cli-mon/yabl"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"log"
"time"
)
const headerHeight int = 4
const contactorsCount = 18
const connectorsCount = 6
func InitCliApp(events <-chan yabl.Packet) {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
var connectorsTab *ui.Grid
//var contactorsErr *widgets.Paragraph
var contactorsStateTab *ui.Grid
var header *widgets.Paragraph
tabpane := widgets.NewTabPane("Connectors", "Contactors States", "Contactors Errors")
tabpane.SetRect(0, 1, 150, 4)
tabpane.Border = true
header = newHeader()
connectorsTab = newConnectorsView()
contactorsStateTab = newContactorsView()
contactorsErr = newContactorsErrView()
renderTab := func() {
switch tabpane.ActiveTabIndex {
case 0:
termWidth, termHeight := ui.TerminalDimensions()
connectorsTab.SetRect(0, headerHeight, termWidth, termHeight)
ui.Render(connectorsTab)
case 1:
termWidth, termHeight := ui.TerminalDimensions()
contactorsStateTab.SetRect(0, headerHeight, termWidth, termHeight)
ui.Render(contactorsStateTab)
case 2:
ui.Render(contactorsErr)
}
}
ui.Render(header, tabpane, connectorsTab)
uiEvents := ui.PollEvents()
ticker := time.NewTicker(2 * time.Second).C
for {
select {
case e := <-uiEvents:
switch e.ID { // event string/identifier
case "q", "<C-c>": // press 'q' or 'C-c' to quit
return
case "<MouseLeft>":
//payload := e.Payload.(ui.Mouse)
//x, y := payload.X, payload.Y
//p.Text = fmt.Sprintf("X = %d, Y = %d", x, y)
case "<Left>":
tabpane.FocusLeft()
ui.Clear()
ui.Render(header, tabpane)
renderTab()
case "<Right>":
tabpane.FocusRight()
ui.Clear()
ui.Render(header, tabpane)
renderTab()
}
switch e.Type {
case ui.KeyboardEvent: // handle all key presses
//eventID := e.ID // keypress string
//p.Text = eventID
}
// use Go's built-in tickers for updating and drawing data
case <-ticker:
renderTab()
case message := <-events:
process(message)
}
}
}