refactor: divide util package
This commit is contained in:
parent
a7d243aa3a
commit
bddfb173b7
8 changed files with 66 additions and 67 deletions
|
|
@ -1,4 +1,4 @@
|
|||
package util
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
|
@ -35,44 +35,7 @@ type Config struct {
|
|||
General GeneralConfig `toml:"general"`
|
||||
}
|
||||
|
||||
func LoadConfig() *Config {
|
||||
configPath, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
configPath += "/discordo/config.toml"
|
||||
// Create a directory as well as create all of the nested directories, recursively.
|
||||
err = os.MkdirAll(filepath.Dir(configPath), os.ModePerm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c := &Config{}
|
||||
// If the configuration file does not exist, create and write the default configuration to the file.
|
||||
if _, err = os.Stat(configPath); os.IsNotExist(err) {
|
||||
f, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
c = newDefaultConfig()
|
||||
err = toml.NewEncoder(f).Encode(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
_, err = toml.DecodeFile(configPath, c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func newDefaultConfig() *Config {
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
General: GeneralConfig{
|
||||
UserAgent: userAgent,
|
||||
|
|
@ -95,3 +58,40 @@ func newDefaultConfig() *Config {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
func LoadConfig() *Config {
|
||||
configPath, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
configPath += "/discordo/config.toml"
|
||||
// Create a directory as well as create all of the nested directories, recursively.
|
||||
err = os.MkdirAll(filepath.Dir(configPath), os.ModePerm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c := &Config{}
|
||||
// If the configuration file does not exist, create and write the default configuration to the file.
|
||||
if _, err = os.Stat(configPath); os.IsNotExist(err) {
|
||||
f, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
c = NewConfig()
|
||||
err = toml.NewEncoder(f).Encode(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
_, err = toml.DecodeFile(configPath, c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package util
|
||||
package discord
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
6
main.go
6
main.go
|
|
@ -3,8 +3,8 @@ package main
|
|||
import (
|
||||
"os"
|
||||
|
||||
"github.com/ayntgl/discordo/discord"
|
||||
"github.com/ayntgl/discordo/ui"
|
||||
"github.com/ayntgl/discordo/util"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/zalando/go-keyring"
|
||||
)
|
||||
|
|
@ -39,7 +39,7 @@ func main() {
|
|||
}
|
||||
|
||||
// Login using the email and password
|
||||
lr, err := util.Login(app.Session, email, password)
|
||||
lr, err := discord.Login(app.Session, email, password)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
lr, err = util.TOTP(app.Session, mfaCode, lr.Ticket)
|
||||
lr, err = discord.TOTP(app.Session, mfaCode, lr.Ticket)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/ayntgl/discordgo"
|
||||
"github.com/ayntgl/discordo/util"
|
||||
"github.com/ayntgl/discordo/config"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
|
|
@ -18,7 +18,7 @@ type App struct {
|
|||
MessageInputField *tview.InputField
|
||||
Session *discordgo.Session
|
||||
SelectedChannel *discordgo.Channel
|
||||
Config *util.Config
|
||||
Config *config.Config
|
||||
SelectedMessage int
|
||||
}
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ func NewApp() *App {
|
|||
MessageInputField: tview.NewInputField(),
|
||||
|
||||
Session: s,
|
||||
Config: util.LoadConfig(),
|
||||
Config: config.LoadConfig(),
|
||||
SelectedMessage: -1,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/ayntgl/discordgo"
|
||||
"github.com/ayntgl/discordo/util"
|
||||
)
|
||||
|
||||
func buildMessage(app *App, m *discordgo.Message) []byte {
|
||||
|
|
@ -89,7 +88,7 @@ func buildReferencedMessage(b *strings.Builder, rm *discordgo.Message, clientID
|
|||
|
||||
if rm.Content != "" {
|
||||
rm.Content = buildMentions(rm.Content, rm.Mentions, clientID)
|
||||
b.WriteString(util.ParseMarkdown(rm.Content))
|
||||
b.WriteString(parseMarkdown(rm.Content))
|
||||
}
|
||||
|
||||
b.WriteString("[::-]")
|
||||
|
|
@ -100,7 +99,7 @@ func buildReferencedMessage(b *strings.Builder, rm *discordgo.Message, clientID
|
|||
func buildContent(b *strings.Builder, m *discordgo.Message, clientID string) {
|
||||
if m.Content != "" {
|
||||
m.Content = buildMentions(m.Content, m.Mentions, clientID)
|
||||
b.WriteString(util.ParseMarkdown(m.Content))
|
||||
b.WriteString(parseMarkdown(m.Content))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -138,7 +137,7 @@ func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) {
|
|||
embedBuilder.WriteString("\n\n")
|
||||
}
|
||||
|
||||
embedBuilder.WriteString(util.ParseMarkdown(e.Description))
|
||||
embedBuilder.WriteString(parseMarkdown(e.Description))
|
||||
}
|
||||
|
||||
if len(e.Fields) != 0 {
|
||||
|
|
@ -151,7 +150,7 @@ func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) {
|
|||
embedBuilder.WriteString(ef.Name)
|
||||
embedBuilder.WriteString("[::-]")
|
||||
embedBuilder.WriteByte('\n')
|
||||
embedBuilder.WriteString(util.ParseMarkdown(ef.Value))
|
||||
embedBuilder.WriteString(parseMarkdown(ef.Value))
|
||||
|
||||
if i != len(e.Fields)-1 {
|
||||
embedBuilder.WriteString("\n\n")
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
"github.com/atotto/clipboard"
|
||||
"github.com/ayntgl/discordgo"
|
||||
"github.com/ayntgl/discordo/util"
|
||||
"github.com/ayntgl/discordo/discord"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
|
@ -59,7 +59,7 @@ func onGuildsListSelected(app *App, guildIdx int) {
|
|||
})
|
||||
|
||||
for _, c := range cs {
|
||||
channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)).
|
||||
channelTreeNode := tview.NewTreeNode(channelToString(c)).
|
||||
SetReference(c.ID)
|
||||
rootTreeNode.AddChild(channelTreeNode)
|
||||
}
|
||||
|
|
@ -71,7 +71,7 @@ func onGuildsListSelected(app *App, guildIdx int) {
|
|||
|
||||
for _, c := range cs {
|
||||
if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID == "") {
|
||||
channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)).
|
||||
channelTreeNode := tview.NewTreeNode(channelToString(c)).
|
||||
SetReference(c.ID)
|
||||
rootTreeNode.AddChild(channelTreeNode)
|
||||
}
|
||||
|
|
@ -108,7 +108,7 @@ func onGuildsListSelected(app *App, guildIdx int) {
|
|||
})
|
||||
|
||||
if parentTreeNode != nil {
|
||||
channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)).
|
||||
channelTreeNode := tview.NewTreeNode(channelToString(c)).
|
||||
SetReference(c.ID)
|
||||
parentTreeNode.AddChild(channelTreeNode)
|
||||
}
|
||||
|
|
@ -139,7 +139,7 @@ func onChannelsTreeViewSelected(app *App, n *tview.TreeNode) {
|
|||
|
||||
app.SelectedChannel = c
|
||||
|
||||
app.MessagesTextView.SetTitle(util.ChannelToString(c))
|
||||
app.MessagesTextView.SetTitle(channelToString(c))
|
||||
app.SetFocus(app.MessageInputField)
|
||||
|
||||
go func() {
|
||||
|
|
@ -223,12 +223,12 @@ func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey
|
|||
return nil
|
||||
}
|
||||
|
||||
_, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
|
||||
_, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0])
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if util.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionSendMessages) {
|
||||
if discord.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionSendMessages) {
|
||||
messageActionsList.
|
||||
AddItem("Reply", "", 'r', nil).
|
||||
AddItem("Mention Reply", "", 'R', nil)
|
||||
|
|
@ -286,7 +286,7 @@ func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Messag
|
|||
SetRoot(app.MainFlex, false).
|
||||
SetFocus(app.MessageInputField)
|
||||
case "Select Reply":
|
||||
app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
|
||||
app.SelectedMessage, _ = discord.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
|
||||
app.MessagesTextView.
|
||||
Highlight(m.ReferencedMessage.ID).
|
||||
ScrollToHighlight()
|
||||
|
|
@ -309,7 +309,7 @@ func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKe
|
|||
}
|
||||
|
||||
if len(app.MessagesTextView.GetHighlights()) != 0 {
|
||||
_, m := util.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
|
||||
_, m := discord.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
|
||||
d := &discordgo.MessageSend{
|
||||
Content: t,
|
||||
Reference: m.Reference(),
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package util
|
||||
package ui
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
|
@ -14,7 +14,7 @@ var (
|
|||
strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`)
|
||||
)
|
||||
|
||||
func ParseMarkdown(md string) string {
|
||||
func parseMarkdown(md string) string {
|
||||
var res string
|
||||
res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
|
||||
res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]")
|
||||
|
|
@ -24,7 +24,7 @@ func ParseMarkdown(md string) string {
|
|||
return res
|
||||
}
|
||||
|
||||
func ChannelToString(c *discordgo.Channel) string {
|
||||
func channelToString(c *discordgo.Channel) string {
|
||||
var repr string
|
||||
if c.Name != "" {
|
||||
repr = "#" + c.Name
|
||||
|
|
@ -43,7 +43,7 @@ func ChannelToString(c *discordgo.Channel) string {
|
|||
return repr
|
||||
}
|
||||
|
||||
func HasKeybinding(ks []string, k string) bool {
|
||||
func hasKeybinding(ks []string, k string) bool {
|
||||
for _, repr := range ks {
|
||||
if repr == k {
|
||||
return true
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package util
|
||||
package ui
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
|
@ -18,7 +18,7 @@ func TestParseMarkdown(t *testing.T) {
|
|||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := ParseMarkdown(test.in); got != test.want {
|
||||
if got := parseMarkdown(test.in); got != test.want {
|
||||
t.Errorf("got: %s\nwant: %s", got, test.want)
|
||||
}
|
||||
})
|
||||
|
|
@ -31,6 +31,6 @@ func BenchmarkParseMarkdown(b *testing.B) {
|
|||
Sit commodi sed iure et sed quae eveniet. *Sit non distinctio nihil sunt. Nesciunt cumque aspernatur *nulla* porro et earum quidem.* Sed omnis at commodi vel quasi. Fuga et **consequatur** molestias dicta vel provident et aspernatur. Dolorem molestias ipsa aut ~~facilis quae dolorem~~ eveniet dicta.`
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
ParseMarkdown(in)
|
||||
parseMarkdown(in)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue