Merge branch 'tcell-events' into feature/tcell-events-vuong

pull/232/head
Simon Roberts 3 years ago committed by GitHub
commit baeee67e64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
}

@ -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.

@ -492,9 +492,32 @@ func (g *Gui) fixColor(c tcell.Color) tcell.Color {
return c
}
// TODO: fixme
func (g *Gui) Style(fg, bg tcell.Color) tcell.Style {
st := tcell.StyleDefault.Foreground(fg).Background(bg)
return st
}
func (g *Gui) MkColor(color Attribute) tcell.Color {
if color == ColorDefault {
return tcell.ColorDefault
} else {
return g.fixColor(tcell.PaletteColor(int(color)&0x1ff - 1))
}
}
// TODO: delete termbox compat
func (g *Gui) MkStyle(fg, bg Attribute) tcell.Style {
st := tcell.StyleDefault
if fg != ColorDefault {
f := tcell.PaletteColor(int(fg)&0x1ff - 1)
f = g.fixColor(f)
st = st.Foreground(f)
}
if bg != ColorDefault {
b := tcell.PaletteColor(int(bg)&0x1ff - 1)
b = g.fixColor(b)
st = st.Background(b)
}
// TODO: fixme
// if (fg|bg)&AttrBold != 0 {
// st = st.Bold(true)

@ -350,7 +350,15 @@ func (v *View) draw() error {
// bgColor = v.BgColor
// }
if err := v.setRune(x, y, c.chr, c.style); err != nil {
st := c.style
fgColor, bgColor, _ := c.style.Decompose()
if fgColor == tcell.ColorDefault {
st = st.Foreground(v.g.MkColor(v.FgColor))
}
if bgColor == tcell.ColorDefault {
st = st.Background(v.g.MkColor(v.BgColor))
}
if err := v.setRune(x, y, c.chr, st); err != nil {
return err
}
x++

Loading…
Cancel
Save