add input widget, filter menu

This commit is contained in:
Bradley Cicenas 2017-01-21 13:15:29 -05:00
parent 5eda9e4d09
commit bf201f55d3
3 changed files with 82 additions and 1 deletions

View File

@ -141,6 +141,10 @@ func Display(g *Grid) bool {
expand = true
ui.StopLoop()
})
ui.Handle("/sys/kbd/f", func(ui.Event) {
menu = FilterMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/h", func(ui.Event) {
menu = HelpMenu
ui.StopLoop()
@ -148,7 +152,7 @@ func Display(g *Grid) bool {
ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("/sys/kbd/r", func(ui.Event) {
ui.Handle("/sys/kbd/r", func(e ui.Event) {
GlobalConfig.toggle("sortReversed")
})
ui.Handle("/sys/kbd/s", func(ui.Event) {

View File

@ -24,6 +24,19 @@ func HelpMenu() {
ui.Loop()
}
func FilterMenu() {
i := widgets.NewInput()
i.TextFgColor = ui.ColorWhite
i.BorderLabel = "Filter"
i.BorderFg = ui.ColorCyan
ui.Render(i)
i.InputHandlers()
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
ui.StopLoop()
})
ui.Loop()
}
func SortMenu() {
m := widgets.NewMenu(SortFields())
m.Selectable = true

64
widgets/input.go Normal file
View File

@ -0,0 +1,64 @@
package widgets
import (
"strings"
ui "github.com/gizak/termui"
)
var (
input_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_."
)
type Input struct {
ui.Block
Label string
Data string
TextFgColor ui.Attribute
TextBgColor ui.Attribute
}
func NewInput() *Input {
i := &Input{
Block: *ui.NewBlock(),
Label: "input",
TextFgColor: ui.ThemeAttr("par.text.fg"),
TextBgColor: ui.ThemeAttr("par.text.bg"),
}
i.Width, i.Height = 30, 3
return i
}
func (i *Input) Buffer() ui.Buffer {
var cell ui.Cell
buf := i.Block.Buffer()
x := x_padding
for _, ch := range i.Data {
cell = ui.Cell{Ch: ch, Fg: i.TextFgColor, Bg: i.TextBgColor}
buf.Set(x, 1, cell)
x++
}
return buf
}
func (i *Input) KeyPress(e ui.Event) {
ch := strings.Replace(e.Path, "/sys/kbd/", "", -1)
if ch == "C-8" {
idx := len(i.Data) - 1
if idx > -1 {
i.Data = i.Data[0:idx]
}
ui.Render(i)
}
if strings.Index(input_chars, ch) > -1 {
i.Data += ch
ui.Render(i)
}
}
// Setup some default handlers for menu navigation
func (i *Input) InputHandlers() {
ui.Handle("/sys/kbd/", i.KeyPress)
}