Преглед на файлове

refactor: divide util package

ayntgl преди 4 години
родител
ревизия
bddfb173b7
променени са 8 файла, в които са добавени 53 реда и са изтрити 54 реда
  1. 26 26
      config/mod.go
  2. 1 1
      discord/mod.go
  3. 3 3
      main.go
  4. 3 3
      ui/app.go
  5. 4 5
      ui/builder.go
  6. 9 9
      ui/handlers.go
  7. 4 4
      ui/util.go
  8. 3 3
      ui/util_test.go

+ 26 - 26
util/config.go → config/mod.go

@@ -1,4 +1,4 @@
-package util
+package config
 
 import (
 	"os"
@@ -35,6 +35,30 @@ type Config struct {
 	General     GeneralConfig     `toml:"general"`
 }
 
+func NewConfig() *Config {
+	return &Config{
+		General: GeneralConfig{
+			UserAgent:          userAgent,
+			FetchMessagesLimit: 50,
+			Mouse:              true,
+			Timestamps:         false,
+		},
+		Keybindings: KeybindingsConfig{
+			ToggleGuildsList:         "Rune[g]",
+			ToggleChannelsTreeView:   "Rune[c]",
+			ToggleMessagesTextView:   "Rune[m]",
+			ToggleMessageInputField:  "Rune[i]",
+			ToggleMessageActionsList: "Rune[a]",
+			ToggleExternalEditor:     "Ctrl-E",
+
+			SelectPreviousMessage: "Up",
+			SelectNextMessage:     "Down",
+			SelectFirstMessage:    "Home",
+			SelectLastMessage:     "End",
+		},
+	}
+}
+
 func LoadConfig() *Config {
 	configPath, err := os.UserConfigDir()
 	if err != nil {
@@ -57,7 +81,7 @@ func LoadConfig() *Config {
 		}
 		defer f.Close()
 
-		c = newDefaultConfig()
+		c = NewConfig()
 		err = toml.NewEncoder(f).Encode(c)
 		if err != nil {
 			panic(err)
@@ -71,27 +95,3 @@ func LoadConfig() *Config {
 
 	return c
 }
-
-func newDefaultConfig() *Config {
-	return &Config{
-		General: GeneralConfig{
-			UserAgent:          userAgent,
-			FetchMessagesLimit: 50,
-			Mouse:              true,
-			Timestamps:         false,
-		},
-		Keybindings: KeybindingsConfig{
-			ToggleGuildsList:         "Rune[g]",
-			ToggleChannelsTreeView:   "Rune[c]",
-			ToggleMessagesTextView:   "Rune[m]",
-			ToggleMessageInputField:  "Rune[i]",
-			ToggleMessageActionsList: "Rune[a]",
-			ToggleExternalEditor:     "Ctrl-E",
-
-			SelectPreviousMessage: "Up",
-			SelectNextMessage:     "Down",
-			SelectFirstMessage:    "Home",
-			SelectLastMessage:     "End",
-		},
-	}
-}

+ 1 - 1
util/discord.go → discord/mod.go

@@ -1,4 +1,4 @@
-package util
+package discord
 
 import (
 	"encoding/json"

+ 3 - 3
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)
 					}

+ 3 - 3
ui/app.go

@@ -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,
 	}
 }

+ 4 - 5
ui/builder.go

@@ -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 - 9
ui/handlers.go

@@ -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(),

+ 4 - 4
util/ui.go → ui/util.go

@@ -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

+ 3 - 3
util/ui_test.go → ui/util_test.go

@@ -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)
 	}
 }