mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
move menu widget into subpackage
This commit is contained in:
parent
c9a207ebfd
commit
222a030e0b
38
menus.go
Normal file
38
menus.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bcicen/ctop/widgets"
|
||||||
|
ui "github.com/gizak/termui"
|
||||||
|
)
|
||||||
|
|
||||||
|
var helpDialog = []string{
|
||||||
|
"[h] - open this help dialog",
|
||||||
|
"[q] - exit ctop",
|
||||||
|
}
|
||||||
|
|
||||||
|
func HelpMenu(g *Grid) {
|
||||||
|
m := widgets.NewMenu(helpDialog)
|
||||||
|
m.TextFgColor = ui.ColorWhite
|
||||||
|
m.BorderLabel = "Help"
|
||||||
|
m.BorderFg = ui.ColorCyan
|
||||||
|
ui.Render(m)
|
||||||
|
ui.Handle("/sys/kbd/", func(ui.Event) {
|
||||||
|
ui.StopLoop()
|
||||||
|
})
|
||||||
|
ui.Loop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SortMenu(g *Grid) {
|
||||||
|
m := widgets.NewMenu(SortFields)
|
||||||
|
m.Selectable = true
|
||||||
|
m.TextFgColor = ui.ColorWhite
|
||||||
|
m.BorderLabel = "Sort Field"
|
||||||
|
m.BorderFg = ui.ColorCyan
|
||||||
|
ui.Render(m)
|
||||||
|
m.NavigationHandlers()
|
||||||
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
|
g.containerMap.config.sortField = m.Items[m.CursorPos]
|
||||||
|
ui.StopLoop()
|
||||||
|
})
|
||||||
|
ui.Loop()
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package widgets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
@ -15,7 +15,7 @@ type Menu struct {
|
|||||||
TextFgColor ui.Attribute
|
TextFgColor ui.Attribute
|
||||||
TextBgColor ui.Attribute
|
TextBgColor ui.Attribute
|
||||||
Selectable bool
|
Selectable bool
|
||||||
cursorPos int
|
CursorPos int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMenu(items []string) *Menu {
|
func NewMenu(items []string) *Menu {
|
||||||
@ -25,12 +25,54 @@ func NewMenu(items []string) *Menu {
|
|||||||
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,
|
Selectable: false,
|
||||||
cursorPos: 0,
|
CursorPos: 0,
|
||||||
}
|
}
|
||||||
m.Width, m.Height = calcSize(items)
|
m.Width, m.Height = calcSize(items)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Menu) Buffer() ui.Buffer {
|
||||||
|
var cell ui.Cell
|
||||||
|
buf := m.Block.Buffer()
|
||||||
|
|
||||||
|
for n, item := range m.Items {
|
||||||
|
x := padding
|
||||||
|
for _, ch := range item {
|
||||||
|
// invert bg/fg colors on currently selected row
|
||||||
|
if m.Selectable && n == m.CursorPos {
|
||||||
|
cell = ui.Cell{Ch: ch, Fg: m.TextBgColor, Bg: m.TextFgColor}
|
||||||
|
} else {
|
||||||
|
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
|
||||||
|
}
|
||||||
|
buf.Set(x, n+padding, cell)
|
||||||
|
x++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Menu) Up(ui.Event) {
|
||||||
|
if m.CursorPos > 0 {
|
||||||
|
m.CursorPos--
|
||||||
|
ui.Render(m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Menu) Down(ui.Event) {
|
||||||
|
if m.CursorPos < (len(m.Items) - 1) {
|
||||||
|
m.CursorPos++
|
||||||
|
ui.Render(m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup some default handlers for menu navigation
|
||||||
|
func (m *Menu) NavigationHandlers() {
|
||||||
|
ui.Handle("/sys/kbd/<up>", m.Up)
|
||||||
|
ui.Handle("/sys/kbd/<down>", m.Down)
|
||||||
|
ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() })
|
||||||
|
}
|
||||||
|
|
||||||
// return width and height based on menu items
|
// return width and height based on menu items
|
||||||
func calcSize(items []string) (w, h int) {
|
func calcSize(items []string) (w, h int) {
|
||||||
h = len(items) + (padding * 2)
|
h = len(items) + (padding * 2)
|
||||||
@ -45,73 +87,3 @@ func calcSize(items []string) (w, h int) {
|
|||||||
|
|
||||||
return w, h
|
return w, h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) Buffer() ui.Buffer {
|
|
||||||
var cell ui.Cell
|
|
||||||
|
|
||||||
buf := m.Block.Buffer()
|
|
||||||
|
|
||||||
for n, item := range m.Items {
|
|
||||||
x := padding
|
|
||||||
for _, ch := range item {
|
|
||||||
// invert bg/fg colors on currently selected row
|
|
||||||
if m.Selectable && n == m.cursorPos {
|
|
||||||
cell = ui.Cell{Ch: ch, Fg: m.TextBgColor, Bg: m.TextFgColor}
|
|
||||||
} else {
|
|
||||||
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
|
|
||||||
}
|
|
||||||
buf.Set(x, n+padding, cell)
|
|
||||||
x++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Menu) Up(ui.Event) {
|
|
||||||
if m.cursorPos > 0 {
|
|
||||||
m.cursorPos--
|
|
||||||
ui.Render(m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Menu) Down(ui.Event) {
|
|
||||||
if m.cursorPos < (len(m.Items) - 1) {
|
|
||||||
m.cursorPos++
|
|
||||||
ui.Render(m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func HelpMenu(g *Grid) {
|
|
||||||
m := NewMenu([]string{
|
|
||||||
"[h] - open this help dialog",
|
|
||||||
"[q] - exit ctop",
|
|
||||||
})
|
|
||||||
m.TextFgColor = ui.ColorWhite
|
|
||||||
m.BorderLabel = "Help"
|
|
||||||
m.BorderFg = ui.ColorCyan
|
|
||||||
ui.Render(m)
|
|
||||||
ui.Handle("/sys/kbd/", func(ui.Event) {
|
|
||||||
ui.StopLoop()
|
|
||||||
})
|
|
||||||
ui.Loop()
|
|
||||||
}
|
|
||||||
|
|
||||||
func SortMenu(g *Grid) {
|
|
||||||
m := NewMenu(SortFields)
|
|
||||||
m.Selectable = true
|
|
||||||
m.TextFgColor = ui.ColorWhite
|
|
||||||
m.BorderLabel = "Sort Field"
|
|
||||||
m.BorderFg = ui.ColorCyan
|
|
||||||
ui.Render(m)
|
|
||||||
ui.Handle("/sys/kbd/<up>", m.Up)
|
|
||||||
ui.Handle("/sys/kbd/<down>", m.Down)
|
|
||||||
ui.Handle("/sys/kbd/q", func(ui.Event) {
|
|
||||||
ui.StopLoop()
|
|
||||||
})
|
|
||||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
|
||||||
g.containerMap.config.sortField = m.Items[m.cursorPos]
|
|
||||||
ui.StopLoop()
|
|
||||||
})
|
|
||||||
ui.Loop()
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user