Finally solved infuriating rendering issue

This commit is contained in:
sloumdrone 2019-12-17 22:32:45 -08:00
parent f68996decf
commit 9e7ac3c866
2 changed files with 57 additions and 29 deletions

View File

@ -238,7 +238,7 @@ func (c *client) TakeControlInput() {
case 'n': case 'n':
// Next search item // Next search item
c.ClearMessage() c.ClearMessage()
err := c.NextSearchItem() err := c.NextSearchItem(1)
if err != nil { if err != nil {
c.SetMessage(err.Error(), false) c.SetMessage(err.Error(), false)
c.DrawMessage() c.DrawMessage()
@ -246,7 +246,7 @@ func (c *client) TakeControlInput() {
case 'N': case 'N':
// Previous search item // Previous search item
c.ClearMessage() c.ClearMessage()
err := c.PreviousSearchItem() err := c.NextSearchItem(-1)
if err != nil { if err != nil {
c.SetMessage(err.Error(), false) c.SetMessage(err.Error(), false)
c.DrawMessage() c.DrawMessage()
@ -270,6 +270,7 @@ func (c *client) TakeControlInput() {
c.SetMessage(err.Error(), true) c.SetMessage(err.Error(), true)
c.DrawMessage() c.DrawMessage()
} }
c.NextSearchItem(0)
case ':', ' ': case ':', ' ':
// Process a command // Process a command
c.ClearMessage() c.ClearMessage()
@ -1089,46 +1090,61 @@ func (c *client) handleWeb(u Url) {
func (c *client) find(s string) error { func (c *client) find(s string) error {
c.PageState.History[c.PageState.Position].SearchTerm = s c.PageState.History[c.PageState.Position].SearchTerm = s
c.PageState.History[c.PageState.Position].FindText() c.PageState.History[c.PageState.Position].FindText()
if s == "" {
return nil
}
if len(c.PageState.History[c.PageState.Position].FoundLinkLines) == 0 { if len(c.PageState.History[c.PageState.Position].FoundLinkLines) == 0 {
return fmt.Errorf("No text matching %q was found", s) return fmt.Errorf("No text matching %q was found", s)
} }
loc := c.PageState.History[c.PageState.Position].ScrollPosition
diff := c.PageState.History[c.PageState.Position].FoundLinkLines[0] - loc
c.Scroll(diff)
c.Draw()
return nil return nil
} }
func (c *client) NextSearchItem() error { func (c *client) NextSearchItem(dir int) error {
if len(c.PageState.History[c.PageState.Position].FoundLinkLines) == 0 { page := c.PageState.History[c.PageState.Position]
if len(page.FoundLinkLines) == 0 {
return fmt.Errorf("The search is over before it has begun") return fmt.Errorf("The search is over before it has begun")
} }
c.PageState.History[c.PageState.Position].SearchIndex++ c.PageState.History[c.PageState.Position].SearchIndex += dir
if c.PageState.History[c.PageState.Position].SearchIndex >= len(c.PageState.History[c.PageState.Position].FoundLinkLines) { if c.PageState.History[c.PageState.Position].SearchIndex < 0 {
c.PageState.History[c.PageState.Position].SearchIndex = 0 c.PageState.History[c.PageState.Position].SearchIndex = 0
} }
loc := c.PageState.History[c.PageState.Position].ScrollPosition if page.SearchIndex+dir >= len(page.FoundLinkLines) {
diff := c.PageState.History[c.PageState.Position].FoundLinkLines[c.PageState.History[c.PageState.Position].SearchIndex] - loc c.PageState.History[c.PageState.Position].SearchIndex = len(page.FoundLinkLines) - 1
c.Scroll(diff) return fmt.Errorf("The search path goes no further")
} else if page.SearchIndex+dir < 0 {
c.PageState.History[c.PageState.Position].SearchIndex = 0
return fmt.Errorf("You are at the beginning of the search path")
}
index := c.PageState.History[c.PageState.Position].SearchIndex
diff := c.PageState.History[c.PageState.Position].FoundLinkLines[index] - page.ScrollPosition
c.ScrollForSearch(diff)
c.Draw() c.Draw()
return nil return nil
} }
func (c *client) PreviousSearchItem() error { func (c *client) ScrollForSearch(amount int) {
if len(c.PageState.History[c.PageState.Position].FoundLinkLines) == 0 { var percentRead int
return fmt.Errorf("The search is over before it has begun") page := c.PageState.History[c.PageState.Position]
} bottom := len(page.WrappedContent) - c.Height + 3 // 3 for the three bars: top, msg, bottom
c.PageState.History[c.PageState.Position].SearchIndex--
if c.PageState.History[c.PageState.Position].SearchIndex < 0 { newScrollPosition := page.ScrollPosition + amount
c.PageState.History[c.PageState.Position].SearchIndex = len(c.PageState.History[c.PageState.Position].FoundLinkLines) - 1 if newScrollPosition < 0 {
newScrollPosition = 0
} else if newScrollPosition > bottom {
newScrollPosition = bottom
} }
loc := c.PageState.History[c.PageState.Position].ScrollPosition c.PageState.History[c.PageState.Position].ScrollPosition = newScrollPosition
diff := c.PageState.History[c.PageState.Position].FoundLinkLines[c.PageState.History[c.PageState.Position].SearchIndex] - loc
c.Scroll(diff) if len(page.WrappedContent) < c.Height-3 {
percentRead = 100
} else {
percentRead = int(float32(newScrollPosition+c.Height-3) / float32(len(page.WrappedContent)) * 100.0)
}
c.FootBar.SetPercentRead(percentRead)
c.Draw() c.Draw()
return nil
} }
//------------------------------------------------\\ //------------------------------------------------\\

22
page.go
View File

@ -116,15 +116,28 @@ func (p *Page) WrapContent(width int, color bool) {
} }
p.WrappedContent = strings.Split(content.String(), "\n") p.WrappedContent = strings.Split(content.String(), "\n")
p.FindText() p.HighlightFoundText()
}
func (p *Page) HighlightFoundText() {
if p.SearchTerm == "" {
return
}
for i, ln := range p.WrappedContent {
found := strings.Index(ln, p.SearchTerm)
if found < 0 {
continue
}
ln = strings.ReplaceAll(ln, p.SearchTerm, fmt.Sprintf("\033[7m%s\033[0m", p.SearchTerm))
p.WrappedContent[i] = ln
}
} }
func (p *Page) FindText() { func (p *Page) FindText() {
matchLines := make([]int, 0) p.FoundLinkLines = make([]int, 0, 10)
s := p.SearchTerm s := p.SearchTerm
p.SearchIndex = 0 p.SearchIndex = 0
if s == "" { if s == "" {
p.FoundLinkLines = matchLines
return return
} }
for i, ln := range p.WrappedContent { for i, ln := range p.WrappedContent {
@ -134,9 +147,8 @@ func (p *Page) FindText() {
} }
ln = strings.ReplaceAll(ln, s, fmt.Sprintf("\033[7m%s\033[0m", s)) ln = strings.ReplaceAll(ln, s, fmt.Sprintf("\033[7m%s\033[0m", s))
p.WrappedContent[i] = ln p.WrappedContent[i] = ln
matchLines = append(matchLines, i) p.FoundLinkLines = append(p.FoundLinkLines, i)
} }
p.FoundLinkLines = matchLines
} }
//------------------------------------------------\\ //------------------------------------------------\\