Segregate configuration management into config package
This commit is contained in:
parent
6683322bb1
commit
44f006bb0f
9 changed files with 109 additions and 105 deletions
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
|
@ -7,61 +7,7 @@ import (
|
|||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const name = "discordo"
|
||||
|
||||
type (
|
||||
MessagesTextKeysConfig struct {
|
||||
CopyContent string `yaml:"copy_content"`
|
||||
|
||||
Reply string `yaml:"reply"`
|
||||
ReplyMention string `yaml:"reply_mention"`
|
||||
SelectReply string `yaml:"select_reply"`
|
||||
|
||||
SelectPrevious string `yaml:"select_previous"`
|
||||
SelectNext string `yaml:"select_next"`
|
||||
SelectFirst string `yaml:"select_first"`
|
||||
SelectLast string `yaml:"select_last"`
|
||||
}
|
||||
|
||||
MessageInputKeysConfig struct {
|
||||
Send string `yaml:"send"`
|
||||
Paste string `yaml:"paste"`
|
||||
|
||||
LaunchEditor string `yaml:"launch_editor"`
|
||||
}
|
||||
|
||||
KeysConfig struct {
|
||||
Cancel string `yaml:"cancel"`
|
||||
|
||||
MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
|
||||
MessageInput MessageInputKeysConfig `yaml:"message_input"`
|
||||
}
|
||||
)
|
||||
|
||||
type (
|
||||
GuildsTreeThemeConfig struct {
|
||||
Graphics bool `yaml:"graphics"`
|
||||
}
|
||||
|
||||
MessagesTextThemeConfig struct {
|
||||
AuthorColor string `yaml:"author_color"`
|
||||
}
|
||||
|
||||
MessageInputThemeConfig struct{}
|
||||
|
||||
ThemeConfig struct {
|
||||
Border bool `yaml:"border"`
|
||||
BorderColor string `yaml:"border_color"`
|
||||
BorderPadding [4]int `yaml:"border_padding,flow"`
|
||||
|
||||
TitleColor string `yaml:"title_color"`
|
||||
BackgroundColor string `yaml:"background_color"`
|
||||
|
||||
GuildsTree GuildsTreeThemeConfig `yaml:"guilds_tree"`
|
||||
MessagesText MessagesTextThemeConfig `yaml:"messages_text"`
|
||||
MessageInput MessageInputThemeConfig `yaml:"message_input"`
|
||||
}
|
||||
)
|
||||
const Name = "discordo"
|
||||
|
||||
type Config struct {
|
||||
Mouse bool `yaml:"mouse"`
|
||||
|
|
@ -73,13 +19,13 @@ type Config struct {
|
|||
Theme ThemeConfig `yaml:"theme"`
|
||||
}
|
||||
|
||||
func newConfig() (*Config, error) {
|
||||
func New() (*Config, error) {
|
||||
path, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
path = filepath.Join(path, name)
|
||||
path = filepath.Join(path, Name)
|
||||
err = os.MkdirAll(path, os.ModePerm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
30
config/keys.go
Normal file
30
config/keys.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package config
|
||||
|
||||
type (
|
||||
MessagesTextKeysConfig struct {
|
||||
CopyContent string `yaml:"copy_content"`
|
||||
|
||||
Reply string `yaml:"reply"`
|
||||
ReplyMention string `yaml:"reply_mention"`
|
||||
SelectReply string `yaml:"select_reply"`
|
||||
|
||||
SelectPrevious string `yaml:"select_previous"`
|
||||
SelectNext string `yaml:"select_next"`
|
||||
SelectFirst string `yaml:"select_first"`
|
||||
SelectLast string `yaml:"select_last"`
|
||||
}
|
||||
|
||||
MessageInputKeysConfig struct {
|
||||
Send string `yaml:"send"`
|
||||
Paste string `yaml:"paste"`
|
||||
|
||||
LaunchEditor string `yaml:"launch_editor"`
|
||||
}
|
||||
|
||||
KeysConfig struct {
|
||||
Cancel string `yaml:"cancel"`
|
||||
|
||||
MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
|
||||
MessageInput MessageInputKeysConfig `yaml:"message_input"`
|
||||
}
|
||||
)
|
||||
26
config/theme.go
Normal file
26
config/theme.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package config
|
||||
|
||||
type (
|
||||
GuildsTreeThemeConfig struct {
|
||||
Graphics bool `yaml:"graphics"`
|
||||
}
|
||||
|
||||
MessagesTextThemeConfig struct {
|
||||
AuthorColor string `yaml:"author_color"`
|
||||
}
|
||||
|
||||
MessageInputThemeConfig struct{}
|
||||
|
||||
ThemeConfig struct {
|
||||
Border bool `yaml:"border"`
|
||||
BorderColor string `yaml:"border_color"`
|
||||
BorderPadding [4]int `yaml:"border_padding,flow"`
|
||||
|
||||
TitleColor string `yaml:"title_color"`
|
||||
BackgroundColor string `yaml:"background_color"`
|
||||
|
||||
GuildsTree GuildsTreeThemeConfig `yaml:"guilds_tree"`
|
||||
MessagesText MessagesTextThemeConfig `yaml:"messages_text"`
|
||||
MessageInput MessageInputThemeConfig `yaml:"message_input"`
|
||||
}
|
||||
)
|
||||
|
|
@ -24,20 +24,20 @@ func newGuildsTree() *GuildsTree {
|
|||
root: tview.NewTreeNode(""),
|
||||
}
|
||||
|
||||
gt.SetGraphics(config.Theme.GuildsTree.Graphics)
|
||||
gt.SetGraphics(cfg.Theme.GuildsTree.Graphics)
|
||||
gt.SetRoot(gt.root)
|
||||
gt.SetTopLevel(1)
|
||||
gt.SetSelectedFunc(gt.onSelected)
|
||||
|
||||
gt.SetBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor))
|
||||
gt.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
|
||||
|
||||
gt.SetTitle("Guilds")
|
||||
gt.SetTitleColor(tcell.GetColor(config.Theme.TitleColor))
|
||||
gt.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
|
||||
gt.SetTitleAlign(tview.AlignLeft)
|
||||
|
||||
p := config.Theme.BorderPadding
|
||||
gt.SetBorder(config.Theme.Border)
|
||||
gt.SetBorderColor(tcell.GetColor(config.Theme.BorderColor))
|
||||
p := cfg.Theme.BorderPadding
|
||||
gt.SetBorder(cfg.Theme.Border)
|
||||
gt.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
|
||||
gt.SetBorderPadding(p[0], p[1], p[2], p[3])
|
||||
|
||||
return gt
|
||||
|
|
@ -150,7 +150,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
gt.selectedChannel = c
|
||||
messagesText.SetTitle(gt.channelToString(*c))
|
||||
|
||||
ms, err := discordState.Messages(ref, config.MessagesLimit)
|
||||
ms, err := discordState.Messages(ref, cfg.MessagesLimit)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -24,11 +24,11 @@ func newLoginForm() *LoginForm {
|
|||
lf.AddButton("Login", lf.onLoginButtonSelected)
|
||||
|
||||
lf.SetTitle("Login")
|
||||
lf.SetTitleColor(tcell.GetColor(config.Theme.TitleColor))
|
||||
lf.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
|
||||
|
||||
p := config.Theme.BorderPadding
|
||||
lf.SetBorder(config.Theme.Border)
|
||||
lf.SetBorderColor(tcell.GetColor(config.Theme.BorderColor))
|
||||
p := cfg.Theme.BorderPadding
|
||||
lf.SetBorder(cfg.Theme.Border)
|
||||
lf.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
|
||||
lf.SetBorderPadding(p[0], p[1], p[2], p[3])
|
||||
|
||||
return lf
|
||||
|
|
|
|||
13
main.go
13
main.go
|
|
@ -7,6 +7,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ayn2op/discordo/config"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/zalando/go-keyring"
|
||||
)
|
||||
|
|
@ -14,7 +15,7 @@ import (
|
|||
var (
|
||||
token string
|
||||
|
||||
config *Config
|
||||
cfg *config.Config
|
||||
discordState *State
|
||||
|
||||
app = tview.NewApplication()
|
||||
|
|
@ -33,7 +34,7 @@ func init() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
path = filepath.Join(path, name)
|
||||
path = filepath.Join(path, config.Name)
|
||||
err = os.MkdirAll(path, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
@ -55,16 +56,16 @@ func main() {
|
|||
var err error
|
||||
// If the token is passed via the flag, set it in the keyring.
|
||||
if token != "" {
|
||||
go keyring.Set(name, "token", token)
|
||||
go keyring.Set(config.Name, "token", token)
|
||||
} else {
|
||||
token, err = keyring.Get(name, "token")
|
||||
token, err = keyring.Get(config.Name, "token")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
config, err = newConfig()
|
||||
cfg, err = config.New()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
@ -95,7 +96,7 @@ func main() {
|
|||
app.SetRoot(flex, true)
|
||||
}
|
||||
|
||||
app.EnableMouse(config.Mouse)
|
||||
app.EnableMouse(cfg.Mouse)
|
||||
err = app.Run()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -24,15 +24,15 @@ func newMessageInput() *MessageInput {
|
|||
}
|
||||
|
||||
mi.SetInputCapture(mi.onInputCapture)
|
||||
mi.SetBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor))
|
||||
mi.SetFieldBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor))
|
||||
mi.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
|
||||
mi.SetFieldBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
|
||||
|
||||
mi.SetTitleColor(tcell.GetColor(config.Theme.TitleColor))
|
||||
mi.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
|
||||
mi.SetTitleAlign(tview.AlignLeft)
|
||||
|
||||
p := config.Theme.BorderPadding
|
||||
mi.SetBorder(config.Theme.Border)
|
||||
mi.SetBorderColor(tcell.GetColor(config.Theme.BorderColor))
|
||||
p := cfg.Theme.BorderPadding
|
||||
mi.SetBorder(cfg.Theme.Border)
|
||||
mi.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
|
||||
mi.SetBorderPadding(p[0], p[1], p[2], p[3])
|
||||
|
||||
return mi
|
||||
|
|
@ -45,16 +45,16 @@ func (mi *MessageInput) reset() {
|
|||
|
||||
func (mi *MessageInput) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch event.Name() {
|
||||
case config.Keys.MessageInput.Send:
|
||||
case cfg.Keys.MessageInput.Send:
|
||||
mi.sendAction()
|
||||
return nil
|
||||
case config.Keys.MessageInput.Paste:
|
||||
case cfg.Keys.MessageInput.Paste:
|
||||
mi.pasteAction()
|
||||
return nil
|
||||
case config.Keys.MessageInput.LaunchEditor:
|
||||
case cfg.Keys.MessageInput.LaunchEditor:
|
||||
messageInput.launchEditorAction()
|
||||
return nil
|
||||
case config.Keys.Cancel:
|
||||
case cfg.Keys.Cancel:
|
||||
mi.reset()
|
||||
return nil
|
||||
}
|
||||
|
|
@ -116,7 +116,7 @@ func (mi *MessageInput) pasteAction() {
|
|||
}
|
||||
|
||||
func (mi *MessageInput) launchEditorAction() {
|
||||
e := config.Editor
|
||||
e := cfg.Editor
|
||||
if e == "default" {
|
||||
e = os.Getenv("EDITOR")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,15 +37,15 @@ func newMessagesText() *MessagesText {
|
|||
app.Draw()
|
||||
})
|
||||
|
||||
mt.SetBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor))
|
||||
mt.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
|
||||
|
||||
mt.SetTitle("Messages")
|
||||
mt.SetTitleColor(tcell.GetColor(config.Theme.TitleColor))
|
||||
mt.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
|
||||
mt.SetTitleAlign(tview.AlignLeft)
|
||||
|
||||
p := config.Theme.BorderPadding
|
||||
mt.SetBorder(config.Theme.Border)
|
||||
mt.SetBorderColor(tcell.GetColor(config.Theme.BorderColor))
|
||||
p := cfg.Theme.BorderPadding
|
||||
mt.SetBorder(cfg.Theme.Border)
|
||||
mt.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
|
||||
mt.SetBorderPadding(p[0], p[1], p[2], p[3])
|
||||
|
||||
return mt
|
||||
|
|
@ -74,7 +74,7 @@ func (mt *MessagesText) createMessage(m *discord.Message) error {
|
|||
mt.buf.WriteByte(' ')
|
||||
|
||||
mt.buf.WriteByte('[')
|
||||
mt.buf.WriteString(config.Theme.MessagesText.AuthorColor)
|
||||
mt.buf.WriteString(cfg.Theme.MessagesText.AuthorColor)
|
||||
mt.buf.WriteByte(']')
|
||||
mt.buf.WriteString(m.ReferencedMessage.Author.Username)
|
||||
mt.buf.WriteString("[-] ")
|
||||
|
|
@ -98,12 +98,12 @@ func (mt *MessagesText) createMessage(m *discord.Message) error {
|
|||
|
||||
func (mt *MessagesText) createHeader(m *discord.Message) {
|
||||
mt.buf.WriteByte('[')
|
||||
mt.buf.WriteString(config.Theme.MessagesText.AuthorColor)
|
||||
mt.buf.WriteString(cfg.Theme.MessagesText.AuthorColor)
|
||||
mt.buf.WriteByte(']')
|
||||
mt.buf.WriteString(m.Author.Username)
|
||||
mt.buf.WriteString("[-] ")
|
||||
|
||||
if config.Timestamps {
|
||||
if cfg.Timestamps {
|
||||
mt.buf.WriteString("[::d]")
|
||||
mt.buf.WriteString(m.Timestamp.Format(time.Kitchen))
|
||||
mt.buf.WriteString("[::-] ")
|
||||
|
|
@ -127,31 +127,31 @@ func (mt *MessagesText) createFooter(m *discord.Message) {
|
|||
|
||||
func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch event.Name() {
|
||||
case config.Keys.MessagesText.CopyContent:
|
||||
case cfg.Keys.MessagesText.CopyContent:
|
||||
mt.copyContentAction()
|
||||
return nil
|
||||
case config.Keys.MessagesText.Reply:
|
||||
case cfg.Keys.MessagesText.Reply:
|
||||
mt.replyAction(false)
|
||||
return nil
|
||||
case config.Keys.MessagesText.ReplyMention:
|
||||
case cfg.Keys.MessagesText.ReplyMention:
|
||||
mt.replyAction(true)
|
||||
return nil
|
||||
case config.Keys.MessagesText.SelectPrevious:
|
||||
case cfg.Keys.MessagesText.SelectPrevious:
|
||||
mt.selectPreviousAction()
|
||||
return nil
|
||||
case config.Keys.MessagesText.SelectNext:
|
||||
case cfg.Keys.MessagesText.SelectNext:
|
||||
mt.selectNextAction()
|
||||
return nil
|
||||
case config.Keys.MessagesText.SelectFirst:
|
||||
case cfg.Keys.MessagesText.SelectFirst:
|
||||
mt.selectFirstAction()
|
||||
return nil
|
||||
case config.Keys.MessagesText.SelectLast:
|
||||
case cfg.Keys.MessagesText.SelectLast:
|
||||
mt.selectLastAction()
|
||||
return nil
|
||||
case config.Keys.MessagesText.SelectReply:
|
||||
case cfg.Keys.MessagesText.SelectReply:
|
||||
mt.selectReplyAction()
|
||||
return nil
|
||||
case config.Keys.Cancel:
|
||||
case cfg.Keys.Cancel:
|
||||
guildsTree.selectedChannel = nil
|
||||
|
||||
messagesText.reset()
|
||||
|
|
|
|||
5
state.go
5
state.go
|
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"runtime"
|
||||
|
||||
"github.com/ayn2op/discordo/config"
|
||||
"github.com/diamondburned/arikawa/v3/api"
|
||||
"github.com/diamondburned/arikawa/v3/gateway"
|
||||
"github.com/diamondburned/arikawa/v3/state"
|
||||
|
|
@ -12,10 +13,10 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
api.UserAgent = fmt.Sprintf("%s/%s %s/%s", name, "0.1", "arikawa", "v3")
|
||||
api.UserAgent = fmt.Sprintf("%s/%s %s/%s", config.Name, "0.1", "arikawa", "v3")
|
||||
gateway.DefaultIdentity = gateway.IdentifyProperties{
|
||||
OS: runtime.GOOS,
|
||||
Browser: name,
|
||||
Browser: config.Name,
|
||||
Device: "",
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue