mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add SortItems param to Menu, AddItems method
This commit is contained in:
parent
b9bdc1c9c5
commit
eb66f32a71
8
menus.go
8
menus.go
@ -17,10 +17,11 @@ func HelpMenu() {
|
|||||||
ResetView()
|
ResetView()
|
||||||
defer ResetView()
|
defer ResetView()
|
||||||
|
|
||||||
m := widgets.NewMenu(helpDialog)
|
m := widgets.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)...)
|
||||||
ui.Render(m)
|
ui.Render(m)
|
||||||
ui.Handle("/sys/kbd/", func(ui.Event) {
|
ui.Handle("/sys/kbd/", func(ui.Event) {
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
@ -50,12 +51,15 @@ func SortMenu() {
|
|||||||
ResetView()
|
ResetView()
|
||||||
defer ResetView()
|
defer ResetView()
|
||||||
|
|
||||||
m := widgets.NewMenu(SortFields())
|
m := widgets.NewMenu()
|
||||||
m.Selectable = true
|
m.Selectable = 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())...)
|
||||||
|
|
||||||
// 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 {
|
||||||
|
10
sort.go
10
sort.go
@ -1,8 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
|
||||||
|
|
||||||
"github.com/bcicen/ctop/config"
|
"github.com/bcicen/ctop/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,13 +15,11 @@ var Sorters = map[string]sortMethod{
|
|||||||
"net": func(c1, c2 *Container) bool { return sumNet(c1) < sumNet(c2) },
|
"net": func(c1, c2 *Container) bool { return sumNet(c1) < sumNet(c2) },
|
||||||
}
|
}
|
||||||
|
|
||||||
func SortFields() []string {
|
func SortFields() (fields []string) {
|
||||||
a := sort.StringSlice{}
|
|
||||||
for k := range Sorters {
|
for k := range Sorters {
|
||||||
a = append(a, k)
|
fields = append(fields, k)
|
||||||
}
|
}
|
||||||
sort.Sort(a)
|
return fields
|
||||||
return a
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Containers []*Container
|
type Containers []*Container
|
||||||
|
@ -23,6 +23,14 @@ func (m MenuItem) Text() string {
|
|||||||
|
|
||||||
type MenuItems []MenuItem
|
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
|
// Sort methods for MenuItems
|
||||||
func (m MenuItems) Len() int { return len(m) }
|
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) Swap(a, b int) { m[a], m[b] = m[b], m[a] }
|
||||||
@ -33,6 +41,7 @@ func (m MenuItems) Less(a, b int) bool {
|
|||||||
type Menu struct {
|
type Menu struct {
|
||||||
ui.Block
|
ui.Block
|
||||||
Items MenuItems
|
Items MenuItems
|
||||||
|
SortItems bool // enable automatic sorting of menu items
|
||||||
TextFgColor ui.Attribute
|
TextFgColor ui.Attribute
|
||||||
TextBgColor ui.Attribute
|
TextBgColor ui.Attribute
|
||||||
Selectable bool
|
Selectable bool
|
||||||
@ -40,28 +49,28 @@ type Menu struct {
|
|||||||
padding Padding
|
padding Padding
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMenu(items []string) *Menu {
|
func NewMenu() *Menu {
|
||||||
m := &Menu{
|
return &Menu{
|
||||||
Block: *ui.NewBlock(),
|
Block: *ui.NewBlock(),
|
||||||
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
||||||
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
||||||
Selectable: false,
|
|
||||||
CursorPos: 0,
|
CursorPos: 0,
|
||||||
padding: Padding{4, 2},
|
padding: Padding{4, 2},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range items {
|
|
||||||
m.Items = append(m.Items, MenuItem{Val: s})
|
|
||||||
}
|
|
||||||
sort.Sort(m.Items)
|
|
||||||
|
|
||||||
m.calcSize()
|
|
||||||
return m
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) SetItems(items []MenuItem) {
|
func (m *Menu) AddItems(items ...MenuItem) {
|
||||||
m.Items = items
|
for _, i := range items {
|
||||||
|
m.Items = append(m.Items, i)
|
||||||
|
}
|
||||||
|
m.refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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()
|
m.calcSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user