2020-04-11 15:25:53 +00:00
|
|
|
package engine
|
2020-04-12 00:15:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-14 11:25:03 +00:00
|
|
|
"sort"
|
2020-04-12 00:15:24 +00:00
|
|
|
"text/tabwriter"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tabPadding = 3
|
|
|
|
const tabPaddingChar = ' '
|
|
|
|
|
2020-04-13 19:45:16 +00:00
|
|
|
// QueryResult defines the structure of a query result
|
2020-04-12 00:15:24 +00:00
|
|
|
type QueryResult struct {
|
|
|
|
File string
|
2020-04-14 11:25:03 +00:00
|
|
|
Score float64
|
2020-04-12 00:15:24 +00:00
|
|
|
FirstMatch string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowResults display results in a table
|
|
|
|
func ShowResults(results []*QueryResult) {
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, tabPadding, tabPaddingChar, tabwriter.AlignRight|tabwriter.Debug)
|
|
|
|
defer w.Flush()
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "File\tCount\tFirst match\t")
|
|
|
|
for _, v := range results {
|
2020-04-14 11:25:03 +00:00
|
|
|
fmt.Fprintf(w, "%v\t%v\t%v\t\n", v.File, v.Score, v.FirstMatch)
|
2020-04-12 00:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 11:25:03 +00:00
|
|
|
|
|
|
|
// SortResultsByScore sort the given array of results by their score.
|
|
|
|
func SortResultsByScore(results []*QueryResult) []*QueryResult {
|
|
|
|
sort.Slice(results, func(i, j int) bool {
|
|
|
|
return results[i].Score > results[j].Score
|
|
|
|
})
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|