ctop/widgets/menu/main.go

170 lines
3.2 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
2018-01-11 13:56:00 +00:00
SortItems bool // enable automatic sorting of menu items
2020-01-03 12:53:25 +00:00
Selectable bool // whether menu is navigable
2018-01-11 13:56:00 +00:00
SubText string // optional text to display before items
TextFgColor ui.Attribute
TextBgColor ui.Attribute
2017-02-19 03:54:24 +00:00
cursorPos int
2017-02-15 07:40:16 +00:00
items Items
padding Padding
2020-01-03 12:53:25 +00:00
toolTip *ToolTip
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
}
// DelItem removes menu item by value or label
2017-02-15 06:15:03 +00:00
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
}
2020-01-02 23:02:53 +00:00
// ClearItems removes all current menu items
func (m *Menu) ClearItems() {
m.items = m.items[:0]
}
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
}
2020-01-03 12:53:25 +00:00
// SetToolTip sets an optional tooltip string to show at bottom of screen
func (m *Menu) SetToolTip(lines ...string) {
m.toolTip = NewToolTip(lines...)
}
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]
}
2020-01-02 23:02:53 +00:00
func (m *Menu) SelectedValue() string {
return m.items[m.cursorPos].Val
}
2017-01-02 23:40:55 +00:00
func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell
buf := m.Block.Buffer()
2018-01-11 13:56:00 +00:00
y := m.Y + m.padding[1]
if m.SubText != "" {
x := m.X + m.padding[0]
for i, ch := range m.SubText {
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
buf.Set(x+i, y, cell)
}
y += 2
}
2017-02-15 07:40:16 +00:00
for n, item := range m.items {
x := m.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-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++
}
}
2020-01-03 12:53:25 +00:00
if m.toolTip != nil {
buf.Merge(m.toolTip.Buffer())
}
2017-01-02 23:40:55 +00:00
return buf
}
func (m *Menu) Up() {
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)
}
}
func (m *Menu) Down() {
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)
}
}
2020-01-03 12:53:25 +00:00
// Sort menu items(if enabled) and re-calculate window size
func (m *Menu) refresh() {
if m.SortItems {
sort.Sort(m.items)
}
m.calcSize()
ui.Render(m)
}
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
2018-01-11 13:56:00 +00:00
var height int
2017-02-15 07:40:16 +00:00
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
}
2018-01-11 13:56:00 +00:00
height++
}
if m.SubText != "" {
if len(m.SubText) > m.Width {
m.Width = len(m.SubText)
}
height += 2
2017-01-04 23:13:17 +00:00
}
2017-01-21 18:41:28 +00:00
m.Width += (m.padding[0] * 2)
2018-01-11 13:56:00 +00:00
m.Height = height + (m.padding[1] * 2)
2017-01-02 23:40:55 +00:00
}