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)
| 494 | // positions rendered using matchStyle, and the rest rendered using baseStyle |
| 495 | // (or unstyled if baseStyle is empty). |
| 496 | func (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. |
| 523 | func (h *Handler) selectedBinding() *Binding { |
no test coverage detected