From f84c232aabb5bb9b9f8ca85b0cc7c35a62d1432f Mon Sep 17 00:00:00 2001 From: ayntgl Date: Fri, 24 Sep 2021 19:16:21 +0400 Subject: [PATCH] Move global variables declaration to pertinent file --- config.go | 2 ++ discord.go | 6 ++++++ main.go | 35 +---------------------------------- ui.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/config.go b/config.go index 489ca39..d3db44c 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,8 @@ import ( "github.com/rivo/tview" ) +var conf *config + type keybindings struct { GuildsTreeViewFocus string diff --git a/discord.go b/discord.go index 7c01950..c4c27b0 100644 --- a/discord.go +++ b/discord.go @@ -10,6 +10,12 @@ import ( "github.com/rivo/tview" ) +var ( + session *discordgo.Session + selectedChannel *discordgo.Channel + selectedMessage *discordgo.Message +) + func newSession() *discordgo.Session { s, err := discordgo.New() if err != nil { diff --git a/main.go b/main.go index d752581..d9f596f 100644 --- a/main.go +++ b/main.go @@ -1,37 +1,17 @@ package main import ( - "github.com/ayntgl/discordgo" - "github.com/gdamore/tcell/v2" "github.com/rivo/tview" "github.com/zalando/go-keyring" ) const service = "discordo" -var ( - app *tview.Application - loginForm *tview.Form - guildsTreeView *tview.TreeView - messagesTextView *tview.TextView - messageInputField *tview.InputField - mainFlex *tview.Flex - - conf *config - session *discordgo.Session - selectedChannel *discordgo.Channel - selectedMessage *discordgo.Message -) - func main() { conf = loadConfig() tview.Styles = conf.Theme - app = tview.NewApplication() - app. - EnableMouse(conf.Mouse). - SetInputCapture(onAppInputCapture) - + app = newApplication() guildsTreeView = newGuildsTreeView() messagesTextView = newMessagesTextView() messageInputField = newMessageInputField() @@ -70,19 +50,6 @@ func main() { } } -func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey { - switch e.Name() { - case conf.Keybindings.GuildsTreeViewFocus: - app.SetFocus(guildsTreeView) - case conf.Keybindings.MessagesTextViewFocus: - app.SetFocus(messagesTextView) - case conf.Keybindings.MessageInputFieldFocus: - app.SetFocus(messageInputField) - } - - return e -} - func onLoginFormLoginButtonSelected() { email := loginForm.GetFormItem(0).(*tview.InputField).GetText() password := loginForm.GetFormItem(1).(*tview.InputField).GetText() diff --git a/ui.go b/ui.go index bd7c0e5..2558920 100644 --- a/ui.go +++ b/ui.go @@ -10,6 +10,37 @@ import ( "github.com/rivo/tview" ) +var ( + app *tview.Application + loginForm *tview.Form + guildsTreeView *tview.TreeView + messagesTextView *tview.TextView + messageInputField *tview.InputField + mainFlex *tview.Flex +) + +func newApplication() *tview.Application { + a := tview.NewApplication() + a. + EnableMouse(conf.Mouse). + SetInputCapture(onAppInputCapture) + + return a +} + +func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey { + switch e.Name() { + case conf.Keybindings.GuildsTreeViewFocus: + app.SetFocus(guildsTreeView) + case conf.Keybindings.MessagesTextViewFocus: + app.SetFocus(messagesTextView) + case conf.Keybindings.MessageInputFieldFocus: + app.SetFocus(messageInputField) + } + + return e +} + func newGuildsTreeView() *tview.TreeView { w := tview.NewTreeView() w.