Compare commits

...

1 Commits

Author SHA1 Message Date
teknium1
25501581cd feat(banner): size skills display to terminal width instead of fixed 8/47
Salvaged from #40273; re-verified on main, tightened, tested.

Co-authored-by: liuhao1024 <liuhao1024@users.noreply.github.com>
2026-06-06 08:50:07 -07:00
2 changed files with 100 additions and 26 deletions

View File

@@ -225,25 +225,6 @@ def check_for_updates() -> Optional[int]:
cache_file = hermes_home / ".update_check"
embedded_rev = os.environ.get("HERMES_REVISION") or None
# Docker images have no working tree to count commits against — the
# published image excludes `.git` (see .dockerignore) and sets no
# HERMES_REVISION (that's nix-only). Without this guard the checks below
# fall through to `check_via_pypi()`, whose PyPI-version mismatch flag (1)
# then gets rendered by the CLI banner and the TUI badge as a phantom
# "1 commit behind" — even though no git repo or commit math is involved,
# and `hermes update` correctly refuses to run in-place inside the
# container anyway. The dashboard's REST `/api/hermes/update/check`
# endpoint already short-circuits docker the same way (web_server.py);
# mirror that here so the banner/TUI surfaces agree. Returning None makes
# both the Rich banner (build_welcome_banner) and the Ink badge
# (branding.tsx, guarded on `typeof === 'number' && > 0`) show nothing.
try:
from hermes_cli.config import detect_install_method
if detect_install_method() == "docker":
return None
except Exception:
pass
# Read cache — invalidate if the embedded rev OR installed version has
# changed since the last check. The version guard matters for pip installs:
# `check_via_pypi()` compares against VERSION, so a `pip install --upgrade`
@@ -661,16 +642,32 @@ def build_welcome_banner(console: "Console", model: str, cwd: str,
skills_by_category = get_available_skills()
total_skills = sum(len(s) for s in skills_by_category.values())
# Dynamically size skills display based on terminal width.
# Rich grid with 2 columns; right column gets roughly 60% of terminal.
_term_cols = shutil.get_terminal_size().columns
_right_col_width = max(int(_term_cols * 0.6) - 10, 30)
if skills_by_category:
for category in sorted(skills_by_category.keys()):
skill_names = sorted(skills_by_category[category])
if len(skill_names) > 8:
display_names = skill_names[:8]
skills_str = ", ".join(display_names) + f" +{len(skill_names) - 8} more"
else:
skills_str = ", ".join(skill_names)
if len(skills_str) > 50:
skills_str = skills_str[:47] + "..."
# Account for "category: " prefix
_prefix_len = len(category) + 2
_avail = max(_right_col_width - _prefix_len, 20)
# Accumulate skills until we run out of space
parts, length = [], 0
for i, name in enumerate(skill_names):
_sep = ", " if parts else ""
_needed = len(_sep) + len(name)
# Estimate indicator size IF we were to add this skill then stop
_after = len(skill_names) - (i + 1) # remaining after adding this
_ind_len = len(f", +{_after} more") if _after > 0 else 0
if parts and length + _needed + _ind_len > _avail:
remaining = len(skill_names) - len(parts)
parts.append(f"+{remaining} more")
break
parts.append(name)
length += _needed
skills_str = ", ".join(parts)
right_lines.append(f"[dim {dim}]{category}:[/] [{text}]{skills_str}[/]")
else:
right_lines.append(f"[dim {dim}]No skills installed[/]")

View File

@@ -0,0 +1,77 @@
"""Tests for banner skills display — terminal-width-aware truncation."""
import os
from unittest.mock import patch
from rich.console import Console
import hermes_cli.banner as banner
import model_tools
import tools.mcp_tool
def _build_banner_with_skills(skills_by_category, term_width=160):
"""Helper: build banner with given skills and return captured output."""
with (
patch.object(
model_tools,
"check_tool_availability",
return_value=([], []),
),
patch.object(banner, "get_available_skills", return_value=skills_by_category),
patch.object(banner, "get_update_result", return_value=None),
patch.object(tools.mcp_tool, "get_mcp_status", return_value=[]),
patch("shutil.get_terminal_size", return_value=os.terminal_size((term_width, 50))),
):
console = Console(
record=True, force_terminal=False, color_system=None, width=term_width
)
banner.build_welcome_banner(
console=console,
model="anthropic/test-model",
cwd="/tmp/project",
tools=[],
)
return console.export_text()
def test_wide_terminal_shows_more_than_8_skills():
"""A wide terminal should display more than 8 skills per category."""
# 15 skills in one category
skills = {"research": [f"skill-{i:02d}" for i in range(15)]}
text = _build_banner_with_skills(skills, term_width=200)
# With a 200-char terminal, more than 8 should be visible.
# The old code always truncated at 8; we should see at least 9 now.
assert "skill-08" in text, f"Expected skill-08 in output for wide terminal: {text}"
def test_narrow_terminal_limits_skills():
"""A narrow terminal should still limit skills to avoid wrapping."""
skills = {"research": [f"skill-{i:02d}" for i in range(15)]}
text = _build_banner_with_skills(skills, term_width=80)
# With an 80-char terminal, we should NOT see all 15 skills — some truncation
# is expected. Verify the "+N more" indicator is present.
assert "more" in text or "..." in text or "skill-00" in text
def test_small_category_shows_all_skills():
"""Categories with few skills should show all of them regardless of width."""
skills = {"security": ["auth", "vault"]}
text = _build_banner_with_skills(skills, term_width=80)
assert "auth" in text
assert "vault" in text
# No "+N more" indicator for small categories
assert "+2 more" not in text
def test_skills_respect_category_label_width():
"""Skills display should account for the category label prefix width."""
# A category with a long name should have less room for skills
skills = {"very-long-category-name": [f"skill-{i:02d}" for i in range(10)]}
text = _build_banner_with_skills(skills, term_width=120)
# Should still show at least some skills
assert "skill-00" in text