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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 29, 2021
Merged
Changes from 1 commit
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
38 changes: 37 additions & 1 deletion cointop/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ func (ct *Cointop) Search(q string) error {
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 {
lyricnz marked this conversation as resolved.
Show resolved Hide resolved
q = q[2:]
log.Debugf("Search, truncated query '%s'", q)
}

idx := -1
min := -1
var hasprefixidx []int
Expand All @@ -107,16 +129,23 @@ func (ct *Cointop) Search(q string) error {
coin := ct.State.allCoins[i]
name := strings.ToLower(coin.Name)
symbol := strings.ToLower(coin.Symbol)

// if query matches symbol, return immediately
if symbol == q {
if canSearchSymbol && symbol == q {
vuon9 marked this conversation as resolved.
Show resolved Hide resolved
ct.GoToGlobalIndex(i)
return nil
}

if !canSearchName {
continue
}

// if query matches name, return immediately
if name == q {
vuon9 marked this conversation as resolved.
Show resolved Hide resolved
ct.GoToGlobalIndex(i)
return nil
}

// store index with the smallest levenshtein
dist := levenshtein.DamerauLevenshteinDistance(name, q)
if min == -1 || dist <= min {
Expand All @@ -131,15 +160,22 @@ func (ct *Cointop) Search(q string) error {
}
}
}

if !canSearchName {
return nil
}

// go to row if prefix match
if len(hasprefixidx) > 0 && hasprefixidx[0] != -1 && min > 0 {
ct.GoToGlobalIndex(hasprefixidx[0])
return nil
}

// go to row if levenshtein distance is small enough
if idx > -1 && min <= 6 {
ct.GoToGlobalIndex(idx)
return nil
}

return nil
}