Support search by /s:keyword - symbol, /n:keyword - name

pull/254/head
ѵµσɳɠ 3 years ago
parent e409a0bdde
commit 6ef43d5ba0

@ -93,6 +93,28 @@ func (ct *Cointop) Search(q string) error {
ct.State.lastSearchQuery = q ct.State.lastSearchQuery = q
} }
hasPrefix := false
canSearchSymbol := true
canSearchName := true
if strings.HasPrefix(q, "s:") {
canSearchSymbol = true
canSearchName = false
hasPrefix = true
log.Debug("Search, by keyword")
}
if strings.HasPrefix(q, "n:") {
canSearchSymbol = false
canSearchName = true
hasPrefix = true
log.Debug("Search, by name")
}
if hasPrefix {
q = q[2:]
log.Debugf("Search, truncated query '%s'", q)
}
idx := -1 idx := -1
min := -1 min := -1
var hasprefixidx []int var hasprefixidx []int
@ -107,16 +129,23 @@ func (ct *Cointop) Search(q string) error {
coin := ct.State.allCoins[i] coin := ct.State.allCoins[i]
name := strings.ToLower(coin.Name) name := strings.ToLower(coin.Name)
symbol := strings.ToLower(coin.Symbol) symbol := strings.ToLower(coin.Symbol)
// if query matches symbol, return immediately // if query matches symbol, return immediately
if symbol == q { if canSearchSymbol && symbol == q {
ct.GoToGlobalIndex(i) ct.GoToGlobalIndex(i)
return nil return nil
} }
if !canSearchName {
continue
}
// if query matches name, return immediately // if query matches name, return immediately
if name == q { if name == q {
ct.GoToGlobalIndex(i) ct.GoToGlobalIndex(i)
return nil return nil
} }
// store index with the smallest levenshtein // store index with the smallest levenshtein
dist := levenshtein.DamerauLevenshteinDistance(name, q) dist := levenshtein.DamerauLevenshteinDistance(name, q)
if min == -1 || dist <= min { if min == -1 || dist <= min {
@ -131,15 +160,22 @@ func (ct *Cointop) Search(q string) error {
} }
} }
} }
if !canSearchName {
return nil
}
// go to row if prefix match // go to row if prefix match
if len(hasprefixidx) > 0 && hasprefixidx[0] != -1 && min > 0 { if len(hasprefixidx) > 0 && hasprefixidx[0] != -1 && min > 0 {
ct.GoToGlobalIndex(hasprefixidx[0]) ct.GoToGlobalIndex(hasprefixidx[0])
return nil return nil
} }
// go to row if levenshtein distance is small enough // go to row if levenshtein distance is small enough
if idx > -1 && min <= 6 { if idx > -1 && min <= 6 {
ct.GoToGlobalIndex(idx) ct.GoToGlobalIndex(idx)
return nil return nil
} }
return nil return nil
} }

Loading…
Cancel
Save