Highlight relevant characters in filter results

This commit is contained in:
Christian Rocha 2020-11-13 18:34:59 -05:00 committed by Christian Rocha
parent f9883e8f8d
commit 43a5ac1b64

View file

@ -3,8 +3,11 @@ package ui
import ( import (
"fmt" "fmt"
"strings" "strings"
"unicode"
"github.com/charmbracelet/charm/ui/common"
rw "github.com/mattn/go-runewidth" rw "github.com/mattn/go-runewidth"
"github.com/muesli/termenv"
) )
const ( const (
@ -65,7 +68,12 @@ func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
default: default:
gutter = dullFuchsiaFg(verticalLine) gutter = dullFuchsiaFg(verticalLine)
icon = dullFuchsiaFg(icon) icon = dullFuchsiaFg(icon)
title = fuchsiaFg(title) if singleFilteredItem {
s := termenv.Style{}.Foreground(common.Fuschia.Color())
title = styleFilteredText(title, m.filterInput.Value(), s, s.Underline())
} else {
title = fuchsiaFg(title)
}
date = dullFuchsiaFg(date) date = dullFuchsiaFg(date)
} }
} else { } else {
@ -73,11 +81,13 @@ func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
if md.markdownType == newsMarkdown { if md.markdownType == newsMarkdown {
gutter = " " gutter = " "
if m.state == stashStateFilterNotes && m.filterInput.Value() == "" { if m.state == stashStateFilterNotes && m.filterInput.Value() == "" {
title = dimIndigoFg(title) title = dimIndigoFg(title)
date = dimSubtleIndigoFg(date) date = dimSubtleIndigoFg(date)
} else { } else {
title = indigoFg(title) s := termenv.Style{}.Foreground(common.Indigo.Color())
title = styleFilteredText(title, m.filterInput.Value(), s, s.Underline())
date = subtleIndigoFg(date) date = subtleIndigoFg(date)
} }
} else if m.state == stashStateFilterNotes && m.filterInput.Value() == "" { } else if m.state == stashStateFilterNotes && m.filterInput.Value() == "" {
@ -91,11 +101,13 @@ func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
date = dimWarmGrayFg(date) date = dimWarmGrayFg(date)
} else { } else {
icon = greenFg(icon) icon = greenFg(icon)
if title == noMemoTitle { if title == noMemoTitle {
title = warmGrayFg(title) title = warmGrayFg(title)
} else { } else {
title = normalFg(title) s := termenv.Style{}.Foreground(common.NewColorPair("#dddddd", "#1a1a1a").Color())
title = styleFilteredText(title, m.filterInput.Value(), s, s.Underline())
} }
gutter = " " gutter = " "
date = warmGrayFg(date) date = warmGrayFg(date)
@ -106,3 +118,20 @@ func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
fmt.Fprintf(b, "%s %s%s\n", gutter, icon, title) fmt.Fprintf(b, "%s %s%s\n", gutter, icon, title)
fmt.Fprintf(b, "%s %s", gutter, date) fmt.Fprintf(b, "%s %s", gutter, date)
} }
func styleFilteredText(haystack, needles string, defaultStyle, matchedStyle termenv.Style) string {
b := strings.Builder{}
n := []rune(needles)
j := 0
for _, h := range []rune(haystack) {
if j < len(n) && unicode.ToLower(h) == unicode.ToLower(n[j]) {
b.WriteString(matchedStyle.Styled(string(h)))
j++
continue
}
b.WriteString(defaultStyle.Styled(string(h)))
}
return b.String()
}