From 9e7ac3c866fbc2c5c8f0736b29ec939c087782f4 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Tue, 17 Dec 2019 22:32:45 -0800 Subject: [PATCH] Finally solved infuriating rendering issue --- client.go | 64 ++++++++++++++++++++++++++++++++++--------------------- page.go | 22 ++++++++++++++----- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/client.go b/client.go index d43ca28..c6a916b 100644 --- a/client.go +++ b/client.go @@ -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 } //------------------------------------------------\\ diff --git a/page.go b/page.go index e32bd58..f287607 100644 --- a/page.go +++ b/page.go @@ -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 } //------------------------------------------------\\