Skip to content

Commit

Permalink
Merge pull request #30 from zMoooooritz/extractReader
Browse files Browse the repository at this point in the history
Extract the logic of the reader into its own class
  • Loading branch information
zMoooooritz committed Nov 17, 2023
2 parents 75d878c + f63fbe1 commit 3554cef
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 144 deletions.
57 changes: 57 additions & 0 deletions pkg/tagesschau/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tagesschau

import "strings"

func ContentToParagraphs(content []Content) []string {
prevType := "text"
prevSection := false
paragraph := ""
var paragraphs []string
for _, c := range content {
switch c.Type {
case "text":
fallthrough
case "headline":
text := c.Value
if strings.Trim(text, " ") == "" {
continue
}

// drop author information
if isHighlighted(text, "em") {
continue
}

// remove hyperlinks from text
for {
startIdx := strings.Index(text, "<a ")
if startIdx == -1 {
break
}
endIndex := strings.Index(text, "\">")
if endIndex == -1 {
break
}
text = text[:startIdx+2] + text[endIndex+1:]
}
sec := isSection(text)
if (prevType != c.Type || sec || prevSection) && paragraph != "" {
paragraphs = append(paragraphs, paragraph)
paragraph = ""
}
paragraph += text + " "
prevSection = sec
}
prevType = c.Type
}
paragraphs = append(paragraphs, paragraph)
return paragraphs
}

func isSection(text string) bool {
return isHighlighted(text, "strong") || isHighlighted(text, "em")
}

func isHighlighted(text string, tag string) bool {
return strings.HasPrefix(text, "<"+tag+">") && strings.HasSuffix(text, "</"+tag+">")
}
104 changes: 104 additions & 0 deletions pkg/tui/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package tui

import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/zMoooooritz/nachrichten/pkg/config"
"github.com/zMoooooritz/nachrichten/pkg/util"
)

type Reader struct {
style config.Style
isFocused bool
toplineText string
dateText string
viewport viewport.Model
}

func NewReader(s config.Style) Reader {
return Reader{
style: s,
viewport: viewport.New(0, 0),
}
}

func (r *Reader) GotoTop() {
r.viewport.GotoTop()
}

func (r *Reader) GotoBottom() {
r.viewport.GotoBottom()
}

func (r *Reader) SetFocused(isFocused bool) {
r.isFocused = isFocused
}

func (r *Reader) IsFocused() bool {
return r.isFocused
}

func (r *Reader) SetDims(w, h int) {
r.viewport.Width = w
r.viewport.Height = h - lipgloss.Height(r.headerView()) - lipgloss.Height(r.footerView())
r.viewport.YPosition = lipgloss.Height(r.headerView())
}

func (r *Reader) SetContent(paragraphs []string) {
repr := util.FormatParagraphs(paragraphs, r.viewport.Width, r.style)
r.viewport.SetContent(repr)
}

func (r *Reader) SetHeaderContent(topline string, date string) {
r.toplineText = topline
r.dateText = date
}

func (r Reader) Init() tea.Cmd {
return nil
}

func (r Reader) Update(msg tea.Msg) (Reader, tea.Cmd) {
var cmd tea.Cmd
r.viewport, cmd = r.viewport.Update(msg)
return r, tea.Batch(cmd)
}

func (r Reader) View() string {
return fmt.Sprintf("%s\n%s\n%s", r.headerView(), r.viewport.View(), r.footerView())
}

func (r Reader) headerView() string {
titleStyle := r.style.ReaderTitleInactiveStyle
lineStyle := r.style.InactiveStyle
dateStyle := r.style.ReaderInfoInactiveStyle
if r.isFocused {
titleStyle = r.style.ReaderTitleActiveStyle
lineStyle = r.style.ActiveStyle
dateStyle = r.style.ReaderInfoActiveStyle
}

title := titleStyle.Render(r.toplineText)
date := dateStyle.Render(r.dateText)
line := lineStyle.Render(strings.Repeat("─", util.Max(0, r.viewport.Width-lipgloss.Width(title)-lipgloss.Width(date))))

return lipgloss.JoinHorizontal(lipgloss.Center, title, line, date)
}

func (r Reader) footerView() string {
infoStyle := r.style.ReaderInfoInactiveStyle
lineStyle := r.style.InactiveStyle
if r.isFocused {
infoStyle = r.style.ReaderInfoActiveStyle
lineStyle = r.style.ActiveStyle
}

info := infoStyle.Render(fmt.Sprintf("%3.f%%", r.viewport.ScrollPercent()*100))
line := lineStyle.Render(strings.Repeat("─", util.Max(0, r.viewport.Width-lipgloss.Width(info))))

return lipgloss.JoinHorizontal(lipgloss.Center, line, info)
}

0 comments on commit 3554cef

Please sign in to comment.