Adds a WrapWidth param to the page struct so that pages.go does not have to rewrap on every render.

This commit is contained in:
sloum 2020-02-15 08:43:54 -08:00
parent 996e209c50
commit cda502fbb8
2 changed files with 7 additions and 2 deletions

View File

@ -24,6 +24,7 @@ type Page struct {
SearchTerm string SearchTerm string
SearchIndex int SearchIndex int
FileType string FileType string
WrapWidth int
} }
//------------------------------------------------\\ //------------------------------------------------\\
@ -56,6 +57,7 @@ func (p *Page) RenderImage(width int) {
w = 300 w = 300
} }
p.WrappedContent = tdiv.Render([]byte(p.RawContent), w) p.WrappedContent = tdiv.Render([]byte(p.RawContent), w)
p.WrapWidth = width
} }
// WrapContent performs a hard wrap to the requested // WrapContent performs a hard wrap to the requested
@ -131,6 +133,7 @@ func (p *Page) WrapContent(width int, color bool) {
} }
p.WrappedContent = strings.Split(content.String(), "\n") p.WrappedContent = strings.Split(content.String(), "\n")
p.WrapWidth = width
p.HighlightFoundText() p.HighlightFoundText()
} }
@ -180,6 +183,6 @@ func (p *Page) FindText() {
// MakePage returns a Page struct with default values // MakePage returns a Page struct with default values
func MakePage(url Url, content string, links []string) Page { func MakePage(url Url, content string, links []string) Page {
p := Page{make([]string, 0), content, links, url, 0, make([]int, 0), "", 0, ""} p := Page{make([]string, 0), content, links, url, 0, make([]int, 0), "", 0, "", 40}
return p return p
} }

View File

@ -75,7 +75,9 @@ func (p *Pages) Render(termHeight, termWidth int, color bool) []string {
// passed in here. If it is different then go through // passed in here. If it is different then go through
// all of that. Otherwise, just send the already wrapped // all of that. Otherwise, just send the already wrapped
// data. // data.
p.History[p.Position].WrapContent(termWidth, color) if termWidth != p.History[p.Position].WrapWidth {
p.History[p.Position].WrapContent(termWidth, color)
}
now := len(p.History[p.Position].WrappedContent) now := len(p.History[p.Position].WrappedContent)
if prev > now { if prev > now {