Skip to content

Commit

Permalink
refacotr: conext
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses committed Jul 2, 2024
1 parent 7d6e5f6 commit c9e48eb
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 106 deletions.
19 changes: 19 additions & 0 deletions cmd/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/isabelroses/izrss/lib"
)

type context struct {
prev string
curr string
keys keyMap
feeds lib.Feeds
post lib.Post
feed lib.Feed
}

func (m Model) swapPage(next string) {
m.context.prev = m.context.curr
m.context.curr = next
}
137 changes: 72 additions & 65 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,7 @@ func (k keyMap) FullHelp() [][]key.Binding {
}
}

var keys = keyMap{
Up: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "move down"),
),
Back: key.NewBinding(
key.WithKeys("left", "h", "shift+tab"),
key.WithHelp("←/h", "back"),
),
Open: key.NewBinding(
key.WithKeys("enter", "o", "right", "l", "tab"),
key.WithHelp("o/enter", "open"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "toggle help"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q/esc", "quit"),
),
var allKeys = keyMap{
Refresh: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "refresh"),
Expand All @@ -89,7 +65,7 @@ var keys = keyMap{

// TODO: refator this so its per page and not global
func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
if m.context == "search" {
if m.context.curr == "search" {
switch msg.String() {
case "enter":
m = m.loadSearchValues()
Expand All @@ -104,71 +80,71 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
}

switch {
case key.Matches(msg, m.keys.Help):
case key.Matches(msg, m.context.keys.Help):
m.help.ShowAll = !m.help.ShowAll
m.table.SetHeight(m.viewport.Height - lipgloss.Height(m.help.View(m.keys)) - lib.MainStyle.GetBorderBottomSize())
m.table.SetHeight(m.viewport.Height - lipgloss.Height(m.help.View(m.context.keys)) - lib.MainStyle.GetBorderBottomSize())

case key.Matches(msg, m.keys.Quit):
err := m.feeds.WriteTracking()
case key.Matches(msg, m.context.keys.Quit):
err := m.context.feeds.WriteTracking()
if err != nil {
log.Fatalf("Could not write tracking data: %s", err)
}
return m, tea.Quit

case key.Matches(msg, m.keys.Refresh):
switch m.context {
case key.Matches(msg, m.context.keys.Refresh):
switch m.context.curr {
case "home":
id := m.table.Cursor()
feed := &m.feeds[id]
feed := &m.context.feeds[id]
lib.FetchURL(feed.URL, false)
feed.Posts = lib.GetPosts(feed.URL)
err := error(nil)
m.feeds, err = m.feeds.ReadTracking()
m.context.feeds, err = m.context.feeds.ReadTracking()
if err != nil {
log.Fatal(err)
}
m = m.loadHome()

case "content":
feed := &m.feed
feed := &m.context.feed
feed.Posts = lib.GetPosts(feed.URL)
err := error(nil)
m.feeds, err = m.feeds.ReadTracking()
m.context.feeds, err = m.context.feeds.ReadTracking()
if err != nil {
log.Fatal(err)
}
m = m.loadContent(m.feed.ID)
m = m.loadContent(m.context.feed.ID)

default:
return m, nil
}

case key.Matches(msg, m.keys.RefreshAll):
if m.context == "home" {
m.feeds = lib.GetAllContent(lib.UserConfig.Urls, false)
case key.Matches(msg, m.context.keys.RefreshAll):
if m.context.curr == "home" {
m.context.feeds = lib.GetAllContent(lib.UserConfig.Urls, false)
err := error(nil)
m.feeds, err = m.feeds.ReadTracking()
m.context.feeds, err = m.context.feeds.ReadTracking()
if err != nil {
log.Fatal(err)
}
m = m.loadHome()
}

case key.Matches(msg, m.keys.Back):
switch m.context {
case key.Matches(msg, m.context.keys.Back):
switch m.context.curr {
case "reader":
m = m.loadContent(m.feed.ID)
m.table.SetCursor(m.post.ID)
m = m.loadContent(m.context.feed.ID)
m.table.SetCursor(m.context.post.ID)
case "content":
m = m.loadHome()
m.table.SetCursor(m.feed.ID)
m.table.SetCursor(m.context.feed.ID)
}
m.viewport.SetYOffset(0)

case key.Matches(msg, m.keys.Open):
switch m.context {
case key.Matches(msg, m.context.keys.Open):
switch m.context.curr {
case "reader":
err := lib.OpenURL(m.post.Link)
err := lib.OpenURL(m.context.post.Link)
if err != nil {
log.Panic(err)
}
Expand All @@ -182,43 +158,74 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
m.viewport.SetYOffset(0)
}

case key.Matches(msg, m.keys.Search):
if m.context != "search" {
case key.Matches(msg, m.context.keys.Search):
if m.context.curr != "search" {
m = m.loadSearch()
}

case key.Matches(msg, m.keys.ToggleRead):
switch m.context {
case key.Matches(msg, m.context.keys.ToggleRead):
switch m.context.curr {
case "reader":
lib.ToggleRead(m.feeds, m.feed.ID, m.post.ID)
m = m.loadContent(m.feed.ID)
lib.ToggleRead(m.context.feeds, m.context.feed.ID, m.context.post.ID)
m = m.loadContent(m.context.feed.ID)
case "content":
lib.ToggleRead(m.feeds, m.feed.ID, m.table.Cursor())
m = m.loadContent(m.feed.ID)
lib.ToggleRead(m.context.feeds, m.context.feed.ID, m.table.Cursor())
m = m.loadContent(m.context.feed.ID)
}
err := m.feeds.WriteTracking()
err := m.context.feeds.WriteTracking()
if err != nil {
log.Fatalf("Could not write tracking data: %s", err)
}

case key.Matches(msg, m.keys.ReadAll):
switch m.context {
case key.Matches(msg, m.context.keys.ReadAll):
switch m.context.curr {
case "reader":
// if we are in the reader view, fall back to the normal mark all as read
lib.ToggleRead(m.feeds, m.feed.ID, m.post.ID)
lib.ToggleRead(m.context.feeds, m.context.feed.ID, m.context.post.ID)
case "content":
lib.ReadAll(m.feeds, m.feed.ID)
m = m.loadContent(m.feed.ID)
lib.ReadAll(m.context.feeds, m.context.feed.ID)
m = m.loadContent(m.context.feed.ID)
case "home":
lib.ReadAll(m.feeds, m.table.Cursor())
lib.ReadAll(m.context.feeds, m.table.Cursor())
m = m.loadHome()
}

err := m.feeds.WriteTracking()
err := m.context.feeds.WriteTracking()
if err != nil {
log.Fatalf("Could not write tracking data: %s", err)
}
}

return m, nil
}

func defaultKeyMap(overrides ...keyMap) keyMap {
base := keyMap{

Check failure on line 203 in cmd/keys.go

View workflow job for this annotation

GitHub Actions / lint

base declared and not used

Check failure on line 203 in cmd/keys.go

View workflow job for this annotation

GitHub Actions / lint

base declared and not used

Check failure on line 203 in cmd/keys.go

View workflow job for this annotation

GitHub Actions / lint

base declared and not used

Check failure on line 203 in cmd/keys.go

View workflow job for this annotation

GitHub Actions / lint

base declared and not used
Up: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "move down"),
),
Back: key.NewBinding(
key.WithKeys("left", "h", "shift+tab"),
key.WithHelp("←/h", "back"),
),
Open: key.NewBinding(
key.WithKeys("enter", "o", "right", "l", "tab"),
key.WithHelp("o/enter", "open"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "toggle help"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q/esc", "quit"),
),
}

return keys

Check failure on line 230 in cmd/keys.go

View workflow job for this annotation

GitHub Actions / lint

undefined: keys (typecheck)

Check failure on line 230 in cmd/keys.go

View workflow job for this annotation

GitHub Actions / lint

undefined: keys) (typecheck)

Check failure on line 230 in cmd/keys.go

View workflow job for this annotation

GitHub Actions / lint

undefined: keys (typecheck)

Check failure on line 230 in cmd/keys.go

View workflow job for this annotation

GitHub Actions / lint

undefined: keys) (typecheck)
}
53 changes: 38 additions & 15 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,69 @@ func (m Model) loadHome() Model {
}

rows := []table.Row{}
for _, Feed := range m.feeds {
for _, Feed := range m.context.feeds {
totalUnread := strconv.Itoa(Feed.GetTotalUnreads())
fraction := fmt.Sprintf("%s/%d", totalUnread, len(Feed.Posts))
rows = append(rows, table.Row{fraction, Feed.Title})
}

m.context = "home"
m.context.curr = "home"
m = m.loadNewTable(columns, rows)

return m
}

func (m Model) loadMixed() Model {
columns := []table.Column{
{Title: "Date", Width: 15},
{Title: "Read", Width: 10},
{Title: "Title", Width: m.table.Width() - 25},
}

posts := []lib.Post{}
for _, feed := range m.context.feeds {
posts = append(posts, feed.Posts...)
}

rows := []table.Row{}
for _, post := range posts {
read := lib.ReadSymbol(post.Read)
rows = append(rows, table.Row{post.Date, read, post.Title})
}

m.context.feed.Posts = posts

m = m.loadNewTable(columns, rows)
m.swapPage("mixed")

return m
}

func (m Model) loadContent(id int) Model {
feed := m.feeds[id]
feed := m.context.feeds[id]
feed.ID = id

columns := []table.Column{
{Title: "", Width: 10},
{Title: "Date", Width: 15},
{Title: "Read", Width: 10},
{Title: "Title", Width: m.table.Width() - 25},
}

rows := []table.Row{}
for _, post := range feed.Posts {
read := "x"
if post.Read {
read = "✓"
}
rows = append(rows, table.Row{post.Date, read, post.Title})
readsym := lib.ReadSymbol(post.Read)
rows = append(rows, table.Row{readsym, post.Date, post.Title})
}

m = m.loadNewTable(columns, rows)
m.context = "content"
m.feed = feed
m.swapPage("content")
m.context.feed = feed

return m
}

func (m Model) loadSearch() Model {
m.context = "search"
m.swapPage("search")

m.table.Blur()

Expand All @@ -73,7 +96,7 @@ func (m Model) loadSearchValues() Model {
var filteredPosts []lib.Post
rows := []table.Row{}

for _, feed := range m.feeds {
for _, feed := range m.context.feeds {
for _, post := range feed.Posts {
if strings.Contains(strings.ToLower(post.Content), strings.ToLower(search)) {
filteredPosts = append(filteredPosts, post)
Expand All @@ -88,8 +111,8 @@ func (m Model) loadSearchValues() Model {
}

m = m.loadNewTable(columns, rows)
m.context = "content"
m.feed.Posts = filteredPosts
m.swapPage("content")
m.context.feed.Posts = filteredPosts
m.table.Focus()
m.filter.Blur()
m.table.SetCursor(0)
Expand Down
Loading

0 comments on commit c9e48eb

Please sign in to comment.