2017-01-04 23:13:17 +00:00
|
|
|
package widgets
|
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-02-15 04:44:03 +00:00
|
|
|
type MenuItem struct {
|
2017-02-15 05:31:04 +00:00
|
|
|
Val string
|
|
|
|
Label string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use label as display text of item, if given
|
|
|
|
func (m MenuItem) Text() string {
|
|
|
|
if m.Label != "" {
|
|
|
|
return m.Label
|
|
|
|
}
|
|
|
|
return m.Val
|
|
|
|
}
|
|
|
|
|
|
|
|
type MenuItems []MenuItem
|
|
|
|
|
2017-02-15 06:01:35 +00:00
|
|
|
// Create new MenuItems from string array
|
|
|
|
func NewMenuItems(a []string) (items MenuItems) {
|
|
|
|
for _, s := range a {
|
|
|
|
items = append(items, MenuItem{Val: s})
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2017-02-15 05:31:04 +00:00
|
|
|
// Sort methods for MenuItems
|
|
|
|
func (m MenuItems) Len() int { return len(m) }
|
|
|
|
func (m MenuItems) Swap(a, b int) { m[a], m[b] = m[b], m[a] }
|
|
|
|
func (m MenuItems) Less(a, b int) bool {
|
|
|
|
return m[a].Text() < m[b].Text()
|
2017-02-15 04:44:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 23:40:55 +00:00
|
|
|
type Menu struct {
|
|
|
|
ui.Block
|
2017-02-15 05:31:04 +00:00
|
|
|
Items MenuItems
|
2017-02-15 06:01:35 +00:00
|
|
|
SortItems bool // enable automatic sorting of menu items
|
2017-02-15 04:44:03 +00:00
|
|
|
TextFgColor ui.Attribute
|
|
|
|
TextBgColor ui.Attribute
|
|
|
|
Selectable bool
|
|
|
|
CursorPos int
|
|
|
|
padding Padding
|
2017-01-02 23:40:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 06:01:35 +00:00
|
|
|
func NewMenu() *Menu {
|
|
|
|
return &Menu{
|
2017-02-15 04:44:03 +00:00
|
|
|
Block: *ui.NewBlock(),
|
|
|
|
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
|
|
|
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
|
|
|
CursorPos: 0,
|
|
|
|
padding: Padding{4, 2},
|
2017-01-02 23:40:55 +00:00
|
|
|
}
|
2017-02-15 06:01:35 +00:00
|
|
|
}
|
2017-02-15 05:31:04 +00:00
|
|
|
|
2017-02-15 06:01:35 +00:00
|
|
|
func (m *Menu) AddItems(items ...MenuItem) {
|
|
|
|
for _, i := range items {
|
|
|
|
m.Items = append(m.Items, i)
|
2017-02-15 05:31:04 +00:00
|
|
|
}
|
2017-02-15 06:01:35 +00:00
|
|
|
m.refresh()
|
2017-01-04 21:31:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 06:01:35 +00:00
|
|
|
// Sort menu items(if enabled) and re-calculate window size
|
|
|
|
func (m *Menu) refresh() {
|
|
|
|
if m.SortItems {
|
|
|
|
sort.Sort(m.Items)
|
|
|
|
}
|
2017-02-15 04:44:03 +00:00
|
|
|
m.calcSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Menu) SelectedItem() MenuItem {
|
|
|
|
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 04:44:03 +00:00
|
|
|
for n, item := range m.Items {
|
2017-01-21 18:41:28 +00:00
|
|
|
x := m.padding[0]
|
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-01-04 23:13:17 +00:00
|
|
|
if m.Selectable && n == m.CursorPos {
|
2017-01-02 23:40:55 +00:00
|
|
|
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-21 18:41:28 +00:00
|
|
|
buf.Set(x, n+m.padding[1], 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-04 23:13:17 +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-01-04 23:13:17 +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() {
|
|
|
|
m.Width = 8 // minimum width
|
2017-01-04 23:13:17 +00:00
|
|
|
|
2017-02-15 04:44:03 +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
|
|
|
}
|