101 lines
2.2 KiB
Go
101 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
miniflux "miniflux.app/client"
|
|
)
|
|
|
|
var (
|
|
titleStyle = lipgloss.NewStyle().MarginLeft(2)
|
|
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
|
|
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
|
|
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
|
|
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
|
|
quitTextStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4)
|
|
)
|
|
|
|
type ViewType uint
|
|
|
|
const (
|
|
ViewListFeedEntries ViewType = iota
|
|
ViewPlayer
|
|
ViewFeeds
|
|
)
|
|
|
|
type MsgChangeView ViewType
|
|
type MsgPlayEntry feedEntry
|
|
type MsgWatchedEntry int64
|
|
type MsgTick time.Time
|
|
|
|
type MinifluxPlayer struct {
|
|
MinifluxClient *miniflux.Client
|
|
DefaultCategory string
|
|
MpvPath string
|
|
|
|
CurrentView ViewType
|
|
|
|
feedEntries feedEntriesModel
|
|
player playerModel
|
|
}
|
|
|
|
func (mp *MinifluxPlayer) Init() tea.Cmd {
|
|
mp.feedEntries = feedEntriesModel{
|
|
minifluxClient: mp.MinifluxClient,
|
|
}
|
|
|
|
mp.player = playerModel{}
|
|
|
|
return tea.Sequence(
|
|
tickEvery(),
|
|
mp.feedEntries.Init(),
|
|
mp.player.Init(),
|
|
)
|
|
}
|
|
|
|
func (mp *MinifluxPlayer) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
var cmds []tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "ctrl+c", "q":
|
|
return mp, tea.Quit
|
|
}
|
|
case MsgTick:
|
|
cmds = append(cmds, tickEvery())
|
|
case MsgChangeView:
|
|
mp.CurrentView = ViewType(msg)
|
|
}
|
|
|
|
// always update all models
|
|
_, cmdFeeds := mp.feedEntries.Update(msg)
|
|
_, cmdPlayer := mp.player.Update(msg)
|
|
|
|
cmds = append(cmds, cmdFeeds)
|
|
cmds = append(cmds, cmdPlayer)
|
|
|
|
return mp, tea.Batch(cmds...)
|
|
}
|
|
|
|
func (mp *MinifluxPlayer) View() string {
|
|
if mp.CurrentView == ViewPlayer {
|
|
return mp.player.View()
|
|
}
|
|
|
|
return lipgloss.JoinVertical(0.2, mp.feedEntries.View(), "", mp.player.View())
|
|
}
|
|
|
|
func (mp *MinifluxPlayer) FetchCategories(ctx context.Context) ([]*miniflux.Category, error) {
|
|
return mp.MinifluxClient.Categories()
|
|
}
|
|
|
|
func tickEvery() tea.Cmd {
|
|
return tea.Every(time.Second, func(t time.Time) tea.Msg {
|
|
return MsgTick(t)
|
|
})
|
|
}
|