| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- package main
- import (
- "bytes"
- "log"
- "time"
- "github.com/atotto/clipboard"
- "github.com/ayn2op/discordo/discordmd"
- "github.com/diamondburned/arikawa/v3/discord"
- "github.com/gdamore/tcell/v2"
- "github.com/rivo/tview"
- )
- const replyIndicator = '╭'
- type MessagesText struct {
- *tview.TextView
- selectedMessage int
- buf bytes.Buffer
- }
- func newMessagesText() *MessagesText {
- mt := &MessagesText{
- TextView: tview.NewTextView(),
- selectedMessage: -1,
- }
- mt.SetDynamicColors(true)
- mt.SetRegions(true)
- mt.SetWordWrap(true)
- mt.SetInputCapture(mt.onInputCapture)
- mt.ScrollToEnd()
- mt.SetChangedFunc(func() {
- app.Draw()
- })
- mt.SetBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor))
- mt.SetTitle("Messages")
- mt.SetTitleColor(tcell.GetColor(config.Theme.TitleColor))
- mt.SetTitleAlign(tview.AlignLeft)
- p := config.Theme.BorderPadding
- mt.SetBorder(config.Theme.Border)
- mt.SetBorderPadding(p[0], p[1], p[2], p[3])
- return mt
- }
- func (mt *MessagesText) reset() {
- messagesText.selectedMessage = -1
- mt.SetTitle("")
- mt.Clear()
- mt.Highlight()
- }
- func (mt *MessagesText) createMessage(m *discord.Message) error {
- switch m.Type {
- case discord.DefaultMessage, discord.InlinedReplyMessage:
- // Region tags are square brackets that contain a region ID in double quotes
- // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
- mt.buf.WriteString(`["`)
- mt.buf.WriteString(m.ID.String())
- mt.buf.WriteString(`"]`)
- if m.ReferencedMessage != nil {
- mt.buf.WriteString("[::d] ")
- mt.buf.WriteRune(replyIndicator)
- mt.buf.WriteByte(' ')
- mt.buf.WriteByte('[')
- mt.buf.WriteString(config.Theme.MessagesText.AuthorColor)
- mt.buf.WriteByte(']')
- mt.buf.WriteString(m.ReferencedMessage.Author.Username)
- mt.buf.WriteString("[-] ")
- mt.buf.WriteString(discordmd.Parse(tview.Escape(m.ReferencedMessage.Content)))
- mt.buf.WriteString("[::-]\n")
- }
- mt.createHeader(m)
- mt.createBody(m)
- mt.createFooter(m)
- // Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region.
- mt.buf.WriteString(`[""]`)
- mt.buf.WriteByte('\n')
- }
- _, err := mt.buf.WriteTo(mt)
- return err
- }
- func (mt *MessagesText) createHeader(m *discord.Message) {
- mt.buf.WriteByte('[')
- mt.buf.WriteString(config.Theme.MessagesText.AuthorColor)
- mt.buf.WriteByte(']')
- mt.buf.WriteString(m.Author.Username)
- mt.buf.WriteString("[-] ")
- if config.Timestamps {
- mt.buf.WriteString("[::d]")
- mt.buf.WriteString(m.Timestamp.Format(time.Kitchen))
- mt.buf.WriteString("[::-] ")
- }
- }
- func (mt *MessagesText) createBody(m *discord.Message) {
- mt.buf.WriteString(discordmd.Parse(tview.Escape(m.Content)))
- }
- func (mt *MessagesText) createFooter(m *discord.Message) {
- for _, a := range m.Attachments {
- mt.buf.WriteByte('\n')
- mt.buf.WriteByte('[')
- mt.buf.WriteString(a.Filename)
- mt.buf.WriteString("]: ")
- mt.buf.WriteString(a.URL)
- }
- }
- func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
- switch event.Name() {
- case config.Keys.MessagesText.CopyContent:
- mt.copyContentAction()
- return nil
- case config.Keys.MessagesText.Reply:
- mt.replyAction(false)
- return nil
- case config.Keys.MessagesText.ReplyMention:
- mt.replyAction(true)
- return nil
- case config.Keys.MessagesText.SelectPrevious:
- mt.selectPreviousAction()
- return nil
- case config.Keys.MessagesText.SelectNext:
- mt.selectNextAction()
- return nil
- case config.Keys.MessagesText.SelectFirst:
- mt.selectFirstAction()
- return nil
- case config.Keys.MessagesText.SelectLast:
- mt.selectLastAction()
- return nil
- case config.Keys.MessagesText.SelectReply:
- mt.selectReplyAction()
- return nil
- case config.Keys.Cancel:
- guildsTree.selectedChannel = nil
- messagesText.reset()
- messageInput.reset()
- return nil
- }
- return event
- }
- func (mt *MessagesText) replyAction(mention bool) {
- if mt.selectedMessage == -1 {
- return
- }
- var title string
- if mention {
- title += "[@] Replying to "
- } else {
- title += "Replying to "
- }
- ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
- if err != nil {
- log.Println(err)
- return
- }
- title += ms[mt.selectedMessage].Author.Tag()
- messageInput.SetTitle(title)
- app.SetFocus(messageInput)
- }
- func (mt *MessagesText) selectPreviousAction() {
- ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
- if err != nil {
- log.Println(err)
- return
- }
- // If no message is currently selected, select the latest message.
- if len(mt.GetHighlights()) == 0 {
- mt.selectedMessage = 0
- } else {
- if mt.selectedMessage < len(ms)-1 {
- mt.selectedMessage++
- }
- }
- mt.Highlight(ms[mt.selectedMessage].ID.String())
- mt.ScrollToHighlight()
- }
- func (mt *MessagesText) selectNextAction() {
- ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
- if err != nil {
- log.Println(err)
- return
- }
- // If no message is currently selected, select the latest message.
- if len(mt.GetHighlights()) == 0 {
- mt.selectedMessage = 0
- } else {
- if mt.selectedMessage > 0 {
- mt.selectedMessage--
- }
- }
- mt.Highlight(ms[mt.selectedMessage].ID.String())
- mt.ScrollToHighlight()
- }
- func (mt *MessagesText) selectFirstAction() {
- ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
- if err != nil {
- log.Println(err)
- return
- }
- mt.selectedMessage = len(ms) - 1
- mt.Highlight(ms[mt.selectedMessage].ID.String())
- mt.ScrollToHighlight()
- }
- func (mt *MessagesText) selectLastAction() {
- ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
- if err != nil {
- log.Println(err)
- return
- }
- mt.selectedMessage = 0
- mt.Highlight(ms[mt.selectedMessage].ID.String())
- mt.ScrollToHighlight()
- }
- func (mt *MessagesText) selectReplyAction() {
- if mt.selectedMessage == -1 {
- return
- }
- ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
- if err != nil {
- log.Println(err)
- return
- }
- ref := ms[mt.selectedMessage].ReferencedMessage
- if ref != nil {
- for i, m := range ms {
- if ref.ID == m.ID {
- mt.selectedMessage = i
- }
- }
- mt.Highlight(ms[mt.selectedMessage].ID.String())
- mt.ScrollToHighlight()
- }
- }
- func (mt *MessagesText) copyContentAction() {
- ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
- if err != nil {
- log.Println(err)
- return
- }
- err = clipboard.WriteAll(ms[mt.selectedMessage].Content)
- if err != nil {
- log.Println(err)
- return
- }
- }
|