From 0ce2a23af30afa6a3e74f92217f0cee502dfc475 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 29 Jul 2022 07:43:55 +0530 Subject: [PATCH] Allow tab title templates to use the current max title length --- kitty/options/definition.py | 15 ++++++++++----- kitty/tab_bar.py | 20 +++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 81b52041e..667266f40 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -1163,11 +1163,16 @@ A template to render the tab title. The default just renders the title with optional symbols for bell and activity. If you wish to include the tab-index as well, use something like: :code:`{index}:{title}`. Useful if you have shortcuts 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 :code:`{tab.active_wd}` for the working directory -of the active_window. +use :code:`{sup.index}`. All data available is: + +* :code:`title` - the current tab title +* :code:`index` - the tab index useable with :code:`goto_tab N` shortcuts +* :code:`layout_name` - the current layout name +* :code:`num_windows` - the number of windows in the tab +* :code:`num_window_groups` - the number of window groups (not counting overlay windows) in the tab +* :code:`tab.active_wd` - the working directory of the currently active window in the tab (expensive, requires syscall) +* :code:`max_title_length` - the maximum title length available + 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 cacaf7a30..2b2284915 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -184,7 +184,12 @@ class TabAccessor: return (tab.get_cwd_of_active_window() if tab else '') or '' -def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) -> None: +safe_builtins = { + 'max': max, 'min': min, 'str': str, 'repr': repr, 'abs': abs, 'len': len, +} + + +def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int, max_title_length: int = 0) -> None: ta = TabAccessor(tab.tab_id) data = { 'index': index, @@ -208,6 +213,7 @@ def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) 'sub': SupSub(data, True), 'bell_symbol': draw_data.bell_on_tab if tab.needs_attention else '', 'activity_symbol': draw_data.tab_activity_symbol if tab.has_activity_since_last_focus else '', + 'max_title_length': max_title_length, } template = draw_data.title_template if tab.is_active and draw_data.active_title_template is not None: @@ -220,7 +226,7 @@ def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) if prefix: template = '{fmt.fg.red}' + prefix + '{fmt.fg.tab}' + template try: - title = eval(compile_template(template), {'__builtins__': {}}, eval_locals) + title = eval(compile_template(template), {'__builtins__': safe_builtins}, eval_locals) except Exception as e: report_template_failure(template, str(e)) title = tab.title @@ -259,7 +265,7 @@ def draw_tab_with_slant( else: draw_sep(left_sep) screen.draw(' ') - draw_title(draw_data, screen, tab, index) + draw_title(draw_data, screen, tab, index, max_title_length) extra = screen.cursor.x - before - max_title_length if extra >= 0: screen.cursor.x -= extra + 3 @@ -280,7 +286,7 @@ def draw_tab_with_separator( ) -> int: if draw_data.leading_spaces: screen.draw(' ' * draw_data.leading_spaces) - draw_title(draw_data, screen, tab, index) + draw_title(draw_data, screen, tab, index, max_title_length) trailing_spaces = min(max_title_length - 1, draw_data.trailing_spaces) max_title_length -= trailing_spaces extra = screen.cursor.x - before - max_title_length @@ -311,11 +317,11 @@ def draw_tab_with_fade( screen.cursor.bg = bg screen.draw(' ') screen.cursor.bg = orig_bg - draw_title(draw_data, screen, tab, index) + draw_title(draw_data, screen, tab, index, max(0, max_title_length - 8)) extra = screen.cursor.x - before - max_title_length if extra > 0: screen.cursor.x = before - draw_title(draw_data, screen, tab, index) + draw_title(draw_data, screen, tab, index, max(0, max_title_length - 8)) extra = screen.cursor.x - before - max_title_length if extra > 0: screen.cursor.x -= extra + 1 @@ -366,7 +372,7 @@ def draw_tab_with_powerline( if min_title_length >= max_title_length: screen.draw('…') else: - draw_title(draw_data, screen, tab, index) + draw_title(draw_data, screen, tab, index, max_title_length) extra = screen.cursor.x + start_draw - before - max_title_length if extra > 0 and extra + 1 < screen.cursor.x: screen.cursor.x -= extra + 1