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>
This commit is contained in:
teknium1 2026-06-06 08:50:07 -07:00 committed by Teknium
parent 5431bf2921
commit 51e6ef5fca
2 changed files with 100 additions and 7 deletions

View file

@ -804,18 +804,34 @@ def build_welcome_banner(console: "Console", model: str, cwd: str,
skills_by_category = {}
total_skills = 0
# 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 not _skills_enabled:
right_lines.append(f"[dim {dim}]Skills toolset disabled[/]")
elif 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