refactor(cmd): do not pass *tview.Application as arg to widgets

This commit is contained in:
ayn2op 2025-06-09 17:38:48 -04:00
parent 2947441669
commit 2ed771ba6e
5 changed files with 28 additions and 35 deletions

View file

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

View file

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

View file

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

View file

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

View file

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