Convert main-level global variable to package-level global variable

This commit is contained in:
ayn2op 2023-01-23 18:02:06 +05:30
parent 02ff951e65
commit 94f13299de
11 changed files with 88 additions and 79 deletions

View file

@ -6,6 +6,7 @@ import (
_ "image/png"
"net/http"
"github.com/ayn2op/discordo/internal/config"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
@ -21,13 +22,13 @@ func newAttachmentImage(a discord.Attachment) (*AttachmentImage, error) {
}
ai.SetInputCapture(ai.onInputCapture)
ai.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
ai.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
ai.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor))
ai.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor))
ai.SetTitleAlign(tview.AlignLeft)
p := cfg.Theme.BorderPadding
ai.SetBorder(cfg.Theme.Border)
ai.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
p := config.Current.Theme.BorderPadding
ai.SetBorder(config.Current.Theme.Border)
ai.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor))
ai.SetBorderPadding(p[0], p[1], p[2], p[3])
resp, err := http.Get(a.URL)
@ -47,7 +48,7 @@ func newAttachmentImage(a discord.Attachment) (*AttachmentImage, error) {
}
func (ai *AttachmentImage) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
if event.Name() == cfg.Keys.Cancel {
if event.Name() == config.Current.Keys.Cancel {
app.SetRoot(mainFlex, true)
app.SetFocus(messagesText)
return nil

View file

@ -6,6 +6,7 @@ import (
"sort"
"strings"
"github.com/ayn2op/discordo/internal/config"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/gdamore/tcell/v2"
@ -26,20 +27,20 @@ func newGuildsTree() *GuildsTree {
root: tview.NewTreeNode(""),
}
gt.SetGraphics(cfg.Theme.GuildsTree.Graphics)
gt.SetGraphics(config.Current.Theme.GuildsTree.Graphics)
gt.SetRoot(gt.root)
gt.SetTopLevel(1)
gt.SetSelectedFunc(gt.onSelected)
gt.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
gt.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor))
gt.SetTitle("Guilds")
gt.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
gt.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor))
gt.SetTitleAlign(tview.AlignLeft)
p := cfg.Theme.BorderPadding
gt.SetBorder(cfg.Theme.Border)
gt.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
p := config.Current.Theme.BorderPadding
gt.SetBorder(config.Current.Theme.Border)
gt.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor))
gt.SetBorderPadding(p[0], p[1], p[2], p[3])
return gt
@ -162,7 +163,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
gt.createChannelNodes(n, cs)
case discord.ChannelID:
ms, err := discordState.Messages(ref, cfg.MessagesLimit)
ms, err := discordState.Messages(ref, config.Current.MessagesLimit)
if err != nil {
log.Println(err)
return

View file

@ -9,6 +9,8 @@ import (
const Name = "discordo"
var Current = defConfig()
type Config struct {
// Mouse indicates whether the mouse is usable or not.
Mouse bool `yaml:"mouse"`
@ -23,53 +25,56 @@ type Config struct {
Theme Theme `yaml:"theme"`
}
// Load reads the configuration file and decodes the configuration file or creates a new one if it does not exist already and writes the default configuration to the newly-created configuration file.
func Load() (*Config, error) {
path, err := os.UserConfigDir()
if err != nil {
return nil, err
}
path = filepath.Join(path, Name)
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return nil, err
}
c := Config{
func defConfig() Config {
return Config{
Mouse: true,
Timestamps: false,
MessagesLimit: 50,
Editor: "default",
Keys: newKeys(),
Theme: newTheme(),
Keys: defKeys(),
Theme: defTheme(),
}
}
func Load() error {
path, err := os.UserConfigDir()
if err != nil {
return err
}
// Create the configuration directory if it does not exist already.
path = filepath.Join(path, Name)
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return err
}
path = filepath.Join(path, "config.yml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
f, err := os.Create(path)
if err != nil {
return nil, err
return err
}
defer f.Close()
err = yaml.NewEncoder(f).Encode(c)
err = yaml.NewEncoder(f).Encode(Current)
if err != nil {
return nil, err
return err
}
} else {
f, err := os.Open(path)
if err != nil {
return nil, err
return err
}
defer f.Close()
err = yaml.NewDecoder(f).Decode(&c)
err = yaml.NewDecoder(f).Decode(&Current)
if err != nil {
return nil, err
return err
}
}
return &c, nil
return err
}

View file

@ -39,7 +39,7 @@ type Keys struct {
MessageInput MessageInputKeys `yaml:"message_input"`
}
func newKeys() Keys {
func defKeys() Keys {
return Keys{
Cancel: "Esc",

View file

@ -25,7 +25,7 @@ type Theme struct {
MessageInput MessageInputTheme `yaml:"message_input"`
}
func newTheme() Theme {
func defTheme() Theme {
return Theme{
Border: true,
BorderColor: "default",

View file

@ -4,6 +4,7 @@ import (
"context"
"log"
"github.com/ayn2op/discordo/internal/config"
"github.com/diamondburned/arikawa/v3/api"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
@ -24,11 +25,11 @@ func newLoginForm() *LoginForm {
lf.AddButton("Login", lf.onLoginButtonSelected)
lf.SetTitle("Login")
lf.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
lf.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor))
p := cfg.Theme.BorderPadding
lf.SetBorder(cfg.Theme.Border)
lf.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
p := config.Current.Theme.BorderPadding
lf.SetBorder(config.Current.Theme.Border)
lf.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor))
lf.SetBorderPadding(p[0], p[1], p[2], p[3])
return lf

View file

@ -6,7 +6,7 @@ import (
"os"
"path/filepath"
"github.com/ayn2op/discordo/config"
"github.com/ayn2op/discordo/internal/config"
"github.com/rivo/tview"
"github.com/zalando/go-keyring"
)
@ -14,7 +14,6 @@ import (
var (
token string
cfg *config.Config
discordState *State
app = tview.NewApplication()
@ -62,8 +61,7 @@ func main() {
}
}
cfg, err = config.Load()
if err != nil {
if err = config.Load(); err != nil {
log.Fatal(err)
}
@ -94,7 +92,7 @@ func main() {
app.SetRoot(mainFlex, true)
}
app.EnableMouse(cfg.Mouse)
app.EnableMouse(config.Current.Mouse)
err = app.Run()
if err != nil {
log.Fatal(err)

View file

@ -1,18 +1,19 @@
package main
import (
"github.com/ayn2op/discordo/internal/config"
"github.com/gdamore/tcell/v2"
)
func onInputCapture(event *tcell.EventKey) *tcell.EventKey {
switch event.Name() {
case cfg.Keys.GuildsTree.Focus:
case config.Current.Keys.GuildsTree.Focus:
app.SetFocus(guildsTree)
return nil
case cfg.Keys.MessagesText.Focus:
case config.Current.Keys.MessagesText.Focus:
app.SetFocus(messagesText)
return nil
case cfg.Keys.MessageInput.Focus:
case config.Current.Keys.MessageInput.Focus:
app.SetFocus(messageInput)
return nil
}

View file

@ -7,6 +7,7 @@ import (
"strings"
"github.com/atotto/clipboard"
"github.com/ayn2op/discordo/internal/config"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/utils/json/option"
@ -24,15 +25,15 @@ func newMessageInput() *MessageInput {
}
mi.SetInputCapture(mi.onInputCapture)
mi.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
mi.SetFieldBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
mi.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor))
mi.SetFieldBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor))
mi.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
mi.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor))
mi.SetTitleAlign(tview.AlignLeft)
p := cfg.Theme.BorderPadding
mi.SetBorder(cfg.Theme.Border)
mi.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
p := config.Current.Theme.BorderPadding
mi.SetBorder(config.Current.Theme.Border)
mi.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor))
mi.SetBorderPadding(p[0], p[1], p[2], p[3])
return mi
@ -45,16 +46,16 @@ func (mi *MessageInput) reset() {
func (mi *MessageInput) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
switch event.Name() {
case cfg.Keys.MessageInput.Send:
case config.Current.Keys.MessageInput.Send:
mi.sendAction()
return nil
case cfg.Keys.MessageInput.Paste:
case config.Current.Keys.MessageInput.Paste:
mi.pasteAction()
return nil
case cfg.Keys.MessageInput.LaunchEditor:
case config.Current.Keys.MessageInput.LaunchEditor:
messageInput.launchEditorAction()
return nil
case cfg.Keys.Cancel:
case config.Current.Keys.Cancel:
mi.reset()
return nil
}
@ -116,7 +117,7 @@ func (mi *MessageInput) pasteAction() {
}
func (mi *MessageInput) launchEditorAction() {
e := cfg.Editor
e := config.Current.Editor
if e == "default" {
e = os.Getenv("EDITOR")
}

View file

@ -8,6 +8,7 @@ import (
"github.com/atotto/clipboard"
"github.com/ayn2op/discordo/discordmd"
"github.com/ayn2op/discordo/internal/config"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
@ -37,15 +38,15 @@ func newMessagesText() *MessagesText {
app.Draw()
})
mt.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor))
mt.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor))
mt.SetTitle("Messages")
mt.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
mt.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor))
mt.SetTitleAlign(tview.AlignLeft)
p := cfg.Theme.BorderPadding
mt.SetBorder(cfg.Theme.Border)
mt.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
p := config.Current.Theme.BorderPadding
mt.SetBorder(config.Current.Theme.Border)
mt.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor))
mt.SetBorderPadding(p[0], p[1], p[2], p[3])
return mt
@ -86,9 +87,9 @@ func (mt *MessagesText) createMessage(m discord.Message) {
}
func (mt *MessagesText) createHeader(w io.Writer, m discord.Message) {
fmt.Fprintf(w, "[%s]%s[-] ", cfg.Theme.MessagesText.AuthorColor, m.Author.Username)
fmt.Fprintf(w, "[%s]%s[-] ", config.Current.Theme.MessagesText.AuthorColor, m.Author.Username)
if cfg.Timestamps {
if config.Current.Timestamps {
fmt.Fprintf(w, "[::d]%s[::-] ", m.Timestamp.Format(time.Kitchen))
}
}
@ -106,34 +107,34 @@ func (mt *MessagesText) createFooter(w io.Writer, m discord.Message) {
func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
switch event.Name() {
case cfg.Keys.MessagesText.CopyContent:
case config.Current.Keys.MessagesText.CopyContent:
mt.copyContentAction()
return nil
case cfg.Keys.MessagesText.Reply:
case config.Current.Keys.MessagesText.Reply:
mt.replyAction(false)
return nil
case cfg.Keys.MessagesText.ReplyMention:
case config.Current.Keys.MessagesText.ReplyMention:
mt.replyAction(true)
return nil
case cfg.Keys.MessagesText.SelectPrevious:
case config.Current.Keys.MessagesText.SelectPrevious:
mt.selectPreviousAction()
return nil
case cfg.Keys.MessagesText.SelectNext:
case config.Current.Keys.MessagesText.SelectNext:
mt.selectNextAction()
return nil
case cfg.Keys.MessagesText.SelectFirst:
case config.Current.Keys.MessagesText.SelectFirst:
mt.selectFirstAction()
return nil
case cfg.Keys.MessagesText.SelectLast:
case config.Current.Keys.MessagesText.SelectLast:
mt.selectLastAction()
return nil
case cfg.Keys.MessagesText.SelectReply:
case config.Current.Keys.MessagesText.SelectReply:
mt.selectReplyAction()
return nil
case cfg.Keys.MessagesText.ShowImage:
case config.Current.Keys.MessagesText.ShowImage:
mt.showImageAction()
return nil
case cfg.Keys.Cancel:
case config.Current.Keys.Cancel:
guildsTree.selectedChannelID = 0
messagesText.reset()

View file

@ -6,7 +6,7 @@ import (
"log"
"runtime"
"github.com/ayn2op/discordo/config"
"github.com/ayn2op/discordo/internal/config"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/state"