refactor: reorganize packages
This commit is contained in:
parent
1264e60d04
commit
00173ab95f
12 changed files with 147 additions and 133 deletions
17
config/general.go
Normal file
17
config/general.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package config
|
||||
|
||||
type GeneralConfig struct {
|
||||
UserAgent string `toml:"user_agent"`
|
||||
FetchMessagesLimit int `toml:"fetch_messages_limit"`
|
||||
Mouse bool `toml:"mouse"`
|
||||
Timestamps bool `toml:"timestamps"`
|
||||
}
|
||||
|
||||
func newGeneralConfig() GeneralConfig {
|
||||
return GeneralConfig{
|
||||
UserAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
|
||||
FetchMessagesLimit: 50,
|
||||
Mouse: true,
|
||||
Timestamps: false,
|
||||
}
|
||||
}
|
||||
31
config/keybindings.go
Normal file
31
config/keybindings.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package config
|
||||
|
||||
type KeybindingsConfig struct {
|
||||
ToggleGuildsList string `toml:"toggle_guilds_list"`
|
||||
ToggleChannelsTreeView string `toml:"toggle_channels_tree_view"`
|
||||
ToggleMessagesTextView string `toml:"toggle_messages_text_view"`
|
||||
ToggleMessageInputField string `toml:"toggle_message_input_field"`
|
||||
ToggleMessageActionsList string `toml:"toggle_message_actions_list"`
|
||||
ToggleExternalEditor string `toml:"toggle_external_editor"`
|
||||
|
||||
SelectPreviousMessage string `toml:"select_previous_message"`
|
||||
SelectNextMessage string `toml:"select_next_message"`
|
||||
SelectFirstMessage string `toml:"select_first_message"`
|
||||
SelectLastMessage string `toml:"select_last_message"`
|
||||
}
|
||||
|
||||
func newKeybindingsConfig() KeybindingsConfig {
|
||||
return 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",
|
||||
}
|
||||
}
|
||||
|
|
@ -7,91 +7,56 @@ import (
|
|||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
const userAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0"
|
||||
|
||||
type GeneralConfig struct {
|
||||
UserAgent string `toml:"user_agent"`
|
||||
FetchMessagesLimit int `toml:"fetch_messages_limit"`
|
||||
Mouse bool `toml:"mouse"`
|
||||
Timestamps bool `toml:"timestamps"`
|
||||
}
|
||||
|
||||
type KeybindingsConfig struct {
|
||||
ToggleGuildsList string `toml:"toggle_guilds_list"`
|
||||
ToggleChannelsTreeView string `toml:"toggle_channels_tree_view"`
|
||||
ToggleMessagesTextView string `toml:"toggle_messages_text_view"`
|
||||
ToggleMessageInputField string `toml:"toggle_message_input_field"`
|
||||
ToggleMessageActionsList string `toml:"toggle_message_actions_list"`
|
||||
ToggleExternalEditor string `toml:"toggle_external_editor"`
|
||||
|
||||
SelectPreviousMessage string `toml:"select_previous_message"`
|
||||
SelectNextMessage string `toml:"select_next_message"`
|
||||
SelectFirstMessage string `toml:"select_first_message"`
|
||||
SelectLastMessage string `toml:"select_last_message"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Keybindings KeybindingsConfig `toml:"keybindings"`
|
||||
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 newConfig() Config {
|
||||
return Config{
|
||||
General: newGeneralConfig(),
|
||||
Keybindings: newKeybindingsConfig(),
|
||||
}
|
||||
}
|
||||
|
||||
func LoadConfig() *Config {
|
||||
configPath, err := os.UserConfigDir()
|
||||
func NewConfig() *Config {
|
||||
path, 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)
|
||||
path += "/discordo/config.toml"
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(path), 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)
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var c Config
|
||||
if _, err = f.Stat(); os.IsNotExist(err) {
|
||||
f, err = os.Create(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
c = NewConfig()
|
||||
c = newConfig()
|
||||
err = toml.NewEncoder(f).Encode(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
_, err = toml.DecodeFile(configPath, c)
|
||||
_, err = toml.NewDecoder(f).Decode(&c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return c
|
||||
return &c
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,25 +6,6 @@ import (
|
|||
"github.com/ayntgl/discordgo"
|
||||
)
|
||||
|
||||
func FindMessageByID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
|
||||
for i, m := range ms {
|
||||
if m.ID == mID {
|
||||
return i, m
|
||||
}
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func HasPermission(s *discordgo.State, cID string, p int64) bool {
|
||||
perm, err := s.UserChannelPermissions(s.User.ID, cID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return perm&p == p
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
Ticket string `json:"ticket"`
|
||||
Token string `json:"token"`
|
||||
20
discord/markdown.go
Normal file
20
discord/markdown.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package discord
|
||||
|
||||
import "regexp"
|
||||
|
||||
var (
|
||||
boldRegex = regexp.MustCompile(`(?m)\*\*(.*?)\*\*`)
|
||||
italicRegex = regexp.MustCompile(`(?m)\*(.*?)\*`)
|
||||
underlineRegex = regexp.MustCompile(`(?m)__(.*?)__`)
|
||||
strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`)
|
||||
)
|
||||
|
||||
func ParseMarkdown(md string) string {
|
||||
var res string
|
||||
res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
|
||||
res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]")
|
||||
res = underlineRegex.ReplaceAllString(res, "[::u]$1[::-]")
|
||||
res = strikeThroughRegex.ReplaceAllString(res, "[::s]$1[::-]")
|
||||
|
||||
return res
|
||||
}
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
package ui
|
||||
package discord
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
import "testing"
|
||||
|
||||
func TestParseMarkdown(t *testing.T) {
|
||||
tests := []struct {
|
||||
|
|
@ -18,7 +16,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 +29,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)
|
||||
}
|
||||
}
|
||||
45
discord/util.go
Normal file
45
discord/util.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package discord
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/ayntgl/discordgo"
|
||||
)
|
||||
|
||||
func ChannelToString(c *discordgo.Channel) string {
|
||||
var repr string
|
||||
if c.Name != "" {
|
||||
repr = "#" + c.Name
|
||||
} else if len(c.Recipients) == 1 {
|
||||
rp := c.Recipients[0]
|
||||
repr = rp.Username + "#" + rp.Discriminator
|
||||
} else {
|
||||
rps := make([]string, len(c.Recipients))
|
||||
for i, r := range c.Recipients {
|
||||
rps[i] = r.Username + "#" + r.Discriminator
|
||||
}
|
||||
|
||||
repr = strings.Join(rps, ", ")
|
||||
}
|
||||
|
||||
return repr
|
||||
}
|
||||
|
||||
func FindMessageByID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
|
||||
for i, m := range ms {
|
||||
if m.ID == mID {
|
||||
return i, m
|
||||
}
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func HasPermission(s *discordgo.State, cID string, p int64) bool {
|
||||
perm, err := s.UserChannelPermissions(s.User.ID, cID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return perm&p == p
|
||||
}
|
||||
|
|
@ -34,7 +34,7 @@ func NewApp() *App {
|
|||
MessageInputField: tview.NewInputField(),
|
||||
|
||||
Session: s,
|
||||
Config: config.LoadConfig(),
|
||||
Config: config.NewConfig(),
|
||||
SelectedMessage: -1,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/ayntgl/discordgo"
|
||||
"github.com/ayntgl/discordo/discord"
|
||||
)
|
||||
|
||||
func buildMessage(app *App, m *discordgo.Message) []byte {
|
||||
|
|
@ -88,7 +89,7 @@ func buildReferencedMessage(b *strings.Builder, rm *discordgo.Message, clientID
|
|||
|
||||
if rm.Content != "" {
|
||||
rm.Content = buildMentions(rm.Content, rm.Mentions, clientID)
|
||||
b.WriteString(parseMarkdown(rm.Content))
|
||||
b.WriteString(discord.ParseMarkdown(rm.Content))
|
||||
}
|
||||
|
||||
b.WriteString("[::-]")
|
||||
|
|
@ -99,7 +100,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(parseMarkdown(m.Content))
|
||||
b.WriteString(discord.ParseMarkdown(m.Content))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -137,7 +138,7 @@ func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) {
|
|||
embedBuilder.WriteString("\n\n")
|
||||
}
|
||||
|
||||
embedBuilder.WriteString(parseMarkdown(e.Description))
|
||||
embedBuilder.WriteString(discord.ParseMarkdown(e.Description))
|
||||
}
|
||||
|
||||
if len(e.Fields) != 0 {
|
||||
|
|
@ -150,7 +151,7 @@ func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) {
|
|||
embedBuilder.WriteString(ef.Name)
|
||||
embedBuilder.WriteString("[::-]")
|
||||
embedBuilder.WriteByte('\n')
|
||||
embedBuilder.WriteString(parseMarkdown(ef.Value))
|
||||
embedBuilder.WriteString(discord.ParseMarkdown(ef.Value))
|
||||
|
||||
if i != len(e.Fields)-1 {
|
||||
embedBuilder.WriteString("\n\n")
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ func onGuildsListSelected(app *App, guildIdx int) {
|
|||
})
|
||||
|
||||
for _, c := range cs {
|
||||
channelTreeNode := tview.NewTreeNode(channelToString(c)).
|
||||
channelTreeNode := tview.NewTreeNode(discord.ChannelToString(c)).
|
||||
SetReference(c.ID)
|
||||
rootTreeNode.AddChild(channelTreeNode)
|
||||
}
|
||||
|
|
@ -70,7 +70,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(channelToString(c)).
|
||||
channelTreeNode := tview.NewTreeNode(discord.ChannelToString(c)).
|
||||
SetReference(c.ID)
|
||||
rootTreeNode.AddChild(channelTreeNode)
|
||||
}
|
||||
|
|
@ -107,7 +107,7 @@ func onGuildsListSelected(app *App, guildIdx int) {
|
|||
})
|
||||
|
||||
if parentTreeNode != nil {
|
||||
channelTreeNode := tview.NewTreeNode(channelToString(c)).
|
||||
channelTreeNode := tview.NewTreeNode(discord.ChannelToString(c)).
|
||||
SetReference(c.ID)
|
||||
parentTreeNode.AddChild(channelTreeNode)
|
||||
}
|
||||
|
|
|
|||
44
ui/util.go
44
ui/util.go
|
|
@ -1,44 +0,0 @@
|
|||
package ui
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/ayntgl/discordgo"
|
||||
)
|
||||
|
||||
var (
|
||||
boldRegex = regexp.MustCompile(`(?m)\*\*(.*?)\*\*`)
|
||||
italicRegex = regexp.MustCompile(`(?m)\*(.*?)\*`)
|
||||
underlineRegex = regexp.MustCompile(`(?m)__(.*?)__`)
|
||||
strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`)
|
||||
)
|
||||
|
||||
func parseMarkdown(md string) string {
|
||||
var res string
|
||||
res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
|
||||
res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]")
|
||||
res = underlineRegex.ReplaceAllString(res, "[::u]$1[::-]")
|
||||
res = strikeThroughRegex.ReplaceAllString(res, "[::s]$1[::-]")
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func channelToString(c *discordgo.Channel) string {
|
||||
var repr string
|
||||
if c.Name != "" {
|
||||
repr = "#" + c.Name
|
||||
} else if len(c.Recipients) == 1 {
|
||||
rp := c.Recipients[0]
|
||||
repr = rp.Username + "#" + rp.Discriminator
|
||||
} else {
|
||||
rps := make([]string, len(c.Recipients))
|
||||
for i, r := range c.Recipients {
|
||||
rps[i] = r.Username + "#" + r.Discriminator
|
||||
}
|
||||
|
||||
repr = strings.Join(rps, ", ")
|
||||
}
|
||||
|
||||
return repr
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue