From 9ddc99f7883234189baf332c0bf080f532d52cfe Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Wed, 15 Feb 2017 07:40:16 +0000 Subject: [PATCH] refactor menu into subpackage --- menus.go | 35 ++++++++++--------- widgets/input.go | 2 ++ widgets/menu/items.go | 30 +++++++++++++++++ widgets/{menu.go => menu/main.go} | 56 +++++++------------------------ 4 files changed, 64 insertions(+), 59 deletions(-) create mode 100644 widgets/menu/items.go rename widgets/{menu.go => menu/main.go} (65%) diff --git a/menus.go b/menus.go index ef67c2b..8ba324f 100644 --- a/menus.go +++ b/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/", func(ui.Event) { - config.Update("sortField", m.Items[m.CursorPos].Val) + config.Update("sortField", m.SelectedItem().Val) ui.StopLoop() }) ui.Loop() diff --git a/widgets/input.go b/widgets/input.go index eed8535..4061d28 100644 --- a/widgets/input.go +++ b/widgets/input.go @@ -10,6 +10,8 @@ var ( input_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_." ) +type Padding [2]int // x,y padding + type Input struct { ui.Block Label string diff --git a/widgets/menu/items.go b/widgets/menu/items.go new file mode 100644 index 0000000..04e98fd --- /dev/null +++ b/widgets/menu/items.go @@ -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() +} diff --git a/widgets/menu.go b/widgets/menu/main.go similarity index 65% rename from widgets/menu.go rename to widgets/menu/main.go index 317a129..4275293 100644 --- a/widgets/menu.go +++ b/widgets/menu/main.go @@ -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)