Skip to content

Commit

Permalink
Selected link and scroll position stays for non-cached pages
Browse files Browse the repository at this point in the history
Fixes #122
  • Loading branch information
makew0rld committed Dec 26, 2021
1 parent 004851f commit 03c4d3e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions display/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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)
}
Expand All @@ -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)
}
3 changes: 3 additions & 0 deletions display/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 34 additions & 2 deletions display/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -290,16 +301,37 @@ 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) {
if t.history.pos < len(t.history.urls)-1 {
// 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.
Expand Down
14 changes: 7 additions & 7 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 03c4d3e

Please sign in to comment.