<commit_before>package handlers

import (
	"github.com/gizak/termui"
	"github.com/nlopes/slack"

	"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))
	incomingMessageHandler(ctx)
}

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()
			case "j":
				actionMoveCursorDownChannels(ctx)
			case "k":
				actionMoveCursorUpChannels(ctx)
			case "i":
				actionInsertMode(ctx)
			case "<previous>":
				actionScrollUpChat(ctx)
			case "C-b":
				actionScrollUpChat(ctx)
			case "C-u":
				actionScrollUpChat(ctx)
			case "<next>":
				actionScrollDownChat(ctx)
			case "C-f":
				actionScrollDownChat(ctx)
			case "C-d":
				actionScrollDownChat(ctx)
			}
		} else if ctx.Mode == context.InsertMode {
			switch key {
			case "<escape>":
				actionCommandMode(ctx)
			case "<enter>":
				actionSend(ctx)
			case "<space>":
				actionInput(ctx.View, " ")
			case "<backspace>":
				actionBackSpace(ctx.View)
			case "C-8":
				actionBackSpace(ctx.View)
			case "<delete>":
				actionDelete(ctx.View)
			case "<right>":
				actionMoveCursorRight(ctx.View)
			case "<left>":
				actionMoveCursorLeft(ctx.View)
			default:
				actionInput(ctx.View, key)
			}
		}
	}
}

func resizeHandler(ctx *context.AppContext) func(termui.Event) {
	return func(e termui.Event) {
		actionResize(ctx)
	}
}

func incomingMessageHandler(ctx *context.AppContext) {
	go func() {
		for {
			select {
			case msg := <-ctx.Service.RTM.IncomingEvents:
				switch ev := msg.Data.(type) {
				case *slack.MessageEvent:

					// Construct message
					msg := ctx.Service.CreateMessageFromMessageEvent(ev)

					// Add message to the selected channel
					if ev.Channel == ctx.Service.Channels[ctx.View.Channels.SelectedChannel].ID {
						for _, m := range msg {
							ctx.View.Chat.AddMessage(m)
						}
						termui.Render(ctx.View.Chat)

						// TODO: set Chat.Offset to 0, to automatically scroll
						// down?<commit_msg>Reverse order for message events<commit_after>package handlers

import (
	"github.com/gizak/termui"
	"github.com/nlopes/slack"

	"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))
	incomingMessageHandler(ctx)
}

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()
			case "j":
				actionMoveCursorDownChannels(ctx)
			case "k":
				actionMoveCursorUpChannels(ctx)
			case "i":
				actionInsertMode(ctx)
			case "<previous>":
				actionScrollUpChat(ctx)
			case "C-b":
				actionScrollUpChat(ctx)
			case "C-u":
				actionScrollUpChat(ctx)
			case "<next>":
				actionScrollDownChat(ctx)
			case "C-f":
				actionScrollDownChat(ctx)
			case "C-d":
				actionScrollDownChat(ctx)
			}
		} else if ctx.Mode == context.InsertMode {
			switch key {
			case "<escape>":
				actionCommandMode(ctx)
			case "<enter>":
				actionSend(ctx)
			case "<space>":
				actionInput(ctx.View, " ")
			case "<backspace>":
				actionBackSpace(ctx.View)
			case "C-8":
				actionBackSpace(ctx.View)
			case "<delete>":
				actionDelete(ctx.View)
			case "<right>":
				actionMoveCursorRight(ctx.View)
			case "<left>":
				actionMoveCursorLeft(ctx.View)
			default:
				actionInput(ctx.View, key)
			}
		}
	}
}

func resizeHandler(ctx *context.AppContext) func(termui.Event) {
	return func(e termui.Event) {
		actionResize(ctx)
	}
}

func incomingMessageHandler(ctx *context.AppContext) {
	go func() {
		for {
			select {
			case msg := <-ctx.Service.RTM.IncomingEvents:
				switch ev := msg.Data.(type) {
				case *slack.MessageEvent:

					// Construct message
					msg := ctx.Service.CreateMessageFromMessageEvent(ev)

					// Add message to the selected channel
					if ev.Channel == ctx.Service.Channels[ctx.View.Channels.SelectedChannel].ID {

						// 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])
						}

						termui.Render(ctx.View.Chat)

						// TODO: set Chat.Offset to 0, to automatically scroll
						// down?
{'fraction_non_alphanumeric': 0.1381156316916488, 'fraction_numerical': 0.001284796573875803, 'mean_word_length': 3.309040590405904, 'pattern_counts': {'":': 36, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1740149', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1634, 'n_words': 304}