Skip to content

Commit

Permalink
add cast command and view cast after publish (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
treethought committed Jun 16, 2024
1 parent 227ea8c commit 061e6b8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
42 changes: 42 additions & 0 deletions cmd/cast.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 23 additions & 4 deletions ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type App struct {
ctx *AppContext
client *api.Client
cfg *config.Config
pubonly bool
focusedModel tea.Model
focused string
navname string
Expand Down Expand Up @@ -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{}
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion ui/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 061e6b8

Please sign in to comment.