Merge branch 'stdin'

This commit is contained in:
erroneousboat 2016-10-15 20:57:12 +02:00
commit 606db0f50e

View File

@ -3,67 +3,73 @@ package handlers
import ( import (
"github.com/gizak/termui" "github.com/gizak/termui"
"github.com/nlopes/slack" "github.com/nlopes/slack"
termbox "github.com/nsf/termbox-go"
"github.com/erroneousboat/slack-term/src/context" "github.com/erroneousboat/slack-term/src/context"
"github.com/erroneousboat/slack-term/src/views" "github.com/erroneousboat/slack-term/src/views"
) )
func RegisterEventHandlers(ctx *context.AppContext) { func RegisterEventHandlers(ctx *context.AppContext) {
termui.Handle("/sys/kbd/", anyKeyHandler(ctx)) anyKeyHandler(ctx)
termui.Handle("/sys/wnd/resize", resizeHandler(ctx))
incomingMessageHandler(ctx) incomingMessageHandler(ctx)
termui.Handle("/sys/wnd/resize", resizeHandler(ctx))
} }
func anyKeyHandler(ctx *context.AppContext) func(termui.Event) { func anyKeyHandler(ctx *context.AppContext) {
return func(e termui.Event) { go func() {
key := e.Data.(termui.EvtKbd).KeyStr for {
ev := termbox.PollEvent()
if ctx.Mode == context.CommandMode { if ev.Type == termbox.EventKey {
switch key { if ctx.Mode == context.CommandMode {
case "q": switch ev.Key {
actionQuit() case termbox.KeyPgup:
case "j": actionScrollUpChat(ctx)
actionMoveCursorDownChannels(ctx) case termbox.KeyCtrlB:
case "k": actionScrollUpChat(ctx)
actionMoveCursorUpChannels(ctx) case termbox.KeyCtrlU:
case "i": actionScrollUpChat(ctx)
actionInsertMode(ctx) case termbox.KeyPgdn:
case "<previous>": actionScrollDownChat(ctx)
actionScrollUpChat(ctx) case termbox.KeyCtrlF:
case "C-b": actionScrollDownChat(ctx)
actionScrollUpChat(ctx) case termbox.KeyCtrlD:
case "C-u": actionScrollDownChat(ctx)
actionScrollUpChat(ctx) default:
case "<next>": switch ev.Ch {
actionScrollDownChat(ctx) case 'q':
case "C-f": actionQuit()
actionScrollDownChat(ctx) case 'j':
case "C-d": actionMoveCursorDownChannels(ctx)
actionScrollDownChat(ctx) case 'k':
} actionMoveCursorUpChannels(ctx)
} else if ctx.Mode == context.InsertMode { case 'i':
switch key { actionInsertMode(ctx)
case "<escape>": }
actionCommandMode(ctx) }
case "<enter>": } else if ctx.Mode == context.InsertMode {
actionSend(ctx) switch ev.Key {
case "<space>": case termbox.KeyEsc:
actionInput(ctx.View, " ") actionCommandMode(ctx)
case "<backspace>": case termbox.KeyEnter:
actionBackSpace(ctx.View) actionSend(ctx)
case "C-8": case termbox.KeySpace:
actionBackSpace(ctx.View) actionInput(ctx.View, " ")
case "<delete>": case termbox.KeyBackspace, termbox.KeyBackspace2:
actionDelete(ctx.View) actionBackSpace(ctx.View)
case "<right>": case termbox.KeyDelete:
actionMoveCursorRight(ctx.View) actionDelete(ctx.View)
case "<left>": case termbox.KeyArrowRight:
actionMoveCursorLeft(ctx.View) actionMoveCursorRight(ctx.View)
default: case termbox.KeyArrowLeft:
actionInput(ctx.View, key) actionMoveCursorLeft(ctx.View)
default:
actionInput(ctx.View, string(ev.Ch))
}
}
} }
} }
} }()
} }
func resizeHandler(ctx *context.AppContext) func(termui.Event) { func resizeHandler(ctx *context.AppContext) func(termui.Event) {