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':
// Next search item
c.ClearMessage()
err := c.NextSearchItem()
err := c.NextSearchItem(1)
if err != nil {
c.SetMessage(err.Error(), false)
c.DrawMessage()
@ -246,7 +246,7 @@ func (c *client) TakeControlInput() {
case 'N':
// Previous search item
c.ClearMessage()
err := c.PreviousSearchItem()
err := c.NextSearchItem(-1)
if err != nil {
c.SetMessage(err.Error(), false)
c.DrawMessage()
@ -270,6 +270,7 @@ func (c *client) TakeControlInput() {
c.SetMessage(err.Error(), true)
c.DrawMessage()
}
c.NextSearchItem(0)
case ':', ' ':
// Process a command
c.ClearMessage()
@ -1089,46 +1090,61 @@ func (c *client) handleWeb(u Url) {
func (c *client) find(s string) error {
c.PageState.History[c.PageState.Position].SearchTerm = s
c.PageState.History[c.PageState.Position].FindText()
if s == "" {
return nil
}
if len(c.PageState.History[c.PageState.Position].FoundLinkLines) == 0 {
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
}
func (c *client) NextSearchItem() error {
if len(c.PageState.History[c.PageState.Position].FoundLinkLines) == 0 {
func (c *client) NextSearchItem(dir int) error {
page := c.PageState.History[c.PageState.Position]
if len(page.FoundLinkLines) == 0 {
return fmt.Errorf("The search is over before it has begun")
}
c.PageState.History[c.PageState.Position].SearchIndex++
if c.PageState.History[c.PageState.Position].SearchIndex >= len(c.PageState.History[c.PageState.Position].FoundLinkLines) {
c.PageState.History[c.PageState.Position].SearchIndex += dir
if c.PageState.History[c.PageState.Position].SearchIndex < 0 {
c.PageState.History[c.PageState.Position].SearchIndex = 0
}
loc := c.PageState.History[c.PageState.Position].ScrollPosition
diff := c.PageState.History[c.PageState.Position].FoundLinkLines[c.PageState.History[c.PageState.Position].SearchIndex] - loc
c.Scroll(diff)
if page.SearchIndex+dir >= len(page.FoundLinkLines) {
c.PageState.History[c.PageState.Position].SearchIndex = len(page.FoundLinkLines) - 1
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()
return nil
}
func (c *client) PreviousSearchItem() error {
if len(c.PageState.History[c.PageState.Position].FoundLinkLines) == 0 {
return fmt.Errorf("The search is over before it has begun")
}
c.PageState.History[c.PageState.Position].SearchIndex--
if c.PageState.History[c.PageState.Position].SearchIndex < 0 {
c.PageState.History[c.PageState.Position].SearchIndex = len(c.PageState.History[c.PageState.Position].FoundLinkLines) - 1
func (c *client) ScrollForSearch(amount int) {
var percentRead int
page := c.PageState.History[c.PageState.Position]
bottom := len(page.WrappedContent) - c.Height + 3 // 3 for the three bars: top, msg, bottom
newScrollPosition := page.ScrollPosition + amount
if newScrollPosition < 0 {
newScrollPosition = 0
} else if newScrollPosition > bottom {
newScrollPosition = bottom
}
loc := c.PageState.History[c.PageState.Position].ScrollPosition
diff := c.PageState.History[c.PageState.Position].FoundLinkLines[c.PageState.History[c.PageState.Position].SearchIndex] - loc
c.Scroll(diff)
c.PageState.History[c.PageState.Position].ScrollPosition = newScrollPosition
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()
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.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() {
matchLines := make([]int, 0)
p.FoundLinkLines = make([]int, 0, 10)
s := p.SearchTerm
p.SearchIndex = 0
if s == "" {
p.FoundLinkLines = matchLines
return
}
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))
p.WrappedContent[i] = ln
matchLines = append(matchLines, i)
p.FoundLinkLines = append(p.FoundLinkLines, i)
}
p.FoundLinkLines = matchLines
}
//------------------------------------------------\\