feat(search): highlight and navigate scrollback matches in place

Searching the scrollback used to replace the whole screen with an external pager, losing the surrounding context. Highlight every match with a marker instead, show the [current/total] counter and provide temporary n/N/Escape bindings, restoring the previous marker and title when the search ends.
This commit is contained in:
BlackAngelTV committed 2026-11-13 08:22:07 +01:00
1 parent dc13716431
commit 7b306f5bed
5 files changed
+342

No files matched your search

+138
View File
@@ -825,6 +825,11 @@ class Window:
self.actions_on_focus_change: list[Callable[['Window', bool], None]] = []
self.actions_on_removal: list[Callable[['Window'], None]] = []
self.current_marker_spec: tuple[str, str | tuple[tuple[int, str], ...]] | None = None
self.scrollback_search: ScrollbackSearch | None = None
self.scrollback_search_mode: str = ''
self.scrollback_search_saved_marker: tuple[str, str | tuple[tuple[int, str], ...]] | None = None
self.scrollback_search_saved_title: str | None = None
self.actions_on_focus_change.append(self._on_focus_change_clear_scrollback_search)
self.kitten_result_processors: list[Callable[['Window', Any], None]] = []
self.child_is_launched = False
self.restored_scrollback = b''
@@ -2666,6 +2671,139 @@ class Window:
w.screen.paste_bytes(sanitized)
w.send_key('enter')
@ac(
'sc',
"""
Search the scrollback in place, highlighting every match. Prefix the query with \\c for a case
sensitive search and with \\r for a regular expression. Matches are navigated with n and N and
the search ends with Escape. The [current/total] counter is shown in the window title.
""",
)
def search_scrollback_prompt(self) -> bool | None:
if not self.screen.is_main_linebuf():
return True
def done(text: str) -> None:
if text:
self.start_scrollback_search(text)
else:
self.clear_scrollback_search()
get_boss().get_line(
_('Search the scrollback. Prefix the query with \\c for a case sensitive search and \\r for a regular expression.'),
done,
window=self,
prompt=_('search> '),
window_title=_('Search scrollback'),
)
return None
def scrollback_search_lines(self) -> list[str]:
# Read the history buffer and the visible screen directly instead of using
# as_text(), so that line numbers are the ones scrolling actions use.
lines: list[str] = []
self.screen.as_text_for_history_buf(lines.append, False, True)
self.screen.as_text(lines.append, False, True)
return [line.replace('\r', '').replace('\n', '') for line in lines]
def start_scrollback_search(self, raw_query: str) -> None:
from .scrollback_search import InvalidQuery, ScrollbackSearch, compile_query, find_matches, parse_query
try:
query = parse_query(raw_query)
pattern = compile_query(query)
except InvalidQuery as err:
get_boss().show_error(_('Invalid search query'), str(err))
return
lines = self.scrollback_search_lines()
matches = find_matches(lines, query, pattern)
if not matches:
from .fast_data_types import ring_bell
ring_bell(self.os_window_id)
return
self.clear_scrollback_search()
from .marks import marker_from_regex
self.scrollback_search = ScrollbackSearch(query, pattern, matches, total_lines=len(lines))
self.scrollback_search_saved_marker = self.current_marker_spec
self.scrollback_search_saved_title = self.override_title
self.screen.set_marker(marker_from_regex(pattern, 1))
self._push_scrollback_search_mode()
self._goto_scrollback_match(0)
@ac('sc', 'Stop the in-place scrollback search and remove its highlights')
def clear_scrollback_search(self) -> None:
if self.scrollback_search is None:
return
self.scrollback_search = None
self.screen.set_marker()
self.current_marker_spec = None
saved = self.scrollback_search_saved_marker
self.scrollback_search_saved_marker = None
if saved is not None:
# Restore the marker that was displayed before the search started.
from .marks import marker_from_spec
ftype, spec = saved
self.screen.set_marker(marker_from_spec(ftype, spec, 0))
self.current_marker_spec = saved
self.set_title(self.scrollback_search_saved_title)
self.scrollback_search_saved_title = None
if self.scrollback_search_mode:
get_boss().mappings.pop_keyboard_mode_if_is(self.scrollback_search_mode)
self.scrollback_search_mode = ''
@ac('sc', 'Jump to the next match of the in-place scrollback search')
def scrollback_search_next(self) -> bool | None:
return self._scrollback_search_step(1)
@ac('sc', 'Jump to the previous match of the in-place scrollback search')
def scrollback_search_prev(self) -> bool | None:
return self._scrollback_search_step(-1)
def _scrollback_search_step(self, step: int) -> bool | None:
search = self.scrollback_search
if search is None:
return None
if not self.screen.is_main_linebuf():
return True
self._goto_scrollback_match(search.index + step)
return None
def _goto_scrollback_match(self, index: int) -> None:
from .scrollback_search import scroll_offset_for
search = self.scrollback_search
if search is None or not search.matches:
return
search.index = index % len(search.matches)
match = search.matches[search.index]
self.set_title(f'[{search.index + 1}/{len(search.matches)}] {search.query.text}')
self.screen.scroll_to_absolute(scroll_offset_for(match, search.total_lines, self.screen.lines))
def _push_scrollback_search_mode(self) -> None:
from .fast_data_types import GLFW_FKEY_ESCAPE, GLFW_MOD_SHIFT
from .options.utils import KeyDefinition, KeyboardMode, SingleKey
km = KeyboardMode('__scrollback_search__')
km.on_unknown = 'passthrough'
km.on_action = 'keep'
next_match = KeyDefinition(definition='scrollback_search_next')
prev_match = KeyDefinition(definition='scrollback_search_prev')
km.keymap[SingleKey(key=ord('n'))].append(next_match)
km.keymap[SingleKey(mods=GLFW_MOD_SHIFT, key=ord('n'))].append(prev_match)
km.keymap[SingleKey(mods=GLFW_MOD_SHIFT, key=ord('N'))].append(prev_match)
km.keymap[SingleKey(key=GLFW_FKEY_ESCAPE)].append(KeyDefinition(definition='clear_scrollback_search'))
self.scrollback_search_mode = km.name
get_boss().mappings._push_keyboard_mode(km)
def _on_focus_change_clear_scrollback_search(self, window: 'Window', focused: bool) -> None:
# The n/N bindings live in a temporary keyboard mode, so the search must
# end when this window stops being the one receiving keys.
if not focused and self.scrollback_search is not None:
self.clear_scrollback_search()
def show_cmd_output(self, which: CommandOutput, title: str = 'Command output', as_ansi: bool = True, add_wrap_markers: bool = True) -> None:
text = self.cmd_output(which, as_ansi=as_ansi, add_wrap_markers=add_wrap_markers)
text = text.replace('\r\n', '\n').replace('\r', '\n')