Read key mappings from configs and execute actions

This commit is contained in:
Chris Marshall 2016-10-27 12:53:37 -04:00
parent 58e1b942b4
commit b7feeede86
2 changed files with 25 additions and 51 deletions

View File

@ -11,7 +11,7 @@ import (
)
const (
CommandMode = "command"
CommandMode = "normal"
InsertMode = "insert"
)

View File

@ -1,6 +1,8 @@
package handlers
import (
"fmt"
"github.com/gizak/termui"
"github.com/nlopes/slack"
termbox "github.com/nsf/termbox-go"
@ -53,59 +55,31 @@ func anyKeyHandler(ctx *context.AppContext) {
for {
ev := termbox.PollEvent()
if ev.Type == termbox.EventKey {
if ctx.Mode == context.CommandMode {
switch ev.Key {
case termbox.KeyPgup:
actionScrollUpChat(ctx)
case termbox.KeyCtrlB:
actionScrollUpChat(ctx)
case termbox.KeyCtrlU:
actionScrollUpChat(ctx)
case termbox.KeyPgdn:
actionScrollDownChat(ctx)
case termbox.KeyCtrlF:
actionScrollDownChat(ctx)
case termbox.KeyCtrlD:
actionScrollDownChat(ctx)
default:
switch ev.Ch {
case 'q':
actionQuit()
case 'j':
actionMoveCursorDownChannels(ctx)
case 'k':
actionMoveCursorUpChannels(ctx)
case 'g':
actionMoveCursorTopChannels(ctx)
case 'G':
actionMoveCursorBottomChannels(ctx)
case 'i':
actionInsertMode(ctx)
if ev.Type != termbox.EventKey {
continue
}
mappedKey := keyMapping[ev.Key]
if mappedKey == "" {
mappedKey = fmt.Sprintf("%c", ev.Ch)
}
} else if ctx.Mode == context.InsertMode {
mappedActionName := ctx.Config.KeyMapping[ctx.Mode][mappedKey]
action := actionMap[mappedActionName]
if action != nil {
action(ctx)
continue
}
if ctx.Mode == context.InsertMode {
switch ev.Key {
case termbox.KeyEsc:
actionCommandMode(ctx)
case termbox.KeyEnter:
actionSend(ctx)
case termbox.KeySpace:
actionInput(ctx.View, ' ')
case termbox.KeyBackspace, termbox.KeyBackspace2:
actionBackSpace(ctx.View)
case termbox.KeyDelete:
actionDelete(ctx.View)
case termbox.KeyArrowRight:
actionMoveCursorRight(ctx.View)
case termbox.KeyArrowLeft:
actionMoveCursorLeft(ctx.View)
default:
actionInput(ctx.View, ev.Ch)
}
}
}
}
}()
}