Skip to content

Commit 757748a

Browse files
feat: highlight matches
1 parent 0d03723 commit 757748a

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

internal/ui/model.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ var (
4242

4343
// Selection Style
4444
selectedStyle = lipgloss.NewStyle().Background(lipgloss.Color("#555555")).Foreground(lipgloss.Color("#ffffff"))
45+
46+
// Match Style (Search Matches)
47+
matchStyle = lipgloss.NewStyle().Background(lipgloss.Color("#FFFF00")).Foreground(lipgloss.Color("#000000"))
4548
)
4649

4750
type Point struct {
@@ -724,6 +727,8 @@ func (m Model) View() string {
724727
// 2. Wrap vs Horizontal Scroll
725728
if m.wrap {
726729
// WRAP MODE
730+
// 0. Highlight Matches (Priority)
731+
line = highlightMatches(line, m.filterText, m.regexMode)
727732
line = highlightLine(line)
728733
if isBookmarked {
729734
line = "🔖 " + line
@@ -851,6 +856,7 @@ func (m Model) View() string {
851856
visiblePart := string(rawRunes[m.xOffset : end])
852857

853858
// Highlight visible part
859+
visiblePart = highlightMatches(visiblePart, m.filterText, m.regexMode)
854860
line = highlightLine(visiblePart)
855861

856862
// 2. Selection Highlighting (Lazy)
@@ -1028,6 +1034,30 @@ func highlightLine(line string) string {
10281034
return line
10291035
}
10301036

1037+
func highlightMatches(line, pattern string, isRegex bool) string {
1038+
if pattern == "" {
1039+
return line
1040+
}
1041+
1042+
var re *regexp.Regexp
1043+
var err error
1044+
1045+
if isRegex {
1046+
re, err = regexp.Compile(pattern)
1047+
} else {
1048+
// Case insensitive literal match
1049+
re, err = regexp.Compile("(?i)" + regexp.QuoteMeta(pattern))
1050+
}
1051+
1052+
if err != nil {
1053+
return line // Return regex errors as is (or handle better?)
1054+
}
1055+
1056+
return re.ReplaceAllStringFunc(line, func(match string) string {
1057+
return matchStyle.Render(match)
1058+
})
1059+
}
1060+
10311061

10321062

10331063
func stripAnsi(str string) string {

0 commit comments

Comments
 (0)