🚧 Feeds page rendering, untested

This commit is contained in:
makeworld 2020-08-17 15:33:53 -04:00
parent c63861230e
commit e5fe550bc2
4 changed files with 100 additions and 15 deletions

View File

@ -106,18 +106,19 @@ func openBkmkModal(name string, exists bool) (string, int) {
return bkmkModalText, action
}
var bkmkPageRaw = "# Bookmarks\r\n\r\n"
// Bookmarks displays the bookmarks page on the current tab.
func Bookmarks(t *tab) {
// Gather bookmarks
rawContent := "# Bookmarks\r\n\r\n"
m, keys := bookmarks.All()
for i := range keys {
rawContent += fmt.Sprintf("=> %s %s\r\n", keys[i], m[keys[i]])
bkmkPageRaw += fmt.Sprintf("=> %s %s\r\n", keys[i], m[keys[i]])
}
// Render and display
content, links := renderer.RenderGemini(rawContent, textWidth(), leftMargin())
content, links := renderer.RenderGemini(bkmkPageRaw, textWidth(), leftMargin())
page := structs.Page{
Raw: rawContent,
Raw: bkmkPageRaw,
Content: content,
Links: links,
Url: "about:bookmarks",

44
display/feeds.go Normal file
View File

@ -0,0 +1,44 @@
package display
import (
"fmt"
"strings"
"github.com/makeworld-the-better-one/amfora/feeds"
"github.com/makeworld-the-better-one/amfora/renderer"
"github.com/makeworld-the-better-one/amfora/structs"
)
var feedPageRaw = "# Feeds & Pages\n\nUpdates" + strings.Repeat(" ", 80-25) + "[Newest -> Oldest]\n" +
strings.Repeat("-", 80) + "\n\n"
// Feeds displays the feeds page on the current tab.
func Feeds(t *tab) {
// TODO; Decide about date in local time vs UTC
// TODO: Cache
pe := feeds.GetPageEntries()
curDay := time.Time.Round(time.Day)
for _, entry := range pe.Entries {
if entry.Published.Round(time.Day).After(curDay) {
// This post is on a new day, add a day header
curDay := entry.Published.Round(time.Day)
feedPageRaw += fmt.Sprintf("\n## %s\n\n", curDay.Format("Jan 02, 2006"))
}
feedPageRaw += fmt.Sprintf("=>%s %s - %s\n", entry.URL, entry.Author, entry.Title)
}
content, links := renderer.RenderGemini(feedPageRaw, textWidth(), leftMargin())
page := structs.Page{
Raw: feedPageRaw,
Content: content,
Links: links,
Url: "about:feeds",
Width: termW,
Mediatype: structs.TextGemini,
}
setPage(t, &page)
t.applyBottomBar()
}

View File

@ -7,6 +7,7 @@ import (
"fmt"
"io"
"mime"
urlPkg "net/url"
"os"
"path"
"sort"
@ -40,9 +41,9 @@ func Init() error {
if err != nil && err != io.EOF {
return fmt.Errorf("feeds json is corrupted: %v", err)
}
return nil
// TODO: Start pulling all feeds in another thread
go updateAll()
return nil
}
// IsTracked returns true if the feed/page URL is already being tracked.
@ -111,7 +112,6 @@ func AddFeed(url string, feed *gofeed.Feed) error {
panic("feed is nil")
}
sort.Sort(feed)
// Remove any content to save memory and disk space
for _, item := range feed.Items {
item.Content = ""
@ -262,7 +262,6 @@ func updateAll() {
pageKeys[i] = k
i++
}
data.RUnlock()
for j := 0; j < numJobs; j++ {
@ -276,3 +275,51 @@ func updateAll() {
wg.Wait()
}
// GetPageEntries returns the current list of PageEntries
// for use in rendering a page.
// The contents of the entries will never change, and this
// function should be called again to get updates.
func GetPageEntries() *PageEntries {
var pe PageEntries
data.RLock()
for _, feed := range data.Feeds {
for _, item := range feed.Items {
var pub time.Time
if !item.UpdatedParsed.IsZero() {
pub = *item.UpdatedParsed
} else if !item.PublishedParsed.IsZero() {
pub = *item.PublishedParsed
} else {
// No time on the post
pub = time.Now()
}
pe.Entries = append(pe.Entries, &PageEntry{
Author: feed.Author.Name,
Title: item.Title,
URL: item.Link,
Published: pub,
})
}
}
for url, page := range data.Pages {
parsed, _ := urlPkg.Parse(url)
pe.Entries = append(pe.Entries, &PageEntry{
Author: parsed.Host, // Domain is author
Title: path.Base(parsed.Path), // Filename is title
URL: url,
Published: page.Changed,
})
}
data.RUnlock()
sort.Sort(&pe)
return &pe
}

View File

@ -82,26 +82,19 @@ type PageEntry struct {
// PageEntries is new-to-old list of Entry structs, used to create a feed page.
// It should always be assumed to be sorted when used in other packages.
type PageEntries struct {
sync.RWMutex
Entries []*PageEntry
}
// Implement sort.Interface
func (e *PageEntries) Len() int {
e.RLock()
defer e.RUnlock()
return len(e.Entries)
}
func (e *PageEntries) Less(i, j int) bool {
e.RLock()
defer e.RUnlock()
return e.Entries[i].Published.Before(e.Entries[j].Published)
}
func (e *PageEntries) Swap(i, j int) {
e.Lock()
e.Entries[i], e.Entries[j] = e.Entries[j], e.Entries[i]
e.Unlock()
}