diff --git a/kitty/tab_bar.py b/kitty/tab_bar.py index 8c5a16d1b..fd8f0e344 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -5,8 +5,9 @@ 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 + Any, Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union, TYPE_CHECKING ) +from weakref import ReferenceType from .borders import Border, BorderColor from .config import build_ansi_color_table @@ -21,11 +22,15 @@ from .typing import EdgeLiteral, PowerlineStyle from .utils import color_as_int, log_error, sgr_sanitizer_pat +if TYPE_CHECKING: + from .tabs import Tab + + class TabBarData(NamedTuple): title: str is_active: bool needs_attention: bool - cwd: str + tab_ref: ReferenceType['Tab'] num_windows: int num_window_groups: int layout_name: str @@ -173,14 +178,28 @@ def template_has_field(template: str, field: str) -> bool: return False +class TabCWD: + + def __init__(self, tab_ref: ReferenceType['Tab']): + self.tab_ref = tab_ref + self.saved_val: Optional[str] = None + + def __str__(self) -> str: + if self.saved_val is None: + tab = self.tab_ref() + self.saved_val = (tab.get_cwd_of_active_window() or '') if tab else '' + return self.saved_val + + def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) -> None: + tcwd = TabCWD(tab.tab_ref) data = { 'index': index, 'layout_name': tab.layout_name, 'num_windows': tab.num_windows, 'num_window_groups': tab.num_window_groups, 'title': tab.title, - 'cwd': tab.cwd, + 'cwd': tcwd, } ColorFormatter.draw_data = draw_data ColorFormatter.tab_data = tab @@ -190,7 +209,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, - 'cwd': tab.cwd, + 'cwd': tcwd, 'fmt': Formatter, 'sup': SupSub(data), 'sub': SupSub(data, True), diff --git a/kitty/tabs.py b/kitty/tabs.py index cffdd9beb..f3e15a049 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -230,10 +230,9 @@ class Tab: # {{{ ans += 1 return ans - @property - def get_cwd(self) -> str: + def get_cwd_of_active_window(self, oldest: bool = False) -> Optional[str]: w = self.active_window - return w.cwd_of_child if w and w.cwd_of_child else self.cwd + return w.get_cwd_of_child(oldest) if w else None def set_title(self, title: str) -> None: self.name = title or '' @@ -1083,14 +1082,13 @@ class TabManager: # {{{ title = (t.name or t.title or appname).strip() needs_attention = False has_activity_since_last_focus = False - cwd = os.path.abspath(t.get_cwd) for w in t: if w.needs_attention: needs_attention = True if w.has_activity_since_last_focus: has_activity_since_last_focus = True ans.append(TabBarData( - title, t is at, needs_attention, cwd, + title, t is at, needs_attention, weakref.ref(t), 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