From 061e6b801340a7308319d882f06836ec906e9a91 Mon Sep 17 00:00:00 2001 From: Cam Sweeney Date: Sat, 15 Jun 2024 23:20:01 -0700 Subject: [PATCH] add cast command and view cast after publish (#8) --- cmd/cast.go | 42 ++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 2 +- ui/app.go | 27 +++++++++++++++++++++++---- ui/publish.go | 2 +- 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 cmd/cast.go diff --git a/cmd/cast.go b/cmd/cast.go new file mode 100644 index 0000000..1ccd246 --- /dev/null +++ b/cmd/cast.go @@ -0,0 +1,42 @@ +package cmd + +import ( + "fmt" + "log" + "os" + + tea "github.com/charmbracelet/bubbletea" + "github.com/spf13/cobra" + + "github.com/treethought/tofui/api" + "github.com/treethought/tofui/db" + "github.com/treethought/tofui/ui" +) + +var castCmd = &cobra.Command{ + Use: "cast", + Short: "publish a cast", + Run: func(cmd *cobra.Command, args []string) { + defer logFile.Close() + defer db.GetDB().Close() + signer := api.GetSigner("local") + if signer != nil { + log.Println("logged in as: ", signer.Username) + } + if signer == nil { + fmt.Println("please sign in to use this command by running `tofui`") + return + } + + app := ui.NewLocalApp(cfg, true) + p := tea.NewProgram(app, tea.WithAltScreen()) + if _, err := p.Run(); err != nil { + fmt.Printf("Alas, there's been an error: %v", err) + os.Exit(1) + } + }, +} + +func init() { + rootCmd.AddCommand(castCmd) +} diff --git a/cmd/root.go b/cmd/root.go index bedab19..f37e911 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -37,7 +37,7 @@ func runLocal() { prgmSessions: make(map[string][]*tea.Program), } go sv.startSigninHTTPServer() - app := ui.NewLocalApp(cfg) + app := ui.NewLocalApp(cfg, false) p := tea.NewProgram(app, tea.WithAltScreen()) sv.prgmSessions["local"] = append(sv.prgmSessions["local"], p) if _, err := p.Run(); err != nil { diff --git a/ui/app.go b/ui/app.go index df4d614..34eb9be 100644 --- a/ui/app.go +++ b/ui/app.go @@ -54,6 +54,7 @@ type App struct { ctx *AppContext client *api.Client cfg *config.Config + pubonly bool focusedModel tea.Model focused string navname string @@ -98,21 +99,21 @@ func NewSSHApp(cfg *config.Config, s ssh.Session, r *lipgloss.Renderer) (*App, e } ctx := &AppContext{s: s, pk: pk, signer: signer} - app := NewApp(cfg, ctx) + app := NewApp(cfg, ctx, false) return app, nil } -func NewLocalApp(cfg *config.Config) *App { +func NewLocalApp(cfg *config.Config, pubInit bool) *App { signer := api.GetSigner("local") if signer != nil { log.Println("logged in locally as: ", signer.Username) } ctx := &AppContext{signer: signer, pk: "local"} - app := NewApp(cfg, ctx) + app := NewApp(cfg, ctx, pubInit) return app } -func NewApp(cfg *config.Config, ctx *AppContext) *App { +func NewApp(cfg *config.Config, ctx *AppContext, pubonly bool) *App { if ctx == nil { ctx = &AppContext{} } @@ -121,6 +122,7 @@ func NewApp(cfg *config.Config, ctx *AppContext) *App { ctx: ctx, client: api.NewClient(cfg), cfg: cfg, + pubonly: pubonly, } a.feed = NewFeedView(a, feedTypeFollowing) a.focusedModel = a.feed @@ -142,6 +144,12 @@ func NewApp(cfg *config.Config, ctx *AppContext) *App { if a.ctx.signer == nil { a.splash.ShowSignin(true) } + if a.pubonly { + a.splash.SetActive(false) + a.FocusPublish() + a.SetNavName("publish") + return a + } a.SetNavName("feed") return a @@ -211,6 +219,17 @@ func (a *App) FocusChannel() tea.Cmd { return a.channel.Init() } +func (a *App) GoToCast(hash string) tea.Cmd { + return func() tea.Msg { + cast, err := a.client.GetCastWithReplies(a.ctx.signer, hash) + if err != nil { + log.Println("error getting cast: ", err) + return nil + } + return tea.Sequence(a.FocusCast(), a.cast.SetCast(cast)) + } +} + func (a *App) FocusCast() tea.Cmd { a.focusMain() a.SetNavName("cast") diff --git a/ui/publish.go b/ui/publish.go index 2979d25..e892610 100644 --- a/ui/publish.go +++ b/ui/publish.go @@ -203,7 +203,7 @@ func (m *PublishInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) { log.Println("cast posted: ", msg.resp.Cast.Hash) m.Clear() m.SetActive(false) - return m, nil + return m, m.app.GoToCast(msg.resp.Cast.Hash) case tea.KeyMsg: if msg.String() == "ctrl+c" { return nil, tea.Quit