mirror of
https://github.com/charmbracelet/gum
synced 2024-06-07 08:12:17 +02:00
perf: speed up filter rendering
We don't need to style each character individually, buffer them and flush before highlights.
This commit is contained in:
parent
db75c218de
commit
86fa35d672
|
@ -90,19 +90,26 @@ func (m model) View() string {
|
||||||
// caused the match. i.e. fuzzy matching.
|
// caused the match. i.e. fuzzy matching.
|
||||||
// We should indicate to the users which characters are being matched.
|
// We should indicate to the users which characters are being matched.
|
||||||
mi := 0
|
mi := 0
|
||||||
|
var buf strings.Builder
|
||||||
for ci, c := range match.Str {
|
for ci, c := range match.Str {
|
||||||
// Check if the current character index matches the current matched
|
// Check if the current character index matches the current matched
|
||||||
// index. If so, color the character to indicate a match.
|
// index. If so, color the character to indicate a match.
|
||||||
if mi < len(match.MatchedIndexes) && ci == match.MatchedIndexes[mi] {
|
if mi < len(match.MatchedIndexes) && ci == match.MatchedIndexes[mi] {
|
||||||
|
// Flush text buffer.
|
||||||
|
s.WriteString(m.textStyle.Render(buf.String()))
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
s.WriteString(m.matchStyle.Render(string(c)))
|
s.WriteString(m.matchStyle.Render(string(c)))
|
||||||
// We have matched this character, so we never have to check it
|
// We have matched this character, so we never have to check it
|
||||||
// again. Move on to the next match.
|
// again. Move on to the next match.
|
||||||
mi++
|
mi++
|
||||||
} else {
|
} else {
|
||||||
// Not a match, simply show the character, unstyled.
|
// Not a match, buffer a regular character.
|
||||||
s.WriteString(m.textStyle.Render(string(c)))
|
buf.WriteRune(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Flush text buffer.
|
||||||
|
s.WriteString(m.textStyle.Render(buf.String()))
|
||||||
|
|
||||||
// We have finished displaying the match with all of it's matched
|
// We have finished displaying the match with all of it's matched
|
||||||
// characters highlighted and the rest filled in.
|
// characters highlighted and the rest filled in.
|
||||||
|
|
Loading…
Reference in a new issue