Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve query script output #668

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions cmd/zoekt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,47 @@ import (
"github.com/sourcegraph/zoekt/shards"
)

func displayMatches(files []zoekt.FileMatch, pat string, withRepo bool, list bool) {
const (
chunkMatchesPerFile = 3
fileMatchesPerSearch = 6
)

func splitAtIndex[E any](s []E, idx int) ([]E, []E) {
if idx < len(s) {
return s[:idx], s[idx:]
}
return s, nil
}

func displayMatches(files []zoekt.FileMatch, withRepo bool, list bool) {
files, hiddenFiles := splitAtIndex(files, fileMatchesPerSearch)
for _, f := range files {
r := ""
if withRepo {
r = f.Repository + "/"
}

fmt.Printf("%s%s%s\n", r, f.FileName, addTabIfNonEmpty(f.Debug))
if list {
fmt.Printf("%s%s%s\n", r, f.FileName, addTabIfNonEmpty(f.Debug))
continue
}

for _, m := range f.LineMatches {
fmt.Printf("%s%s:%d:%s%s\n", r, f.FileName, m.LineNumber, m.Line, addTabIfNonEmpty(f.Debug))
chunks, hidden := splitAtIndex(f.ChunkMatches, chunkMatchesPerFile)

for _, m := range chunks {
fmt.Printf("%d:%s%s\n", m.ContentStart.LineNumber, string(m.Content), addTabIfNonEmpty(m.DebugScore))
}

if len(hidden) > 0 {
fmt.Printf("hidden %d more chunk matches\n", len(hidden))
}
fmt.Println()
}

if len(hiddenFiles) > 0 {
fmt.Printf("hidden %d more file matches\n", len(hiddenFiles))
}
fmt.Println()
}

func addTabIfNonEmpty(s string) string {
Expand Down Expand Up @@ -180,6 +206,7 @@ func main() {

sOpts := zoekt.SearchOptions{
DebugScore: *debug,
ChunkMatches: true,
}
sres, err := searcher.Search(context.Background(), query, &sOpts)
if err != nil {
Expand All @@ -195,7 +222,7 @@ func main() {
sres, _ = searcher.Search(context.Background(), query, &sOpts)
}

displayMatches(sres.Files, pat, *withRepo, *list)
displayMatches(sres.Files, *withRepo, *list)
if *verbose {
log.Printf("stats: %#v", sres.Stats)
}
Expand Down
Loading