kitten @ ls: Allow limiting output to matched windows/tabs

Fixes #6520
This commit is contained in:
Kovid Goyal
2023-08-03 11:53:17 +05:30
parent 9678e9fa1b
commit cade8efc25
5 changed files with 78 additions and 39 deletions

View File

@@ -11,6 +11,7 @@ from operator import attrgetter
from time import monotonic
from typing import (
Any,
Callable,
Deque,
Dict,
Generator,
@@ -770,13 +771,14 @@ class Tab: # {{{
def move_window_backward(self) -> None:
self.move_window(-1)
def list_windows(self, self_window: Optional[Window] = None) -> Generator[WindowDict, None, None]:
def list_windows(self, self_window: Optional[Window] = None, window_filter: Optional[Callable[[Window], bool]] = None) -> Generator[WindowDict, None, None]:
active_window = self.active_window
for w in self:
yield w.as_dict(
is_active=w is active_window,
is_focused=w.os_window_id == current_focused_os_window_id() and w is active_window,
is_self=w is self_window)
if window_filter is None or window_filter(w):
yield w.as_dict(
is_active=w is active_window,
is_focused=w.os_window_id == current_focused_os_window_id() and w is active_window,
is_self=w is self_window)
def matches_query(self, field: str, query: str, active_tab_manager: Optional['TabManager'] = None) -> bool:
if field == 'title':
@@ -1011,21 +1013,28 @@ class TabManager: # {{{
def __len__(self) -> int:
return len(self.tabs)
def list_tabs(self, self_window: Optional[Window] = None) -> Generator[TabDict, None, None]:
def list_tabs(
self, self_window: Optional[Window] = None,
tab_filter: Optional[Callable[[Tab], bool]] = None,
window_filter: Optional[Callable[[Window], bool]] = None
) -> Generator[TabDict, None, None]:
active_tab = self.active_tab
for tab in self:
yield {
'id': tab.id,
'is_focused': tab is active_tab and tab.os_window_id == current_focused_os_window_id(),
'is_active': tab is active_tab,
'title': tab.name or tab.title,
'layout': str(tab.current_layout.name),
'layout_state': tab.current_layout.layout_state(),
'layout_opts': tab.current_layout.layout_opts.serialized(),
'enabled_layouts': tab.enabled_layouts,
'windows': list(tab.list_windows(self_window)),
'active_window_history': list(tab.windows.active_window_history),
}
if tab_filter is None or tab_filter(tab):
windows = list(tab.list_windows(self_window, window_filter))
if windows:
yield {
'id': tab.id,
'is_focused': tab is active_tab and tab.os_window_id == current_focused_os_window_id(),
'is_active': tab is active_tab,
'title': tab.name or tab.title,
'layout': str(tab.current_layout.name),
'layout_state': tab.current_layout.layout_state(),
'layout_opts': tab.current_layout.layout_opts.serialized(),
'enabled_layouts': tab.enabled_layouts,
'windows': windows,
'active_window_history': list(tab.windows.active_window_history),
}
def serialize_state(self) -> Dict[str, Any]:
return {