Don't expose all Tab methods in title template

They can have side effects so prevent user from foot shot
This commit is contained in:
Kovid Goyal
2022-07-26 13:13:00 +05:30
parent 352c07987b
commit 546cdbefae
3 changed files with 26 additions and 16 deletions

View File

@@ -1106,6 +1106,13 @@ class Boss:
w, h = get_new_os_window_size(metrics, width, height, unit, incremental, has_window_scaling) w, h = get_new_os_window_size(metrics, width, height, unit, incremental, has_window_scaling)
set_os_window_size(os_window_id, w, h) 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: def default_bg_changed_for(self, window_id: int) -> None:
w = self.window_id_map.get(window_id) w = self.window_id_map.get(window_id)
if w is not None: if w is not None:

View File

@@ -5,15 +5,15 @@ import os
from functools import lru_cache, partial, wraps from functools import lru_cache, partial, wraps
from string import Formatter as StringFormatter from string import Formatter as StringFormatter
from typing import ( 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 .borders import Border, BorderColor
from .config import build_ansi_color_table from .config import build_ansi_color_table
from .constants import config_dir from .constants import config_dir
from .fast_data_types import ( from .fast_data_types import (
DECAWM, Region, Screen, cell_size_for_window, get_options, pt_to_px, DECAWM, Color, Region, Screen, cell_size_for_window, get_boss, get_options,
set_tab_bar_render_data, viewport_for_window, Color 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 .rgb import alpha_blend, color_as_sgr, color_from_int, to_color
from .types import WindowGeometry, run_once 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 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): class TabBarData(NamedTuple):
title: str title: str
is_active: bool is_active: bool
needs_attention: bool needs_attention: bool
tab_ref: 'ReferenceType[Tab]' tab_id: int
num_windows: int num_windows: int
num_window_groups: int num_window_groups: int
layout_name: str layout_name: str
@@ -178,14 +173,26 @@ def template_has_field(template: str, field: str) -> bool:
return False 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: def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) -> None:
ta = TabAccessor(tab.tab_id)
data = { data = {
'index': index, 'index': index,
'layout_name': tab.layout_name, 'layout_name': tab.layout_name,
'num_windows': tab.num_windows, 'num_windows': tab.num_windows,
'num_window_groups': tab.num_window_groups, 'num_window_groups': tab.num_window_groups,
'title': tab.title, 'title': tab.title,
'tab': tab.tab_ref(), 'tab': ta,
} }
ColorFormatter.draw_data = draw_data ColorFormatter.draw_data = draw_data
ColorFormatter.tab_data = tab 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_windows': tab.num_windows,
'num_window_groups': tab.num_window_groups, 'num_window_groups': tab.num_window_groups,
'title': tab.title, 'title': tab.title,
'tab': tab.tab_ref(), 'tab': ta,
'fmt': Formatter, 'fmt': Formatter,
'sup': SupSub(data), 'sup': SupSub(data),
'sub': SupSub(data, True), 'sub': SupSub(data, True),

View File

@@ -234,10 +234,6 @@ class Tab: # {{{
w = self.active_window w = self.active_window
return w.get_cwd_of_child(oldest) if w else None 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: def set_title(self, title: str) -> None:
self.name = title or '' self.name = title or ''
self.mark_tab_bar_dirty() self.mark_tab_bar_dirty()
@@ -1092,7 +1088,7 @@ class TabManager: # {{{
if w.has_activity_since_last_focus: if w.has_activity_since_last_focus:
has_activity_since_last_focus = True has_activity_since_last_focus = True
ans.append(TabBarData( 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 '', len(t), t.num_window_groups, t.current_layout.name or '',
has_activity_since_last_focus, t.active_fg, t.active_bg, has_activity_since_last_focus, t.active_fg, t.active_bg,
t.inactive_fg, t.inactive_bg t.inactive_fg, t.inactive_bg