ctop/widgets/menu/main.go

139 lines
2.7 KiB
Go
Raw Normal View History

2017-02-15 07:40:16 +00:00
package menu
2017-01-02 23:40:55 +00:00
import (
2017-02-15 05:31:04 +00:00
"sort"
2017-01-02 23:40:55 +00:00
ui "github.com/gizak/termui"
)
2017-01-21 18:41:28 +00:00
type Padding [2]int // x,y padding
2017-01-04 21:31:27 +00:00
2017-01-02 23:40:55 +00:00
type Menu struct {
ui.Block
SortItems bool // enable automatic sorting of menu items
TextFgColor ui.Attribute
TextBgColor ui.Attribute
Selectable bool
2017-02-19 03:54:24 +00:00
cursorPos int
2017-02-15 07:40:16 +00:00
items Items
padding Padding
2017-01-02 23:40:55 +00:00
}
func NewMenu() *Menu {
2017-03-06 02:00:30 +00:00
m := &Menu{
Block: *ui.NewBlock(),
2017-03-07 23:40:03 +00:00
TextFgColor: ui.ThemeAttr("menu.text.fg"),
TextBgColor: ui.ThemeAttr("menu.text.bg"),
2017-02-19 03:54:24 +00:00
cursorPos: 0,
padding: Padding{4, 2},
2017-01-02 23:40:55 +00:00
}
2017-03-07 23:40:03 +00:00
m.BorderFg = ui.ThemeAttr("menu.border.fg")
m.BorderLabelFg = ui.ThemeAttr("menu.label.fg")
2017-03-06 02:00:30 +00:00
m.X = 1
return m
}
2017-02-15 05:31:04 +00:00
2017-02-19 03:54:24 +00:00
// Append Item to Menu
2017-02-15 07:40:16 +00:00
func (m *Menu) AddItems(items ...Item) {
for _, i := range items {
2017-02-15 07:40:16 +00:00
m.items = append(m.items, i)
2017-02-15 05:31:04 +00:00
}
m.refresh()
2017-01-04 21:31:27 +00:00
}
2017-02-15 06:15:03 +00:00
// Remove menu item by value or label
func (m *Menu) DelItem(s string) (success bool) {
2017-02-15 07:40:16 +00:00
for n, i := range m.items {
2017-02-15 06:15:03 +00:00
if i.Val == s || i.Label == s {
2017-02-15 07:40:16 +00:00
m.items = append(m.items[:n], m.items[n+1:]...)
2017-02-15 06:15:03 +00:00
success = true
m.refresh()
break
}
}
return success
}
2017-02-19 03:54:24 +00:00
// Move cursor to an position by Item value or label
func (m *Menu) SetCursor(s string) (success bool) {
for n, i := range m.items {
if i.Val == s || i.Label == s {
m.cursorPos = n
return true
}
}
return false
}
// Sort menu items(if enabled) and re-calculate window size
func (m *Menu) refresh() {
if m.SortItems {
2017-02-15 07:40:16 +00:00
sort.Sort(m.items)
}
m.calcSize()
2017-02-15 06:15:03 +00:00
ui.Render(m)
}
2017-02-15 07:40:16 +00:00
func (m *Menu) SelectedItem() Item {
2017-02-19 03:54:24 +00:00
return m.items[m.cursorPos]
}
2017-01-02 23:40:55 +00:00
func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell
buf := m.Block.Buffer()
2017-02-15 07:40:16 +00:00
for n, item := range m.items {
x := m.X + m.padding[0]
y := m.Y + m.padding[1]
2017-02-15 05:31:04 +00:00
for _, ch := range item.Text() {
2017-01-03 17:37:09 +00:00
// invert bg/fg colors on currently selected row
2017-02-19 03:54:24 +00:00
if m.Selectable && n == m.cursorPos {
2017-03-07 23:40:03 +00:00
cell = ui.Cell{Ch: ch, Fg: ui.ColorBlack, Bg: m.TextFgColor}
2017-01-02 23:40:55 +00:00
} else {
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
}
buf.Set(x, y+n, 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-02-19 03:54:24 +00:00
if m.cursorPos > 0 {
m.cursorPos--
2017-01-02 23:40:55 +00:00
ui.Render(m)
}
}
2017-01-04 22:25:27 +00:00
func (m *Menu) Down(ui.Event) {
2017-02-19 03:54:24 +00:00
if m.cursorPos < (len(m.items) - 1) {
m.cursorPos++
2017-01-02 23:40:55 +00:00
ui.Render(m)
}
}
2017-01-04 23:13:17 +00:00
// Setup some default handlers for menu navigation
func (m *Menu) NavigationHandlers() {
2017-01-04 22:25:27 +00:00
ui.Handle("/sys/kbd/<up>", m.Up)
ui.Handle("/sys/kbd/<down>", m.Down)
2017-01-04 23:13:17 +00:00
ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() })
}
2017-01-21 18:41:28 +00:00
// Set width and height based on menu items
func (m *Menu) calcSize() {
2017-03-06 02:00:30 +00:00
m.Width = 7 // minimum width
2017-01-04 23:13:17 +00:00
2017-02-15 07:40:16 +00:00
items := m.items
for _, i := range m.items {
2017-02-15 05:31:04 +00:00
s := i.Text()
2017-01-21 18:41:28 +00:00
if len(s) > m.Width {
m.Width = len(s)
2017-01-04 23:13:17 +00:00
}
}
2017-01-21 18:41:28 +00:00
m.Width += (m.padding[0] * 2)
m.Height = len(items) + (m.padding[1] * 2)
2017-01-02 23:40:55 +00:00
}