refactor menu into subpackage

This commit is contained in:
Bradley Cicenas 2017-02-15 07:40:16 +00:00
parent c9632c9bf6
commit 9ddc99f788
4 changed files with 64 additions and 59 deletions

View File

@ -3,25 +3,26 @@ package main
import ( import (
"github.com/bcicen/ctop/config" "github.com/bcicen/ctop/config"
"github.com/bcicen/ctop/widgets" "github.com/bcicen/ctop/widgets"
"github.com/bcicen/ctop/widgets/menu"
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
var helpDialog = []string{ var helpDialog = []menu.Item{
"[h] - open this help dialog", menu.Item{"[h] - open this help dialog", ""},
"[s] - select container sort field", menu.Item{"[s] - select container sort field", ""},
"[r] - reverse container sort order", menu.Item{"[r] - reverse container sort order", ""},
"[q] - exit ctop", menu.Item{"[q] - exit ctop", ""},
} }
func HelpMenu() { func HelpMenu() {
ResetView() ResetView()
defer ResetView() defer ResetView()
m := widgets.NewMenu() m := menu.NewMenu()
m.TextFgColor = ui.ColorWhite m.TextFgColor = ui.ColorWhite
m.BorderLabel = "Help" m.BorderLabel = "Help"
m.BorderFg = ui.ColorCyan m.BorderFg = ui.ColorCyan
m.AddItems(widgets.NewMenuItems(helpDialog)...) m.AddItems(helpDialog...)
ui.Render(m) ui.Render(m)
ui.Handle("/sys/kbd/", func(ui.Event) { ui.Handle("/sys/kbd/", func(ui.Event) {
ui.StopLoop() ui.StopLoop()
@ -51,27 +52,29 @@ func SortMenu() {
ResetView() ResetView()
defer ResetView() defer ResetView()
m := widgets.NewMenu() m := menu.NewMenu()
m.Selectable = true m.Selectable = true
m.SortItems = true m.SortItems = true
m.TextFgColor = ui.ColorWhite m.TextFgColor = ui.ColorWhite
m.BorderLabel = "Sort Field" m.BorderLabel = "Sort Field"
m.BorderFg = ui.ColorCyan 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 // set cursor position to current sort field
current := config.Get("sortField") //current := config.Get("sortField")
for n, item := range m.Items { //for n, item := range m.Items {
if item.Val == current { //if item.Val == current {
m.CursorPos = n //m.CursorPos = n
} //}
} //}
ui.Render(m) ui.Render(m)
m.NavigationHandlers() m.NavigationHandlers()
ui.Handle("/sys/kbd/<enter>", func(ui.Event) { 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.StopLoop()
}) })
ui.Loop() ui.Loop()

View File

@ -10,6 +10,8 @@ var (
input_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_." input_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_."
) )
type Padding [2]int // x,y padding
type Input struct { type Input struct {
ui.Block ui.Block
Label string Label string

30
widgets/menu/items.go Normal file
View 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()
}

View File

@ -1,4 +1,4 @@
package widgets package menu
import ( import (
"sort" "sort"
@ -8,44 +8,14 @@ import (
type Padding [2]int // x,y padding 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 { type Menu struct {
ui.Block ui.Block
Items MenuItems
SortItems bool // enable automatic sorting of menu items SortItems bool // enable automatic sorting of menu items
TextFgColor ui.Attribute TextFgColor ui.Attribute
TextBgColor ui.Attribute TextBgColor ui.Attribute
Selectable bool Selectable bool
CursorPos int CursorPos int
items Items
padding Padding 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 { for _, i := range items {
m.Items = append(m.Items, i) m.items = append(m.items, i)
} }
m.refresh() m.refresh()
} }
// Remove menu item by value or label // Remove menu item by value or label
func (m *Menu) DelItem(s string) (success bool) { 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 { 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 success = true
m.refresh() m.refresh()
break break
@ -82,21 +52,21 @@ func (m *Menu) DelItem(s string) (success bool) {
// Sort menu items(if enabled) and re-calculate window size // Sort menu items(if enabled) and re-calculate window size
func (m *Menu) refresh() { func (m *Menu) refresh() {
if m.SortItems { if m.SortItems {
sort.Sort(m.Items) sort.Sort(m.items)
} }
m.calcSize() m.calcSize()
ui.Render(m) ui.Render(m)
} }
func (m *Menu) SelectedItem() MenuItem { func (m *Menu) SelectedItem() Item {
return m.Items[m.CursorPos] return m.items[m.CursorPos]
} }
func (m *Menu) Buffer() ui.Buffer { func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell var cell ui.Cell
buf := m.Block.Buffer() buf := m.Block.Buffer()
for n, item := range m.Items { for n, item := range m.items {
x := m.padding[0] x := m.padding[0]
for _, ch := range item.Text() { for _, ch := range item.Text() {
// invert bg/fg colors on currently selected row // invert bg/fg colors on currently selected row
@ -121,7 +91,7 @@ func (m *Menu) Up(ui.Event) {
} }
func (m *Menu) Down(ui.Event) { func (m *Menu) Down(ui.Event) {
if m.CursorPos < (len(m.Items) - 1) { if m.CursorPos < (len(m.items) - 1) {
m.CursorPos++ m.CursorPos++
ui.Render(m) ui.Render(m)
} }
@ -138,8 +108,8 @@ func (m *Menu) NavigationHandlers() {
func (m *Menu) calcSize() { func (m *Menu) calcSize() {
m.Width = 8 // minimum width m.Width = 8 // minimum width
items := m.Items items := m.items
for _, i := range m.Items { for _, i := range m.items {
s := i.Text() s := i.Text()
if len(s) > m.Width { if len(s) > m.Width {
m.Width = len(s) m.Width = len(s)