Update event handlers
* Terminal is now returned to normal when quitting * Fix resize handler * Update structure event handling
This commit is contained in:
parent
38637e3755
commit
1ae769c7ca
@ -40,49 +40,31 @@ var actionMap = map[string]func(*context.AppContext){
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RegisterEventHandlers(ctx *context.AppContext) {
|
func RegisterEventHandlers(ctx *context.AppContext) {
|
||||||
anyKeyHandler(ctx)
|
eventHandler(ctx)
|
||||||
incomingMessageHandler(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() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
ev := <-ctx.EventQueue
|
ev := <-ctx.EventQueue
|
||||||
|
|
||||||
if ev.Type != termbox.EventKey {
|
switch ev.Type {
|
||||||
continue
|
case termbox.EventKey:
|
||||||
}
|
actionKeyEvent(ctx, ev)
|
||||||
|
case termbox.EventResize:
|
||||||
keyStr := getKeyString(ev)
|
actionResizeEvent(ctx, 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 resizeHandler(ctx *context.AppContext) func(termui.Event) {
|
|
||||||
return func(e termui.Event) {
|
|
||||||
actionResize(ctx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func incomingMessageHandler(ctx *context.AppContext) {
|
func incomingMessageHandler(ctx *context.AppContext) {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@ -124,9 +106,30 @@ func incomingMessageHandler(ctx *context.AppContext) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: resize only seems to work for width and resizing it too small
|
func actionKeyEvent(ctx *context.AppContext, ev termbox.Event) {
|
||||||
// will cause termui to panic
|
|
||||||
func actionResize(ctx *context.AppContext) {
|
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.Width = termui.TermWidth()
|
||||||
termui.Body.Align()
|
termui.Body.Align()
|
||||||
termui.Render(termui.Body)
|
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
|
// we won't be able to call termui.StopLoop() on. See main.go
|
||||||
// for the customEvtStream and why this is done.
|
// for the customEvtStream and why this is done.
|
||||||
func actionQuit(ctx *context.AppContext) {
|
func actionQuit(ctx *context.AppContext) {
|
||||||
|
termbox.Close()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
main.go
9
main.go
@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/erroneousboat/slack-term/context"
|
"github.com/erroneousboat/slack-term/context"
|
||||||
"github.com/erroneousboat/slack-term/handlers"
|
"github.com/erroneousboat/slack-term/handlers"
|
||||||
termbox "github.com/nsf/termbox-go"
|
|
||||||
|
|
||||||
"github.com/gizak/termui"
|
"github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
@ -68,7 +67,7 @@ func main() {
|
|||||||
// Create custom event stream for termui because
|
// Create custom event stream for termui because
|
||||||
// termui's one has data race conditions with its
|
// termui's one has data race conditions with its
|
||||||
// event handling. We're circumventing it here until
|
// event handling. We're circumventing it here until
|
||||||
// it has been fix.
|
// it has been fixed.
|
||||||
customEvtStream := &termui.EvtStream{
|
customEvtStream := &termui.EvtStream{
|
||||||
Handlers: make(map[string]func(termui.Event)),
|
Handlers: make(map[string]func(termui.Event)),
|
||||||
}
|
}
|
||||||
@ -97,11 +96,5 @@ func main() {
|
|||||||
// Register handlers
|
// Register handlers
|
||||||
handlers.RegisterEventHandlers(ctx)
|
handlers.RegisterEventHandlers(ctx)
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
ctx.EventQueue <- termbox.PollEvent()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
termui.Loop()
|
termui.Loop()
|
||||||
}
|
}
|
||||||
|
@ -374,9 +374,10 @@ func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent
|
|||||||
// - mentions
|
// - mentions
|
||||||
func parseMessage(s *SlackService, msg string) string {
|
func parseMessage(s *SlackService, msg string) string {
|
||||||
// NOTE: Commented out because rendering of the emoji's
|
// 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
|
// double width emoji's
|
||||||
// msg = parseEmoji(msg)
|
// msg = parseEmoji(msg)
|
||||||
|
|
||||||
msg = parseMentions(s, msg)
|
msg = parseMentions(s, msg)
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
Loading…
x
Reference in New Issue
Block a user