Skip to content

Commit

Permalink
improve robustness of application; minor ui improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zMoooooritz committed Oct 12, 2024
1 parent fb93ee9 commit e6bdb09
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 118 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yuin/goldmark v1.7.4 // indirect
github.com/yuin/goldmark-emoji v1.0.3 // indirect
github.com/yuin/goldmark v1.7.6 // indirect
github.com/yuin/goldmark-emoji v1.0.4 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.7.6 h1:cZgJxVh5mL5cu8KOnwxvFJy5TFB0BHUskZZyq7TYbDg=
github.com/yuin/goldmark v1.7.6/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark-emoji v1.0.3 h1:aLRkLHOuBR2czCY4R8olwMjID+tENfhyFDMCRhbIQY4=
github.com/yuin/goldmark-emoji v1.0.3/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U=
github.com/yuin/goldmark-emoji v1.0.4 h1:vCwMkPZSNefSUnOW2ZKRUjBSD5Ok3W78IXhGxxAEF90=
github.com/yuin/goldmark-emoji v1.0.4/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
Expand Down
10 changes: 10 additions & 0 deletions pkg/tagesschau/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
homepageAPI string = baseUrl + "api2u/homepage/"
searchAPI string = baseUrl + "api2u/search/"
shortNewsUrl string = baseUrl + "multimedia/sendung/tagesschau_in_100_sekunden"

emptyArticleToken string = "EMPTY_ARTICLE"
)

type ImageSize int
Expand Down Expand Up @@ -68,6 +70,14 @@ type Article struct {
DetailsWeb string `json:"detailsweb"`
}

func EMPTY_ARTICLE() Article {
return Article{Type: emptyArticleToken}
}

func (n Article) IsEmptyArticle() bool {
return n.Type == emptyArticleToken
}

