mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor menu into subpackage
This commit is contained in:
parent
c9632c9bf6
commit
9ddc99f788
35
menus.go
35
menus.go
@ -3,25 +3,26 @@ package main
|
||||
import (
|
||||
"github.com/bcicen/ctop/config"
|
||||
"github.com/bcicen/ctop/widgets"
|
||||
"github.com/bcicen/ctop/widgets/menu"
|
||||
ui "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
var helpDialog = []string{
|
||||
"[h] - open this help dialog",
|
||||
"[s] - select container sort field",
|
||||
"[r] - reverse container sort order",
|
||||
"[q] - exit ctop",
|
||||
var helpDialog = []menu.Item{
|
||||
menu.Item{"[h] - open this help dialog", ""},
|
||||
menu.Item{"[s] - select container sort field", ""},
|
||||
menu.Item{"[r] - reverse container sort order", ""},
|
||||
menu.Item{"[q] - exit ctop", ""},
|
||||
}
|
||||
|
||||
func HelpMenu() {
|
||||
ResetView()
|
||||
defer ResetView()
|
||||
|
||||
m := widgets.NewMenu()
|
||||
m := menu.NewMenu()
|
||||
m.TextFgColor = ui.ColorWhite
|
||||
m.BorderLabel = "Help"
|
||||
m.BorderFg = ui.ColorCyan
|
||||
m.AddItems(widgets.NewMenuItems(helpDialog)...)
|
||||
m.AddItems(helpDialog...)
|
||||
ui.Render(m)
|
||||
ui.Handle("/sys/kbd/", func(ui.Event) {
|
||||
ui.StopLoop()
|
||||
@ -51,27 +52,29 @@ func SortMenu() {
|
||||
ResetView()
|
||||
defer ResetView()
|
||||
|
||||
m := widgets.NewMenu()
|
||||
m := menu.NewMenu()
|
||||
m.Selectable = true
|
||||
m.SortItems = true
|
||||
m.TextFgColor = ui.ColorWhite
|
||||
m.BorderLabel = "Sort Field"
|
||||
m.BorderFg = ui.ColorCyan
|
||||
|
||||
m.AddItems(widgets.NewMenuItems(SortFields())...)
|
||||
for _, field := range SortFields() {
|
||||
m.AddItems(menu.Item{field, ""})
|
||||
}
|
||||
|
||||
// set cursor position to current sort field
|
||||
current := config.Get("sortField")
|
||||
for n, item := range m.Items {
|
||||
if item.Val == current {
|
||||
m.CursorPos = n
|
||||
}
|
||||
}
|
||||
//current := config.Get("sortField")
|
||||
//for n, item := range m.Items {
|
||||
//if item.Val == current {
|
||||
//m.CursorPos = n
|
||||
//}
|
||||
//}
|
||||
|
||||
ui.Render(m)
|
||||
m.NavigationHandlers()
|
||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||
config.Update("sortField", m.Items[m.CursorPos].Val)
|
||||
config.Update("sortField", m.SelectedItem().Val)
|
||||
ui.StopLoop()
|
||||
})
|
||||
ui.Loop()
|
||||
|
@ -10,6 +10,8 @@ var (
|
||||
input_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_."
|
||||
)
|
||||
|
||||
type Padding [2]int // x,y padding
|
||||
|
||||
type Input struct {
|
||||
ui.Block
|
||||
Label string
|
||||
|
30
widgets/menu/items.go
Normal file
30
widgets/menu/items.go
Normal file
@ -0,0 +1,30 @@
|
||||
package menu
|
||||
|
||||
type Item struct {
|
||||
Val string
|
||||
Label string
|
||||
}
|
||||
|
||||
// Use label as display text of item, if given
|
||||
func (m Item) Text() string {
|
||||
if m.Label != "" {
|
||||
return m.Label
|
||||
}
|
||||
return m.Val
|
||||
}
|
||||
|
||||
type Items []Item
|
||||
|
||||
func NewItems(items ...Item) (mitems Items) {
|
||||
for _, i := range items {
|
||||
mitems = append(mitems, i)
|
||||
}
|
||||
return mitems
|
||||
}
|
||||
|
||||
// Sort methods for Items
|
||||
func (m Items) Len() int { return len(m) }
|
||||
func (m Items) Swap(a, b int) { m[a], m[b] = m[b], m[a] }
|
||||
func (m Items) Less(a, b int) bool {
|
||||
return m[a].Text() < m[b].Text()
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package widgets
|
||||
package menu
|
||||
|
||||
import (
|
||||
"sort"
|
||||
@ -8,44 +8,14 @@ import (
|
||||
|
||||
type Padding [2]int // x,y padding
|
||||
|
||||
type MenuItem struct {
|
||||
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
|
||||
|
||||
// Create new MenuItems from string array
|
||||
func NewMenuItems(a []string) (items MenuItems) {
|
||||
for _, s := range a {
|
||||
items = append(items, MenuItem{Val: s})
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
type Menu struct {
|
||||
ui.Block
|
||||
Items MenuItems
|
||||
SortItems bool // enable automatic sorting of menu items
|
||||
TextFgColor ui.Attribute
|
||||
TextBgColor ui.Attribute
|
||||
Selectable bool
|
||||
CursorPos int
|
||||
items Items
|
||||
padding Padding
|
||||
}
|
||||
|
||||
@ -59,18 +29,18 @@ func NewMenu() *Menu {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Menu) AddItems(items ...MenuItem) {
|
||||
func (m *Menu) AddItems(items ...Item) {
|
||||
for _, i := range items {
|
||||
m.Items = append(m.Items, i)
|
||||
m.items = append(m.items, i)
|
||||
}
|
||||
m.refresh()
|
||||
}
|
||||
|
||||
// Remove menu item by value or label
|
||||
func (m *Menu) DelItem(s string) (success bool) {
|
||||
for n, i := range m.Items {
|
||||
for n, i := range m.items {
|
||||
if i.Val == s || i.Label == s {
|
||||
m.Items = append(m.Items[:n], m.Items[n+1:]...)
|
||||
m.items = append(m.items[:n], m.items[n+1:]...)
|
||||
success = true
|
||||
m.refresh()
|
||||
break
|
||||
@ -82,21 +52,21 @@ func (m *Menu) DelItem(s string) (success bool) {
|
||||
// Sort menu items(if enabled) and re-calculate window size
|
||||
func (m *Menu) refresh() {
|
||||
if m.SortItems {
|
||||
sort.Sort(m.Items)
|
||||
sort.Sort(m.items)
|
||||
}
|
||||
m.calcSize()
|
||||
ui.Render(m)
|
||||
}
|
||||
|
||||
func (m *Menu) SelectedItem() MenuItem {
|
||||
return m.Items[m.CursorPos]
|
||||
func (m *Menu) SelectedItem() Item {
|
||||
return m.items[m.CursorPos]
|
||||
}
|
||||
|
||||
func (m *Menu) Buffer() ui.Buffer {
|
||||
var cell ui.Cell
|
||||
buf := m.Block.Buffer()
|
||||
|
||||
for n, item := range m.Items {
|
||||
for n, item := range m.items {
|
||||
x := m.padding[0]
|
||||
for _, ch := range item.Text() {
|
||||
// invert bg/fg colors on currently selected row
|
||||
@ -121,7 +91,7 @@ func (m *Menu) Up(ui.Event) {
|
||||
}
|
||||
|
||||
func (m *Menu) Down(ui.Event) {
|
||||
if m.CursorPos < (len(m.Items) - 1) {
|
||||
if m.CursorPos < (len(m.items) - 1) {
|
||||
m.CursorPos++
|
||||
ui.Render(m)
|
||||
}
|
||||
@ -138,8 +108,8 @@ func (m *Menu) NavigationHandlers() {
|
||||
func (m *Menu) calcSize() {
|
||||
m.Width = 8 // minimum width
|
||||
|
||||
items := m.Items
|
||||
for _, i := range m.Items {
|
||||
items := m.items
|
||||
for _, i := range m.items {
|
||||
s := i.Text()
|
||||
if len(s) > m.Width {
|
||||
m.Width = len(s)
|
Loading…
Reference in New Issue
Block a user