summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Bütow <1224732+snonux@users.noreply.github.com>2025-06-21 10:40:43 +0300
committerPaul Bütow <1224732+snonux@users.noreply.github.com>2025-06-21 10:40:43 +0300
commit1ec857941b2b84c9388542ccfb50d81999c3abb7 (patch)
treea7974d60cc77326674845e5516b0652bde7a1dc0 /internal
parent52adb72a3a7b059744a517194bc578284907f561 (diff)
highlight only matching text
Diffstat (limited to 'internal')
-rw-r--r--internal/ui/table.go32
1 files changed, 21 insertions, 11 deletions
diff --git a/internal/ui/table.go b/internal/ui/table.go
index 49a3c75..c03bc7e 100644
--- a/internal/ui/table.go
+++ b/internal/ui/table.go
@@ -628,12 +628,25 @@ func (m Model) priorityView() string {
return "priority: " + strings.Join(parts, " ")
}
-func highlightCell(rendered string, re *regexp.Regexp, raw string, width int) string {
+func highlightCell(base lipgloss.Style, re *regexp.Regexp, raw string) string {
if re == nil || !re.MatchString(raw) {
- return rendered
+ return base.Render(raw)
}
- style := lipgloss.NewStyle().Background(lipgloss.Color("226")).Foreground(lipgloss.Color("21")).Width(width)
- return style.Render(rendered)
+
+ highlight := lipgloss.NewStyle().Background(lipgloss.Color("226")).Foreground(lipgloss.Color("21"))
+ var b strings.Builder
+ last := 0
+ for _, loc := range re.FindAllStringIndex(raw, -1) {
+ if loc[0] > last {
+ b.WriteString(base.Render(raw[last:loc[0]]))
+ }
+ b.WriteString(base.Copy().Inherit(highlight).Render(raw[loc[0]:loc[1]]))
+ last = loc[1]
+ }
+ if last < len(raw) {
+ b.WriteString(base.Render(raw[last:]))
+ }
+ return b.String()
}
func taskToRowSearch(t task.Task, re *regexp.Regexp) atable.Row {
@@ -661,14 +674,11 @@ func taskToRowSearch(t task.Task, re *regexp.Regexp) atable.Row {
ageStr := style.Render(age)
dueStr := formatDue(t.Due, dueWidth)
urgStr := style.Render(urg)
- tagStr := style.Render(tags)
- descStr := style.Render(t.Description)
- annRaw := strings.Join(anns, "; ")
- annStr := style.Render(annRaw)
- tagStr = highlightCell(tagStr, re, tags, tagsWidth)
- descStr = highlightCell(descStr, re, t.Description, descWidth)
- annStr = highlightCell(annStr, re, annRaw, annWidth)
+ tagStr := highlightCell(style, re, tags)
+ descStr := highlightCell(style, re, t.Description)
+ annRaw := strings.Join(anns, "; ")
+ annStr := highlightCell(style, re, annRaw)
return atable.Row{
idStr,