mirror of https://github.com/curusarn/resh
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.8 KiB
88 lines
1.8 KiB
package searchapp
|
|
|
|
import (
|
|
"log"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// Query holds information that is used for result scoring
|
|
type Query struct {
|
|
terms []string
|
|
host string
|
|
pwd string
|
|
gitOriginRemote string
|
|
// pwdTilde string
|
|
}
|
|
|
|
func isValidTerm(term string) bool {
|
|
if len(term) == 0 {
|
|
return false
|
|
}
|
|
if strings.Contains(term, " ") {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func filterTerms(terms []string) []string {
|
|
var newTerms []string
|
|
for _, term := range terms {
|
|
if isValidTerm(term) {
|
|
newTerms = append(newTerms, term)
|
|
}
|
|
}
|
|
return newTerms
|
|
}
|
|
|
|
// NewQueryFromString .
|
|
func NewQueryFromString(queryInput string, host string, pwd string, gitOriginRemote string, debug bool) Query {
|
|
if debug {
|
|
log.Println("QUERY input = <" + queryInput + ">")
|
|
}
|
|
terms := strings.Fields(queryInput)
|
|
var logStr string
|
|
for _, term := range terms {
|
|
logStr += " <" + term + ">"
|
|
}
|
|
if debug {
|
|
log.Println("QUERY raw terms =" + logStr)
|
|
}
|
|
terms = filterTerms(terms)
|
|
logStr = ""
|
|
for _, term := range terms {
|
|
logStr += " <" + term + ">"
|
|
}
|
|
if debug {
|
|
log.Println("QUERY filtered terms =" + logStr)
|
|
log.Println("QUERY pwd =" + pwd)
|
|
}
|
|
sort.SliceStable(terms, func(i, j int) bool { return len(terms[i]) < len(terms[j]) })
|
|
return Query{
|
|
terms: terms,
|
|
host: host,
|
|
pwd: pwd,
|
|
gitOriginRemote: gitOriginRemote,
|
|
}
|
|
}
|
|
|
|
// GetRawTermsFromString .
|
|
func GetRawTermsFromString(queryInput string, debug bool) []string {
|
|
if debug {
|
|
log.Println("QUERY input = <" + queryInput + ">")
|
|
}
|
|
terms := strings.Fields(queryInput)
|
|
var logStr string
|
|
for _, term := range terms {
|
|
logStr += " <" + term + ">"
|
|
}
|
|
if debug {
|
|
log.Println("QUERY raw terms =" + logStr)
|
|
}
|
|
terms = filterTerms(terms)
|
|
logStr = ""
|
|
for _, term := range terms {
|
|
logStr += " <" + term + ">"
|
|
}
|
|
return terms
|
|
}
|
|
|