Skip to content

Commit

Permalink
refactor: use pointers where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses committed Jul 3, 2024
1 parent 6cb4eea commit f2c9db6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
28 changes: 14 additions & 14 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
case "home":
switch {
case key.Matches(msg, m.keys.Open):
m = m.loadContent(m.table.Cursor())
m.loadContent(m.table.Cursor())
m.table.SetCursor(0)
m.viewport.SetYOffset(0)

Expand All @@ -84,7 +84,7 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
if err != nil {
log.Fatal(err)
}
m = m.loadHome()
m.loadHome()

case key.Matches(msg, m.keys.RefreshAll):
m.context.feeds = lib.GetAllContent(lib.UserConfig.Urls, false)
Expand All @@ -93,11 +93,11 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
if err != nil {
log.Fatal(err)
}
m = m.loadHome()
m.loadHome()

case key.Matches(msg, m.keys.ReadAll):
lib.ReadAll(m.context.feeds, m.table.Cursor())
m = m.loadHome()
m.loadHome()
err := m.context.feeds.WriteTracking()
if err != nil {
log.Fatalf("Could not write tracking data: %s", err)
Expand All @@ -114,27 +114,27 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
if err != nil {
log.Fatal(err)
}
m = m.loadContent(m.context.feed.ID)
m.loadContent(m.context.feed.ID)

case key.Matches(msg, m.keys.Back):
m = m.loadHome()
m.loadHome()
m.table.SetCursor(m.context.feed.ID)
m.viewport.SetYOffset(0)

case key.Matches(msg, m.keys.Open):
m = m.loadReader()
m.loadReader()

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

case key.Matches(msg, m.keys.ReadAll):
lib.ReadAll(m.context.feeds, m.context.feed.ID)
m = m.loadContent(m.context.feed.ID)
m.loadContent(m.context.feed.ID)
err := m.context.feeds.WriteTracking()
if err != nil {
log.Fatalf("Could not write tracking data: %s", err)
Expand All @@ -144,7 +144,7 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
case "reader":
switch {
case key.Matches(msg, m.keys.Back):
m = m.loadContent(m.context.feed.ID)
m.loadContent(m.context.feed.ID)
m.table.SetCursor(m.context.post.ID)
m.viewport.SetYOffset(0)

Expand All @@ -156,7 +156,7 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {

case key.Matches(msg, m.keys.ToggleRead):
lib.ToggleRead(m.context.feeds, m.context.feed.ID, m.context.post.ID)
m = m.loadContent(m.context.feed.ID)
m.loadContent(m.context.feed.ID)
err := m.context.feeds.WriteTracking()
if err != nil {
log.Fatalf("Could not write tracking data: %s", err)
Expand All @@ -174,10 +174,10 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
case "search":
switch msg.String() {
case "enter":
m = m.loadSearchValues()
m.loadSearchValues()

case "ctrl+c", "esc", "/":
m = m.loadContent(m.table.Cursor())
m.loadContent(m.table.Cursor())
m.table.Focus()
m.filter.Blur()

Expand All @@ -189,7 +189,7 @@ func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) {
switch {
case key.Matches(msg, m.keys.Search):
if m.context.curr != "search" {
m = m.loadSearch()
m.loadSearch()
}

case key.Matches(msg, m.keys.Help):
Expand Down
32 changes: 10 additions & 22 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// load the home view, this conists of the list of feeds
func (m Model) loadHome() Model {
func (m *Model) loadHome() {
columns := []table.Column{
{Title: "Unread", Width: 10},
{Title: "Title", Width: m.table.Width() - 10},
Expand All @@ -25,12 +25,10 @@ func (m Model) loadHome() Model {
}

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

return m
m.loadNewTable(columns, rows)
}

func (m Model) loadMixed() Model {
func (m *Model) loadMixed() {
columns := []table.Column{
{Title: "Date", Width: 15},
{Title: "Read", Width: 10},
Expand All @@ -50,13 +48,11 @@ func (m Model) loadMixed() Model {

m.context.feed.Posts = posts

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

return m
}

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

Expand All @@ -72,25 +68,21 @@ func (m Model) loadContent(id int) Model {
rows = append(rows, table.Row{readsym, post.Date, post.Title})
}

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

return m
}

func (m Model) loadSearch() Model {
func (m *Model) loadSearch() {
m.swapPage("search")

m.table.Blur()

m.filter.Focus()
m.filter.SetValue("")

return m
}

func (m Model) loadSearchValues() Model {
func (m Model) loadSearchValues() {
search := m.filter.Value()

var filteredPosts []lib.Post
Expand All @@ -110,24 +102,20 @@ func (m Model) loadSearchValues() Model {
{Title: "Title", Width: m.table.Width() - 15},
}

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

return m
}

func (m Model) loadNewTable(columns []table.Column, rows []table.Row) Model {
func (m *Model) loadNewTable(columns []table.Column, rows []table.Row) {
t := &m.table

// NOTE: clear the rows first to prevent panic
t.SetRows([]table.Row{})

t.SetColumns(columns)
t.SetRows(rows)

return m
}
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ func (m Model) handleWindowSize(msg tea.WindowSizeMsg) Model {
)

if lib.UserConfig.Home == "mixed" {
m = m.loadMixed()
m.loadMixed()
} else {
m = m.loadHome()
m.loadHome()
}

m.ready = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

var htom = tomd.NewConverter("", true, nil)

func (m Model) loadReader() Model {
func (m *Model) loadReader() {
id := m.table.Cursor()
post := m.context.feed.Posts[id]
post.ID = id
Expand All @@ -28,6 +28,4 @@ func (m Model) loadReader() Model {
log.Fatalf("could not render markdown: %v", err)
}
m.viewport.SetContent(out)

return m
}

0 comments on commit f2c9db6

Please sign in to comment.