From 2ed771ba6ed89275f1aba2cc14e6682242d5932a Mon Sep 17 00:00:00 2001 From: ayn2op Date: Mon, 9 Jun 2025 17:38:48 -0400 Subject: [PATCH] refactor(cmd): do not pass *tview.Application as arg to widgets --- cmd/application.go | 43 +++++++++++++++++++++---------------------- cmd/guilds_tree.go | 6 ++---- cmd/message_input.go | 6 ++---- cmd/messages_text.go | 6 ++---- cmd/root.go | 2 +- 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/cmd/application.go b/cmd/application.go index 477c09d..cea6efd 100644 --- a/cmd/application.go +++ b/cmd/application.go @@ -23,24 +23,23 @@ type application struct { messageInput *messageInput } -func newApp(cfg *config.Config) *application { - app := tview.NewApplication() - a := &application{ - Application: app, +func newApplication(cfg *config.Config) *application { + app := &application{ + Application: tview.NewApplication(), cfg: cfg, pages: tview.NewPages(), flex: tview.NewFlex(), - guildsTree: newGuildsTree(app, cfg), - messagesText: newMessagesText(app, cfg), - messageInput: newMessageInput(app, cfg), + guildsTree: newGuildsTree(cfg), + messagesText: newMessagesText(cfg), + messageInput: newMessageInput(cfg), } - a.EnableMouse(cfg.Mouse) - a.SetInputCapture(a.onInputCapture) - a.flex.SetInputCapture(a.onFlexInputCapture) - return a + app.EnableMouse(cfg.Mouse) + app.SetInputCapture(app.onInputCapture) + app.flex.SetInputCapture(app.onFlexInputCapture) + return app } func (app *application) show(token string) error { @@ -73,24 +72,24 @@ func (app *application) run(token string) error { return app.Run() } -func (a *application) clearPages() { - for _, name := range a.pages.GetPageNames(false) { - a.pages.RemovePage(name) +func (app *application) clearPages() { + for _, name := range app.pages.GetPageNames(false) { + app.pages.RemovePage(name) } } -func (a *application) init() { - a.clearPages() - a.flex.Clear() +func (app *application) init() { + app.clearPages() + app.flex.Clear() right := tview.NewFlex() right.SetDirection(tview.FlexRow) - right.AddItem(a.messagesText, 0, 1, false) - right.AddItem(a.messageInput, 3, 1, false) + right.AddItem(app.messagesText, 0, 1, false) + right.AddItem(app.messageInput, 3, 1, false) // The guilds tree is always focused first at start-up. - a.flex.AddItem(a.guildsTree, 0, 1, true) - a.flex.AddItem(right, 0, 4, false) - a.pages.AddAndSwitchToPage("flex", a.flex, true) + app.flex.AddItem(app.guildsTree, 0, 1, true) + app.flex.AddItem(right, 0, 4, false) + app.pages.AddAndSwitchToPage("flex", app.flex, true) } func (app *application) onInputCapture(event *tcell.EventKey) *tcell.EventKey { diff --git a/cmd/guilds_tree.go b/cmd/guilds_tree.go index ef2c151..d759c11 100644 --- a/cmd/guilds_tree.go +++ b/cmd/guilds_tree.go @@ -18,15 +18,13 @@ import ( type guildsTree struct { *tview.TreeView cfg *config.Config - app *tview.Application selectedChannelID discord.ChannelID } -func newGuildsTree(app *tview.Application, cfg *config.Config) *guildsTree { +func newGuildsTree(cfg *config.Config) *guildsTree { gt := &guildsTree{ TreeView: tview.NewTreeView(), cfg: cfg, - app: app, } gt.Box = ui.NewConfiguredBox(gt.Box, &cfg.Theme) @@ -203,7 +201,7 @@ func (gt *guildsTree) onSelected(node *tview.TreeNode) { app.messagesText.SetTitle(gt.channelToString(*channel)) gt.selectedChannelID = channel.ID - gt.app.SetFocus(app.messageInput) + app.SetFocus(app.messageInput) case nil: // Direct messages channels, err := discordState.PrivateChannels() if err != nil { diff --git a/cmd/message_input.go b/cmd/message_input.go index af3650a..530cb3f 100644 --- a/cmd/message_input.go +++ b/cmd/message_input.go @@ -22,15 +22,13 @@ const tmpFilePattern = consts.Name + "_*.md" type messageInput struct { *tview.TextArea cfg *config.Config - app *tview.Application replyMessageID discord.MessageID } -func newMessageInput(app *tview.Application, cfg *config.Config) *messageInput { +func newMessageInput(cfg *config.Config) *messageInput { mi := &messageInput{ TextArea: tview.NewTextArea(), cfg: cfg, - app: app, } mi.Box = ui.NewConfiguredBox(mi.Box, &cfg.Theme) @@ -126,7 +124,7 @@ func (mi *messageInput) editor() { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - mi.app.Suspend(func() { + app.Suspend(func() { err := cmd.Run() if err != nil { slog.Error("failed to run command", "args", cmd.Args, "err", err) diff --git a/cmd/messages_text.go b/cmd/messages_text.go index e6b1917..25905f8 100644 --- a/cmd/messages_text.go +++ b/cmd/messages_text.go @@ -29,7 +29,6 @@ import ( type messagesText struct { *tview.TextView cfg *config.Config - app *tview.Application selectedMessageID discord.MessageID fetchingMembers struct { @@ -39,11 +38,10 @@ type messagesText struct { } } -func newMessagesText(app *tview.Application, cfg *config.Config) *messagesText { +func newMessagesText(cfg *config.Config) *messagesText { mt := &messagesText{ TextView: tview.NewTextView(), cfg: cfg, - app: app, } mt.Box = ui.NewConfiguredBox(mt.Box, &cfg.Theme) @@ -524,7 +522,7 @@ func (mt *messagesText) reply(mention bool) { title += mt.authorName(msg.Author, msg.GuildID) app.messageInput.SetTitle(title) app.messageInput.replyMessageID = mt.selectedMessageID - mt.app.SetFocus(app.messageInput) + app.SetFocus(app.messageInput) } func (mt *messagesText) delete() { diff --git a/cmd/root.go b/cmd/root.go index 0b6dd55..3b1b7e0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -66,6 +66,6 @@ func Run() error { tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(cfg.Theme.BackgroundColor) - app = newApp(cfg) + app = newApplication(cfg) return app.run(tok) }