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 ( const (
CommandMode = "command" CommandMode = "normal"
InsertMode = "insert" InsertMode = "insert"
) )

View File

@ -1,6 +1,8 @@
package handlers package handlers
import ( import (
"fmt"
"github.com/gizak/termui" "github.com/gizak/termui"
"github.com/nlopes/slack" "github.com/nlopes/slack"
termbox "github.com/nsf/termbox-go" termbox "github.com/nsf/termbox-go"
@ -53,56 +55,28 @@ func anyKeyHandler(ctx *context.AppContext) {
for { for {
ev := termbox.PollEvent() ev := termbox.PollEvent()
if ev.Type == termbox.EventKey { if ev.Type != termbox.EventKey {
if ctx.Mode == context.CommandMode { continue
switch ev.Key { }
case termbox.KeyPgup:
actionScrollUpChat(ctx) mappedKey := keyMapping[ev.Key]
case termbox.KeyCtrlB: if mappedKey == "" {
actionScrollUpChat(ctx) mappedKey = fmt.Sprintf("%c", ev.Ch)
case termbox.KeyCtrlU: }
actionScrollUpChat(ctx)
case termbox.KeyPgdn: mappedActionName := ctx.Config.KeyMapping[ctx.Mode][mappedKey]
actionScrollDownChat(ctx) action := actionMap[mappedActionName]
case termbox.KeyCtrlF: if action != nil {
actionScrollDownChat(ctx) action(ctx)
case termbox.KeyCtrlD: continue
actionScrollDownChat(ctx) }
default:
switch ev.Ch { if ctx.Mode == context.InsertMode {
case 'q': switch ev.Key {
actionQuit() case termbox.KeySpace:
case 'j': actionInput(ctx.View, ' ')
actionMoveCursorDownChannels(ctx) default:
case 'k': actionInput(ctx.View, ev.Ch)
actionMoveCursorUpChannels(ctx)
case 'g':
actionMoveCursorTopChannels(ctx)
case 'G':
actionMoveCursorBottomChannels(ctx)
case 'i':
actionInsertMode(ctx)
}
}
} else 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)
}
} }
} }
} }