Created
July 31, 2026 06:49
-
-
Save hassaku63/7738333b3cb2b57736203fe07ab8a72a to your computer and use it in GitHub Desktop.
Claude Code statusline customize example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| from typing import Any, Optional | |
| STATE_DIR = "/tmp/claude-statusline-state" | |
| BOLD = "\033[1m" | |
| YELLOW = "\033[33m" | |
| RED = "\033[31m" | |
| RESET = "\033[0m" | |
| BAR_WIDTH = 20 | |
| def build_bar(pct: float) -> str: | |
| filled = max(0, min(BAR_WIDTH, int(pct // 5))) | |
| bar = "█" * filled + "░" * (BAR_WIDTH - filled) | |
| if pct > 75: | |
| return f"{RED}{bar}{RESET}" | |
| if pct > 50: | |
| return f"{YELLOW}{bar}{RESET}" | |
| return bar | |
| def dig(d: dict, *keys: str, default: Any = None) -> Any: | |
| cur = d | |
| for k in keys: | |
| if not isinstance(cur, dict): | |
| return default | |
| cur = cur.get(k) | |
| if cur is None: | |
| return default | |
| return cur if cur is not None else default | |
| def num(v: Any, default: float = 0) -> float: | |
| try: | |
| return float(v) | |
| except (TypeError, ValueError): | |
| return default | |
| def state_path(session_id: str) -> str: | |
| return os.path.join(STATE_DIR, f"{session_id}.json") | |
| def read_state(path: str) -> dict: | |
| try: | |
| with open(path) as f: | |
| return json.load(f) | |
| except Exception: | |
| return {} | |
| def write_state_atomic(path: str, data: dict) -> None: | |
| os.makedirs(os.path.dirname(path), exist_ok=True) | |
| tmp = f"{path}.tmp.{os.getpid()}" | |
| with open(tmp, "w") as f: | |
| json.dump(data, f) | |
| os.replace(tmp, path) | |
| def git_branch(cwd: str) -> Optional[str]: | |
| if not cwd: | |
| return None | |
| try: | |
| inside = subprocess.run( | |
| ["git", "--no-optional-locks", "-C", cwd, "rev-parse", "--is-inside-work-tree"], | |
| capture_output=True, text=True, timeout=1, | |
| ) | |
| if inside.returncode != 0: | |
| return None | |
| out = subprocess.run( | |
| ["git", "--no-optional-locks", "-C", cwd, "branch", "--show-current"], | |
| capture_output=True, text=True, timeout=1, | |
| ) | |
| return out.stdout.strip() or None | |
| except Exception: | |
| return None | |
| def turn_delta(state: dict, pid: Optional[str], key: str, current: float) -> float: | |
| if pid != state.get(f"{key}_pid"): | |
| state[f"{key}_pid"] = pid | |
| state[f"{key}_base"] = state.get(f"{key}_last", current) | |
| delta = current - state.get(f"{key}_base", current) | |
| state[f"{key}_last"] = current | |
| return max(delta, 0) | |
| def shorten(cwd: str) -> str: | |
| home = os.path.expanduser("~") | |
| if cwd and cwd.startswith(home): | |
| return "~" + cwd[len(home):] | |
| return cwd or "-" | |
| def main() -> int: | |
| try: | |
| raw = sys.stdin.read() | |
| data = json.loads(raw) if raw.strip() else {} | |
| except Exception: | |
| data = {} | |
| session_id = data.get("session_id") or "unknown" | |
| prompt_id = data.get("prompt_id") | |
| cwd = dig(data, "workspace", "current_dir", default=data.get("cwd", "")) | |
| model = dig(data, "model", "display_name", default="?") | |
| effort = dig(data, "effort", "level", default="") | |
| cost_total = num(dig(data, "cost", "total_cost_usd"), 0.0) | |
| used_pct = num(dig(data, "context_window", "used_percentage"), 0) | |
| path = state_path(session_id) | |
| state = read_state(path) | |
| turn_cost = turn_delta(state, prompt_id, "cost", cost_total) | |
| write_state_atomic(path, state) | |
| branch = git_branch(cwd) | |
| line1 = f"{shorten(cwd)} | {branch or '-'} | {model}" | |
| if effort: | |
| line1 += f" (effort={effort})" | |
| line2 = f"cost: {BOLD}{YELLOW}+${turn_cost:.3f}{RESET} this turn, ${cost_total:.2f} total" | |
| line3 = f"ctx : {build_bar(used_pct)} {used_pct:.0f}% used" | |
| print(line1) | |
| print(line2) | |
| print(line3) | |
| return 0 | |
| if __name__ == "__main__": | |
| try: | |
| sys.exit(main()) | |
| except Exception: | |
| print("(statusline-minimal: error)") | |
| sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment