feat(search): highlight the current match in its own color
Every match of the in-place search used the same mark color, so after pressing n or N the only hint of the current match was the counter in the window title, which is easy to miss on a short query or a dense screen. The marker now colors the current match with the second mark color and keeps the rest on the first one, rebuilding the marker whenever the index changes. Fixes #51
This commit is contained in:
6 files changed
+91
-6
No files matched your search
@@ -13,11 +13,17 @@ modifier syntax, so that the two prefixes can be searched for literally.
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
CASE_SENSITIVE_MODIFIER = 'c'
|
||||
REGEX_MODIFIER = 'r'
|
||||
|
||||
# Mark numbers (see :opt:`mark1_background`) used to highlight the matches. The
|
||||
# current match gets its own color so that it stands out from the other ones.
|
||||
MATCH_COLOR = 1
|
||||
CURRENT_MATCH_COLOR = 2
|
||||
|
||||
|
||||
class InvalidQuery(ValueError):
|
||||
pass
|
||||
@@ -52,6 +58,25 @@ class ScrollbackSearch:
|
||||
def __len__(self) -> int:
|
||||
return len(self.matches)
|
||||
|
||||
@property
|
||||
def current_match(self) -> Match | None:
|
||||
return self.matches[self.index] if self.matches else None
|
||||
|
||||
def marker_spans(self, lines: Sequence[str]) -> dict[str, tuple[tuple[int, int, int], ...]]:
|
||||
"""Spans to highlight, keyed by the text of the line holding them.
|
||||
|
||||
Marker functions are called once per line with only its text, never with
|
||||
its number, so the spans are grouped by line text. Lines that are
|
||||
identical to the current one are therefore highlighted alike, which the
|
||||
user cannot tell apart since their text is the same.
|
||||
"""
|
||||
current = self.current_match
|
||||
spans: dict[str, list[tuple[int, int, int]]] = {}
|
||||
for match in self.matches:
|
||||
color = CURRENT_MATCH_COLOR if match == current else MATCH_COLOR
|
||||
spans.setdefault(lines[match.line], []).append((match.start, match.end, color))
|
||||
return {text: tuple(found) for text, found in spans.items()}
|
||||
|
||||
|
||||
def parse_query(raw: str) -> Query:
|
||||
text = (raw or '').strip()
|
||||
|
||||
Reference in new issue
Block a user