caret

[ACTIVE] a command line tool for browsing Lobsters in your terminal
git clone git://git.figbert.com/caret.git
Log | Files | Refs | README | LICENSE

commit da39c5877ec49f7e203f5de886087986fc37eb53
Author: FIGBERT <figbert@figbert.com>
Date:   Mon,  4 Sep 2023 17:18:04 -0700

Initial commit

Diffstat:
Aapi/api.go | 35+++++++++++++++++++++++++++++++++++
Aapi/schema.go | 46++++++++++++++++++++++++++++++++++++++++++++++
Adelegate.go | 32++++++++++++++++++++++++++++++++
Ago.mod | 30++++++++++++++++++++++++++++++
Ago.sum | 48++++++++++++++++++++++++++++++++++++++++++++++++
Aitem.go | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Amain.go | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aui/styles.go | 36++++++++++++++++++++++++++++++++++++
8 files changed, 351 insertions(+), 0 deletions(-)

diff --git a/api/api.go b/api/api.go @@ -0,0 +1,35 @@ +package api + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/charmbracelet/bubbletea" +) + +type PageMsg []Post +type ErrorMsg error + +func FetchPage(index int) tea.Cmd { + return func() tea.Msg { + posts, err := page(index) + if err != nil { + return ErrorMsg(err) + } + + return PageMsg(*posts) + } +} + +func page(i int) (*[]Post, error) { + resp, err := http.Get(fmt.Sprintf("https://lobste.rs/page/%d.json", i)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var posts []Post + json.NewDecoder(resp.Body).Decode(&posts) + return &posts, nil +} diff --git a/api/schema.go b/api/schema.go @@ -0,0 +1,46 @@ +package api + +type User struct { + Username string `json:"username"` + CreatedAt string `json:"created_at"` + IsAdmin bool `json:"is_admin"` + About string `json:"about"` + IsModerator bool `json:"is_moderator"` + Karma int `json:"karma"` + AvatarURL string `json:"avatar_url"` + InvitedByUser string `json:"invited_by_user"` + GitHubUsername string `json:"github_username"` +} + +type Comment struct { + ShortID string `json:"short_id"` + ShortIDURL string `json:"short_id_url"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + IsDeleted bool `json:"is_deleted"` + IsModerated bool `json:"is_moderated"` + Score int `json:"score"` + Flags int `json:"flags"` + ParentComment string `json:"parent_comment"` + Comment string `json:"comment"` + CommentPlain string `json:"comment_plain"` + URL string `json:"url"` + Depth int `json:"depth"` + User User `json:"commenting_user"` +} + +type Post struct { + ShortID string `json:"short_id"` + ShortIDURL string `json:"short_id_url"` + CreatedAt string `json:"created_at"` + Headline string `json:"title"` + URL string `json:"url"` + Score int `json:"score"` + Flags int `json:"flags"` + CommentCount int `json:"comment_count"` + Paragraph string `json:"description"` + DescriptionPlain string `json:"description_plain"` + CommentsURL string `json:"comments_url"` + User User `json:"submitter_user"` + Tags []string `json:"tags"` +} diff --git a/delegate.go b/delegate.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "io" + + "github.com/charmbracelet/bubbles/list" + "github.com/charmbracelet/bubbletea" + gloss "github.com/charmbracelet/lipgloss" +) + +type itemDelegate struct{} + +func (d itemDelegate) Height() int { return 2 } +func (d itemDelegate) Spacing() int { return 1 } +func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil } +func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) { + i, ok := listItem.(item) + if !ok { + return + } + + highlight := gloss.NewStyle().Background(gloss.Color("7")).Foreground(gloss.Color("0")) + title := fmt.Sprintf("%d. %s", index+1, i.Title()) + if index == m.Index() { + title = highlight.Render(title) + } + title = " " + title + + fmt.Fprint(w, title) + fmt.Fprint(w, fmt.Sprintf("\n %s", i.Description())) +} diff --git a/go.mod b/go.mod @@ -0,0 +1,30 @@ +module git.figbert.com/caret + +go 1.21.0 + +require ( + github.com/charmbracelet/bubbles v0.16.1 + github.com/charmbracelet/bubbletea v0.24.2 + github.com/charmbracelet/lipgloss v0.8.0 + github.com/dustin/go-humanize v1.0.1 +) + +require ( + github.com/atotto/clipboard v0.1.4 // indirect + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.18 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/sahilm/fuzzy v0.1.0 // indirect + golang.org/x/sync v0.1.0 // indirect + golang.org/x/sys v0.7.0 // indirect + golang.org/x/term v0.6.0 // indirect + golang.org/x/text v0.3.8 // indirect +) diff --git a/go.sum b/go.sum @@ -0,0 +1,48 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY= +github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc= +github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06RaW2cx/SY= +github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg= +github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU= +github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU= +github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= +github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34= +github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI= +github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= diff --git a/item.go b/item.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "net/url" + "strings" + "time" + + "git.figbert.com/caret/api" + "git.figbert.com/caret/ui" + + "github.com/dustin/go-humanize" +) + +type item struct { + post api.Post +} + +func (i item) FilterValue() string { return "" } +func (i item) Title() string { + parsedURL, err := url.Parse(i.post.URL) + if err != nil { + return i.post.Headline + } + displayURL := ui.SecondaryText().Render(fmt.Sprintf("(%s)", parsedURL.Hostname())) + + return fmt.Sprintf("%s %s", i.post.Headline, displayURL) +} +func (itm item) Description() string { + created, _ := time.Parse("2006-01-02T15:04:05.999-07:00", itm.post.CreatedAt) + + intro := ui.SecondaryText().Render( + fmt.Sprintf( + "%d upvotes via %s | %s | ", + itm.post.Score, itm.post.User.Username, humanize.Time(created), + ), + ) + + var tags strings.Builder + for i, t := range itm.post.Tags { + tags.WriteString(ui.TagText().Render(t)) + if i != len(itm.post.Tags)-1 { + tags.WriteString(ui.SecondaryText().Render(", ")) + } + } + + comments := ui.SecondaryText().Render(fmt.Sprintf(" | %d comments", itm.post.CommentCount)) + + return intro + tags.String() + comments +} diff --git a/main.go b/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "strings" + + "git.figbert.com/caret/api" + "git.figbert.com/caret/ui" + + "github.com/charmbracelet/bubbles/list" + "github.com/charmbracelet/bubbletea" +) + +type model struct { + width, height int + + page int + posts list.Model +} + +func (m model) Init() tea.Cmd { + return api.FetchPage(m.page) +} + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + if dimension, ok := msg.(tea.WindowSizeMsg); ok { + m.width = dimension.Width + m.height = dimension.Height + m.posts.SetWidth(dimension.Width) + m.posts.SetHeight(dimension.Height - 4) + } else if key, ok := msg.(tea.KeyMsg); ok { + switch key.String() { + case "ctrl+c": + return m, tea.Quit + default: + var cmd tea.Cmd + m.posts, cmd = m.posts.Update(msg) + return m, cmd + } + } else if psts, ok := msg.(api.PageMsg); ok { + for _, pst := range []api.Post(psts) { + m.posts.SetItems(append(m.posts.Items(), item{post: pst})) + } + } + return m, nil +} + +func (m model) View() string { + var view strings.Builder + + view.WriteString("\n " + ui.Title().Render("Lobsters") + "\n") + view.WriteString(" " + ui.HR(m.width-2) + "\n") + + if len(m.posts.Items()) == 0 { + view.WriteString(" " + ui.SecondaryText().Render("Loading…")) + } else { + view.WriteString(m.posts.View()) + } + + return view.String() +} + +func main() { + m := model{ + page: 1, + posts: list.New([]list.Item{}, itemDelegate{}, 0, 0), + } + m.posts.SetShowStatusBar(false) + m.posts.SetShowHelp(false) + m.posts.SetFilteringEnabled(false) + m.posts.SetShowTitle(false) + m.posts.SetShowPagination(false) + + tea.NewProgram(m, tea.WithAltScreen()).Run() +} diff --git a/ui/styles.go b/ui/styles.go @@ -0,0 +1,36 @@ +package ui + +import gloss "github.com/charmbracelet/lipgloss" + +func Title() gloss.Style { + return gloss.NewStyle().Foreground(LobstersRed()) +} + +func SecondaryText() gloss.Style { + return gloss.NewStyle().Foreground(Gray()) +} + +func TagText() gloss.Style { + return gloss.NewStyle().Foreground(Orange()) +} + +func LobstersRed() gloss.Color { + return gloss.Color("#AC130D") +} + +func Gray() gloss.Color { + return gloss.Color("8") +} + +func Orange() gloss.Color { + return gloss.Color("#ffc227") +} + +func HR(width int) string { + return gloss.NewStyle(). + Width(width). + BorderStyle(gloss.NormalBorder()). + BorderTop(true). + Foreground(Gray()). + Render() +}