Files
BlackAngelTV 4df542f590 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
2026-12-03 10:06:04 +01:00

128 lines
5.4 KiB
Python

#!/usr/bin/env python
# License: GPLv3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
import re
import time
from kitty.scrollback_search import (
CURRENT_MATCH_COLOR,
MATCH_COLOR,
InvalidQuery,
Match,
ScrollbackSearch,
compile_query,
find_matches,
parse_query,
scroll_offset_for,
)
from .base import BaseTest
class TestScrollbackSearch(BaseTest):
def test_parse_query(self):
q = parse_query('needle')
self.ae((q.text, q.case_sensitive, q.is_regex), ('needle', False, False))
self.assertTrue(compile_query(q).flags & re.IGNORECASE)
q = parse_query(' padded ')
self.ae(q.text, 'padded')
def test_parse_query_modifiers(self):
q = parse_query('\\c Needle')
self.ae((q.text, q.case_sensitive, q.is_regex), ('Needle', True, False))
self.ae(compile_query(q).flags & re.IGNORECASE, 0)
q = parse_query('\\r a.c')
self.ae((q.text, q.case_sensitive, q.is_regex), ('a.c', False, True))
for raw in ('\\r\\c a+b', '\\c\\r a+b', '\\C\\R a+b'):
q = parse_query(raw)
self.ae((q.text, q.case_sensitive, q.is_regex), ('a+b', True, True), raw)
with self.assertRaises(InvalidQuery):
parse_query('')
with self.assertRaises(InvalidQuery):
parse_query(' ')
with self.assertRaises(InvalidQuery):
parse_query('\\c')
with self.assertRaises(InvalidQuery):
compile_query(parse_query('\\r ('))
def test_parse_query_escaped_backslash(self):
# A doubled backslash searches text starting with a backslash instead of
# being read as a modifier, so both modifier prefixes remain reachable.
for raw, expected in (('\\\\c', '\\c'), ('\\\\r', '\\r'), ('\\\\C', '\\C'), ('\\\\', '\\')):
with self.subTest(raw=raw):
q = parse_query(raw)
self.ae((q.text, q.case_sensitive, q.is_regex), (expected, False, False))
# Modifiers still combine with an escaped prefix in the searched text
q = parse_query('\\c\\\\r')
self.ae((q.text, q.case_sensitive, q.is_regex), ('\\r', True, False))
# The escaped prefix is matched like any other text, case insensitively
# by default, and the case sensitive modifier still applies to it.
matches = find_matches(['a \\c b', 'a \\C b'], parse_query('\\\\c'))
self.ae([(m.line, m.start, m.end) for m in matches], [(0, 2, 4), (1, 2, 4)])
matches = find_matches(['a \\c b', 'a \\C b'], parse_query('\\c\\\\c'))
self.ae([(m.line, m.start, m.end) for m in matches], [(0, 2, 4)])
def test_find_matches_plain(self):
lines = ['needle at start', 'Needle again', 'nothing', 'a needle here']
matches = find_matches(lines, parse_query('needle'))
self.ae([(m.line, m.start, m.end) for m in matches], [(0, 0, 6), (1, 0, 6), (3, 2, 8)])
matches = find_matches(lines, parse_query('\\c Needle'))
self.ae([(m.line, m.start, m.end) for m in matches], [(1, 0, 6)])
# Regex metacharacters are literal in a plain query
self.ae(find_matches(['a+b', 'aab'], parse_query('a+b')), [Match(0, 0, 3)])
def test_find_matches_regex(self):
matches = find_matches(['abc abc', 'nothing here'], parse_query('\\r a.c'))
self.ae([(m.line, m.start, m.end) for m in matches], [(0, 0, 3), (0, 4, 7)])
# Empty matches are skipped and do not loop forever
self.ae(find_matches(['abc'], parse_query('\\r x*')), [])
def test_current_match(self):
lines = ['one 17 here', 'nothing', '17 and 17']
query = parse_query('17')
matches = find_matches(lines, query)
search = ScrollbackSearch(query, compile_query(query), matches, total_lines=len(lines))
self.ae(len(search), 3)
self.ae(search.current_match, Match(0, 4, 6))
search.index = 2
self.ae(search.current_match, Match(2, 7, 9))
# The current match is the only one with its own color, and navigation
# moves that color along
spans = search.marker_spans(lines)
self.ae(spans['one 17 here'], ((4, 6, MATCH_COLOR),))
self.ae(spans['17 and 17'], ((0, 2, MATCH_COLOR), (7, 9, CURRENT_MATCH_COLOR)))
search.index = 1
self.ae(search.marker_spans(lines)['17 and 17'], ((0, 2, CURRENT_MATCH_COLOR), (7, 9, MATCH_COLOR)))
# Wrapping the index keeps exactly one current match selected
search.index = 4 % len(search.matches)
self.ae(sum(1 for found in search.marker_spans(lines).values() for span in found if span[2] == CURRENT_MATCH_COLOR), 1)
def test_scroll_offset(self):
self.ae(scroll_offset_for(Match(0, 0, 1), 100, 10), 90)
self.ae(scroll_offset_for(Match(50, 0, 1), 100, 10), 43)
self.ae(scroll_offset_for(Match(99, 0, 1), 100, 10), 0)
# No scrollback: the visible screen is always shown as is
self.ae(scroll_offset_for(Match(3, 0, 1), 5, 5), 0)
def test_performance_on_large_scrollback(self):
lines = [f'line {i} with some ordinary text' for i in range(10_000)]
for i in range(0, 10_000, 10):
lines[i] = 'needle ' + lines[i]
query = parse_query('needle')
start = time.monotonic()
matches = find_matches(lines, query)
elapsed = time.monotonic() - start
self.ae(len(matches), 1000)
self.assertLess(elapsed, 0.05, f'searching 10 000 lines took {elapsed * 1000:.1f} ms')