From e09c4f0d3bbf177eadb2ff5cb4b74ac8f1f39dca Mon Sep 17 00:00:00 2001 From: giuliano Date: Thu, 22 Feb 2024 00:18:54 +0100 Subject: [PATCH] Fix issue #33 --- internal/display/display.go | 14 ++++++++++++++ internal/display/loading.go | 26 ++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/internal/display/display.go b/internal/display/display.go index 777210c..d58294e 100644 --- a/internal/display/display.go +++ b/internal/display/display.go @@ -5,6 +5,8 @@ import ( "fmt" "log" "os" + "sort" + "strings" "sync" "time" "unicode/utf8" @@ -287,6 +289,18 @@ func (d *display) appendToRaw(s string) { d.raw = append(d.raw, []byte(s)) } +func (d *display) indexOf(url string) int { + sort.SliceStable(d.config.Feeds, func(i, j int) bool { + return strings.ToLower(d.config.Feeds[i].Name) < strings.ToLower(d.config.Feeds[j].Name) + }) + for idx, f := range d.config.Feeds { + if f.Url == url { + return idx + } + } + return -1 +} + func (d *display) appendToRendered(cells []*cell) { d.rendered = append(d.rendered, cells) } diff --git a/internal/display/loading.go b/internal/display/loading.go index 42a7c19..7a1a2d8 100644 --- a/internal/display/loading.go +++ b/internal/display/loading.go @@ -262,11 +262,33 @@ func (d *display) addNewFeed() { return } + d.cache.Merge(d.config) + d.appendToRaw(url) + d.resetRows() + + sort.SliceStable(d.config.Feeds, func(i, j int) bool { + return strings.ToLower(d.config.Feeds[i].Name) < strings.ToLower(d.config.Feeds[j].Name) + }) + for _, f := range d.config.Feeds { + d.appendToRaw(f.Url) + } + + d.renderFeedList() + + idx := d.indexOf(url) + if idx == -1 { + log.Default().Println("cannot find url:", url) + d.setTmpBottomMessage(2*time.Second, "cannot find the find you added!") + return + } + idx++ + d.current.cx = 1 - d.current.cy = len(d.raw) % d.getContentWindowLen() - d.current.startoff = (len(d.raw) - 1) / d.getContentWindowLen() * d.getContentWindowLen() + max := d.getContentWindowLen() + d.current.cy = idx % max + d.current.startoff = idx / max * max d.setBottomMessage(urlsListSectionMsg) d.setTmpBottomMessage(2*time.Second, "new feed saved!")