Skip to content

Commit

Permalink
add help and keybindings for cast view (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
treethought committed Jun 16, 2024
1 parent 0579dec commit 227ea8c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func NewApp(cfg *config.Config, ctx *AppContext) *App {
a.quickSelect = NewQuickSelect(a)
a.publish = NewPublishInput(a)
a.statusLine = NewStatusLine(a)
a.help = NewHelpView(a)
a.help = NewHelpView(a, GlobalKeyMap)
a.notifications = NewNotificationsView(a)
a.splash = NewSplashView(a)
a.splash.SetActive(true)
Expand Down
80 changes: 70 additions & 10 deletions ui/cast_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"fmt"
"log"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -23,6 +24,7 @@ type CastView struct {
vp *viewport.Model
header *viewport.Model
hasImg bool
help *HelpView

pubReply *PublishInput
w, h int
Expand All @@ -42,8 +44,10 @@ func NewCastView(app *App, cast *api.Cast) *CastView {
header: &hp,
pubReply: NewPublishInput(app),
hasImg: false,
help: NewHelpView(app, CastViewKeyMap),
}
c.pfp.SetSize(4, 4)
c.help.SetFull(false)
return c
}

Expand All @@ -56,6 +60,67 @@ func (m *CastView) Clear() {
m.pfp.Clear()
}

func (m *CastView) LikeCast() tea.Cmd {
if m.cast == nil {
return nil
}
return likeCastCmd(m.app.client, m.app.ctx.signer, m.cast)
}

func (m *CastView) OpenCast() tea.Cmd {
if m.cast == nil {
return nil
}
return OpenURL(fmt.Sprintf("https://warpcast.com/%s/%s", m.cast.Author.Username, m.cast.Hash))
}

func (m *CastView) Reply() {
if m.cast == nil {
return
}
m.pubReply.SetActive(true)
m.pubReply.SetFocus(true)
}

func (m *CastView) ViewProfile() tea.Cmd {
if m.cast == nil {
return nil
}

userFid := m.cast.Author.FID
return tea.Sequence(
m.app.FocusProfile(),
getUserCmd(m.app.client, userFid, m.app.ctx.signer.FID),
getUserFeedCmd(m.app.client, userFid, m.app.ctx.signer.FID),
)
}
func (m *CastView) ViewChannel() tea.Cmd {
if m.cast == nil || m.cast.ParentURL == "" {
return nil
}

return tea.Batch(
getChannelFeedCmd(m.app.client, m.cast.ParentURL),
fetchChannelCmd(m.app.client, m.cast.ParentURL),
m.app.FocusChannel(),
)
}

func (m *CastView) ViewParent() tea.Cmd {
if m.cast == nil || m.cast.ParentHash == "" {
return nil
}
return func() tea.Msg {
cast, err := m.app.client.GetCastWithReplies(m.app.ctx.signer, m.cast.ParentHash)
if err != nil {
log.Println("failed to get parent cast", err)
return nil
}

return m.SetCast(cast)
}
}

func (m *CastView) SetCast(cast *api.Cast) tea.Cmd {
m.Clear()
m.cast = cast
Expand Down Expand Up @@ -91,6 +156,8 @@ func (m *CastView) resize() tea.Cmd {
w := min(m.w-fx, int(float64(GetWidth())*0.75))
h := min(m.h-fy, GetHeight()-4)

m.help.SetSize(m.w, 1)

m.header.Height = min(10, int(float64(h)*0.2))

hHeight := lipgloss.Height(m.header.View())
Expand Down Expand Up @@ -130,16 +197,8 @@ func (m *CastView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
_, cmd := m.pubReply.Update(msg)
return m, cmd
}
if msg.String() == "o" {
return m, OpenURL(fmt.Sprintf("https://warpcast.com/%s/%s", m.cast.Author.Username, m.cast.Hash))
}
if msg.String() == "l" {
return m, likeCastCmd(m.app.client, m.app.ctx.signer, m.cast)
}
if msg.String() == "C" {
m.pubReply.SetActive(true)
m.pubReply.SetFocus(true)
return m, nil
if CastViewKeyMap.HandleMsg(m, msg) != nil {
return m, CastViewKeyMap.HandleMsg(m, msg)
}
}
m.vp.SetContent(CastContent(m.cast, 10))
Expand Down Expand Up @@ -181,6 +240,7 @@ func (m *CastView) View() string {
m.header.View(),
m.vp.View(),
m.img.View(),
m.help.View(),
m.replies.View(),
),
)
Expand Down
16 changes: 12 additions & 4 deletions ui/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ package ui

import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
)

type keymap interface {
ShortHelp() []key.Binding
FullHelp() [][]key.Binding
}

type HelpView struct {
app *App
h help.Model
vp viewport.Model
full bool
km keymap
}

func NewHelpView(app *App) *HelpView {
func NewHelpView(app *App, km keymap) *HelpView {
return &HelpView{
app: app,
h: help.New(),
vp: viewport.Model{},
km: km,
}
}

Expand All @@ -33,10 +41,10 @@ func (m *HelpView) IsFull() bool {
func (m *HelpView) SetFull(full bool) {
m.full = full
if m.full {
m.vp.SetContent(m.h.FullHelpView(GlobalKeyMap.FullHelp()))
m.vp.SetContent(m.h.FullHelpView(m.km.FullHelp()))
return
}
hv := GlobalKeyMap.ShortHelp()
hv := m.km.ShortHelp()
m.vp.SetContent(m.h.ShortHelpView(hv))
}

Expand All @@ -52,7 +60,7 @@ func (m *HelpView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *HelpView) ShortView() string {
hv := GlobalKeyMap.ShortHelp()
hv := m.km.ShortHelp()
return m.h.ShortHelpView(hv)
}

Expand Down
82 changes: 82 additions & 0 deletions ui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,85 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

type casetViewKeymap struct {
LikeCast key.Binding
ViewProfile key.Binding
ViewChannel key.Binding
ViewParent key.Binding
Comment key.Binding
OpenCast key.Binding
}

func (k casetViewKeymap) ShortHelp() []key.Binding {
return []key.Binding{
k.LikeCast,
k.ViewParent,
k.Comment,
}
}
func (k casetViewKeymap) All() []key.Binding {
return []key.Binding{
k.LikeCast,
k.ViewProfile,
k.ViewChannel,
k.ViewParent,
k.Comment,
k.OpenCast,
}
}

func (k casetViewKeymap) FullHelp() [][]key.Binding {
return [][]key.Binding{
k.All(),
}
}

func (k casetViewKeymap) HandleMsg(c *CastView, msg tea.KeyMsg) tea.Cmd {
switch {
case key.Matches(msg, k.LikeCast):
return c.LikeCast()
case key.Matches(msg, k.ViewProfile):
return c.ViewProfile()
case key.Matches(msg, k.ViewChannel):
return c.ViewChannel()
case key.Matches(msg, k.ViewParent):
return c.ViewParent()
case key.Matches(msg, k.Comment):
c.Reply()
return noOp()
case key.Matches(msg, k.OpenCast):
return c.OpenCast()
}
return nil
}

var CastViewKeyMap = casetViewKeymap{
LikeCast: key.NewBinding(
key.WithKeys("l"),
key.WithHelp("l", "like cast"),
),
ViewProfile: key.NewBinding(
key.WithKeys("p"),
key.WithHelp("p", "view profile"),
),
ViewChannel: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "view channel"),
),
ViewParent: key.NewBinding(
key.WithKeys("t"),
key.WithHelp("t", "view parent"),
),
Comment: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "reply"),
),
OpenCast: key.NewBinding(
key.WithKeys("o"),
key.WithHelp("o", "open in browser"),
),
}

type feedKeymap struct {
ViewCast key.Binding
LikeCast key.Binding
Expand Down Expand Up @@ -198,11 +277,13 @@ func (k navKeymap) HandleMsg(a *App, msg tea.KeyMsg) tea.Cmd {
type kmap struct {
nav navKeymap
feed feedKeymap
cast casetViewKeymap
}

var GlobalKeyMap = kmap{
nav: NavKeyMap,
feed: FeedKeyMap,
cast: CastViewKeyMap,
}

func (k kmap) ShortHelp() []key.Binding {
Expand All @@ -213,6 +294,7 @@ func (k kmap) FullHelp() [][]key.Binding {
return [][]key.Binding{
k.nav.All(),
k.feed.All(),
k.cast.All(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion ui/statusline.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewStatusLine(app *App) *StatusLine {
return &StatusLine{
sb: sb,
app: app,
help: NewHelpView(app),
help: NewHelpView(app, GlobalKeyMap),
full: false,
}
}
Expand Down

0 comments on commit 227ea8c

Please sign in to comment.