add Menu widget type, sort field menu

This commit is contained in:
Bradley Cicenas 2017-01-02 23:40:55 +00:00
parent 157b51b6f6
commit e1e989c220
3 changed files with 116 additions and 32 deletions

15
grid.go
View File

@ -3,7 +3,6 @@ package main
import (
"fmt"
"github.com/bcicen/ctop/views"
ui "github.com/gizak/termui"
)
@ -98,17 +97,17 @@ func headerPar(s string) *ui.Par {
return p
}
type View func()
type View func(*Grid)
func ResetView() {
ui.DefaultEvtStream.ResetHandlers()
ui.Clear()
}
func OpenView(v View) {
func (g *Grid) OpenView(v View) {
ResetView()
defer ResetView()
v()
v(g)
}
func Display(g *Grid) bool {
@ -126,7 +125,11 @@ func Display(g *Grid) bool {
g.cursorDown()
})
ui.Handle("/sys/kbd/h", func(ui.Event) {
newView = views.Help
newView = HelpMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/s", func(ui.Event) {
newView = SortMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/q", func(ui.Event) {
@ -146,7 +149,7 @@ func Display(g *Grid) bool {
ui.Loop()
if newView != nil {
OpenView(newView)
g.OpenView(newView)
return false
}
return true

107
menu.go Normal file
View File

@ -0,0 +1,107 @@
package main
import (
ui "github.com/gizak/termui"
)
type Menu struct {
ui.Block
Items []string
TextFgColor ui.Attribute
TextBgColor ui.Attribute
Selectable bool
cursorPos int
}
func NewMenu(items []string) *Menu {
return &Menu{
Block: *ui.NewBlock(),
Items: items,
TextFgColor: ui.ThemeAttr("par.text.fg"),
TextBgColor: ui.ThemeAttr("par.text.bg"),
Selectable: false,
cursorPos: 0,
}
}
func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell
buf := m.Block.Buffer()
for n, item := range m.Items {
//if n >= m.innerArea.Dy() {
//buf.Set(m.innerArea.Min.X+m.innerArea.Dx()-1,
//m.innerArea.Min.Y+m.innerArea.Dy()-1,
//ui.Cell{Ch: '…', Fg: m.TextFgColor, Bg: m.TextBgColor})
//break
//}
x := 2 // initial offset
// invert bg/fg colors on currently selected row
for _, ch := range item {
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+2, cell)
x++
}
}
return buf
}
func (m *Menu) Up() {
if m.cursorPos > 0 {
m.cursorPos--
ui.Render(m)
}
}
func (m *Menu) Down() {
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.Height = 10
m.Width = 50
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.Height = 10
m.Width = 50
m.Selectable = true
m.TextFgColor = ui.ColorWhite
m.BorderLabel = "Sort Field"
m.BorderFg = ui.ColorCyan
ui.Render(m)
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
m.Up()
})
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
m.Down()
})
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
g.containerMap.sortField = m.Items[m.cursorPos]
ui.StopLoop()
})
ui.Loop()
}

View File

@ -1,26 +0,0 @@
package views
import (
"strings"
ui "github.com/gizak/termui"
)
var helpDialog = []string{
"[h] - open this help dialog",
"[q] - exit ctop",
}
func Help() {
p := ui.NewPar(strings.Join(helpDialog, "\n"))
p.Height = 10
p.Width = 50
p.TextFgColor = ui.ColorWhite
p.BorderLabel = "Help"
p.BorderFg = ui.ColorCyan
ui.Render(p)
ui.Handle("/sys/kbd/", func(ui.Event) {
ui.StopLoop()
})
ui.Loop()
}