#!/usr/bin/env python # License: GPLv3 Copyright: 2026, Kovid Goyal import re import time from kitty.scrollback_search import ( InvalidQuery, Match, 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_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')