|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import json |
| 3 | +import re |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import time |
| 8 | + |
| 9 | +GREEN = "\033[32m" |
| 10 | +YELLOW = "\033[33m" |
| 11 | +RED = "\033[31m" |
| 12 | +RESET = "\033[0m" |
| 13 | + |
| 14 | +ANSI = re.compile(r"\033\[[0-9;]*m") |
| 15 | + |
| 16 | + |
| 17 | +def visible_len(s): |
| 18 | + return len(ANSI.sub("", s)) |
| 19 | + |
| 20 | + |
| 21 | +def bar(pct, width=8): |
| 22 | + pct = max(0, min(100, int(pct or 0))) |
| 23 | + filled = round(pct * width / 100) |
| 24 | + color = RED if pct >= 90 else YELLOW if pct >= 70 else GREEN |
| 25 | + return f"{color}{'█' * filled}{'░' * (width - filled)}{RESET} {pct}%" |
| 26 | + |
| 27 | + |
| 28 | +data = json.load(sys.stdin) |
| 29 | + |
| 30 | +model = data.get("model", {}).get("display_name", "?") |
| 31 | +project_dir = data.get("workspace", {}).get("project_dir") or data.get( |
| 32 | + "cwd", "?" |
| 33 | +) |
| 34 | + |
| 35 | +ctx_pct = int(data.get("context_window", {}).get("used_percentage") or 0) |
| 36 | + |
| 37 | +rl = data.get("rate_limits", {}) |
| 38 | +fh = rl.get("five_hour", {}) |
| 39 | +fh_pct = fh.get("used_percentage") |
| 40 | +fh_resets_at = fh.get("resets_at") |
| 41 | +sd_pct = rl.get("seven_day", {}).get("used_percentage") |
| 42 | + |
| 43 | + |
| 44 | +def time_until(ts): |
| 45 | + secs = max(0, int(ts - time.time())) |
| 46 | + h, m = divmod(secs // 60, 60) |
| 47 | + return f"{h}h{m:02d}m" if h else f"{m}m" |
| 48 | + |
| 49 | + |
| 50 | +try: |
| 51 | + branch = ( |
| 52 | + subprocess.check_output( |
| 53 | + ["git", "branch", "--show-current"], |
| 54 | + cwd=project_dir, |
| 55 | + stderr=subprocess.DEVNULL, |
| 56 | + ) |
| 57 | + .decode() |
| 58 | + .strip() |
| 59 | + or None |
| 60 | + ) |
| 61 | +except Exception: |
| 62 | + branch = None |
| 63 | + |
| 64 | +branch_str = f" ({branch})" if branch else "" |
| 65 | +left = f"{project_dir}{branch_str}" |
| 66 | + |
| 67 | +parts = [f"ctx {bar(ctx_pct)}"] |
| 68 | +if fh_pct is not None: |
| 69 | + reset_str = f" | {time_until(fh_resets_at)}" if fh_resets_at else "" |
| 70 | + parts.append(f"5h{reset_str} {bar(fh_pct)}") |
| 71 | +if sd_pct is not None: |
| 72 | + parts.append(f"7d {bar(sd_pct)}") |
| 73 | +right = f"{model} " |
| 74 | +right += " ".join(parts) |
| 75 | + |
| 76 | +right_extra_pad = 18 |
| 77 | +term_width = shutil.get_terminal_size().columns |
| 78 | +padding = max( |
| 79 | + 1, term_width - visible_len(left) - visible_len(right) - right_extra_pad |
| 80 | +) |
| 81 | +print(left + " " * padding + right) |
0 commit comments