smutok/internal/tui/fetcher.go(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package tui
import (
"time"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"olexsmir.xyz/smutok/internal/store"
)
type fetchedArticles []store.Article
func (m *Model) fetchArticles(kind store.ArticleKind) tea.Cmd {
return func() tea.Msg {
articles, err := m.store.GetArticles(m.ctx, kind)
if err != nil {
return sendErr(err)
}
return fetchedArticles(articles)
}
}
func (m *Model) setupTableWithArticles() {
// clean up previous state
m.table.SetRows([]table.Row{})
columns := []table.Column{
{Title: "date", Width: 10},
{Title: "status", Width: 6},
{Title: "author", Width: 14},
{Title: "title", Width: m.table.Width() - 30},
}
rows := make([]table.Row, len(m.articles))
for i, a := range m.articles {
rows[i] = table.Row{
m.toArticleDate(a.PublishedAt),
m.toArticleStatus(a.IsRead, a.IsStarred),
a.Author,
a.Title,
}
}
m.table.SetColumns(columns)
m.table.SetRows(rows)
}
func (m *Model) toArticleStatus(isRead, isStarred bool) string {
var out string
if isRead {
out += "✓ "
}
if isStarred {
out += "★ "
}
return out
}
func (m *Model) toArticleDate(publishedAt int64) string {
return time.Unix(publishedAt, 0).Format("2006/01/02")
}
|