2017-01-02 23:40:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
2017-01-04 21:31:27 +00:00
|
|
|
var (
|
|
|
|
padding = 2
|
|
|
|
minWidth = 30
|
|
|
|
)
|
|
|
|
|
2017-01-02 23:40:55 +00:00
|
|
|
type Menu struct {
|
|
|
|
ui.Block
|
|
|
|
Items []string
|
|
|
|
TextFgColor ui.Attribute
|
|
|
|
TextBgColor ui.Attribute
|
|
|
|
Selectable bool
|
|
|
|
cursorPos int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMenu(items []string) *Menu {
|
2017-01-04 21:31:27 +00:00
|
|
|
m := &Menu{
|
2017-01-02 23:40:55 +00:00
|
|
|
Block: *ui.NewBlock(),
|
|
|
|
Items: items,
|
|
|
|
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
|
|
|
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
|
|
|
Selectable: false,
|
|
|
|
cursorPos: 0,
|
|
|
|
}
|
2017-01-04 22:25:27 +00:00
|
|
|
m.Width, m.Height = calcSize(items)
|
2017-01-04 21:31:27 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:25:27 +00:00
|
|
|
// return width and height based on menu items
|
|
|
|
func calcSize(items []string) (w, h int) {
|
|
|
|
h = len(items) + (padding * 2)
|
|
|
|
|
|
|
|
w = minWidth
|
2017-01-04 21:31:27 +00:00
|
|
|
for _, s := range items {
|
2017-01-04 22:25:27 +00:00
|
|
|
if len(s) > w {
|
|
|
|
w = len(s)
|
2017-01-04 21:31:27 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-04 22:25:27 +00:00
|
|
|
w += (padding * 2)
|
|
|
|
|
|
|
|
return w, h
|
2017-01-02 23:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Menu) Buffer() ui.Buffer {
|
|
|
|
var cell ui.Cell
|
|
|
|
|
|
|
|
buf := m.Block.Buffer()
|
|
|
|
|
|
|
|
for n, item := range m.Items {
|
2017-01-04 22:25:27 +00:00
|
|
|
x := padding
|
2017-01-02 23:40:55 +00:00
|
|
|
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}
|
|
|
|
}
|
2017-01-04 22:25:27 +00:00
|
|
|
buf.Set(x, n+padding, cell)
|
2017-01-02 23:40:55 +00:00
|
|
|
x++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:25:27 +00:00
|
|
|
func (m *Menu) Up(ui.Event) {
|
2017-01-02 23:40:55 +00:00
|
|
|
if m.cursorPos > 0 {
|
|
|
|
m.cursorPos--
|
|
|
|
ui.Render(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:25:27 +00:00
|
|
|
func (m *Menu) Down(ui.Event) {
|
2017-01-02 23:40:55 +00:00
|
|
|
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.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.Selectable = true
|
|
|
|
m.TextFgColor = ui.ColorWhite
|
|
|
|
m.BorderLabel = "Sort Field"
|
|
|
|
m.BorderFg = ui.ColorCyan
|
|
|
|
ui.Render(m)
|
2017-01-04 22:25:27 +00:00
|
|
|
ui.Handle("/sys/kbd/<up>", m.Up)
|
|
|
|
ui.Handle("/sys/kbd/<down>", m.Down)
|
|
|
|
ui.Handle("/sys/kbd/q", func(ui.Event) {
|
|
|
|
ui.StopLoop()
|
2017-01-02 23:40:55 +00:00
|
|
|
})
|
|
|
|
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()
|
|
|
|
}
|