Directly write to io.Writer using fmt.Fprint*
This commit is contained in:
parent
016ce8d601
commit
50d09cbd0a
3 changed files with 23 additions and 52 deletions
|
|
@ -178,11 +178,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
}
|
||||
|
||||
for i := len(ms) - 1; i >= 0; i-- {
|
||||
err = messagesText.createMessage(&ms[i])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
messagesText.createMessage(&ms[i])
|
||||
}
|
||||
|
||||
app.SetFocus(messageInput)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
|
|
@ -18,7 +19,6 @@ type MessagesText struct {
|
|||
*tview.TextView
|
||||
|
||||
selectedMessage int
|
||||
buf bytes.Buffer
|
||||
}
|
||||
|
||||
func newMessagesText() *MessagesText {
|
||||
|
|
@ -59,69 +59,48 @@ func (mt *MessagesText) reset() {
|
|||
mt.Highlight()
|
||||
}
|
||||
|
||||
func (mt *MessagesText) createMessage(m *discord.Message) error {
|
||||
func (mt *MessagesText) createMessage(m *discord.Message) {
|
||||
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(`"]`)
|
||||
fmt.Fprintf(mt, `["%s"]`, m.ID)
|
||||
|
||||
if m.ReferencedMessage != nil {
|
||||
mt.buf.WriteString("[::d] ")
|
||||
mt.buf.WriteRune(replyIndicator)
|
||||
mt.buf.WriteByte(' ')
|
||||
fmt.Fprintf(mt, "[::d]%c ", replyIndicator)
|
||||
|
||||
mt.buf.WriteByte('[')
|
||||
mt.buf.WriteString(cfg.Theme.MessagesText.AuthorColor)
|
||||
mt.buf.WriteByte(']')
|
||||
mt.buf.WriteString(m.ReferencedMessage.Author.Username)
|
||||
mt.buf.WriteString("[-] ")
|
||||
mt.createHeader(mt, m.ReferencedMessage)
|
||||
mt.createBody(mt, m.ReferencedMessage)
|
||||
|
||||
mt.buf.WriteString(discordmd.Parse(tview.Escape(m.ReferencedMessage.Content)))
|
||||
mt.buf.WriteString("[::-]\n")
|
||||
fmt.Fprint(mt, "[::-]\n")
|
||||
}
|
||||
|
||||
mt.createHeader(m)
|
||||
mt.createBody(m)
|
||||
mt.createFooter(m)
|
||||
mt.createHeader(mt, m)
|
||||
mt.createBody(mt, m)
|
||||
mt.createFooter(mt, 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')
|
||||
fmt.Fprint(mt, `[""]`)
|
||||
fmt.Fprintln(mt)
|
||||
}
|
||||
|
||||
_, err := mt.buf.WriteTo(mt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (mt *MessagesText) createHeader(m *discord.Message) {
|
||||
mt.buf.WriteByte('[')
|
||||
mt.buf.WriteString(cfg.Theme.MessagesText.AuthorColor)
|
||||
mt.buf.WriteByte(']')
|
||||
mt.buf.WriteString(m.Author.Username)
|
||||
mt.buf.WriteString("[-] ")
|
||||
func (mt *MessagesText) createHeader(w io.Writer, m *discord.Message) {
|
||||
fmt.Fprintf(w, "[%s]%s[-] ", cfg.Theme.MessagesText.AuthorColor, m.Author.Username)
|
||||
|
||||
if cfg.Timestamps {
|
||||
mt.buf.WriteString("[::d]")
|
||||
mt.buf.WriteString(m.Timestamp.Format(time.Kitchen))
|
||||
mt.buf.WriteString("[::-] ")
|
||||
fmt.Fprintf(w, "[::d]%s[::-] ", m.Timestamp.Format(time.Kitchen))
|
||||
}
|
||||
}
|
||||
|
||||
func (mt *MessagesText) createBody(m *discord.Message) {
|
||||
mt.buf.WriteString(discordmd.Parse(tview.Escape(m.Content)))
|
||||
func (mt *MessagesText) createBody(w io.Writer, m *discord.Message) {
|
||||
fmt.Fprint(w, discordmd.Parse(tview.Escape(m.Content)))
|
||||
}
|
||||
|
||||
func (mt *MessagesText) createFooter(m *discord.Message) {
|
||||
func (mt *MessagesText) createFooter(w io.Writer, 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)
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintf(w, "[%s]: %s", a.Filename, a.URL)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
6
state.go
6
state.go
|
|
@ -59,10 +59,6 @@ func (s *State) onReady(r *gateway.ReadyEvent) {
|
|||
|
||||
func (s *State) onMessageCreate(m *gateway.MessageCreateEvent) {
|
||||
if guildsTree.selectedChannel != nil && guildsTree.selectedChannel.ID == m.ChannelID {
|
||||
err := messagesText.createMessage(&m.Message)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
messagesText.createMessage(&m.Message)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue