From 1ae769c7cac3edb50974654c2a913d139ab024c0 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sun, 30 Jul 2017 15:23:47 +0200 Subject: [PATCH] Update event handlers * Terminal is now returned to normal when quitting * Fix resize handler * Update structure event handling --- handlers/event.go | 72 +++++++++++++++++++++++++---------------------- main.go | 9 +----- service/slack.go | 3 +- 3 files changed, 41 insertions(+), 43 deletions(-) diff --git a/handlers/event.go b/handlers/event.go index 38aef8d..c7a0fe7 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -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) } diff --git a/main.go b/main.go index 0f46945..59329f0 100644 --- a/main.go +++ b/main.go @@ -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() } diff --git a/service/slack.go b/service/slack.go index 5a2494b..fbadc16 100644 --- a/service/slack.go +++ b/service/slack.go @@ -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