Update event handlers

* Terminal is now returned to normal when quitting
* Fix resize handler
* Update structure event handling
This commit is contained in:
erroneousboat 2017-07-30 15:23:47 +02:00
parent 38637e3755
commit 1ae769c7ca
3 changed files with 41 additions and 43 deletions

View File

@ -40,49 +40,31 @@ var actionMap = map[string]func(*context.AppContext){
}
func RegisterEventHandlers(ctx *context.AppContext) {
anyKeyHandler(ctx)
eventHandler(ctx)
incomingMessageHandler(ctx)
termui.Handle("/sys/wnd/resize", resizeHandler(ctx))
}
func anyKeyHandler(ctx *context.AppContext) {
func eventHandler(ctx *context.AppContext) {
go func() {
for {
ctx.EventQueue <- termbox.PollEvent()
}
}()
go func() {
for {
ev := <-ctx.EventQueue
if ev.Type != termbox.EventKey {
continue
}
keyStr := getKeyString(ev)
// Get the action name (actionStr) from the key that
// has been pressed. If this is found try to uncover
// the associated function with this key and execute
// it.
actionStr, ok := ctx.Config.KeyMap[ctx.Mode][keyStr]
if ok {
action, ok := actionMap[actionStr]
if ok {
action(ctx)
}
} else {
if ctx.Mode == context.InsertMode && ev.Ch != 0 {
actionInput(ctx.View, ev.Ch)
} else if ctx.Mode == context.SearchMode && ev.Ch != 0 {
actionSearch(ctx, ev.Ch)
}
switch ev.Type {
case termbox.EventKey:
actionKeyEvent(ctx, ev)
case termbox.EventResize:
actionResizeEvent(ctx, ev)
}
}
}()
}
func resizeHandler(ctx *context.AppContext) func(termui.Event) {
return func(e termui.Event) {
actionResize(ctx)
}
}
func incomingMessageHandler(ctx *context.AppContext) {
go func() {
for {
@ -124,9 +106,30 @@ func incomingMessageHandler(ctx *context.AppContext) {
}()
}
// FIXME: resize only seems to work for width and resizing it too small
// will cause termui to panic
func actionResize(ctx *context.AppContext) {
func actionKeyEvent(ctx *context.AppContext, ev termbox.Event) {
keyStr := getKeyString(ev)
// Get the action name (actionStr) from the key that
// has been pressed. If this is found try to uncover
// the associated function with this key and execute
// it.
actionStr, ok := ctx.Config.KeyMap[ctx.Mode][keyStr]
if ok {
action, ok := actionMap[actionStr]
if ok {
action(ctx)
}
} else {
if ctx.Mode == context.InsertMode && ev.Ch != 0 {
actionInput(ctx.View, ev.Ch)
} else if ctx.Mode == context.SearchMode && ev.Ch != 0 {
actionSearch(ctx, ev.Ch)
}
}
}
func actionResizeEvent(ctx *context.AppContext, ev termbox.Event) {
termui.Body.Width = termui.TermWidth()
termui.Body.Align()
termui.Render(termui.Body)
@ -209,6 +212,7 @@ func actionSearch(ctx *context.AppContext, key rune) {
// we won't be able to call termui.StopLoop() on. See main.go
// for the customEvtStream and why this is done.
func actionQuit(ctx *context.AppContext) {
termbox.Close()
os.Exit(0)
}

View File

@ -9,7 +9,6 @@ import (
"github.com/erroneousboat/slack-term/context"
"github.com/erroneousboat/slack-term/handlers"
termbox "github.com/nsf/termbox-go"
"github.com/gizak/termui"
)
@ -68,7 +67,7 @@ func main() {
// Create custom event stream for termui because
// termui's one has data race conditions with its
// event handling. We're circumventing it here until
// it has been fix.
// it has been fixed.
customEvtStream := &termui.EvtStream{
Handlers: make(map[string]func(termui.Event)),
}
@ -97,11 +96,5 @@ func main() {
// Register handlers
handlers.RegisterEventHandlers(ctx)
go func() {
for {
ctx.EventQueue <- termbox.PollEvent()
}
}()
termui.Loop()
}

View File

@ -374,9 +374,10 @@ func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent
// - mentions
func parseMessage(s *SlackService, msg string) string {
// NOTE: Commented out because rendering of the emoji's
// create artifacts from the last view because of
// creates artifacts from the last view because of
// double width emoji's
// msg = parseEmoji(msg)
msg = parseMentions(s, msg)
return msg