erroneousboat-slack-term/src/handlers/event.go

221 lines
4.9 KiB
Go
Raw Normal View History

2016-09-11 17:55:19 +02:00
package handlers
import (
"github.com/gizak/termui"
2016-09-25 22:34:02 +02:00
"github.com/nlopes/slack"
2016-09-11 17:55:19 +02:00
"github.com/erroneousboat/slack-term/src/context"
"github.com/erroneousboat/slack-term/src/views"
)
func RegisterEventHandlers(ctx *context.AppContext) {
termui.Handle("/sys/kbd/", anyKeyHandler(ctx))
termui.Handle("/sys/wnd/resize", resizeHandler(ctx))
2016-09-25 22:34:02 +02:00
termui.Handle("/timer/1s", timeHandler(ctx))
2016-09-27 17:12:38 +02:00
incomingMessageHandler(ctx)
2016-09-11 17:55:19 +02:00
}
func anyKeyHandler(ctx *context.AppContext) func(termui.Event) {
return func(e termui.Event) {
key := e.Data.(termui.EvtKbd).KeyStr
if ctx.Mode == context.CommandMode {
switch key {
case "q":
actionQuit()
return
2016-09-27 22:05:44 +02:00
case "j":
actionMoveCursorDownChannels(ctx)
return
case "k":
actionMoveCursorUpChannels(ctx)
return
2016-09-11 17:55:19 +02:00
case "i":
actionInsertMode(ctx)
return
2016-09-30 16:36:41 +02:00
case "<previous>":
actionScrollUpChat(ctx)
case "<next>":
actionScrollDownChat(ctx)
2016-09-11 17:55:19 +02:00
}
} else if ctx.Mode == context.InsertMode {
switch key {
case "<escape>":
actionCommandMode(ctx)
return
case "<enter>":
actionSend(ctx)
return
case "<space>":
actionInput(ctx.View, " ")
return
case "<backspace>":
actionBackSpace(ctx.View)
case "C-8":
actionBackSpace(ctx.View)
case "<right>":
actionMoveCursorRight(ctx.View)
case "<left>":
actionMoveCursorLeft(ctx.View)
default:
actionInput(ctx.View, key)
return
}
}
}
}
func resizeHandler(ctx *context.AppContext) func(termui.Event) {
return func(e termui.Event) {
actionResize(ctx)
}
}
2016-09-25 22:34:02 +02:00
func timeHandler(ctx *context.AppContext) func(termui.Event) {
return func(e termui.Event) {
}
}
2016-09-27 17:12:38 +02:00
func incomingMessageHandler(ctx *context.AppContext) {
go func() {
for {
select {
case msg := <-ctx.Service.RTM.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
2016-09-29 21:19:09 +02:00
// Construct message
m := ctx.Service.CreateMessageFromMessageEvent(ev)
2016-09-27 17:12:38 +02:00
// Add message to the selected channel
2016-09-27 22:05:44 +02:00
if ev.Channel == ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].ID {
2016-09-27 17:12:38 +02:00
ctx.View.Chat.AddMessage(m)
2016-09-27 22:05:44 +02:00
termui.Render(ctx.View.Chat)
2016-09-30 16:36:41 +02:00
// TODO: set Chat.Offset to 0?
2016-09-27 17:12:38 +02:00
}
2016-09-30 12:09:03 +02:00
// Set new message indicator for channel
actionNewMessage(ctx, ev.Channel)
2016-09-27 17:12:38 +02:00
}
}
}
}()
}
2016-09-11 17:55:19 +02:00
// FIXME: resize only seems to work for width and resizing it too small
// will cause termui to panic
func actionResize(ctx *context.AppContext) {
termui.Body.Width = termui.TermWidth()
termui.Body.Align()
termui.Render(termui.Body)
}
func actionInput(view *views.View, key string) {
view.Input.Insert(key)
termui.Render(view.Input)
}
func actionBackSpace(view *views.View) {
view.Input.Remove()
termui.Render(view.Input)
}
func actionMoveCursorRight(view *views.View) {
view.Input.MoveCursorRight()
termui.Render(view.Input)
}
func actionMoveCursorLeft(view *views.View) {
view.Input.MoveCursorLeft()
termui.Render(view.Input)
}
func actionSend(ctx *context.AppContext) {
if !ctx.View.Input.IsEmpty() {
2016-09-27 22:05:44 +02:00
ctx.View.Input.SendMessage(
ctx.Service,
ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].ID,
ctx.View.Input.Text(),
)
2016-09-11 17:55:19 +02:00
ctx.View.Input.Clear()
ctx.View.Refresh()
}
}
func actionQuit() {
termui.StopLoop()
}
func actionInsertMode(ctx *context.AppContext) {
ctx.Mode = context.InsertMode
2016-09-25 22:34:02 +02:00
ctx.View.Mode.Par.Text = "INSERT"
2016-09-11 17:55:19 +02:00
termui.Render(ctx.View.Mode)
}
func actionCommandMode(ctx *context.AppContext) {
ctx.Mode = context.CommandMode
2016-09-25 22:34:02 +02:00
ctx.View.Mode.Par.Text = "NORMAL"
2016-09-11 17:55:19 +02:00
termui.Render(ctx.View.Mode)
}
2016-09-25 22:34:02 +02:00
func actionGetMessages(ctx *context.AppContext) {
2016-09-27 22:05:44 +02:00
ctx.View.Chat.GetMessages(
ctx.Service,
ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].ID,
)
2016-09-25 22:34:02 +02:00
termui.Render(ctx.View.Chat)
}
func actionGetChannels(ctx *context.AppContext) {
ctx.View.Channels.GetChannels(ctx.Service)
termui.Render(ctx.View.Channels)
}
2016-09-27 22:05:44 +02:00
func actionMoveCursorUpChannels(ctx *context.AppContext) {
ctx.View.Channels.MoveCursorUp()
2016-09-29 19:09:30 +02:00
actionChangeChannel(ctx)
2016-09-27 22:05:44 +02:00
}
func actionMoveCursorDownChannels(ctx *context.AppContext) {
ctx.View.Channels.MoveCursorDown()
2016-09-29 19:09:30 +02:00
actionChangeChannel(ctx)
}
func actionChangeChannel(ctx *context.AppContext) {
// Clear messages from Chat pane
ctx.View.Chat.ClearMessages()
2016-09-27 22:05:44 +02:00
2016-09-29 19:09:30 +02:00
// Get message for the new channel
2016-09-27 22:05:44 +02:00
ctx.View.Chat.GetMessages(
ctx.Service,
ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].ID,
)
2016-09-29 19:09:30 +02:00
// Set channel name for the Chat pane
2016-09-28 22:10:04 +02:00
ctx.View.Chat.SetBorderLabel(
ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].Name,
)
2016-09-27 22:05:44 +02:00
termui.Render(ctx.View.Channels)
termui.Render(ctx.View.Chat)
}
2016-09-30 12:09:03 +02:00
func actionNewMessage(ctx *context.AppContext, channelID string) {
ctx.View.Channels.NewMessage(channelID)
termui.Render(ctx.View.Channels)
}
2016-09-30 16:36:41 +02:00
func actionScrollUpChat(ctx *context.AppContext) {
ctx.View.Chat.ScrollUp()
termui.Render(ctx.View.Chat)
}
func actionScrollDownChat(ctx *context.AppContext) {
ctx.View.Chat.ScrollDown()
termui.Render(ctx.View.Chat)
}