From 03c4d3e2862556c9c662a2cd296271fb96040f32 Mon Sep 17 00:00:00 2001 From: makeworld Date: Sun, 26 Dec 2021 16:22:29 -0500 Subject: [PATCH] Selected link and scroll position stays for non-cached pages Fixes #122 --- CHANGELOG.md | 1 + display/history.go | 20 ++++++++++++++++++++ display/private.go | 3 +++ display/tab.go | 36 ++++++++++++++++++++++++++++++++++-- logger/logger.go | 14 +++++++------- 5 files changed, 65 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77a370e..1b32332 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Syntax highlighting for preformatted text blocks with alt text (#252, #263, [wiki page](https://github.com/makeworld-the-better-one/amfora/wiki/Source-Code-Highlighting)) - [Client certificates](https://github.com/makeworld-the-better-one/amfora/wiki/Client-Certificates) can be restricted to certain paths of a host (#115) - `header` config option in `[subscriptions]` to allow disabling the header text on the subscriptions page (#191) +- Selected link and scroll position stays for non-cached pages (#122) ### Changed - Center text automatically, removing `left_margin` from the config (#233) diff --git a/display/history.go b/display/history.go index 333adcc..313d941 100644 --- a/display/history.go +++ b/display/history.go @@ -3,6 +3,18 @@ package display // applyHist is a history.go internal function, to load a URL in the history. func applyHist(t *tab) { handleURL(t, t.history.urls[t.history.pos], 0) // Load that position in history + + // Set page's scroll and link info from history cache, in case it didn't have it in the page already + // Like for non-cached pages like about: pages + // This fixes #122 + pg := t.history.pageCache[t.history.pos] + p := t.page + p.Row = pg.row + p.Column = pg.column + p.Selected = pg.selected + p.SelectedID = pg.selectedID + p.Mode = pg.mode + t.applyAll() } @@ -11,6 +23,10 @@ func histForward(t *tab) { // Already on the most recent URL in the history return } + + // Update page cache in history for #122 + t.historyCachePage() + t.history.pos++ go applyHist(t) } @@ -20,6 +36,10 @@ func histBack(t *tab) { // First tab in history return } + + // Update page cache in history for #122 + t.historyCachePage() + t.history.pos-- go applyHist(t) } diff --git a/display/private.go b/display/private.go index d118dce..0405971 100644 --- a/display/private.go +++ b/display/private.go @@ -137,6 +137,9 @@ func setPage(t *tab, p *structs.Page) { // // It should be called in a goroutine. func goURL(t *tab, u string) { + // Update page cache in history for #122 + t.historyCachePage() + final, displayed := handleURL(t, u, 0) if displayed { t.addToHistory(final) diff --git a/display/tab.go b/display/tab.go index 5074f2f..ba20106 100644 --- a/display/tab.go +++ b/display/tab.go @@ -20,9 +20,20 @@ const ( tabModeLoading ) +// tabHistoryPageCache is fields from the Page struct, cached here to solve #122 +// See structs/structs.go for an explanation of the fields. +type tabHistoryPageCache struct { + row int + column int + selected string + selectedID string + mode structs.PageMode +} + type tabHistory struct { - urls []string - pos int // Position: where in the list of URLs we are + urls []string + pos int // Position: where in the list of URLs we are + pageCache []*tabHistoryPageCache } // tab hold the information needed for each browser tab. @@ -290,6 +301,21 @@ func makeNewTab() *tab { return &t } +// historyCachePage caches certain info about the current page in the tab's history, +// see #122 for details. +func (t *tab) historyCachePage() { + if t.page == nil || t.page.URL == "" || t.history.pageCache == nil || len(t.history.pageCache) == 0 { + return + } + t.history.pageCache[t.history.pos] = &tabHistoryPageCache{ + row: t.page.Row, + column: t.page.Column, + selected: t.page.Selected, + selectedID: t.page.SelectedID, + mode: t.page.Mode, + } +} + // addToHistory adds the given URL to history. // It assumes the URL is currently being loaded and displayed on the page. func (t *tab) addToHistory(u string) { @@ -297,9 +323,15 @@ func (t *tab) addToHistory(u string) { // We're somewhere in the middle of the history instead, with URLs ahead and behind. // The URLs ahead need to be removed so this new URL is the most recent item in the history t.history.urls = t.history.urls[:t.history.pos+1] + // Same for page cache + t.history.pageCache = t.history.pageCache[:t.history.pos+1] } t.history.urls = append(t.history.urls, u) t.history.pos++ + + // Cache page info for #122 + t.history.pageCache = append(t.history.pageCache, &tabHistoryPageCache{}) // Add new spot + t.historyCachePage() // Fill it with data } // pageUp scrolls up 75% of the height of the terminal, like Bombadillo. diff --git a/logger/logger.go b/logger/logger.go index 3f17861..6160e48 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -9,11 +9,11 @@ import ( "os" ) -var logger *log.Logger +var Logger *log.Logger func GetLogger() (*log.Logger, error) { - if logger != nil { - return logger, nil + if Logger != nil { + return Logger, nil } var writer io.Writer @@ -30,15 +30,15 @@ func GetLogger() (*log.Logger, error) { writer = ioutil.Discard } - logger = log.New(writer, "", log.LstdFlags) + Logger = log.New(writer, "", log.LstdFlags) if !debugModeEnabled { // Clear all flags to skip log output formatting step to increase // performance somewhat if we're not logging anything - logger.SetFlags(0) + Logger.SetFlags(0) } - logger.Println("Started logger") + Logger.Println("Started logger") - return logger, nil + return Logger, nil }