diff --git a/main.go b/main.go index f2ba797..6a2e124 100644 --- a/main.go +++ b/main.go @@ -177,7 +177,7 @@ func main() { "format", "f", "text", - "set output format [text, json]", + "set output format [text, json, vimgrep]", ) flags.StringVar( &processor.Ranker, diff --git a/processor/worker_summarize.go b/processor/worker_summarize.go index 88f777c..3d5764f 100644 --- a/processor/worker_summarize.go +++ b/processor/worker_summarize.go @@ -10,6 +10,7 @@ import ( "github.com/mattn/go-isatty" "io/ioutil" "os" + "strings" ) type ResultSummarizer struct { @@ -53,11 +54,41 @@ func (f *ResultSummarizer) Start() { switch f.Format { case "json": f.formatJson(results) + case "vimgrep": + f.formatVimGrep(results) default: f.formatDefault(results) } } +func (f *ResultSummarizer) formatVimGrep(results []*FileJob) { + var vimGrepOutput []string + SnippetLength = 50 // vim quickfix puts each hit on its own line. + documentFrequency := calculateDocumentTermFrequency(results) + + // Cycle through files with matches and process each snippets inside it. + for _, res := range results { + snippets := extractRelevantV3(res, documentFrequency, int(SnippetLength), "…") + if int64(len(snippets)) > f.SnippetCount { + snippets = snippets[:f.SnippetCount] + } + + for _, snip := range snippets { + hint := strings.ReplaceAll(snip.Content, "\n", "\\n") + line := fmt.Sprintf("%v:%v:%v:%v", res.Location, snip.LineStart, snip.StartPos, hint) + vimGrepOutput = append(vimGrepOutput, line) + } + } + + printable := strings.Join(vimGrepOutput, "\n") + if f.FileOutput == "" { + fmt.Println(printable) + } else { + _ = ioutil.WriteFile(FileOutput, []byte(printable), 0600) + fmt.Println("results written to " + FileOutput) + } +} + func (f *ResultSummarizer) formatJson(results []*FileJob) { var jsonResults []jsonResult