From 546cdbefae36e078398fe1dcaac42f63e0e582e4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 26 Jul 2022 13:13:00 +0530 Subject: [PATCH] Don't expose all Tab methods in title template They can have side effects so prevent user from foot shot --- kitty/boss.py | 7 +++++++ kitty/tab_bar.py | 29 ++++++++++++++++++----------- kitty/tabs.py | 6 +----- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 423008535..3fb638fa9 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -1106,6 +1106,13 @@ class Boss: w, h = get_new_os_window_size(metrics, width, height, unit, incremental, has_window_scaling) set_os_window_size(os_window_id, w, h) + def tab_for_id(self, tab_id: int) -> Optional[Tab]: + for tm in self.os_window_map.values(): + tab = tm.tab_for_id(tab_id) + if tab is not None: + return tab + return None + def default_bg_changed_for(self, window_id: int) -> None: w = self.window_id_map.get(window_id) if w is not None: diff --git a/kitty/tab_bar.py b/kitty/tab_bar.py index cf1243bc7..cacaf7a30 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -5,15 +5,15 @@ import os from functools import lru_cache, partial, wraps from string import Formatter as StringFormatter from typing import ( - Any, Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union, TYPE_CHECKING + Any, Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union ) from .borders import Border, BorderColor from .config import build_ansi_color_table from .constants import config_dir from .fast_data_types import ( - DECAWM, Region, Screen, cell_size_for_window, get_options, pt_to_px, - set_tab_bar_render_data, viewport_for_window, Color + DECAWM, Color, Region, Screen, cell_size_for_window, get_boss, get_options, + pt_to_px, set_tab_bar_render_data, viewport_for_window ) from .rgb import alpha_blend, color_as_sgr, color_from_int, to_color from .types import WindowGeometry, run_once @@ -21,16 +21,11 @@ from .typing import EdgeLiteral, PowerlineStyle from .utils import color_as_int, log_error, sgr_sanitizer_pat -if TYPE_CHECKING: - from weakref import ReferenceType - from .tabs import Tab - - class TabBarData(NamedTuple): title: str is_active: bool needs_attention: bool - tab_ref: 'ReferenceType[Tab]' + tab_id: int num_windows: int num_window_groups: int layout_name: str @@ -178,14 +173,26 @@ def template_has_field(template: str, field: str) -> bool: return False +class TabAccessor: + + def __init__(self, tab_id: int): + self.tab_id = tab_id + + @property + def active_wd(self) -> str: + tab = get_boss().tab_for_id(self.tab_id) + return (tab.get_cwd_of_active_window() if tab else '') or '' + + def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) -> None: + ta = TabAccessor(tab.tab_id) data = { 'index': index, 'layout_name': tab.layout_name, 'num_windows': tab.num_windows, 'num_window_groups': tab.num_window_groups, 'title': tab.title, - 'tab': tab.tab_ref(), + 'tab': ta, } ColorFormatter.draw_data = draw_data ColorFormatter.tab_data = tab @@ -195,7 +202,7 @@ def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) 'num_windows': tab.num_windows, 'num_window_groups': tab.num_window_groups, 'title': tab.title, - 'tab': tab.tab_ref(), + 'tab': ta, 'fmt': Formatter, 'sup': SupSub(data), 'sub': SupSub(data, True), diff --git a/kitty/tabs.py b/kitty/tabs.py index 9d41b3dc3..2e917c9bf 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -234,10 +234,6 @@ class Tab: # {{{ w = self.active_window return w.get_cwd_of_child(oldest) if w else None - @property - def active_wd(self) -> str: - return self.get_cwd_of_active_window() or '' - def set_title(self, title: str) -> None: self.name = title or '' self.mark_tab_bar_dirty() @@ -1092,7 +1088,7 @@ class TabManager: # {{{ if w.has_activity_since_last_focus: has_activity_since_last_focus = True ans.append(TabBarData( - title, t is at, needs_attention, weakref.ref(t), + title, t is at, needs_attention, t.id, len(t), t.num_window_groups, t.current_layout.name or '', has_activity_since_last_focus, t.active_fg, t.active_bg, t.inactive_fg, t.inactive_bg