Quellcode durchsuchen

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

ayn2op vor 3 Jahren
Ursprung
Commit
94f13299de
11 geänderte Dateien mit 85 neuen und 76 gelöschten Zeilen
  1. 7 6
      attachment_image.go
  2. 8 7
      guilds_tree.go
  3. 25 20
      internal/config/config.go
  4. 1 1
      internal/config/keys.go
  5. 1 1
      internal/config/theme.go
  6. 5 4
      login_form.go
  7. 3 5
      main.go
  8. 4 3
      main_flex.go
  9. 12 11
      message_input.go
  10. 18 17
      messages_text.go
  11. 1 1
      state.go

+ 7 - 6
attachment_image.go

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

+ 8 - 7
guilds_tree.go

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

+ 25 - 20
config/config.go → internal/config/config.go

@@ -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) {
+func defConfig() Config {
+	return Config{
+		Mouse:         true,
+		Timestamps:    false,
+		MessagesLimit: 50,
+		Editor:        "default",
+
+		Keys:  defKeys(),
+		Theme: defTheme(),
+	}
+}
+
+func Load() error {
 	path, err := os.UserConfigDir()
 	if err != nil {
-		return nil, err
+		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 nil, err
+		return err
 	}
 
-	c := Config{
-		Mouse:         true,
-		Timestamps:    false,
-		MessagesLimit: 50,
-		Editor:        "default",
-
-		Keys:  newKeys(),
-		Theme: newTheme(),
-	}
 	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
 }

+ 1 - 1
config/keys.go → internal/config/keys.go

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

+ 1 - 1
config/theme.go → internal/config/theme.go

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

+ 5 - 4
login_form.go

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

+ 3 - 5
main.go

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

+ 4 - 3
main_flex.go

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

+ 12 - 11
message_input.go

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

+ 18 - 17
messages_text.go

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

+ 1 - 1
state.go

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