1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-06-19 19:25:24 +00:00

️ Fix perf on loading long cached pages - #26

This commit is contained in:
makeworld 2020-06-28 18:09:52 -04:00
parent 7500d33de9
commit 6b1882f1a4
5 changed files with 41 additions and 17 deletions

View File

@ -5,8 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## Changed
- Actual unicode bullet symbol is used for lists, U+2022
### Fixed
- Actual unicode bullet symbol is used for lists: U+2022
- Performance when loading very long cached pages improved (#26)
## [1.1.0] - 2020-06-24
### Added

View File

@ -72,7 +72,8 @@ var App = cview.NewApplication().
// XXX: This is hacky but works. The biggest issue is that there will sometimes be a tiny flash
// of the old not shifted tab on startup.
if tabMap[curTab] == &newTabPage {
tabViews[curTab].SetText(addLeftMargin(renderedNewTabContent))
setLeftMargin(tabMap[curTab])
tabViews[curTab].SetText(tabMap[curTab].Content)
}
})
@ -289,12 +290,13 @@ func NewTab() {
curTab = NumTabs()
tabMap[curTab] = &newTabPage
setLeftMargin(tabMap[curTab])
tabViews[curTab] = cview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetScrollable(true).
SetWrap(false).
SetText(addLeftMargin(renderedNewTabContent)).
SetText(tabMap[curTab].Content).
ScrollToBeginning().
SetChangedFunc(func() {
App.Draw()
@ -426,6 +428,7 @@ func SwitchTab(tab int) {
func Reload() {
cache.Remove(tabMap[curTab].Url)
tabMap[curTab].LeftMargin = 0 // Redo left margin
go handleURL(tabMap[curTab].Url)
}

View File

@ -132,21 +132,40 @@ func followLink(prev, next string) {
}()
}
func addLeftMargin(text string) string {
var shifted string
for _, line := range strings.Split(text, "\n") {
shifted += strings.Repeat(" ", leftMargin()) + line + "\n"
func setLeftMargin(p *structs.Page) {
lM := leftMargin()
if lM != p.LeftMargin {
// Left margin needs to be added or changed
var shifted string
if p.LeftMargin < 1 {
// The page content doesn't have a margin yet
lines := strings.Split(p.Content, "\n")
for i := range lines {
shifted += strings.Repeat(" ", lM) + lines[i] + "\n"
}
} else {
// Old margin needs to be removed, new one added
lines := strings.Split(p.Content, "\n")
for i := range lines {
shifted += strings.Repeat(" ", lM) + lines[i][p.LeftMargin:] + "\n"
}
}
p.Content = shifted
p.LeftMargin = lM
}
return shifted
}
// setPage displays a Page on the current tab.
func setPage(p *structs.Page) {
saveScroll() // Save the scroll of the previous page
setLeftMargin(p)
// Change page on screen
tabMap[curTab] = p
tabViews[curTab].SetText(addLeftMargin(p.Content))
tabViews[curTab].SetText(p.Content)
tabViews[curTab].Highlight("") // Turn off highlights
tabViews[curTab].ScrollToBeginning()

View File

@ -83,10 +83,11 @@ func MakePage(url string, res *gemini.Response, width int) (*structs.Page, error
Links: links,
}, nil
} else if strings.HasPrefix(mediatype, "text/") {
// Treated as plaintext
return &structs.Page{
Url: url,
Content: utfText,
Links: []string{}, // Non-gemini links are not supported
Links: []string{},
}, nil
}

View File

@ -2,12 +2,12 @@ package structs
// Page is for storing UTF-8 text/gemini pages, as well as text/plain pages.
type Page struct {
Url string
Content string // The processed content, NOT raw. Uses cview colour tags. All link/link texts must have region tags.
Links []string // URLs, for each region in the content.
Row int // Scroll position
Column int // ditto
//Displayable bool // Set to true once the content has been modified to display nicely on the screen - margins added
Url string
Content string // The processed content, NOT raw. Uses cview colour tags. All link/link texts must have region tags.
Links []string // URLs, for each region in the content.
Row int // Scroll position
Column int // ditto
LeftMargin int // <1 when the content is unmodified. Otherwise it indicates how many spaces have been prepended to each line
}
// Size returns an approx. size of a Page in bytes.