"""Focus areas widget rendering."""
from ..models import FocusCategory
from ..themes import THEMES, FOCUS_COLORS
from ..utils import escape, card_wrapper
from .attribution import FOOTER_H, attribution_text
def render_focus_widget(categories: list[FocusCategory], theme_name: str = "dark", period: str = "1y", settings: dict | None = None) -> str:
"""Renders the focus areas widget with horizontal bars.
Settings:
max_categories (int): Max categories to show (default 6)
"""
t = THEMES[theme_name]
s = settings or {}
max_cats = min(max(int(s.get("max_categories", 6)), 1), 10)
cats = sorted(categories, key=lambda c: -c.percentage)[:max_cats]
if not cats:
return ""
# Format period for display
period_map = {
"1w": "last week",
"1m": "last month",
"3m": "last 3 months",
"6m": "last 6 months",
"1y": "last year",
"all": "all time"
}
period_display = period_map.get(period, period)
max_pct = cats[0].percentage if cats else 100
max_pct = max(max_pct, 1) # Avoid division by zero
bar_max_w = 210
items = ""
for i, cat in enumerate(cats):
color = FOCUS_COLORS[i % len(FOCUS_COLORS)]
y = i * 36
bar_w = (cat.percentage / max_pct) * bar_max_w
items += f'''
{escape(cat.category)}
{cat.commit_count} commits
{cat.percentage:.0f}%
'''
# Add period subtitle
subtitle_y = len(cats) * 36 + 8
items += f'''
Recent activity ยท {period_display}'''
# The period subtitle leaves only 8px below it, so reserve FOOTER_H for
# the attribution footer.
card_h = len(cats) * 36 + 54 + FOOTER_H
items += attribution_text(t, 190, card_h - 36 - 8)
return card_wrapper(items, 380, card_h, t, "Recent Focus")