diff options
author | Miguel Mota <hello@miguelmota.com> | 2021-10-29 09:19:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-29 09:19:38 -0700 |
commit | 9a9ee307d49de6f0e7d9f28a8db3afd7dbc37ea9 (patch) | |
tree | 17d8a34c5998d57e332be4cc535a9f825423821f | |
parent | e26816b26cb10544696adbe75e10f584ccd93a4b (diff) | |
parent | dd1c83ee67397201e312224023dc7aa1dac26bb7 (diff) |
Merge pull request #254 from vuon9/feature/types-4-search-keywordv1.6.10
Support search by /s:keyword - symbol, /n:keyword - name
-rw-r--r-- | cointop/search.go | 32 | ||||
-rw-r--r-- | docs/content/faq.md | 2 |
2 files changed, 33 insertions, 1 deletions
diff --git a/cointop/search.go b/cointop/search.go index dd06844..cc3f7c4 100644 --- a/cointop/search.go +++ b/cointop/search.go @@ -93,6 +93,22 @@ func (ct *Cointop) Search(q string) error { ct.State.lastSearchQuery = q } + canSearchSymbol := true + canSearchName := true + if strings.HasPrefix(q, "s:") { + canSearchSymbol = true + canSearchName = false + q = q[2:] + log.Debug("Search, by keyword") + } + + if strings.HasPrefix(q, "n:") { + canSearchSymbol = false + canSearchName = true + q = q[2:] + log.Debug("Search, by name") + } + idx := -1 min := -1 var hasprefixidx []int @@ -107,16 +123,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 { ct.GoToGlobalIndex(i) return nil } + + if !canSearchName { + continue + } + // if query matches name, return immediately if name == q { ct.GoToGlobalIndex(i) return nil } + // store index with the smallest levenshtein dist := levenshtein.DamerauLevenshteinDistance(name, q) if min == -1 || dist <= min { @@ -131,15 +154,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 } diff --git a/docs/content/faq.md b/docs/content/faq.md index 70055f1..9acbf5c 100644 --- a/docs/content/faq.md +++ b/docs/content/faq.md @@ -135,6 +135,8 @@ draft: false The default key to open search is <kbd>/</kbd>. Type the search query after the `/` in the field and hit <kbd>Enter</kbd>. Each search starts from the current cursor position. To search for the same term again, hit <kbd>/</kbd> then <kbd>Enter</kbd>. + The default behaviour will start to search by symbol first, then it will continues searching by name if there is no result. To search by only symbol, type the search query after `/s:`. To search by only name, type the search query after `/n:`. + ## How do I exit search? Press <kbd>Esc</kbd> to exit search. |