erroneousboat-slack-term/handlers/event.go

419 lines
10 KiB
Go
Raw Normal View History

2016-09-11 17:55:19 +02:00
package handlers
import (
"os"
2016-10-29 17:57:58 +02:00
"strconv"
"time"
2017-09-23 13:56:45 +02:00
"github.com/erroneousboat/termui"
"github.com/nlopes/slack"
termbox "github.com/nsf/termbox-go"
2016-09-11 17:55:19 +02:00
2016-10-19 09:08:31 +02:00
"github.com/erroneousboat/slack-term/context"
"github.com/erroneousboat/slack-term/views"
2016-09-11 17:55:19 +02:00
)
var timer *time.Timer
2016-10-29 17:57:58 +02:00
// actionMap binds specific action names to the function counterparts,
// these action names can then be used to bind them to specific keys
// in the Config.
var actionMap = map[string]func(*context.AppContext){
2016-10-29 17:57:58 +02:00
"space": actionSpace,
"backspace": actionBackSpace,
"delete": actionDelete,
"cursor-right": actionMoveCursorRight,
"cursor-left": actionMoveCursorLeft,
"send": actionSend,
"quit": actionQuit,
2016-10-29 17:57:58 +02:00
"mode-insert": actionInsertMode,
"mode-command": actionCommandMode,
2017-07-15 23:56:39 +02:00
"mode-search": actionSearchMode,
"clear-input": actionClearInput,
"channel-up": actionMoveCursorUpChannels,
"channel-down": actionMoveCursorDownChannels,
"channel-top": actionMoveCursorTopChannels,
"channel-bottom": actionMoveCursorBottomChannels,
"chat-up": actionScrollUpChat,
"chat-down": actionScrollDownChat,
2016-10-30 14:26:12 +01:00
"help": actionHelp,
}
2016-10-29 17:57:58 +02:00
func RegisterEventHandlers(ctx *context.AppContext) {
eventHandler(ctx)
messageHandler(ctx)
2016-10-29 17:57:58 +02:00
}
func eventHandler(ctx *context.AppContext) {
go func() {
for {
ctx.EventQueue <- termbox.PollEvent()
}
}()
go func() {
for {
ev := <-ctx.EventQueue
handleTermboxEvents(ctx, ev)
handleMoreTermboxEvents(ctx, ev)
2017-12-02 15:24:31 +01:00
// Place your debugging statements here
if ctx.Debug {
ctx.View.Debug.Println("hello world")
}
}
}()
}
func handleTermboxEvents(ctx *context.AppContext, ev termbox.Event) bool {
switch ev.Type {
case termbox.EventKey:
actionKeyEvent(ctx, ev)
case termbox.EventResize:
actionResizeEvent(ctx, ev)
}
return true
}
func handleMoreTermboxEvents(ctx *context.AppContext, ev termbox.Event) bool {
for {
select {
2017-09-23 13:56:45 +02:00
case ev := <-ctx.EventQueue:
ok := handleTermboxEvents(ctx, ev)
if !ok {
return false
2016-09-11 17:55:19 +02:00
}
default:
return true
2016-09-11 17:55:19 +02:00
}
}
2016-09-11 17:55:19 +02:00
}
func messageHandler(ctx *context.AppContext) {
2016-09-27 17:12:38 +02:00
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
2016-10-09 14:19:28 +02:00
msg := ctx.Service.CreateMessageFromMessageEvent(ev)
2016-09-27 17:12:38 +02:00
// Add message to the selected channel
2016-09-30 23:52:26 +02:00
if ev.Channel == ctx.Service.Channels[ctx.View.Channels.SelectedChannel].ID {
2016-10-11 18:50:25 +02:00
// reverse order of messages, mainly done
// when attachments are added to message
for i := len(msg) - 1; i >= 0; i-- {
ctx.View.Chat.AddMessage(msg[i])
2016-10-09 14:19:28 +02:00
}
2016-10-11 18:50:25 +02:00
2016-09-27 22:05:44 +02:00
termui.Render(ctx.View.Chat)
2016-09-30 16:36:41 +02:00
2016-10-01 12:48:15 +02:00
// TODO: set Chat.Offset to 0, to automatically scroll
// down?
2016-09-27 17:12:38 +02:00
}
2016-09-30 12:09:03 +02:00
2016-10-01 12:48:15 +02:00
// Set new message indicator for channel, I'm leaving
// this here because I also want to be notified when
// I'm currently in a channel but not in the terminal
// window (tmux). But only create a notification when
// it comes from someone else but the current user.
if ev.User != ctx.Service.CurrentUserID {
actionNewMessage(ctx, ev.Channel)
}
case *slack.PresenceChangeEvent:
actionSetPresence(ctx, ev.User, ev.Presence)
2016-09-27 17:12:38 +02:00
}
}
}
}()
}
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) {
2016-09-11 17:55:19 +02:00
termui.Body.Width = termui.TermWidth()
termui.Body.Align()
termui.Render(termui.Body)
}
2016-10-21 21:23:25 +02:00
func actionInput(view *views.View, key rune) {
2016-09-11 17:55:19 +02:00
view.Input.Insert(key)
termui.Render(view.Input)
}
2017-07-15 23:56:39 +02:00
func actionClearInput(ctx *context.AppContext) {
// Clear input
ctx.View.Input.Clear()
ctx.View.Refresh()
// Set command mode
actionCommandMode(ctx)
}
2016-10-29 17:57:58 +02:00
func actionSpace(ctx *context.AppContext) {
actionInput(ctx.View, ' ')
}
func actionBackSpace(ctx *context.AppContext) {
ctx.View.Input.Backspace()
termui.Render(ctx.View.Input)
2016-10-09 14:59:48 +02:00
}
func actionDelete(ctx *context.AppContext) {
ctx.View.Input.Delete()
termui.Render(ctx.View.Input)
2016-09-11 17:55:19 +02:00
}
func actionMoveCursorRight(ctx *context.AppContext) {
ctx.View.Input.MoveCursorRight()
termui.Render(ctx.View.Input)
2016-09-11 17:55:19 +02:00
}
func actionMoveCursorLeft(ctx *context.AppContext) {
ctx.View.Input.MoveCursorLeft()
termui.Render(ctx.View.Input)
2016-09-11 17:55:19 +02:00
}
func actionSend(ctx *context.AppContext) {
if !ctx.View.Input.IsEmpty() {
// Clear message before sending, to combat
// quick succession of actionSend
2016-10-21 21:23:25 +02:00
message := ctx.View.Input.GetText()
ctx.View.Input.Clear()
ctx.View.Refresh()
2016-09-27 22:05:44 +02:00
ctx.View.Input.SendMessage(
ctx.Service,
2016-09-30 23:52:26 +02:00
ctx.Service.Channels[ctx.View.Channels.SelectedChannel].ID,
message,
2016-09-27 22:05:44 +02:00
)
2016-09-11 17:55:19 +02:00
}
}
2017-07-15 23:56:39 +02:00
func actionSearch(ctx *context.AppContext, key rune) {
go func() {
if timer != nil {
timer.Stop()
}
actionInput(ctx.View, key)
timer = time.NewTimer(time.Second / 4)
<-timer.C
term := ctx.View.Input.GetText()
ctx.View.Channels.Search(term)
actionChangeChannel(ctx)
}()
}
// actionQuit will exit the program by using os.Exit, this is
// done because we are using a custom termui EvtStream. Which
// 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)
2016-09-11 17:55:19 +02:00
}
func actionInsertMode(ctx *context.AppContext) {
ctx.Mode = context.InsertMode
2017-12-02 11:09:01 +01:00
ctx.View.Mode.SetInsertMode()
2016-09-11 17:55:19 +02:00
}
func actionCommandMode(ctx *context.AppContext) {
ctx.Mode = context.CommandMode
2017-12-02 11:09:01 +01:00
ctx.View.Mode.SetCommandMode()
2016-09-11 17:55:19 +02:00
}
2016-09-25 22:34:02 +02:00
2017-07-15 23:56:39 +02:00
func actionSearchMode(ctx *context.AppContext) {
ctx.Mode = context.SearchMode
2017-12-02 11:09:01 +01:00
ctx.View.Mode.SetSearchMode()
2017-07-15 23:56:39 +02:00
}
2016-09-25 22:34:02 +02:00
func actionGetMessages(ctx *context.AppContext) {
2017-12-01 23:52:25 +01:00
messages := ctx.Service.GetMessages(
2016-09-30 23:52:26 +02:00
ctx.Service.Channels[ctx.View.Channels.SelectedChannel],
2017-12-01 23:52:25 +01:00
ctx.View.Chat.GetMaxItems(),
2016-09-27 22:05:44 +02:00
)
2017-12-01 23:52:25 +01:00
ctx.View.Chat.SetMessages(messages)
2016-09-27 22:05:44 +02:00
2016-09-25 22:34:02 +02:00
termui.Render(ctx.View.Chat)
}
2017-12-01 23:52:25 +01:00
// actionMoveCursorUpChannels will execute the actionChangeChannel
// function. A time is implemented to support fast scrolling through
// the list without executing the actionChangeChannel event
2016-09-27 22:05:44 +02:00
func actionMoveCursorUpChannels(ctx *context.AppContext) {
go func() {
if timer != nil {
timer.Stop()
}
ctx.View.Channels.MoveCursorUp()
termui.Render(ctx.View.Channels)
timer = time.NewTimer(time.Second / 4)
<-timer.C
2017-12-01 23:52:25 +01:00
// Only actually change channel when the timer expires
actionChangeChannel(ctx)
}()
2016-09-27 22:05:44 +02:00
}
2017-12-01 23:52:25 +01:00
// actionMoveCursorDownChannels will execute the actionChangeChannel
// function. A time is implemented to support fast scrolling through
// the list without executing the actionChangeChannel event
2016-09-27 22:05:44 +02:00
func actionMoveCursorDownChannels(ctx *context.AppContext) {
go func() {
if timer != nil {
timer.Stop()
}
ctx.View.Channels.MoveCursorDown()
termui.Render(ctx.View.Channels)
timer = time.NewTimer(time.Second / 4)
<-timer.C
2017-12-01 23:52:25 +01:00
// Only actually change channel when the timer expires
actionChangeChannel(ctx)
}()
2016-09-29 19:09:30 +02:00
}
func actionMoveCursorTopChannels(ctx *context.AppContext) {
ctx.View.Channels.MoveCursorTop()
actionChangeChannel(ctx)
}
func actionMoveCursorBottomChannels(ctx *context.AppContext) {
ctx.View.Channels.MoveCursorBottom()
actionChangeChannel(ctx)
}
2016-09-29 19:09:30 +02:00
func actionChangeChannel(ctx *context.AppContext) {
// Clear messages from Chat pane
ctx.View.Chat.ClearMessages()
2016-09-27 22:05:44 +02:00
2017-12-01 23:52:25 +01:00
// Get messages of the SelectedChannel, and get the count of messages
// that fit into the Chat component
messages := ctx.Service.GetMessages(
ctx.Service.GetSlackChannel(ctx.View.Channels.SelectedChannel),
ctx.View.Chat.GetMaxItems(),
2016-09-27 22:05:44 +02:00
)
2017-12-01 23:52:25 +01:00
// Set messages for the channel
ctx.View.Chat.SetMessages(messages)
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.Service.Channels[ctx.View.Channels.SelectedChannel],
2016-09-28 22:10:04 +02:00
)
2017-12-01 23:52:25 +01:00
// Clear notification icon if there is any
ctx.View.Channels.MarkAsRead()
2016-10-29 23:59:16 +02:00
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) {
2017-12-01 23:52:25 +01:00
ctx.View.Channels.MarkAsUnread(ctx.Service.Channels, channelID)
termui.Render(ctx.View.Channels)
}
func actionSetPresence(ctx *context.AppContext, channelID string, presence string) {
2017-12-02 17:35:48 +01:00
ctx.View.Channels.SetPresenceChannelEvent(ctx.Service.Channels, channelID, presence)
2016-09-30 12:09:03 +02:00
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)
}
2016-10-29 17:57:58 +02:00
2016-10-30 14:26:12 +01:00
func actionHelp(ctx *context.AppContext) {
ctx.View.Chat.Help(ctx.Config)
termui.Render(ctx.View.Chat)
}
2016-10-29 17:57:58 +02:00
// GetKeyString will return a string that resembles the key event from
// termbox. This is blatanly copied from termui because it is an unexported
// function.
//
// See:
// - https://github.com/gizak/termui/blob/a7e3aeef4cdf9fa2edb723b1541cb69b7bb089ea/events.go#L31-L72
// - https://github.com/nsf/termbox-go/blob/master/api_common.go
func getKeyString(e termbox.Event) string {
var ek string
k := string(e.Ch)
pre := ""
mod := ""
if e.Mod == termbox.ModAlt {
mod = "M-"
}
if e.Ch == 0 {
if e.Key > 0xFFFF-12 {
k = "<f" + strconv.Itoa(0xFFFF-int(e.Key)+1) + ">"
} else if e.Key > 0xFFFF-25 {
ks := []string{"<insert>", "<delete>", "<home>", "<end>", "<previous>", "<next>", "<up>", "<down>", "<left>", "<right>"}
k = ks[0xFFFF-int(e.Key)-12]
}
if e.Key <= 0x7F {
pre = "C-"
k = string('a' - 1 + int(e.Key))
kmap := map[termbox.Key][2]string{
termbox.KeyCtrlSpace: {"C-", "<space>"},
termbox.KeyBackspace: {"", "<backspace>"},
termbox.KeyTab: {"", "<tab>"},
termbox.KeyEnter: {"", "<enter>"},
termbox.KeyEsc: {"", "<escape>"},
termbox.KeyCtrlBackslash: {"C-", "\\"},
termbox.KeyCtrlSlash: {"C-", "/"},
termbox.KeySpace: {"", "<space>"},
termbox.KeyCtrl8: {"C-", "8"},
}
if sk, ok := kmap[e.Key]; ok {
pre = sk[0]
k = sk[1]
}
}
}
ek = pre + mod + k
return ek
}