| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package ui
- import (
- "sort"
- "strings"
- "github.com/atotto/clipboard"
- "github.com/ayntgl/discordgo"
- "github.com/ayntgl/discordo/config"
- "github.com/ayntgl/discordo/util"
- "github.com/gdamore/tcell/v2"
- "github.com/rivo/tview"
- )
- func onAppInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
- if hasKeybinding(config.Keybindings.FocusChannelsTreeView, e.Name()) {
- app.SetFocus(app.ChannelsTreeView)
- return nil
- } else if hasKeybinding(config.Keybindings.FocusMessagesView, e.Name()) {
- app.SetFocus(app.MessagesTextView)
- return nil
- } else if hasKeybinding(config.Keybindings.FocusMessageInputField, e.Name()) {
- app.SetFocus(app.MessageInputField)
- return nil
- }
- return e
- }
- func onGuildsListSelected(app *App, guildIdx int) {
- rootTreeNode := app.ChannelsTreeView.GetRoot()
- rootTreeNode.ClearChildren()
- app.MessagesTextView.
- Highlight().
- Clear().
- SetTitle("")
- app.MessageInputField.SetText("")
- if guildIdx == 0 { // Direct Messages
- cs := app.Session.State.PrivateChannels
- sort.Slice(cs, func(i, j int) bool {
- return cs[i].LastMessageID > cs[j].LastMessageID
- })
- for _, c := range cs {
- channelTreeNode := tview.NewTreeNode(channelToString(c)).
- SetReference(c.ID)
- rootTreeNode.AddChild(channelTreeNode)
- }
- } else { // Guild
- cs := app.Session.State.Guilds[guildIdx-1].Channels
- sort.Slice(cs, func(i, j int) bool {
- return cs[i].Position < cs[j].Position
- })
- for _, c := range cs {
- if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID == "") {
- channelTreeNode := tview.NewTreeNode(channelToString(c)).
- SetReference(c.ID)
- rootTreeNode.AddChild(channelTreeNode)
- }
- }
- CATEGORY:
- for _, c := range cs {
- if c.Type == discordgo.ChannelTypeGuildCategory {
- for _, nestedChannel := range cs {
- if nestedChannel.ParentID == c.ID {
- channelTreeNode := tview.NewTreeNode(c.Name).
- SetReference(c.ID)
- rootTreeNode.AddChild(channelTreeNode)
- continue CATEGORY
- }
- }
- channelTreeNode := tview.NewTreeNode(c.Name).
- SetReference(c.ID)
- rootTreeNode.AddChild(channelTreeNode)
- }
- }
- for _, c := range cs {
- if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID != "") {
- var parentTreeNode *tview.TreeNode
- rootTreeNode.Walk(func(node, _ *tview.TreeNode) bool {
- if node.GetReference() == c.ParentID {
- parentTreeNode = node
- return false
- }
- return true
- })
- if parentTreeNode != nil {
- channelTreeNode := tview.NewTreeNode(channelToString(c)).
- SetReference(c.ID)
- parentTreeNode.AddChild(channelTreeNode)
- }
- }
- }
- }
- app.ChannelsTreeView.SetCurrentNode(rootTreeNode)
- app.SetFocus(app.ChannelsTreeView)
- }
- func onChannelsTreeViewSelected(app *App, n *tview.TreeNode) {
- c, err := app.Session.State.Channel(n.GetReference().(string))
- if err != nil {
- return
- }
- if c.Type == discordgo.ChannelTypeGuildCategory {
- n.SetExpanded(!n.IsExpanded())
- return
- }
- app.SelectedChannel = c
- app.MessagesTextView.SetTitle(channelToString(c))
- app.SetFocus(app.MessageInputField)
- go func() {
- ms, err := app.Session.ChannelMessages(c.ID, config.General.FetchMessagesLimit, "", "", "")
- if err != nil {
- return
- }
- for i := len(ms) - 1; i >= 0; i-- {
- app.SelectedChannel.Messages = ms
- app.MessagesTextView.Write(buildMessage(app, ms[i]))
- }
- app.MessagesTextView.ScrollToEnd()
- }()
- }
- func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
- if app.SelectedChannel == nil {
- return nil
- }
- ms := app.SelectedChannel.Messages
- if len(ms) == 0 {
- return nil
- }
- if hasKeybinding(config.Keybindings.SelectPreviousMessage, e.Name()) {
- if len(app.MessagesTextView.GetHighlights()) == 0 {
- app.SelectedMessage = len(ms) - 1
- } else {
- app.SelectedMessage--
- if app.SelectedMessage < 0 {
- app.SelectedMessage = 0
- }
- }
- app.MessagesTextView.
- Highlight(ms[app.SelectedMessage].ID).
- ScrollToHighlight()
- return nil
- } else if hasKeybinding(config.Keybindings.SelectNextMessage, e.Name()) {
- if len(app.MessagesTextView.GetHighlights()) == 0 {
- app.SelectedMessage = len(ms) - 1
- } else {
- app.SelectedMessage++
- if app.SelectedMessage >= len(ms) {
- app.SelectedMessage = len(ms) - 1
- }
- }
- app.MessagesTextView.
- Highlight(ms[app.SelectedMessage].ID).
- ScrollToHighlight()
- return nil
- } else if hasKeybinding(config.Keybindings.SelectFirstMessage, e.Name()) {
- app.SelectedMessage = 0
- app.MessagesTextView.
- Highlight(ms[app.SelectedMessage].ID).
- ScrollToHighlight()
- return nil
- } else if hasKeybinding(config.Keybindings.SelectLastMessage, e.Name()) {
- app.SelectedMessage = len(ms) - 1
- app.MessagesTextView.
- Highlight(ms[app.SelectedMessage].ID).
- ScrollToHighlight()
- return nil
- } else if hasKeybinding(config.Keybindings.SelectMessageReference, e.Name()) {
- hs := app.MessagesTextView.GetHighlights()
- if len(hs) == 0 {
- return nil
- }
- _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
- if m.ReferencedMessage != nil {
- app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
- app.MessagesTextView.
- Highlight(m.ReferencedMessage.ID).
- ScrollToHighlight()
- }
- return nil
- } else if hasKeybinding(config.Keybindings.ReplySelectedMessage, e.Name()) {
- hs := app.MessagesTextView.GetHighlights()
- if len(hs) == 0 {
- return nil
- }
- _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
- app.MessageInputField.SetTitle("Replying to " + m.Author.String())
- app.SetFocus(app.MessageInputField)
- return nil
- } else if hasKeybinding(config.Keybindings.MentionReplySelectedMessage, e.Name()) {
- hs := app.MessagesTextView.GetHighlights()
- if len(hs) == 0 {
- return nil
- }
- _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
- app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
- app.SetFocus(app.MessageInputField)
- return nil
- } else if hasKeybinding(config.Keybindings.CopySelectedMessage, e.Name()) {
- hs := app.MessagesTextView.GetHighlights()
- if len(hs) == 0 {
- return nil
- }
- _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
- err := clipboard.WriteAll(m.Content)
- if err != nil {
- return nil
- }
- return nil
- }
- return e
- }
- func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
- switch e.Key() {
- case tcell.KeyEnter:
- if app.SelectedChannel == nil {
- return nil
- }
- t := strings.TrimSpace(app.MessageInputField.GetText())
- if t == "" {
- return nil
- }
- if len(app.MessagesTextView.GetHighlights()) != 0 {
- _, m := util.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
- d := &discordgo.MessageSend{
- Content: t,
- Reference: m.Reference(),
- AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
- }
- if strings.HasPrefix(app.MessageInputField.GetTitle(), "[@]") {
- d.AllowedMentions.RepliedUser = true
- } else {
- d.AllowedMentions.RepliedUser = false
- }
- go app.Session.ChannelMessageSendComplex(m.ChannelID, d)
- app.SelectedMessage = -1
- app.MessagesTextView.Highlight()
- app.MessageInputField.SetTitle("")
- } else {
- go app.Session.ChannelMessageSend(app.SelectedChannel.ID, t)
- }
- app.MessageInputField.SetText("")
- return nil
- case tcell.KeyCtrlV:
- text, _ := clipboard.ReadAll()
- text = app.MessageInputField.GetText() + text
- app.MessageInputField.SetText(text)
- return nil
- case tcell.KeyEscape:
- app.MessageInputField.SetText("")
- app.MessageInputField.SetTitle("")
- app.SelectedMessage = -1
- app.MessagesTextView.Highlight()
- return nil
- }
- return e
- }
|