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 ev.Type == termbox.EventKey {
if ctx.Mode == context.CommandMode { if ctx.Mode == context.CommandMode {
switch key { switch ev.Key {
case "q": 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() actionQuit()
case "j": case 'j':
actionMoveCursorDownChannels(ctx) actionMoveCursorDownChannels(ctx)
case "k": case 'k':
actionMoveCursorUpChannels(ctx) actionMoveCursorUpChannels(ctx)
case "i": case 'i':
actionInsertMode(ctx) actionInsertMode(ctx)
case "<previous>": }
actionScrollUpChat(ctx)
case "C-b":
actionScrollUpChat(ctx)
case "C-u":
actionScrollUpChat(ctx)
case "<next>":
actionScrollDownChat(ctx)
case "C-f":
actionScrollDownChat(ctx)
case "C-d":
actionScrollDownChat(ctx)
} }
} else if ctx.Mode == context.InsertMode { } else if ctx.Mode == context.InsertMode {
switch key { switch ev.Key {
case "<escape>": case termbox.KeyEsc:
actionCommandMode(ctx) actionCommandMode(ctx)
case "<enter>": case termbox.KeyEnter:
actionSend(ctx) actionSend(ctx)
case "<space>": case termbox.KeySpace:
actionInput(ctx.View, " ") actionInput(ctx.View, " ")
case "<backspace>": case termbox.KeyBackspace, termbox.KeyBackspace2:
actionBackSpace(ctx.View) actionBackSpace(ctx.View)
case "C-8": case termbox.KeyDelete:
actionBackSpace(ctx.View)
case "<delete>":
actionDelete(ctx.View) actionDelete(ctx.View)
case "<right>": case termbox.KeyArrowRight:
actionMoveCursorRight(ctx.View) actionMoveCursorRight(ctx.View)
case "<left>": case termbox.KeyArrowLeft:
actionMoveCursorLeft(ctx.View) actionMoveCursorLeft(ctx.View)
default: default:
actionInput(ctx.View, key) actionInput(ctx.View, string(ev.Ch))
} }
} }
} }
}
}()
} }
func resizeHandler(ctx *context.AppContext) func(termui.Event) { func resizeHandler(ctx *context.AppContext) func(termui.Event) {