rewrite
This commit is contained in:
parent
456ce6739c
commit
07d431a219
4 changed files with 79 additions and 29 deletions
25
config.go
25
config.go
|
|
@ -10,13 +10,27 @@ const name = "discordo"
|
|||
|
||||
type ThemeConfig struct {
|
||||
BorderPadding [4]int
|
||||
MessageFormat string
|
||||
}
|
||||
|
||||
type GuildsTreeKeysConfig struct {
|
||||
Graphics bool
|
||||
}
|
||||
|
||||
type MessagesTextKeysConfig struct {
|
||||
SelectPreviousMessage string
|
||||
}
|
||||
|
||||
type KeysConfig struct {
|
||||
GuildsTree GuildsTreeKeysConfig
|
||||
MessagesText MessagesTextKeysConfig
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Mouse bool
|
||||
MessagesLimit uint
|
||||
Theme ThemeConfig
|
||||
|
||||
Theme ThemeConfig
|
||||
Keys KeysConfig
|
||||
}
|
||||
|
||||
func newConfig() (*Config, error) {
|
||||
|
|
@ -36,9 +50,10 @@ func newConfig() (*Config, error) {
|
|||
|
||||
Theme: ThemeConfig{
|
||||
BorderPadding: [...]int{1, 1, 1, 1},
|
||||
MessageFormat: `{{.Author.Username}}
|
||||
{{.Content}}
|
||||
`,
|
||||
},
|
||||
Keys: KeysConfig{
|
||||
GuildsTree: GuildsTreeKeysConfig{Graphics: true},
|
||||
MessagesText: MessagesTextKeysConfig{SelectPreviousMessage: "Up"},
|
||||
},
|
||||
}
|
||||
path = filepath.Join(path, "config.json")
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -4,12 +4,12 @@ go 1.19
|
|||
|
||||
require (
|
||||
github.com/diamondburned/arikawa/v3 v3.1.0
|
||||
github.com/gdamore/tcell/v2 v2.5.3
|
||||
github.com/rivo/tview v0.0.0-20221229180733-b86a50a5126c
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gdamore/encoding v1.0.0 // indirect
|
||||
github.com/gdamore/tcell/v2 v2.5.3 // indirect
|
||||
github.com/gorilla/schema v1.2.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
"github.com/rivo/tview"
|
||||
|
|
@ -21,6 +22,7 @@ func newGuildsTree() *GuildsTree {
|
|||
root: tview.NewTreeNode(""),
|
||||
}
|
||||
|
||||
gt.SetGraphics(cfg.Keys.GuildsTree.Graphics)
|
||||
gt.SetRoot(gt.root)
|
||||
gt.SetTopLevel(1)
|
||||
gt.SetSelectedFunc(gt.onSelected)
|
||||
|
|
@ -68,6 +70,7 @@ func (gt *GuildsTree) channelToString(c discord.Channel) string {
|
|||
func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
||||
if len(n.GetChildren()) != 0 {
|
||||
n.SetExpanded(!n.IsExpanded())
|
||||
return
|
||||
}
|
||||
|
||||
switch ref := n.GetReference().(type) {
|
||||
|
|
@ -78,6 +81,10 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
return
|
||||
}
|
||||
|
||||
sort.Slice(cs, func(i, j int) bool {
|
||||
return cs[i].Position < cs[j].Position
|
||||
})
|
||||
|
||||
// Orphan (top-level) channels
|
||||
for _, c := range cs {
|
||||
if c.Type != discord.GuildCategory && !c.ParentID.IsValid() {
|
||||
|
|
@ -102,7 +109,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
|
||||
// Children (category-bound) channels
|
||||
for _, c := range cs {
|
||||
if c.ParentID.IsValid() {
|
||||
if c.Type != discord.GuildCategory && c.ParentID.IsValid() {
|
||||
var parent *tview.TreeNode
|
||||
n.Walk(func(node, _ *tview.TreeNode) bool {
|
||||
if node.GetReference() == c.ParentID {
|
||||
|
|
@ -133,8 +140,8 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
return
|
||||
}
|
||||
|
||||
for _, m := range ms {
|
||||
if messagesText.newMessage(&m); err != nil {
|
||||
for i := len(ms) - 1; i >= 0; i-- {
|
||||
if messagesText.newMessage(&ms[i]); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"text/template"
|
||||
"bytes"
|
||||
"time"
|
||||
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
var p = sync.Pool{
|
||||
New: func() any {
|
||||
return new(template.Template)
|
||||
},
|
||||
}
|
||||
|
||||
type MessagesText struct {
|
||||
*tview.TextView
|
||||
|
||||
buffer bytes.Buffer
|
||||
}
|
||||
|
||||
func newMessagesText() *MessagesText {
|
||||
|
|
@ -26,7 +21,9 @@ func newMessagesText() *MessagesText {
|
|||
|
||||
mt.SetDynamicColors(true)
|
||||
mt.SetRegions(true)
|
||||
mt.SetWordWrap(true)
|
||||
|
||||
mt.SetTitle("Messages")
|
||||
mt.SetBorder(true)
|
||||
mt.SetBorderPadding(cfg.BorderPadding())
|
||||
|
||||
|
|
@ -35,24 +32,55 @@ func newMessagesText() *MessagesText {
|
|||
|
||||
func (mt *MessagesText) newMessage(m *discord.Message) error {
|
||||
switch m.Type {
|
||||
case discord.DefaultMessage:
|
||||
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
|
||||
fmt.Fprintf(mt, `["%s"]`, m.ID)
|
||||
mt.buffer.WriteString(`["`)
|
||||
mt.buffer.WriteString(m.ID.String())
|
||||
mt.buffer.WriteString(`"]`)
|
||||
|
||||
if m.ReferencedMessage != nil {
|
||||
fmt.Fprintf(mt, "[blue::bd]%s[-:-:-] [::-]", m.ReferencedMessage.Author.Username)
|
||||
fmt.Fprint(mt, m.ReferencedMessage.Content)
|
||||
fmt.Fprintln(mt)
|
||||
mt.buffer.WriteString("> [::d]")
|
||||
|
||||
// Author
|
||||
mt.newAuthor(m.ReferencedMessage)
|
||||
// Content
|
||||
mt.newContent(m.ReferencedMessage)
|
||||
|
||||
mt.buffer.WriteString("[::-]")
|
||||
mt.buffer.WriteByte('\n')
|
||||
}
|
||||
|
||||
fmt.Fprintf(mt, "[blue::b]%s[-:-:-] ", m.Author.Username)
|
||||
fmt.Fprint(mt, m.Content)
|
||||
// Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region.
|
||||
fmt.Fprint(mt, `[""]`)
|
||||
// Author
|
||||
mt.newAuthor(m)
|
||||
// Timestamps
|
||||
mt.newTimestamp(m)
|
||||
// Content
|
||||
mt.buffer.WriteByte('\n')
|
||||
mt.newContent(m)
|
||||
|
||||
fmt.Fprintln(mt)
|
||||
// Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region.
|
||||
mt.buffer.WriteString(`[""]`)
|
||||
}
|
||||
|
||||
return nil
|
||||
mt.buffer.WriteString("\n\n")
|
||||
|
||||
_, err := mt.buffer.WriteTo(mt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (mt *MessagesText) newAuthor(m *discord.Message) {
|
||||
mt.buffer.WriteString("[blue]")
|
||||
mt.buffer.WriteString(m.Author.Username)
|
||||
mt.buffer.WriteString("[-] ")
|
||||
}
|
||||
|
||||
func (mt *MessagesText) newTimestamp(m *discord.Message) {
|
||||
mt.buffer.WriteString("[::d]")
|
||||
mt.buffer.WriteString(m.Timestamp.Format(time.Kitchen))
|
||||
mt.buffer.WriteString("[-:-:-] ")
|
||||
}
|
||||
|
||||
func (mt *MessagesText) newContent(m *discord.Message) {
|
||||
mt.buffer.WriteString(m.Content)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue