From 352c07987b2a39088a2d010a348bbcd743d8a25a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 26 Jul 2022 12:33:53 +0530 Subject: [PATCH] Nicer fix for showing cwd in tab titles --- docs/changelog.rst | 3 +++ kitty/options/definition.py | 3 ++- kitty/tab_bar.py | 18 ++---------------- kitty/tabs.py | 4 ++++ 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 095326a13..6b7a4f67b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -84,6 +84,9 @@ Detailed list of changes - Expand ~ in paths configured in :opt:`editor` and :opt:`exe_search_path` (:disc:`5298`) +- Allow showing the working directory of the active window in tab titles + (:pull:`5314`) + 0.25.2 [2022-06-07] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 5eda644d6..81b52041e 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -1166,7 +1166,8 @@ mapped for :code:`goto_tab N`. If you prefer to see the index as a superscript, use :code:`{sup.index}`. In addition you can use :code:`{layout_name}` for the current layout name, :code:`{num_windows}` for the number of windows in the tab, :code:`{num_window_groups}` for the number of window groups (not counting -overlay windows) in the tab, and {cwd} for current working directory. +overlay windows) in the tab, and :code:`{tab.active_wd}` for the working directory +of the active_window. Note that formatting is done by Python's string formatting machinery, so you can use, for instance, :code:`{layout_name[:2].upper()}` to show only the first two letters of the layout name, upper-cased. If you want to style the text, you can diff --git a/kitty/tab_bar.py b/kitty/tab_bar.py index 67ab8df5c..cf1243bc7 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -178,28 +178,14 @@ 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': tcwd, + 'tab': tab.tab_ref(), } ColorFormatter.draw_data = draw_data ColorFormatter.tab_data = tab @@ -209,7 +195,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': tcwd, + 'tab': tab.tab_ref(), 'fmt': Formatter, 'sup': SupSub(data), 'sub': SupSub(data, True), diff --git a/kitty/tabs.py b/kitty/tabs.py index f3e15a049..9d41b3dc3 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -234,6 +234,10 @@ 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()