From 4c35eaad3b829115eb6aeee5ed4945e037f08fda Mon Sep 17 00:00:00 2001 From: Nate Dobbins Date: Mon, 17 Jun 2019 13:22:12 -0500 Subject: [PATCH] Add flag to specify number of articles per source --- openring.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/openring.go b/openring.go index 1a8c79f..7ea5b91 100644 --- a/openring.go +++ b/openring.go @@ -27,12 +27,13 @@ type Article struct { func main() { var ( - narticles int = 3 - summaryLen int = 256 + narticles int = 3 + perSource int = 1 + summaryLen int = 256 sources []*url.URL ) - opts, optind, err := getopt.Getopts(os.Args[1:], "l:n:s:") + opts, optind, err := getopt.Getopts(os.Args[1:], "l:n:p:s:") if err != nil { panic(err) } @@ -48,6 +49,11 @@ func main() { if err != nil { panic(err) } + case 'p': + perSource, err = strconv.Atoi(opt.Value) + if err != nil { + panic(err) + } case 's': u, err := url.Parse(opt.Value) if err != nil { @@ -103,17 +109,22 @@ func main() { log.Printf("Warning: feed %s has no items", feed.Title) continue } - item := feed.Items[0] - summary := runewidth.Truncate( - policy.Sanitize(item.Summary), summaryLen, "…") - articles = append(articles, &Article{ - Date: item.Date, - SourceLink: feed.Link, - SourceTitle: feed.Title, - Summary: template.HTML(summary), - Title: item.Title, - Link: item.Link, - }) + items := feed.Items + if len(items) > perSource { + items = items[:perSource] + } + for _, item := range items { + summary := runewidth.Truncate( + policy.Sanitize(item.Summary), summaryLen, "…") + articles = append(articles, &Article{ + Date: item.Date, + SourceLink: feed.Link, + SourceTitle: feed.Title, + Summary: template.HTML(summary), + Title: item.Title, + Link: item.Link, + }) + } } sort.Slice(articles, func(i, j int) bool { return articles[i].Date.After(articles[j].Date)