1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-12-04 14:46:29 -05: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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
## Changed ### Fixed
- Actual unicode bullet symbol is used for lists, U+2022 - 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 ## [1.1.0] - 2020-06-24
### Added ### 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 // 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. // of the old not shifted tab on startup.
if tabMap[curTab] == &newTabPage { 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() curTab = NumTabs()
tabMap[curTab] = &newTabPage tabMap[curTab] = &newTabPage
setLeftMargin(tabMap[curTab])
tabViews[curTab] = cview.NewTextView(). tabViews[curTab] = cview.NewTextView().
SetDynamicColors(true). SetDynamicColors(true).
SetRegions(true). SetRegions(true).
SetScrollable(true). SetScrollable(true).
SetWrap(false). SetWrap(false).
SetText(addLeftMargin(renderedNewTabContent)). SetText(tabMap[curTab].Content).
ScrollToBeginning(). ScrollToBeginning().
SetChangedFunc(func() { SetChangedFunc(func() {
App.Draw() App.Draw()
@ -426,6 +428,7 @@ func SwitchTab(tab int) {
func Reload() { func Reload() {
cache.Remove(tabMap[curTab].Url) cache.Remove(tabMap[curTab].Url)
tabMap[curTab].LeftMargin = 0 // Redo left margin
go handleURL(tabMap[curTab].Url) go handleURL(tabMap[curTab].Url)
} }

View File

@ -132,21 +132,40 @@ func followLink(prev, next string) {
}() }()
} }
func addLeftMargin(text string) string { func setLeftMargin(p *structs.Page) {
lM := leftMargin()
if lM != p.LeftMargin {
// Left margin needs to be added or changed
var shifted string var shifted string
for _, line := range strings.Split(text, "\n") {
shifted += strings.Repeat(" ", leftMargin()) + line + "\n" 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. // setPage displays a Page on the current tab.
func setPage(p *structs.Page) { func setPage(p *structs.Page) {
saveScroll() // Save the scroll of the previous page saveScroll() // Save the scroll of the previous page
setLeftMargin(p)
// Change page on screen // Change page on screen
tabMap[curTab] = p tabMap[curTab] = p
tabViews[curTab].SetText(addLeftMargin(p.Content)) tabViews[curTab].SetText(p.Content)
tabViews[curTab].Highlight("") // Turn off highlights tabViews[curTab].Highlight("") // Turn off highlights
tabViews[curTab].ScrollToBeginning() tabViews[curTab].ScrollToBeginning()

View File

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

View File

@ -7,7 +7,7 @@ type Page struct {
Links []string // URLs, for each region in the content. Links []string // URLs, for each region in the content.
Row int // Scroll position Row int // Scroll position
Column int // ditto Column int // ditto
//Displayable bool // Set to true once the content has been modified to display nicely on the screen - margins added 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. // Size returns an approx. size of a Page in bytes.