ctop/menu.go

101 lines
1.8 KiB
Go
Raw Normal View History

2017-01-02 23:40:55 +00:00
package main
import (
ui "github.com/gizak/termui"
)
type Menu struct {
ui.Block
Items []string
TextFgColor ui.Attribute
TextBgColor ui.Attribute
Selectable bool
cursorPos int
}
func NewMenu(items []string) *Menu {
return &Menu{
Block: *ui.NewBlock(),
Items: items,
TextFgColor: ui.ThemeAttr("par.text.fg"),
TextBgColor: ui.ThemeAttr("par.text.bg"),
Selectable: false,
cursorPos: 0,
}
}
func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell
buf := m.Block.Buffer()
for n, item := range m.Items {
x := 2 // initial offset
for _, ch := range item {
2017-01-03 17:37:09 +00:00
// invert bg/fg colors on currently selected row
2017-01-02 23:40:55 +00:00
if m.Selectable && n == m.cursorPos {
cell = ui.Cell{Ch: ch, Fg: m.TextBgColor, Bg: m.TextFgColor}
} else {
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
}
buf.Set(x, n+2, cell)
x++
}
}
return buf
}
func (m *Menu) Up() {
if m.cursorPos > 0 {
m.cursorPos--
ui.Render(m)
}
}
func (m *Menu) Down() {
if m.cursorPos < (len(m.Items) - 1) {
m.cursorPos++
ui.Render(m)
}
}
func HelpMenu(g *Grid) {
m := NewMenu([]string{
"[h] - open this help dialog",
"[q] - exit ctop",
})
m.Height = 10
m.Width = 50
m.TextFgColor = ui.ColorWhite
m.BorderLabel = "Help"
m.BorderFg = ui.ColorCyan
ui.Render(m)
ui.Handle("/sys/kbd/", func(ui.Event) {
ui.StopLoop()
})
ui.Loop()
}
func SortMenu(g *Grid) {
m := NewMenu(SortFields)
m.Height = 10
m.Width = 50
m.Selectable = true
m.TextFgColor = ui.ColorWhite
m.BorderLabel = "Sort Field"
m.BorderFg = ui.ColorCyan
ui.Render(m)
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
m.Up()
})
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
m.Down()
})
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
2017-01-03 17:37:09 +00:00
g.containerMap.config.sortField = m.Items[m.cursorPos]
2017-01-02 23:40:55 +00:00
ui.StopLoop()
})
ui.Loop()
}