Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customise feed titles #15

Merged
merged 6 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ to use with the argument `-db` to the binary.

## Configuration Example (Default config)
It's possible to specify configuration file as a flag, default is `gorss.conf`.

The configuration file can specify URLs of feeds as strings, or, if you want to
customise the name of the feed as it is shown in your Gorss, as objects with url
and name fields. (See the example below for supported options).
```
./gorss -config my.conf
```
Expand All @@ -71,10 +75,10 @@ It's possible to specify configuration file as a flag, default is `gorss.conf`.
"OPMLFile": "../example_ompl.xml",
"feeds": [
"https://news.ycombinator.com/rss",
"https://www.sweclockers.com/feeds/nyheter",
"https://www.reddit.com/r/homeassistant/.rss",
"https://www.reddit.com/r/golang/.rss",
"https://www.reddit.com/r/programming/.rss"
("url": "https://www.sweclockers.com/feeds/nyheter", "name": "Swedish Overclocking"},
("url": "https://www.reddit.com/r/homeassistant/.rss", "name": "Home Assistant"},
("url": "https://www.reddit.com/r/golang/.rss"},
Lallassu marked this conversation as resolved.
Show resolved Hide resolved
{"url": "https://www.reddit.com/r/programming/.rss"}
],
"feedWindowSizeRatio": 2,
"articlePreviewWindowSizeRatio": 5,
Expand Down
8 changes: 4 additions & 4 deletions gorss.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"OPMLFile": "../example_ompl.xml",
"feeds": [
"https://news.ycombinator.com/rss",
"https://www.sweclockers.com/feeds/nyheter",
"https://www.reddit.com/r/homeassistant/.rss",
"https://www.reddit.com/r/golang/.rss",
"https://www.reddit.com/r/programming/.rss"
{"url": "https://www.sweclockers.com/feeds/nyheter", "name": "Swedish Overclocking"},
{"url": "https://www.reddit.com/r/homeassistant/.rss", "name": "Home Assistant"},
{"url": "https://www.reddit.com/r/golang/.rss"},
{"url": "https://www.reddit.com/r/programming/.rss"}
],
"feedWindowSizeRatio": 2,
"articlePreviewWindowSizeRatio": 5,
Expand Down
21 changes: 11 additions & 10 deletions internal/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (

// Article holds the content of a feed item
type Article struct {
c *Controller
id int
feed string
title string
content string
link string
read bool
deleted bool
highlight bool
published time.Time
c *Controller
id int
feed string
feedDisplay string
title string
content string
link string
read bool
deleted bool
highlight bool
published time.Time
}
97 changes: 62 additions & 35 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,47 @@ import (

// Config load the configuration from JSON file
type Config struct {
Highlights []string `json:"highlights"`
Feeds []string `json:"feeds"`
OPMLFile string `json:"opmlFile"`
FeedWindowSizeRatio int `json:"feedWindowSizeRatio"`
ArticleWindowSizeRatio int `json:"articleWindowSizeRatio"`
PreviewWindowSizeRatio int `json:"previewWindowSizeRatio"`
ArticlePreviewWindowSizeRatio int `json:"articlePreviewWindowSizeRatio"`
SecondsBetweenUpdates int `json:"secondsBetweenUpdates"`
SkipArticlesOlderThanDays int `json:"skipArticlesOlderThanDays"`
DaysToKeepDeletedArticlesInDB int `json:"daysToKeepDeletedArticlesInDB"`
DaysToKeepReadArticlesInDB int `json:"daysToKeepReadArticlesInDB"`
SkipPreviewInTab bool `json:"skipPreviewInTab"`
KeyOpenLink string `json:"keyOpenLink"`
KeyMarkLink string `json:"keyMarkLink"`
KeyOpenMarked string `json:"keyOpenMarked"`
KeyDeleteArticle string `json:"keyDeleteArticle"`
KeyMoveDown string `json:"keyMoveDown"`
KeyMoveUp string `json:"keyMoveUp"`
KeySortByDate string `json:"keySortByDate"`
KeySortByTitle string `json:"keySortByTitle"`
KeySortByUnread string `json:"keySortByUnread"`
KeySortByFeed string `json:"keySortByFeed"`
KeyUpdateFeeds string `json:"keyUpdateFeeds"`
KeyMarkAllRead string `json:"keyMarkAllRead"`
KeyMarkAllUnread string `json:"keyMarkAllUnread"`
KeyTogglePreview string `json:"keyTogglePreview"`
KeySelectFeedWindow string `json:"keySelectFeedWindow"`
KeySelectArticleWindow string `json:"keySelectArticleWindow"`
KeySelectPreviewWindow string `json:"keySelectPreviewWindow"`
KeyToggleHelp string `json:"keyToggleHelp"`
KeySwitchWindows string `json:"keySwitchWindows"`
KeyQuit string `json:"keyQuit"`
KeyUndoLastRead string `json:"keyUndoLastRead"`
CustomCommands []Command `json:"customCommands"`
Highlights []string `json:"highlights"`
InputFeeds []interface{} `json:"feeds"`
Feeds []Feed `json:"-"`
OPMLFile string `json:"opmlFile"`
FeedWindowSizeRatio int `json:"feedWindowSizeRatio"`
ArticleWindowSizeRatio int `json:"articleWindowSizeRatio"`
PreviewWindowSizeRatio int `json:"previewWindowSizeRatio"`
ArticlePreviewWindowSizeRatio int `json:"articlePreviewWindowSizeRatio"`
SecondsBetweenUpdates int `json:"secondsBetweenUpdates"`
SkipArticlesOlderThanDays int `json:"skipArticlesOlderThanDays"`
DaysToKeepDeletedArticlesInDB int `json:"daysToKeepDeletedArticlesInDB"`
DaysToKeepReadArticlesInDB int `json:"daysToKeepReadArticlesInDB"`
SkipPreviewInTab bool `json:"skipPreviewInTab"`
KeyOpenLink string `json:"keyOpenLink"`
KeyMarkLink string `json:"keyMarkLink"`
KeyOpenMarked string `json:"keyOpenMarked"`
KeyDeleteArticle string `json:"keyDeleteArticle"`
KeyMoveDown string `json:"keyMoveDown"`
KeyMoveUp string `json:"keyMoveUp"`
KeySortByDate string `json:"keySortByDate"`
KeySortByTitle string `json:"keySortByTitle"`
KeySortByUnread string `json:"keySortByUnread"`
KeySortByFeed string `json:"keySortByFeed"`
KeyUpdateFeeds string `json:"keyUpdateFeeds"`
KeyMarkAllRead string `json:"keyMarkAllRead"`
KeyMarkAllUnread string `json:"keyMarkAllUnread"`
KeyTogglePreview string `json:"keyTogglePreview"`
KeySelectFeedWindow string `json:"keySelectFeedWindow"`
KeySelectArticleWindow string `json:"keySelectArticleWindow"`
KeySelectPreviewWindow string `json:"keySelectPreviewWindow"`
KeyToggleHelp string `json:"keyToggleHelp"`
KeySwitchWindows string `json:"keySwitchWindows"`
KeyQuit string `json:"keyQuit"`
KeyUndoLastRead string `json:"keyUndoLastRead"`
CustomCommands []Command `json:"customCommands"`
}

// Feed -
type Feed struct {
URL string
Name string
}

// Command is used to parse a custom key->command from configuration file.
Expand All @@ -69,8 +76,28 @@ func LoadConfiguration(file string) Config {
log.Fatal("Failed to parse config file:", err)
}

// Convert Feeds to []Feed{}
conf.Feeds = make([]Feed, len(conf.InputFeeds))
for idx := range conf.InputFeeds {
switch v := conf.InputFeeds[idx].(type) {
case string:
// Old style
conf.Feeds[idx] = Feed{URL: v}
case map[string]interface{}:
// New style
url := v["url"].(string)
name := ""
if _, ok := v["name"]; ok {
name = v["name"].(string)
}
conf.Feeds[idx] = Feed{URL: url, Name: name}
default:
log.Fatalf("unable to convert %v to a feed", v)
}
}

// Validate that no keys are the same
keys := make(map[string]struct{}, 0)
keys := make(map[string]struct{})
val := reflect.Indirect(reflect.ValueOf(conf))
for i := 0; i < val.NumField(); i++ {
if strings.HasPrefix(val.Type().Field(i).Name, "Key") {
Expand Down
54 changes: 31 additions & 23 deletions internal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package internal

import (
"fmt"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"log"
"os"
"os/exec"
Expand All @@ -13,6 +11,9 @@ import (
"strings"
"sync"
"time"

"github.com/gdamore/tcell"
"github.com/rivo/tview"
)

// Controller handles the logic and keep everything together
Expand Down Expand Up @@ -68,7 +69,7 @@ func (c *Controller) Init(cfg, theme, db string) {
// GetConfigKeys creates a list of keys that we want to present
// in the help section.
func (c *Controller) GetConfigKeys() map[string]string {
keys := make(map[string]string, 0)
keys := make(map[string]string)

keys["Open Link"] = c.conf.KeyOpenLink
keys["Mark Link"] = c.conf.KeyMarkLink
Expand Down Expand Up @@ -136,10 +137,10 @@ func (c *Controller) Quit() {
func (c *Controller) UpdateFeeds() {
c.rss.Update()
for _, f := range c.rss.feeds {
if f == nil {
if f.feed == nil {
continue
}
for _, item := range f.Items {
for _, item := range f.feed.Items {
if item == nil {
continue
}
Expand All @@ -165,13 +166,14 @@ func (c *Controller) UpdateFeeds() {
content = item.Content
}
a := Article{
c: c,
feed: f.Title,
title: item.Title,
content: content,
link: item.Link,
published: published,
read: false,
c: c,
feed: f.feed.Title,
title: item.Title,
content: content,
link: item.Link,
published: published,
read: false,
feedDisplay: f.displayName,
}
// Make sure the same article doesn't exists.
exists := false
Expand Down Expand Up @@ -239,43 +241,49 @@ func (c *Controller) ShowFeeds() {
hc++
}
}
c.win.AddToFeeds(fmt.Sprintf("[%s]Highlight", c.theme.Highlights), hc, total, &Article{feed: "highlight"})
c.win.AddToFeeds(fmt.Sprintf("[%s]Highlight", c.theme.Highlights), "", hc, total, &Article{feed: "highlight"})

feeds := make(map[string]int, 0)
feedsTotal := make(map[string]int, 0)
type feed struct {
count int
display string
}
feeds := make(map[string]*feed)
feedsTotal := make(map[string]int)
urTotal := 0
total = 0
for _, a := range c.articles {
total++
if _, ok := feeds[a.feed]; !ok {
feeds[a.feed] = 0
feeds[a.feed] = &feed{0, a.feedDisplay}
feedsTotal[a.feed] = 0
}
feedsTotal[a.feed]++
if !a.read {
feeds[a.feed]++
feeds[a.feed].count++
urTotal++
}
}

c.win.AddToFeeds("Unread", urTotal, urTotal, &Article{feed: "unread"})
c.win.AddToFeeds("Unread", "", urTotal, urTotal, &Article{feed: "unread"})

// If there are no unread left, then we remove the prevArticle so that
// we don't add it again when updating the window.
if urTotal == 0 {
c.prevArticle = nil
}

c.win.AddToFeeds("All Articles", urTotal, total, &Article{feed: "allarticles"})
c.win.AddToFeeds("All Articles", "", urTotal, total, &Article{feed: "allarticles"})

var keys []string
var keys [][2]string
for k := range feeds {
keys = append(keys, k)
keys = append(keys, [2]string{k, feeds[k].display})
}
sort.Strings(keys)
sort.SliceStable(keys, func(i, j int) bool {
return keys[i][0] < keys[j][0]
})

for _, k := range keys {
c.win.AddToFeeds(k, feeds[k], feedsTotal[k], &Article{feed: k})
c.win.AddToFeeds(k[0], k[1], feeds[k[0]].count, feedsTotal[k[0]], &Article{feed: k[0]})
}
}

Expand Down
16 changes: 9 additions & 7 deletions internal/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ func (d *DB) Init(c *Controller, dbFile string) error {

_, err = d.db.Exec(`
create table if not exists articles(
id integer not null primary key,
id integer not null primary key,
feed text,
title text,
title text,
content text,
link text,
read bool,
display_name string,
deleted bool,
published DATETIME
);`)
Expand Down Expand Up @@ -75,7 +76,7 @@ func (d *DB) CleanupDB() {

// All fetches all articles from the database
func (d *DB) All() []Article {
st, err := d.db.Prepare("select id,feed,title,content,published,link,read from articles where deleted = false order by id")
st, err := d.db.Prepare("select id,feed,title,content,published,link,read,display_name from articles where deleted = false order by id")
if err != nil {
log.Println(err)
return nil
Expand All @@ -96,13 +97,14 @@ func (d *DB) All() []Article {
feed string
link string
read bool
display string
published time.Time
)

articles := []Article{}

for rows.Next() {
err = rows.Scan(&id, &feed, &title, &content, &published, &link, &read)
err = rows.Scan(&id, &feed, &title, &content, &published, &link, &read, &display)
if err != nil {
log.Println(err)
}
Expand All @@ -121,7 +123,7 @@ func (d *DB) All() []Article {
break
}
}
articles = append(articles, Article{id: id, highlight: highlight, feed: feed, title: title, content: content, published: published, link: link, read: read})
articles = append(articles, Article{id: id, highlight: highlight, feed: feed, title: title, content: content, published: published, link: link, read: read, feedDisplay: display})
}
return articles
}
Expand Down Expand Up @@ -149,13 +151,13 @@ func (d *DB) Save(a Article) error {
log.Println(err)
}

st, err = tx.Prepare("insert into articles(feed, title, content, link, read, published, deleted) values(?, ?, ?, ?, ?,?,?)")
st, err = tx.Prepare("insert into articles(feed, title, content, link, read, display_name, published, deleted) values(?, ?, ?, ?, ?, ?, ?,?)")
if err != nil {
log.Println(err)
}
defer st.Close()

if _, err := st.Exec(a.feed, a.title, a.content, a.link, false, a.published, false); err != nil {
if _, err := st.Exec(a.feed, a.title, a.content, a.link, false, a.feedDisplay, a.published, false); err != nil {
log.Println(err)
}

Expand Down
Loading