add keymap, handle wrapper for common keybindings

This commit is contained in:
Bradley Cicenas 2017-03-14 22:34:09 +00:00
parent b3cdb33efc
commit 38599bbd19
4 changed files with 55 additions and 30 deletions

34
grid.go
View File

@ -44,21 +44,19 @@ func ExpandView(c *Container) {
ex.Align()
ui.Render(ex)
ui.Handle("/sys/kbd/<up>", func(ui.Event) { ex.Up() })
ui.Handle("/sys/kbd/<down>", func(ui.Event) { ex.Down() })
ui.Handle("/timer/1s", func(ui.Event) {
ui.Render(ex)
})
HandleKeys("up", ex.Up)
HandleKeys("down", ex.Down)
ui.Handle("/sys/kbd/", func(ui.Event) { ui.StopLoop() })
ui.Handle("/timer/1s", func(ui.Event) { ui.Render(ex) })
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
ex.SetWidth(ui.TermWidth())
ex.Align()
log.Infof("resize: width=%v max-rows=%v", ex.Width, cGrid.MaxRows())
})
ui.Handle("/sys/kbd/", func(ui.Event) {
ui.StopLoop()
})
ui.Loop()
ui.Loop()
c.SetUpdater(c.Widgets)
}
@ -79,16 +77,18 @@ func Display() bool {
cursor.RefreshContainers()
RedrawRows(true)
ui.Handle("/sys/kbd/<up>", func(ui.Event) { cursor.Up() })
ui.Handle("/sys/kbd/<down>", func(ui.Event) { cursor.Down() })
HandleKeys("up", cursor.Up)
HandleKeys("down", cursor.Down)
HandleKeys("exit", ui.StopLoop)
HandleKeys("help", func() {
menu = HelpMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
expand = true
ui.StopLoop()
})
ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() })
ui.Handle("/sys/kbd/C-c", func(ui.Event) { ui.StopLoop() })
ui.Handle("/sys/kbd/a", func(ui.Event) {
config.Toggle("allContainers")
RefreshDisplay()
@ -100,10 +100,6 @@ func Display() bool {
menu = FilterMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/h", func(ui.Event) {
menu = HelpMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/H", func(ui.Event) {
config.Toggle("enableHeader")
RedrawRows(true)

32
keys.go Normal file
View File

@ -0,0 +1,32 @@
package main
import (
ui "github.com/gizak/termui"
)
// Common action keybindings
var keyMap = map[string][]string{
"up": []string{
"/sys/kbd/<up>",
"/sys/kbd/k",
},
"down": []string{
"/sys/kbd/<down>",
"/sys/kbd/j",
},
"exit": []string{
"/sys/kbd/q",
"/sys/kbd/C-c",
},
"help": []string{
"/sys/kbd/h",
"/sys/kbd/?",
},
}
// Apply a common handler function to all given keys
func HandleKeys(i string, f func()) {
for _, k := range keyMap[i] {
ui.Handle(k, func(ui.Event) { f() })
}
}

View File

@ -81,11 +81,15 @@ func SortMenu() {
// set cursor position to current sort field
m.SetCursor(config.GetVal("sortField"))
ui.Render(m)
m.NavigationHandlers()
HandleKeys("up", m.Up)
HandleKeys("down", m.Down)
HandleKeys("exit", ui.StopLoop)
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
config.Update("sortField", m.SelectedItem().Val)
ui.StopLoop()
})
ui.Render(m)
ui.Loop()
}

View File

@ -100,27 +100,20 @@ func (m *Menu) Buffer() ui.Buffer {
return buf
}
func (m *Menu) Up(ui.Event) {
func (m *Menu) Up() {
if m.cursorPos > 0 {
m.cursorPos--
ui.Render(m)
}
}
func (m *Menu) Down(ui.Event) {
func (m *Menu) Down() {
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() })
}
// Set width and height based on menu items
func (m *Menu) calcSize() {
m.Width = 7 // minimum width