Skip to content

Commit

Permalink
Merge branch 'master' into feat/favorite-within-post
Browse files Browse the repository at this point in the history
  • Loading branch information
guyfedwards authored Dec 20, 2024
2 parents 27ab362 + c34238e commit e874e79
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/commands/feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c Commands) GetAllFeeds() ([]store.Item, error) {
return []store.Item{}, fmt.Errorf("[commands.go] GetAllFeeds: %w", err)
}

is, err := c.store.GetAllItems()
is, err := c.store.GetAllItems(c.config.Ordering)
if err != nil {
return []store.Item{}, fmt.Errorf("commands.go: GetAllFeeds %w", err)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/commands/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ListKeyMapT struct {
ToggleFavourites key.Binding
Refresh key.Binding
OpenInBrowser key.Binding
Sort key.Binding
oQuit key.Binding
oForceQuit key.Binding
oClearFilter key.Binding
Expand Down Expand Up @@ -73,6 +74,10 @@ var ListKeyMap = ListKeyMapT{
key.WithKeys("o"),
key.WithHelp("o", "open in browser"),
),
Sort: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "sort"),
),
EditConfig: key.NewBinding(
key.WithKeys("E"),
key.WithHelp("E", "edit config in $EDITOR"),
Expand Down Expand Up @@ -171,7 +176,7 @@ func (k ViewportKeyMapT) ShortHelp() []key.Binding {
func (k ListKeyMapT) FullHelp() []key.Binding {
return []key.Binding{
k.Open, k.Read, k.Favourite, k.Refresh,
k.OpenInBrowser, k.ToggleFavourites, k.ToggleReads,
k.OpenInBrowser, k.Sort, k.ToggleFavourites, k.ToggleReads,
k.MarkAllRead, k.EditConfig,
}
}
Expand Down
31 changes: 31 additions & 0 deletions internal/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/charmbracelet/lipgloss"

"github.com/guyfedwards/nom/v2/internal/config"
"github.com/guyfedwards/nom/v2/internal/constants"
"github.com/guyfedwards/nom/v2/internal/store"
)

Expand Down Expand Up @@ -91,6 +92,25 @@ func (m *model) UpdateList() tea.Cmd {
return cmd
}

func sortList(m model) func() tea.Msg {
return func() tea.Msg {
// reverse sorting order
if m.commands.config.Ordering == constants.AscendingOrdering {
m.commands.config.Ordering = constants.DescendingOrdering
} else {
m.commands.config.Ordering = constants.AscendingOrdering
}

items, err := m.commands.GetAllFeeds()
if err != nil {
m.errors = []string{err.Error()}
}
return listUpdate{
items: convertItems(items),
}
}
}

func refreshList(m model) func() tea.Msg {
return func() tea.Msg {
var errorItems []ErrorItem
Expand Down Expand Up @@ -232,6 +252,17 @@ func updateList(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
cmd = m.commands.OpenLink(current.URL)
cmds = append(cmds, cmd)

case key.Matches(msg, ListKeyMap.Sort):
if m.list.SettingFilter() || m.list.IsFiltered() {
break
}

if len(m.list.Items()) == 0 {
return m, m.list.NewStatusMessage("No items to sort.")
}

cmds = append(cmds, sortList(m))

case key.Matches(msg, ListKeyMap.Open):
if m.list.SettingFilter() {
m.list.FilterInput.Blur()
Expand Down
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"

"github.com/guyfedwards/nom/v2/internal/constants"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -57,6 +58,7 @@ type Config struct {
ConfigDir string `yaml:"-"`
Pager string `yaml:"pager,omitempty"`
Feeds []Feed `yaml:"feeds"`
Ordering string `yaml:"ordering"`
// Preview feeds are distinguished from Feeds because we don't want to inadvertenly write those into the config file.
PreviewFeeds []Feed `yaml:"previewfeeds,omitempty"`
Backends *Backends `yaml:"backends,omitempty"`
Expand Down Expand Up @@ -110,6 +112,7 @@ func New(configPath string, pager string, previewFeeds []string, version string)
TitleColor: "62",
FilterColor: "62",
},
Ordering: constants.DefaultOrdering,
HTTPOptions: &HTTPOptions{
MinTLSVersion: tls.VersionName(tls.VersionTLS12),
},
Expand Down Expand Up @@ -150,6 +153,10 @@ func (c *Config) Load() error {
c.HTTPOptions = fileConfig.HTTPOptions
}

if len(fileConfig.Ordering) > 0 {
c.Ordering = fileConfig.Ordering
}

if fileConfig.Theme.Glamour != "" {
c.Theme.Glamour = fileConfig.Theme.Glamour
}
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func TestConfigLoad(t *testing.T) {
if len(c.Feeds) != 3 || c.Feeds[0].URL != "cattle" {
t.Fatalf("Parsing failed")
}

if len(c.Ordering) == 0 || c.Ordering != "desc" {
t.Fatalf("Parsing failed")
}
}

func TestConfigLoadPrecidence(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions internal/constants/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package constants

// Store constants
const (
AscendingOrdering = "asc"
DescendingOrdering = "desc"
DefaultOrdering = AscendingOrdering
)
17 changes: 13 additions & 4 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"time"

"github.com/guyfedwards/nom/v2/internal/constants"
_ "github.com/mattn/go-sqlite3"
)

Expand All @@ -32,7 +33,7 @@ func (i Item) Read() bool {

type Store interface {
UpsertItem(item Item) error
GetAllItems() ([]Item, error)
GetAllItems(ordering string) ([]Item, error)
GetItemByID(ID int) (Item, error)
GetAllFeedURLs() ([]string, error)
ToggleRead(ID int) error
Expand Down Expand Up @@ -184,11 +185,19 @@ func (sls SQLiteStore) UpsertItem(item Item) error {
}

// TODO: pagination
func (sls SQLiteStore) GetAllItems() ([]Item, error) {
stmt := `
select id, feedurl, link, title, content, author, readat, favourite, publishedat, createdat, updatedat from items order by coalesce(publishedat, createdat);
func (sls SQLiteStore) GetAllItems(ordering string) ([]Item, error) {
itemStmt := `
select id, feedurl, link, title, content, author, readat, favourite, publishedat, createdat, updatedat from items order by coalesce(publishedat, createdat) %s;
`

var stmt string
switch ordering {
case constants.DescendingOrdering:
stmt = fmt.Sprintf(itemStmt, constants.DescendingOrdering)
default:
stmt = fmt.Sprintf(itemStmt, constants.DefaultOrdering)
}

rows, err := sls.db.Query(stmt)
if err != nil {
return []Item{}, fmt.Errorf("store.go: GetAllItems: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/test/data/config_fixture.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ feeds:
- url: cattle
- url: bird
- url: dog
ordering: desc

0 comments on commit e874e79

Please sign in to comment.