Skip to content

Commit

Permalink
feat: highlight search text
Browse files Browse the repository at this point in the history
  • Loading branch information
pamela committed Sep 4, 2019
1 parent ce70bc8 commit c5aa81c
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions processor/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package processor

import (
"fmt"
"github.com/boyter/cs/processor/snippet"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/boyter/cs/processor/snippet"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)

func debounce(interval time.Duration, input chan string, app *tview.Application, textView *tview.TextView, cb func(app *tview.Application, textView *tview.TextView, arg string)) {
Expand Down Expand Up @@ -122,13 +123,34 @@ func drawResults(app *tview.Application, results []*FileJob, textView *tview.Tex

// TODO need to escape the output https://godoc.org/github.com/rivo/tview#hdr-Colors
locations := GetResultLocations(res)
rel := snippet.ExtractRelevant(string(res.Content), locations, int(SnippetLength), snippet.GetPrevCount(int(SnippetLength)), "…")
coloredContent := colorSearchString(res)
rel := snippet.ExtractRelevant(coloredContent, locations, int(SnippetLength), snippet.GetPrevCount(int(SnippetLength)), "…")
resultText += rel + "\n\n"
}

drawText(app, textView, resultText)
}

func colorSearchString(res *FileJob) string {
var coloredContent string
content := string(res.Content)
var counter int
for k, locs := range res.Locations {
var offset int
for _, loc := range locs {
coloredContent += content[offset:loc]
//red := color.New(color.FgRed).SprintFunc()
//red(k)
coloredContent += fmt.Sprintf("[red]%s", k) + "[white]"
counter++
offset = loc + len(k)
}
coloredContent += content[offset:]
}

return coloredContent
}

func drawText(app *tview.Application, textView *tview.TextView, text string) {
app.QueueUpdateDraw(func() {
textView.Clear()
Expand Down

0 comments on commit c5aa81c

Please sign in to comment.