Skip to content

Commit

Permalink
Modify and shuffle around
Browse files Browse the repository at this point in the history
  • Loading branch information
boyter committed Jan 28, 2020
1 parent 469855c commit 0213162
Show file tree
Hide file tree
Showing 22 changed files with 340 additions and 310 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,21 @@ try using sc and its right at the top
4. filter on filename
5. ignore minified/generated files
6. display the number of processed files
7. be able to look though the results using keys
7. be able to look though the results using keys


interesting bug

# bboyter @ SurfaceBook2 in ~/Go/src/bitbucket.org/lendlease_corp/chelsea-datalake-go on git:master x [11:28:56]
$ cs javascript
fargate/api/integration_tests/blns.json (1.980)
…86expression(javascript:alert(1)\">DEF",
"ABC<div style=\"x:\\xE2\\x80\\x85expression(javascript:alert(1)\">DEF",
"ABC<div style=\"x:\\xE2\\x80\\x82expression(javascript:alert(1)\">DEF",
"ABC<div style=\"x:\\x0Bexpression(javascript:alert(1…

fargate/xray-api/integration_tests/blns.json (1.980)
…86expression(javascript:alert(1)\">DEF",
"ABC<div style=\"x:\\xE2\\x80\\x85expression(javascript:alert(1)\">DEF",
"ABC<div style=\"x:\\xE2\\x80\\x82expression(javascript:alert(1)\">DEF",
"ABC<div style=\"x:\\x0Bexpression(javascript:alert(1…
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"github.com/boyter/cs/processor"
sccprocessor "github.com/boyter/scc/processor"
"github.com/spf13/cobra"
"os"
sccprocessor "github.com/boyter/scc/processor"
)

func main() {
Expand All @@ -14,8 +14,8 @@ func main() {

rootCmd := &cobra.Command{
Use: "cs",
Long: "cs code search command line.\nBen Boyter <ben@boyter.org> and Pamela Gangopadhyay <pamela.gangopadhyay@gmail.com>",
Version: "0.0.1",
Long: "cs code search command line.\nBen Boyter <ben@boyter.org>",
Version: "0.0.3",
Run: func(cmd *cobra.Command, args []string) {
processor.SearchString = args
sccprocessor.ProcessConstants()
Expand Down
1 change: 0 additions & 1 deletion processor/file_test.go
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package processor

3 changes: 1 addition & 2 deletions processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

"github.com/boyter/cs/processor/printer"
"github.com/boyter/cs/processor/snippet"
"github.com/fatih/color"
)
Expand Down Expand Up @@ -39,7 +38,7 @@ func fileSummarize(input chan *FileJob) string {
color.Magenta("%s (%.3f)", res.Location, res.Score)

locations := GetResultLocations(res)
coloredContent := printer.WriteColored(res.Content, res.Locations, fmtBegin, fmtEnd)
coloredContent := snippet.WriteHighlights(res.Content, res.Locations, fmtBegin, fmtEnd)
rel := snippet.ExtractRelevant(coloredContent, locations, int(SnippetLength), snippet.CalculatePrevCount(int(SnippetLength), 6), "…")

fmt.Println(rel)
Expand Down
20 changes: 19 additions & 1 deletion processor/snippet/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func determineSnipLocations(locations []LocationType, previousCount int) int {
if locCount > 2 {
// We don't need to iterate the last value in this so chop off the last one
// however note that we access the element anyway as that's how the inner loop works
for i := 0; i < locCount - 1; i++ {
for i := 0; i < locCount-1; i++ {

// We don't need to worry about the +1 out of bounds here
// because we never loop the last term as the result
Expand Down Expand Up @@ -149,6 +149,24 @@ func CalculatePrevCount(relLength int, divisor int) int {
return t
}

// When we use the ExtractRelevant method it is possible that it cut on things that were inserted
// using the WriteHighlights method which might make the output look odd. This method attempts to fix
// the issue by removing partial matches on the edges
func PatchRelevant(relevant, in, out, indicator string) string {
if in == "" && out == "" {
return relevant
}

//// if the indicator
//start := len(indicator)
//
//for i := range relevant {
//
//}

return relevant
}

func ExtractRelevant(fulltext string, locations []LocationType, relLength int, prevCount int, indicator string) string {
textLength := len(fulltext)

Expand Down
3 changes: 1 addition & 2 deletions processor/snippet/snippet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func fileSummarize(input chan *FileJob) string {
color.Magenta("%s (%.3f)", res.Location, res.Score)
locations := GetResultLocations(res)
coloredContent := printer.WriteColored(res.Content, res.Locations, fmtBegin, fmtEnd)
coloredContent := printer.WriteHighlights(res.Content, res.Locations, fmtBegin, fmtEnd)
rel := snippet.ExtractRelevant(coloredContent, locations, int(SnippetLength), snippet.CalculatePrevCount(int(SnippetLength), 6), "…")
fmt.Println(rel)
Expand Down Expand Up @@ -197,7 +197,6 @@ func TestGetPrevCount(t *testing.T) {
}
}


// Designed to catch out any issues with unicode and the like
func TestFuzzy(t *testing.T) {
content, _ := ioutil.ReadFile("blns.json")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package printer
package snippet

import (
"strings"
Expand All @@ -7,7 +7,7 @@ import (
// WriteColoured takes in some content and locations and then inserts in/out
// strings which can be used for highlighting around matching terms. For example
// you could pass in "test" and have it return "<strong>te</strong>st"
func WriteColored(content []byte, locations map[string][]int, in string, out string) string {
func WriteHighlights(content []byte, locations map[string][]int, in string, out string) string {
var str strings.Builder

end := -1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package printer
package snippet

import "testing"

func TestWriteColoredSimple(t *testing.T) {
loc := map[string][]int{}
loc["this"] = []int{0}

got := WriteColored([]byte("this"), loc, "[red]", "[white]")
got := WriteHighlights([]byte("this"), loc, "[red]", "[white]")

expected := "[red]this[white]"
if got != expected {
Expand All @@ -18,7 +18,7 @@ func TestWriteColoredTermSimple(t *testing.T) {
loc := map[string][]int{}
loc["this"] = []int{0}

got := WriteColored([]byte("this"), loc, "\033[1;31m", "\033[0m")
got := WriteHighlights([]byte("this"), loc, "\033[1;31m", "\033[0m")

expected := "\033[1;31mthis\033[0m"
if got != expected {
Expand All @@ -30,7 +30,7 @@ func TestWriteColoredCheckInOut(t *testing.T) {
loc := map[string][]int{}
loc["this"] = []int{0}

got := WriteColored([]byte("this"), loc, "__", "__")
got := WriteHighlights([]byte("this"), loc, "__", "__")

expected := "__this__"
if got != expected {
Expand All @@ -42,7 +42,7 @@ func TestWriteColoredCheck2(t *testing.T) {
loc := map[string][]int{}
loc["bing"] = []int{0}

got := WriteColored([]byte("bing"), loc, "__", "__")
got := WriteHighlights([]byte("bing"), loc, "__", "__")

expected := "__bing__"
if got != expected {
Expand All @@ -54,7 +54,7 @@ func TestWriteColoredCheckTwoWords(t *testing.T) {
loc := map[string][]int{}
loc["this"] = []int{0, 5}

got := WriteColored([]byte("this this"), loc, "__", "__")
got := WriteHighlights([]byte("this this"), loc, "__", "__")

expected := "__this__ __this__"
if got != expected {
Expand All @@ -67,7 +67,7 @@ func TestWriteColoredCheckMixedWords(t *testing.T) {
loc["this"] = []int{0, 5}
loc["something"] = []int{10}

got := WriteColored([]byte("this this something"), loc, "__", "__")
got := WriteHighlights([]byte("this this something"), loc, "__", "__")

expected := "__this__ __this__ __something__"
if got != expected {
Expand All @@ -80,7 +80,7 @@ func TestWriteColoredCaseCheck(t *testing.T) {
loc["this"] = []int{0}
loc["t"] = []int{0}

got := WriteColored([]byte("THIS"), loc, "__", "__")
got := WriteHighlights([]byte("THIS"), loc, "__", "__")

expected := "__THIS__"
if got != expected {
Expand All @@ -93,7 +93,7 @@ func TestWriteColoredOverlapStart(t *testing.T) {
loc["this"] = []int{0}
loc["t"] = []int{0}

got := WriteColored([]byte("this"), loc, "__", "__")
got := WriteHighlights([]byte("this"), loc, "__", "__")

expected := "__this__"
if got != expected {
Expand All @@ -106,7 +106,7 @@ func TestWriteColoredOverlapMiddle(t *testing.T) {
loc["this"] = []int{0}
loc["h"] = []int{1}

got := WriteColored([]byte("this"), loc, "__", "__")
got := WriteHighlights([]byte("this"), loc, "__", "__")

expected := "__this__"
if got != expected {
Expand All @@ -119,7 +119,7 @@ func TestWriteColoredOverlapMiddleLonger(t *testing.T) {
loc["th"] = []int{0}
loc["his"] = []int{1}

got := WriteColored([]byte("this"), loc, "__", "__")
got := WriteHighlights([]byte("this"), loc, "__", "__")

expected := "__this__"
if got != expected {
Expand All @@ -131,7 +131,7 @@ func TestBugOne(t *testing.T) {
loc := map[string][]int{}
loc["expected"] = []int{10}

got := WriteColored([]byte("this is unexpected"), loc, "__", "__")
got := WriteHighlights([]byte("this is unexpected"), loc, "__", "__")

expected := "this is un__expected__"
if got != expected {
Expand All @@ -144,7 +144,7 @@ func TestBugTwo(t *testing.T) {
loc["got"] = []int{22, 71, 77}
loc["expected"] = []int{0, 29}

got := WriteColored([]byte(`expected := "this" if got != expected { t.Error("Expected", expected, "got", got)}`), loc, "[red]", "[white]")
got := WriteHighlights([]byte(`expected := "this" if got != expected { t.Error("Expected", expected, "got", got)}`), loc, "[red]", "[white]")

expected := `[red]expected[white] := "this" if [red]got[white] != [red]expected[white] { t.Error("Expected", expected, "[red]got[white]", [red]got[white])}`
if got != expected {
Expand All @@ -157,7 +157,7 @@ func TestBugThree(t *testing.T) {
loc[`"`] = []int{5, 8}
loc[`",`] = []int{8}

got := WriteColored([]byte(`Use: "cs",`), loc, "[red]", "[white]")
got := WriteHighlights([]byte(`Use: "cs",`), loc, "[red]", "[white]")

expected := `Use: [red]"[white]cs[red]",[white]`
if got != expected {
Expand Down
4 changes: 1 addition & 3 deletions processor/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package processor

import (
"fmt"
"github.com/boyter/cs/processor/printer"
"github.com/boyter/cs/processor/snippet"
"runtime"
"strconv"
Expand All @@ -14,7 +13,6 @@ import (
"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)) {
var item string
timer := time.NewTimer(interval)
Expand Down Expand Up @@ -124,7 +122,7 @@ 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)
coloredContent := printer.WriteColored(res.Content, res.Locations, "[red]", "[white]")
coloredContent := snippet.WriteHighlights(res.Content, res.Locations, "[red]", "[white]")
rel := snippet.ExtractRelevant(coloredContent, locations, int(SnippetLength), snippet.GetPrevCount(int(SnippetLength)), "…")

resultText += rel + "\n\n"
Expand Down
Loading

0 comments on commit 0213162

Please sign in to comment.