MCPcopy
hub / github.com/kovidgoyal/kitty / highlightMatchedChars

Method highlightMatchedChars

kittens/command_palette/main.go:496–520  ·  view source on GitHub ↗

highlightMatchedChars returns a string with characters at the given rune positions rendered using matchStyle, and the rest rendered using baseStyle (or unstyled if baseStyle is empty).

(text string, positions []int, baseStyle, matchStyle string)

Source from the content-addressed store, hash-verified

494// positions rendered using matchStyle, and the rest rendered using baseStyle
495// (or unstyled if baseStyle is empty).
496func (h *Handler) highlightMatchedChars(text string, positions []int, baseStyle, matchStyle string) string {
497 if len(positions) == 0 {
498 if baseStyle != "" {
499 return h.lp.SprintStyled(baseStyle, text)
500 }
501 return text
502 }
503 posSet := make(map[int]bool, len(positions))
504 for _, p := range positions {
505 posSet[p] = true
506 }
507 runes := []rune(text)
508 var sb strings.Builder
509 for i, r := range runes {
510 ch := string(r)
511 if posSet[i] {
512 sb.WriteString(h.lp.SprintStyled(matchStyle, ch))
513 } else if baseStyle != "" {
514 sb.WriteString(h.lp.SprintStyled(baseStyle, ch))
515 } else {
516 sb.WriteString(ch)
517 }
518 }
519 return sb.String()
520}
521
522// selectedBinding returns the currently selected binding, or nil if none.
523func (h *Handler) selectedBinding() *Binding {

Callers 2

drawCategorySuffixMethod · 0.95
drawBindingLineMethod · 0.95

Calls 3

SprintStyledMethod · 0.80
StringMethod · 0.65
WriteStringMethod · 0.45

Tested by

no test coverage detected