fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

cmd/zoekt: add support for jsonl output (#997)

Currently the zoekt command's output isn't able to surface information
like which branch a match corresponds to. By streaming out the matches
as lines of json, it can be easily processed by other programs like jq.

This is potentially useful for a lot of cli tools. Personally I'm
looking to use this with fzf and bat to build a little search tui.

+15 -1
+15 -1
cmd/zoekt/main.go
··· 18 18 import ( 19 19 "bytes" 20 20 "context" 21 + "encoding/json" 21 22 "flag" 22 23 "fmt" 23 24 "io" ··· 51 52 l := bytes.TrimSuffix(m.Line, []byte{'\n'}) 52 53 fmt.Printf("%s%s:%d:%s%s\n", r, f.FileName, m.LineNumber, l, addTabIfNonEmpty(f.Debug)) 53 54 } 55 + } 56 + } 57 + 58 + func displayMatchesJSONL(files []zoekt.FileMatch) { 59 + encoder := json.NewEncoder(os.Stdout) 60 + for _, f := range files { 61 + encoder.Encode(f) 54 62 } 55 63 } 56 64 ··· 165 173 verbose := flag.Bool("v", false, "print some background data") 166 174 withRepo := flag.Bool("r", false, "print the repo before the file name") 167 175 list := flag.Bool("l", false, "print matching filenames only") 176 + jsonl := flag.Bool("jsonl", false, "print results in jsonl format") 168 177 sym := flag.Bool("sym", false, "do experimental symbol search") 169 178 170 179 flag.Usage = func() { ··· 229 238 sres, _ = searcher.Search(context.Background(), q, &sOpts) 230 239 } 231 240 232 - displayMatches(sres.Files, pat, *withRepo, *list) 241 + if *jsonl { 242 + displayMatchesJSONL(sres.Files) 243 + } else { 244 + displayMatches(sres.Files, pat, *withRepo, *list) 245 + } 246 + 233 247 if *verbose { 234 248 log.Printf("stats: %#v", sres.Stats) 235 249 }