Working search

This commit is contained in:
sloumdrone 2019-12-18 22:17:52 -08:00
parent 9e7ac3c866
commit 10f28027dc
2 changed files with 26 additions and 15 deletions

View File

@ -90,9 +90,8 @@ func (c *client) Draw() {
var re *regexp.Regexp var re *regexp.Regexp
if c.Options["theme"] == "inverse" { if c.Options["theme"] == "inverse" {
screen.WriteString("\033[7m") screen.WriteString("\033[7m")
} else if c.Options["theme"] == "color" {
re = regexp.MustCompile(`\033\[(?:\d*;?)+[A-Za-z]`)
} }
re = regexp.MustCompile(`\033\[(?:\d*;?)+[A-Za-z]`)
if c.BookMarks.IsOpen { if c.BookMarks.IsOpen {
bm := c.BookMarks.Render(c.Width, c.Height) bm := c.BookMarks.Render(c.Width, c.Height)
bmWidth := len([]rune(bm[0])) bmWidth := len([]rune(bm[0]))
@ -137,12 +136,12 @@ func (c *client) Draw() {
for i := 0; i < c.Height-3; i++ { for i := 0; i < c.Height-3; i++ {
if i < len(pageContent) { if i < len(pageContent) {
extra := 0 extra := 0
if c.Options["theme"] == "color" { // if c.Options["theme"] == "color" {
escapes := re.FindAllString(pageContent[i], -1) escapes := re.FindAllString(pageContent[i], -1)
for _, esc := range escapes { for _, esc := range escapes {
extra += len(esc) extra += len(esc)
}
} }
// }
screen.WriteString(fmt.Sprintf("%-*.*s", c.Width+extra, c.Width+extra, pageContent[i])) screen.WriteString(fmt.Sprintf("%-*.*s", c.Width+extra, c.Width+extra, pageContent[i]))
screen.WriteString("\n") screen.WriteString("\n")
} else { } else {
@ -270,7 +269,10 @@ func (c *client) TakeControlInput() {
c.SetMessage(err.Error(), true) c.SetMessage(err.Error(), true)
c.DrawMessage() c.DrawMessage()
} }
c.NextSearchItem(0) err = c.NextSearchItem(0)
if err != nil {
c.Draw()
}
case ':', ' ': case ':', ' ':
// Process a command // Process a command
c.ClearMessage() c.ClearMessage()
@ -1105,20 +1107,21 @@ func (c *client) NextSearchItem(dir int) error {
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 += dir c.PageState.History[c.PageState.Position].SearchIndex += dir
if c.PageState.History[c.PageState.Position].SearchIndex < 0 { page.SearchIndex += dir
if page.SearchIndex < 0 {
c.PageState.History[c.PageState.Position].SearchIndex = 0 c.PageState.History[c.PageState.Position].SearchIndex = 0
page.SearchIndex = 0
} }
if page.SearchIndex+dir >= len(page.FoundLinkLines) { if page.SearchIndex >= len(page.FoundLinkLines) {
c.PageState.History[c.PageState.Position].SearchIndex = len(page.FoundLinkLines) - 1 c.PageState.History[c.PageState.Position].SearchIndex = len(page.FoundLinkLines) - 1
return fmt.Errorf("The search path goes no further") return fmt.Errorf("The search path goes no further")
} else if page.SearchIndex+dir < 0 { } else if page.SearchIndex < 0 {
c.PageState.History[c.PageState.Position].SearchIndex = 0 c.PageState.History[c.PageState.Position].SearchIndex = 0
return fmt.Errorf("You are at the beginning of the search path") return fmt.Errorf("You are at the beginning of the search path")
} }
index := c.PageState.History[c.PageState.Position].SearchIndex diff := page.FoundLinkLines[page.SearchIndex] - page.ScrollPosition
diff := c.PageState.History[c.PageState.Position].FoundLinkLines[index] - page.ScrollPosition
c.ScrollForSearch(diff) c.ScrollForSearch(diff)
c.Draw() c.Draw()
return nil return nil

12
page.go
View File

@ -128,7 +128,11 @@ func (p *Page) HighlightFoundText() {
if found < 0 { if found < 0 {
continue continue
} }
ln = strings.ReplaceAll(ln, p.SearchTerm, fmt.Sprintf("\033[7m%s\033[0m", p.SearchTerm)) format := "\033[7m%s\033[27m"
if bombadillo.Options["theme"] == "inverse" {
format = "\033[27m%s\033[7m"
}
ln = strings.ReplaceAll(ln, p.SearchTerm, fmt.Sprintf(format, p.SearchTerm))
p.WrappedContent[i] = ln p.WrappedContent[i] = ln
} }
} }
@ -140,12 +144,16 @@ func (p *Page) FindText() {
if s == "" { if s == "" {
return return
} }
format := "\033[7m%s\033[27m"
if bombadillo.Options["theme"] == "inverse" {
format = "\033[27m%s\033[7m"
}
for i, ln := range p.WrappedContent { for i, ln := range p.WrappedContent {
found := strings.Index(ln, s) found := strings.Index(ln, s)
if found < 0 { if found < 0 {
continue continue
} }
ln = strings.ReplaceAll(ln, s, fmt.Sprintf("\033[7m%s\033[0m", s)) ln = strings.ReplaceAll(ln, s, fmt.Sprintf(format, s))
p.WrappedContent[i] = ln p.WrappedContent[i] = ln
p.FoundLinkLines = append(p.FoundLinkLines, i) p.FoundLinkLines = append(p.FoundLinkLines, i)
} }