feat(tab-bar): add command status and abbreviated cwd indicators

The tab bar only showed the window title, so it was impossible to tell at a
glance whether the last command failed or which directory a tab was in. Expose
the active window's last exit status and a compact working directory to
tab_title_template, and refresh the tab bar when a command finishes.
This commit is contained in:
BlackAngelTV committed 2026-11-09 11:14:07 +01:00
1 parent 90e6355841
commit bcf3fdff7b
5 files changed
+157 -11

No files matched your search

+64 -4
View File
@@ -1,11 +1,27 @@
#!/usr/bin/env python
# License: GPL v3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
import os
from types import SimpleNamespace
from unittest.mock import patch
from kitty.fast_data_types import LEFT_EDGE, Color, Region, Screen
from kitty.options.utils import tab_title_wrap
from kitty.tab_bar import CellRange, DrawData, ExtraData, TabBar, TabBarData, TabExtent, WindowDropTarget, as_rgb, powerline_symbols, truncate_line, wrap_title
from kitty.tab_bar import (
CellRange,
DrawData,
ExtraData,
TabBar,
TabBarData,
TabExtent,
WindowDropTarget,
abbreviate_path,
as_rgb,
exit_status_indicator,
powerline_symbols,
truncate_line,
wrap_title,
)
from kitty.utils import color_as_int
from .base import BaseTest
@@ -392,6 +408,47 @@ class TestTabBar(BaseTest):
# empty line still gets ellipsis
self.ae(truncate_line('', 4), '…')
def test_abbreviate_path(self) -> None:
self.ae(abbreviate_path(''), '')
self.ae(abbreviate_path('/home/user/projects/fws-kitty', '/home/user'), '~/p/fws-kitty')
# the home directory itself has no component to abbreviate
self.ae(abbreviate_path('/home/user', '/home/user'), '~')
self.ae(abbreviate_path('/home/user/', '/home/user'), '~')
# a trailing slash does not add an empty component
self.ae(abbreviate_path('/usr/local/bin/'), '/u/l/bin')
self.ae(abbreviate_path('/'), '/')
self.ae(abbreviate_path('some/relative/dir'), 's/r/dir')
# the last component is never abbreviated, it is the most informative
self.ae(abbreviate_path('/home/user/a/very-long-project', '/home/user'), '~/a/very-long-project')
# when the result is too wide, middle directories are elided rather
# than cutting off the end of the title
self.ae(abbreviate_path('/home/user/one/two/three/project', '/home/user', 14), '~/…/project')
# when even that is too wide, the last component is truncated too
self.ae(abbreviate_path('/home/user/a/very-long-final-dir', '/home/user', 12), '~/…/very-lo…')
# widths are measured in terminal cells, so wide characters count double
self.ae(abbreviate_path('/日本語/東京'), '/日/東京')
def test_exit_status_indicator(self) -> None:
self.ae(exit_status_indicator(0), '')
self.ae(exit_status_indicator(1), '✘ 1 ')
self.ae(exit_status_indicator(130), '✘ 130 ')
def test_tab_title_indicators(self) -> None:
home = os.path.expanduser('~')
fake_tab = SimpleNamespace(
active_window=SimpleNamespace(last_cmd_exit_status=127),
get_cwd_of_active_window=lambda oldest=False: os.path.join(home, 'projects', 'fws-kitty'),
)
boss = SimpleNamespace(tab_for_id=lambda tab_id: fake_tab, mappings=SimpleNamespace(current_keyboard_mode_name=''))
line = self.horizontal_tab_bar_line(
['ignored'],
60,
boss=boss,
tab_bar_style='separator',
tab_title_template='{last_exit_code}:{exit_status_symbol}{tab.active_wd_abbreviated}',
)
self.assertIn('127:✘ 127 ~/p/fws-kitty', line)
def test_tab_title_wrap_option(self) -> None:
for off in ('no', 'No', 'n', 'false', 'none', '0'):
self.ae(tab_title_wrap(off), 0)
@@ -601,7 +658,7 @@ class TestTabBar(BaseTest):
[f' t0 {separator}', f'abcdefghijk{separator}', f'x {separator}'],
)
def horizontal_tab_bar_line(self, titles: list[str], columns: int, **opts: object) -> str:
def horizontal_tab_bar(self, titles: list[str], columns: int, boss: object | None = None, **opts: object) -> TabBar:
self.set_options({'tab_title_template': '{title}', **opts})
central = region(0, 0, columns * 10, 180)
tab_bar = region(0, 180, columns * 10, 200)
@@ -610,12 +667,15 @@ class TestTabBar(BaseTest):
patch('kitty.tab_bar.viewport_for_window', return_value=(central, tab_bar, columns * 10, 200, 10, 20)),
patch('kitty.tab_bar.set_tab_bar_render_data'),
patch('kitty.tab_bar.update_tab_bar_edge_colors', return_value=None),
patch('kitty.tab_bar.get_boss', return_value=DummyBoss()),
patch('kitty.tab_bar.get_boss', return_value=boss if boss is not None else DummyBoss()),
):
tb = TabBar(1)
tb.layout()
tb.update(tuple(TabBarData(title=t, tab_id=i + 1, is_active=i == 0) for i, t in enumerate(titles)))
return str(tb.screen.line(0)).rstrip()
return tb
def horizontal_tab_bar_line(self, titles: list[str], columns: int, boss: object | None = None, **opts: object) -> str:
return str(self.horizontal_tab_bar(titles, columns, boss, **opts).screen.line(0)).rstrip()
def test_horizontal_tab_bar_uses_free_space(self) -> None:
line = self.horizontal_tab_bar_line