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:
5 files changed
+157
-11
No files matched your search
+73
-2
@@ -260,6 +260,17 @@ class TabAccessor:
|
||||
tab = get_boss().tab_for_id(self.tab_id)
|
||||
return os.path.basename((tab.get_exe_of_active_window(oldest=True) if tab else '') or '')
|
||||
|
||||
@property
|
||||
def active_wd_abbreviated(self) -> str:
|
||||
tab = get_boss().tab_for_id(self.tab_id)
|
||||
return abbreviate_path((tab.get_cwd_of_active_window() if tab else '') or '')
|
||||
|
||||
@property
|
||||
def last_cmd_exit_status(self) -> int:
|
||||
tab = get_boss().tab_for_id(self.tab_id)
|
||||
w = tab.active_window if tab else None
|
||||
return w.last_cmd_exit_status if w else 0
|
||||
|
||||
@property
|
||||
def last_focused_progress_percent(self) -> str:
|
||||
tab = get_boss().tab_for_id(self.tab_id)
|
||||
@@ -384,6 +395,7 @@ def evaluate_title_template(draw_data: DrawData, tab: TabBarData, index: int, ma
|
||||
ColorFormatter.draw_data = draw_data
|
||||
ColorFormatter.tab_data = tab
|
||||
boss = get_boss()
|
||||
last_cmd_exit_status = ta.last_cmd_exit_status
|
||||
eval_locals = {
|
||||
'index': index,
|
||||
'layout_name': tab.layout_name,
|
||||
@@ -400,6 +412,8 @@ def evaluate_title_template(draw_data: DrawData, tab: TabBarData, index: int, ma
|
||||
'bell_symbol': draw_data.bell_on_tab if tab.needs_attention else '',
|
||||
'secure_input_symbol': si,
|
||||
'activity_symbol': draw_data.tab_activity_symbol if tab.has_activity_since_last_focus else '',
|
||||
'last_exit_code': str(last_cmd_exit_status) if last_cmd_exit_status else '',
|
||||
'exit_status_symbol': exit_status_indicator(last_cmd_exit_status),
|
||||
'max_title_length': max_title_length,
|
||||
'keyboard_mode': boss.mappings.current_keyboard_mode_name,
|
||||
}
|
||||
@@ -407,7 +421,7 @@ def evaluate_title_template(draw_data: DrawData, tab: TabBarData, index: int, ma
|
||||
if tab.is_active and draw_data.active_title_template is not None:
|
||||
template = draw_data.active_title_template
|
||||
prefix = ''
|
||||
for default_prefix in ('bell_symbol', 'activity_symbol', 'secure_input_symbol'):
|
||||
for default_prefix in ('bell_symbol', 'activity_symbol', 'exit_status_symbol', 'secure_input_symbol'):
|
||||
if eval_locals[default_prefix] and not template_has_field(template, default_prefix):
|
||||
prefix += '{' + default_prefix + '}'
|
||||
if prefix:
|
||||
@@ -498,8 +512,65 @@ def truncate_line(line: str, width: int) -> str:
|
||||
return out + '…'
|
||||
|
||||
|
||||
def exit_status_indicator(exit_status: int) -> str:
|
||||
"""A short indicator for the exit status of the last command, empty on success."""
|
||||
return f'✘ {exit_status} ' if exit_status else ''
|
||||
|
||||
|
||||
def abbreviate_path(path: str, home: str | None = None, max_length: int = 0) -> str:
|
||||
"""Return a compact representation of path for use in a tab title.
|
||||
|
||||
The home directory is shown as ``~`` and the directories between it and
|
||||
the last component are reduced to their first character, so
|
||||
``~/projects/fws-kitty`` becomes ``~/p/fws-kitty``. When max_length is
|
||||
positive and the result is wider, the middle directories are replaced by
|
||||
an ellipsis, keeping the last component, which is the most informative
|
||||
part. Widths are measured in terminal cells.
|
||||
"""
|
||||
if not path:
|
||||
return ''
|
||||
home = os.path.normpath(home or os.path.expanduser('~'))
|
||||
p = os.path.normpath(path)
|
||||
if home and (p == home or p.startswith(home + os.sep)):
|
||||
prefix, rest = '~', p[len(home) :]
|
||||
elif p.startswith(os.sep):
|
||||
prefix, rest = os.sep, p
|
||||
else:
|
||||
prefix, rest = '', p
|
||||
components = [c for c in rest.split(os.sep) if c]
|
||||
if not components:
|
||||
return prefix or os.sep
|
||||
|
||||
def join(parts: Sequence[str]) -> str:
|
||||
if prefix == os.sep:
|
||||
return os.sep + os.sep.join(parts)
|
||||
if prefix == '~':
|
||||
return '~' + (os.sep + os.sep.join(parts) if parts else '')
|
||||
return os.sep.join(parts)
|
||||
|
||||
ans = join([c[0] for c in components[:-1]] + [components[-1]])
|
||||
if max_length > 0 and wcswidth(ans) > max_length:
|
||||
last = components[-1]
|
||||
marker = prefix + os.sep + '…' if prefix else '…'
|
||||
for candidate in (marker + os.sep + last, '…' + os.sep + last if prefix else last):
|
||||
if wcswidth(candidate) <= max_length:
|
||||
return candidate
|
||||
# Even the last component alone is too wide, keep its end as that is
|
||||
# the part that distinguishes it from its siblings.
|
||||
available = max_length - wcswidth(marker) - 1
|
||||
if available > 0:
|
||||
return marker + os.sep + truncate_line(last, available)
|
||||
return truncate_line(ans, max_length)
|
||||
return ans
|
||||
|
||||
|
||||
def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int, max_title_length: int = 0) -> None:
|
||||
title = apply_title_template(draw_data, tab, index, max_title_length)
|
||||
# Close buttons live in horizontal tab bars, where there is a spare cell at
|
||||
# the start of each tab. Vertical tabs are already crowded and their titles
|
||||
# wrap, so they keep closing with the middle mouse button.
|
||||
show_close = tab.tab_id > 0 and draw_data.tab_bar_edge in ('top', 'bottom') and (max_title_length <= 0 or max_title_length >= 3)
|
||||
title_width = max(1, max_title_length - 2) if show_close and max_title_length > 0 else max_title_length
|
||||
title = apply_title_template(draw_data, tab, index, title_width)
|
||||
if draw_data.max_tab_title_lines > 1 and draw_data.wrap_width:
|
||||
title = wrap_title(title, draw_data.wrap_width)
|
||||
if '\n' in title:
|
||||
|
||||
Reference in new issue
Block a user