package ui import ( "cli-mon/yabl" "fmt" "github.com/gdamore/tcell/v2" tview "github.com/rivo/tview" "reflect" ) type table struct { tview.Table views map[key]*tview.TableCell content []key } type key struct { action yabl.AName field yabl.FName id uint } func (t *table) update(event *yabl.Event) { if cell, ok := t.views[key{*event.ActionName, *event.Field, event.GetUnitId()}]; ok { cell.SetText(fmt.Sprintf("%v", event.Value)) } } func (t *table) header(header string) *table { t.SetTitle(header) return t } func (t *table) columns(name string, cols int) *table { for i := 1; i <= cols; i++ { t.SetCell(0, i, tview.NewTableCell(fmt.Sprintf("%s %d", name, i)).SetExpansion(2)).SetBackgroundColor(tcell.ColorDefault) } return t } func (t *table) rows(name string, rows int) *table { for i := 1; i <= rows; i++ { t.SetCell(i, 0, tview.NewTableCell(fmt.Sprintf("%s %d", name, i))).SetBackgroundColor(tcell.ColorDefault) //.SetExpansion(2)) } return t } func newTable(params []key, min, max int, invert bool, short bool) *table { t := &table{} t.Box = tview.NewBox() //t.SetBorderColor(tview.Styles.GraphicsColor) t.SetSeparator(' ') t.SetContent(nil) t.SetBorders(true) t.SetBorder(true) t.views = make(map[key]*tview.TableCell) for _, param := range params { for i := min; i <= max; i++ { k := param k.id = uint(i) cell := tview.NewTableCell("").SetBackgroundColor(tcell.ColorDefault) t.views[k] = cell var col = i if min == 0 { col++ } if !invert { t.SetCell(int(param.id), col, cell) } else { t.SetCell(col, int(param.id), cell) } } var desc string if short { desc = fmt.Sprintf("%s", param.field) } else { desc = fmt.Sprintf("%s.%s", param.action, param.field) } cell := tview.NewTableCell(desc).SetBackgroundColor(tcell.ColorDefault) if !invert { t.SetCell(int(param.id), 0, cell) } else { t.SetCell(0, int(param.id), cell) } } return t } func getField(v yabl.Action, field yabl.FName) interface{} { r := reflect.ValueOf(v) f := reflect.Indirect(r).FieldByName(string(field)) return f.Interface() }