diff --git a/context/context.go b/context/context.go index 573d058..490a888 100644 --- a/context/context.go +++ b/context/context.go @@ -11,7 +11,7 @@ import ( ) const ( - CommandMode = "command" + CommandMode = "normal" InsertMode = "insert" ) diff --git a/handlers/event.go b/handlers/event.go index 44bdf19..454ab93 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -1,6 +1,8 @@ package handlers import ( + "fmt" + "github.com/gizak/termui" "github.com/nlopes/slack" termbox "github.com/nsf/termbox-go" @@ -53,56 +55,28 @@ 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) - } - } - } 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) - } + if ev.Type != termbox.EventKey { + continue + } + + mappedKey := keyMapping[ev.Key] + if mappedKey == "" { + mappedKey = fmt.Sprintf("%c", ev.Ch) + } + + 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.KeySpace: + actionInput(ctx.View, ' ') + default: + actionInput(ctx.View, ev.Ch) } } }