refactor: merge CLI struct into Config

This commit is contained in:
ayntgl 2022-03-17 19:57:19 +04:00
parent a7cb969fc6
commit 252548606b
5 changed files with 38 additions and 41 deletions

View file

@ -1,6 +1,6 @@
package config
type KeybindingsConfig struct {
type KeysConfig struct {
ToggleGuildsList string `toml:"toggle_guilds_list"`
ToggleChannelsTreeView string `toml:"toggle_channels_tree_view"`
ToggleMessagesTextView string `toml:"toggle_messages_text_view"`
@ -15,8 +15,8 @@ type KeybindingsConfig struct {
SelectLastMessage string `toml:"select_last_message"`
}
func newKeybindingsConfig() KeybindingsConfig {
return KeybindingsConfig{
func newKeysConfig() KeysConfig {
return KeysConfig{
ToggleGuildsList: "Rune[g]",
ToggleChannelsTreeView: "Rune[c]",
ToggleMessagesTextView: "Rune[m]",

View file

@ -8,27 +8,32 @@ import (
)
type Config struct {
General GeneralConfig `toml:"general"`
Theme ThemeConfig `toml:"theme"`
Keybindings KeybindingsConfig `toml:"keybindings"`
Token string `toml:"-" name:"token" help:"The authentication token." short:"T"`
Config string `toml:"-" name:"config" help:"The path of the configuration file." type:"path" short:"C"`
General GeneralConfig `toml:"general" kong:"-"`
Theme ThemeConfig `toml:"theme" kong:"-"`
Keys KeysConfig `toml:"keys" kong:"-"`
}
func New() *Config {
return &Config{
General: newGeneralConfig(),
Theme: newThemeConfig(),
Keybindings: newKeybindingsConfig(),
Config: DefaultPath(),
General: newGeneralConfig(),
Theme: newThemeConfig(),
Keys: newKeysConfig(),
}
}
func (c *Config) Load(path string) {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
func (c *Config) Load() {
err := os.MkdirAll(filepath.Dir(c.Config), os.ModePerm)
if err != nil {
panic(err)
}
if _, err = os.Stat(path); os.IsNotExist(err) {
f, err := os.Create(path)
if _, err = os.Stat(c.Config); os.IsNotExist(err) {
f, err := os.Create(c.Config)
if err != nil {
panic(err)
}
@ -38,7 +43,7 @@ func (c *Config) Load(path string) {
panic(err)
}
} else {
_, err = toml.DecodeFile(path, &c)
_, err = toml.DecodeFile(c.Config, &c)
if err != nil {
panic(err)
}

24
main.go
View file

@ -13,26 +13,16 @@ import (
const name = "discordo"
func main() {
var cli struct {
Token string `name:"token" help:"The authentication token." short:"T"`
Config string `name:"config" help:"The path of the configuration file." short:"C"`
}
kong.Parse(&cli, kong.Name(name), kong.UsageOnError())
cfg := config.New()
kong.Parse(cfg, kong.Name(name), kong.UsageOnError())
if cli.Token == "" {
cli.Token, _ = keyring.Get(name, "token")
if cfg.Token == "" {
cfg.Token, _ = keyring.Get(name, "token")
}
if cli.Config == "" {
cli.Config = config.DefaultPath()
}
c := config.New()
c.Load(cli.Config)
app := ui.NewApp(c)
if cli.Token != "" {
err := app.Connect(cli.Token)
app := ui.NewApp(cfg)
if cfg.Token != "" {
err := app.Connect(cfg.Token)
if err != nil {
panic(err)
}

View file

@ -30,6 +30,8 @@ func NewApp(c *config.Config) *App {
SelectedMessage: -1,
}
c.Load()
app.GuildsList = NewGuildsList(app)
app.ChannelsTreeView = NewChannelsTreeView(app)
app.MessagesTextView = NewMessagesTextView(app)
@ -76,16 +78,16 @@ func (app *App) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
if app.MainFlex.GetItemCount() != 0 {
switch e.Name() {
case app.Config.Keybindings.ToggleGuildsList:
case app.Config.Keys.ToggleGuildsList:
app.SetFocus(app.GuildsList)
return nil
case app.Config.Keybindings.ToggleChannelsTreeView:
case app.Config.Keys.ToggleChannelsTreeView:
app.SetFocus(app.ChannelsTreeView)
return nil
case app.Config.Keybindings.ToggleMessagesTextView:
case app.Config.Keys.ToggleMessagesTextView:
app.SetFocus(app.MessagesTextView)
return nil
case app.Config.Keybindings.ToggleMessageInputField:
case app.Config.Keys.ToggleMessageInputField:
app.SetFocus(app.MessageInputField)
return nil
}

View file

@ -54,7 +54,7 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
}
switch e.Name() {
case mtv.app.Config.Keybindings.SelectPreviousMessage:
case mtv.app.Config.Keys.SelectPreviousMessage:
if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
mtv.app.SelectedMessage = len(ms) - 1
} else {
@ -69,7 +69,7 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
ScrollToHighlight()
return nil
case mtv.app.Config.Keybindings.SelectNextMessage:
case mtv.app.Config.Keys.SelectNextMessage:
if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
mtv.app.SelectedMessage = len(ms) - 1
} else {
@ -84,21 +84,21 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
ScrollToHighlight()
return nil
case mtv.app.Config.Keybindings.SelectFirstMessage:
case mtv.app.Config.Keys.SelectFirstMessage:
mtv.app.SelectedMessage = 0
mtv.app.MessagesTextView.
Highlight(ms[mtv.app.SelectedMessage].ID).
ScrollToHighlight()
return nil
case mtv.app.Config.Keybindings.SelectLastMessage:
case mtv.app.Config.Keys.SelectLastMessage:
mtv.app.SelectedMessage = len(ms) - 1
mtv.app.MessagesTextView.
Highlight(ms[mtv.app.SelectedMessage].ID).
ScrollToHighlight()
return nil
case mtv.app.Config.Keybindings.OpenMessageActionsList:
case mtv.app.Config.Keys.OpenMessageActionsList:
messageActionsList := tview.NewList()
hs := mtv.app.MessagesTextView.GetHighlights()
@ -327,7 +327,7 @@ func (mi *MessageInputField) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
mi.app.MessagesTextView.Highlight()
return nil
case mi.app.Config.Keybindings.OpenExternalEditor:
case mi.app.Config.Keys.OpenExternalEditor:
e := os.Getenv("EDITOR")
if e == "" {
return nil