func (n Article) Title() string {
if n.Topline != "" {
return n.Topline
Expand Down
8 changes: 4 additions & 4 deletions pkg/tui/base_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
)

type Selector interface {
PushCurrentArticle() tea.Cmd
PushSelectedArticle() tea.Cmd
SelectorType() SelectorType
SetVisible(bool)
IsVisible() bool
Expand Down Expand Up @@ -80,9 +80,9 @@ func (s *BaseSelector) getSelectedArticle() tagesschau.Article {
return s.articles[s.selectedIndex]
}

func (s BaseSelector) PushCurrentArticle() tea.Cmd {
func (s BaseSelector) PushSelectedArticle() tea.Cmd {
return func() tea.Msg {
return ChangedActiveArticle(s.getSelectedArticle())
return UpdatedArticle(s.getSelectedArticle())
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ func (s BaseSelector) Update(msg tea.Msg) (BaseSelector, tea.Cmd) {

if s.list.Index() != s.selectedIndex {
s.selectedIndex = s.list.Index()
cmds = append(cmds, s.PushCurrentArticle())
cmds = append(cmds, s.PushSelectedArticle())
}

return s, tea.Batch(cmds...)
Expand Down
14 changes: 12 additions & 2 deletions pkg/tui/base_viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tui
import (
"fmt"
"strings"
"time"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
Expand All @@ -12,6 +13,10 @@ import (
"github.com/zMoooooritz/nachrichten/pkg/tagesschau"
)

const (
emptyArticleHeader string = "LEER"
)

type ViewerType int

const (
Expand Down Expand Up @@ -112,16 +117,21 @@ func (v *BaseViewer) SetDims(w, h int) {
}

func (v *BaseViewer) SetHeaderData(article tagesschau.Article) {
if article.IsRegionalArticle() {
date := time.Now()
if article.IsEmptyArticle() {
v.title = emptyArticleHeader
} else if article.IsRegionalArticle() {
v.title = article.Desc
date = article.Date
} else {
if article.Topline != "" {
v.title = article.Topline
} else {
v.title = article.Desc
}
date = article.Date
}
v.date = article.Date.Format(germanDateFormat)
v.date = date.Format(germanDateFormat)
}

func (v BaseViewer) Init() tea.Cmd {
Expand Down
10 changes: 5 additions & 5 deletions pkg/tui/details_viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ func (d *Details) Update(msg tea.Msg) (Viewer, tea.Cmd) {
)

switch msg := msg.(type) {
case ChangedActiveArticle:
case UpdatedArticle:
d.SetArticle(tagesschau.Article(msg))
case RefreshActiveViewer:
d.SetArticle(d.shared.activeArticle)
}

if d.isActive {
Expand Down Expand Up @@ -61,7 +59,7 @@ func (d *Details) handleNumberInput(number int) tea.Cmd {
article, err := tagesschau.LoadArticle(related[index].Details)
if err == nil {
return tea.Batch(
func() tea.Msg { return ChangedActiveArticle(*article) },
func() tea.Msg { return UpdatedArticle(*article) },
func() tea.Msg { return ShowTextViewer{} },
)
}
Expand All @@ -76,7 +74,9 @@ func (d *Details) SetArticle(article tagesschau.Article) {

func (d Details) buildDetails(article tagesschau.Article) string {
details := ""
if article.IsRegionalArticle() {
if article.IsEmptyArticle() {
details = ""
} else if article.IsRegionalArticle() {
details = d.buildRegionalArticleDetails(article)
} else {
details = d.buildNationalArticleDetails(article)
Expand Down
14 changes: 4 additions & 10 deletions pkg/tui/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ func NewHelper(shared *SharedState, hstate HelpState) *Helper {
state: hstate,
}
h.model.FullSeparator = " • "
h.model.ShortSeparator = " • "
h.model.Styles.ShortKey = shared.style.InactiveStyle
h.model.Styles.FullKey = shared.style.InactiveStyle

return &h
}

func (h Helper) View() string {
if h.IsVisible() {
return "\n" + lipgloss.NewStyle().Width(h.model.Width).AlignHorizontal(lipgloss.Center).Render(h.model.View(h.shared.keymap))
if !h.IsVisible() {
return ""
}
return ""
return lipgloss.NewStyle().Width(h.model.Width).AlignHorizontal(lipgloss.Center).Render(h.model.View(h.shared.keymap))
}

func (h *Helper) Update(msg tea.Msg) (*Helper, tea.Cmd) {
Expand Down Expand Up @@ -66,13 +67,6 @@ func (h *Helper) nextState() {
}
}

func (h *Helper) Height() int {
if h.IsVisible() {
return 2
}
return 0
}

func (h *Helper) SetWidth(width int) {
h.model.Width = width
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/tui/image_viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ func (i *ImageViewer) Update(msg tea.Msg) (Viewer, tea.Cmd) {
)

switch msg := msg.(type) {
case ChangedActiveArticle:
case UpdatedArticle:
i.SetArticle(tagesschau.Article(msg))
case RefreshActiveViewer:
i.SetArticle(i.shared.activeArticle)
}

if i.isFocused || i.isFullScreen {
Expand All @@ -46,8 +44,12 @@ func (i *ImageViewer) Update(msg tea.Msg) (Viewer, tea.Cmd) {

func (i *ImageViewer) SetArticle(article tagesschau.Article) {
i.SetHeaderData(article)
img := i.shared.imageCache.GetImage(article.ID, article.ImageData.ImageVariants.RectSmall)
i.pushImageToViewer(img)
if article.IsEmptyArticle() {
i.viewport.SetContent("")
} else {
img := i.shared.imageCache.GetImage(article.ID, article.ImageData.ImageVariants.RectSmall)
i.pushImageToViewer(img)
}
}

func (i *ImageViewer) pushImageToViewer(img image.Image) {
Expand Down
19 changes: 2 additions & 17 deletions pkg/tui/keybinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,7 @@ func (k KeyMap) ShortHelp() []key.Binding {

func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.left},
{k.right},
{k.up},
{k.down},
{k.next},
{k.prev},
{k.full},
{k.start},
{k.end},
{k.article},
{k.image},
{k.details},
{k.open},
{k.video},
{k.shortNews},
{k.help},
{k.quit},
{k.left, k.right, k.up, k.down, k.prev, k.next, k.help, k.quit},
{k.full, k.start, k.end, k.article, k.image, k.details, k.open, k.video, k.shortNews},
}
}
54 changes: 28 additions & 26 deletions pkg/tui/navigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,34 @@ func (n *Navigator) Update(msg tea.Msg) (*Navigator, tea.Cmd) {

switch msg := msg.(type) {
case tea.KeyMsg:
if n.shared.mode == NORMAL_MODE {
switch {
case key.Matches(msg, n.shared.keymap.next):
if n.isFocused && n.isVisible {
n.nextSelector()
cmds = append(cmds, n.selectors[n.activeSelectorIndex].PushCurrentArticle())
}
case key.Matches(msg, n.shared.keymap.prev):
if n.isFocused && n.isVisible {
n.prevSelector()
cmds = append(cmds, n.selectors[n.activeSelectorIndex].PushCurrentArticle())
}
case key.Matches(msg, n.shared.keymap.right):
if n.isVisible {
n.isFocused = false
}
case key.Matches(msg, n.shared.keymap.left):
if n.isVisible {
n.isFocused = true
}
case key.Matches(msg, n.shared.keymap.full):
n.isVisible = !n.isVisible
case key.Matches(msg, n.shared.keymap.search):
if n.isFocused && n.isVisible {
n.selectSearchSelector()
}
if n.shared.mode == INSERT_MODE {
break
}

switch {
case key.Matches(msg, n.shared.keymap.next):
if n.isFocused && n.isVisible {
n.nextSelector()
cmds = append(cmds, n.selectors[n.activeSelectorIndex].PushSelectedArticle())
}
case key.Matches(msg, n.shared.keymap.prev):
if n.isFocused && n.isVisible {
n.prevSelector()
cmds = append(cmds, n.selectors[n.activeSelectorIndex].PushSelectedArticle())
}
case key.Matches(msg, n.shared.keymap.right):
if n.isVisible {
n.isFocused = false
}
case key.Matches(msg, n.shared.keymap.left):
if n.isVisible {
n.isFocused = true
}
case key.Matches(msg, n.shared.keymap.full):
n.isVisible = !n.isVisible
case key.Matches(msg, n.shared.keymap.search):
if n.isFocused && n.isVisible {
n.selectSearchSelector()
}
}
}
Expand Down
19 changes: 4 additions & 15 deletions pkg/tui/search_selector.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package tui

import (
"time"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
Expand All @@ -11,15 +9,6 @@ import (
"github.com/zMoooooritz/nachrichten/pkg/tagesschau"
)

var (
noArticles = []tagesschau.Article{
{
Topline: "LEER",
Date: time.Now(),
},
}
)

type SearchSelector struct {
BaseSelector
search textinput.Model
Expand All @@ -44,7 +33,7 @@ func NewSearchSelector(selector BaseSelector) *SearchSelector {
searchInput.Cursor.TextStyle = selector.shared.style.InactiveStyle
searchInput.TextStyle = selector.shared.style.InactiveStyle

selector.articles = noArticles
selector.articles = []tagesschau.Article{tagesschau.EMPTY_ARTICLE()}
return &SearchSelector{
BaseSelector: selector,
search: searchInput,
Expand All @@ -63,18 +52,18 @@ func (s *SearchSelector) Update(msg tea.Msg) (Selector, tea.Cmd) {

switch msg := msg.(type) {
case LoadingArticlesFailed:
s.articles = noArticles
s.articles = []tagesschau.Article{tagesschau.EMPTY_ARTICLE()}
s.list.SetItems([]list.Item{})
s.selectedIndex = 0
cmds = append(cmds, s.PushCurrentArticle())
cmds = append(cmds, s.PushSelectedArticle())
case tagesschau.SearchResult:
result := tagesschau.SearchResult(msg)
s.articles = result.Articles
s.rebuildList()
if s.shared.config.Settings.PreloadThumbnails {
go s.shared.imageCache.LoadThumbnails(s.articles)
}
cmds = append(cmds, s.PushCurrentArticle())
cmds = append(cmds, s.PushSelectedArticle())
case tea.KeyMsg:
if s.isFocused && s.isVisible {
if s.shared.mode == NORMAL_MODE {
Expand Down
4 changes: 1 addition & 3 deletions pkg/tui/text_viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ func (r *Reader) Update(msg tea.Msg) (Viewer, tea.Cmd) {
)

switch msg := msg.(type) {
case ChangedActiveArticle:
case UpdatedArticle:
r.SetArticle(tagesschau.Article(msg))
case RefreshActiveViewer:
r.SetArticle(r.shared.activeArticle)
}

if r.IsFocused() || r.isFullScreen {
Expand Down
Loading

0 comments on commit e6bdb09

Please sign in to comment.