Created
January 31, 2026 00:15
-
-
Save mrf/76b21a1a039b98224c3dd5e86dd2669d to your computer and use it in GitHub Desktop.
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
| #!/bin/bash | |
| # Claude Code Statusline - Shows cwd, model, context usage, cost, git branch | |
| # Receives JSON via stdin from Claude Code | |
| input=$(cat) | |
| # Extract data using jq | |
| MODEL=$(echo "$input" | jq -r '.model.display_name // "?"') | |
| CURRENT_DIR=$(echo "$input" | jq -r '.workspace.current_dir // ""') | |
| PROJECT_DIR=$(echo "$input" | jq -r '.workspace.project_dir // ""') | |
| COST=$(echo "$input" | jq -r '.cost.total_cost_usd // 0') | |
| # Context window data | |
| CONTEXT_MAX=$(echo "$input" | jq -r '.context_window.context_window_size // 0') | |
| INPUT_TOKENS=$(echo "$input" | jq -r '.context_window.current_usage.input_tokens // 0') | |
| OUTPUT_TOKENS=$(echo "$input" | jq -r '.context_window.current_usage.output_tokens // 0') | |
| CACHE_READ=$(echo "$input" | jq -r '.context_window.current_usage.cache_read_input_tokens // 0') | |
| CACHE_CREATION=$(echo "$input" | jq -r '.context_window.current_usage.cache_creation_input_tokens // 0') | |
| USED_PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0') | |
| # Calculate total content tokens (inspired by your snippet) | |
| CONTENT_TOKENS=$((CACHE_READ + INPUT_TOKENS + CACHE_CREATION + OUTPUT_TOKENS)) | |
| CONTENT_K=$((CONTENT_TOKENS / 1000)) | |
| MAX_K=$((CONTEXT_MAX / 1000)) | |
| # Format cost (strip trailing zeros) | |
| if command -v bc &>/dev/null && [ "$COST" != "0" ] && [ "$COST" != "null" ]; then | |
| COST_FMT=$(printf "%.2f" "$COST" 2>/dev/null || echo "$COST") | |
| else | |
| COST_FMT="0.00" | |
| fi | |
| # Shorten directory - show relative to project or just basename | |
| if [ -n "$PROJECT_DIR" ] && [ "$CURRENT_DIR" != "$PROJECT_DIR" ]; then | |
| # Show relative path from project | |
| DIR_DISPLAY="${CURRENT_DIR#"$PROJECT_DIR"/}" | |
| else | |
| # Just show basename | |
| DIR_DISPLAY="${CURRENT_DIR##*/}" | |
| fi | |
| # Git branch (cached to avoid slowdown) | |
| GIT_BRANCH="" | |
| if [ -n "$CURRENT_DIR" ] && [ -d "$CURRENT_DIR/.git" ] || git -C "$CURRENT_DIR" rev-parse --git-dir &>/dev/null 2>&1; then | |
| BRANCH=$(git -C "$CURRENT_DIR" branch --show-current 2>/dev/null) | |
| [ -n "$BRANCH" ] && GIT_BRANCH=" $BRANCH" | |
| fi | |
| # Color codes for visual indicators | |
| RESET="\033[0m" | |
| BOLD="\033[1m" | |
| DIM="\033[2m" | |
| GREEN="\033[32m" | |
| YELLOW="\033[33m" | |
| RED="\033[31m" | |
| CYAN="\033[36m" | |
| MAGENTA="\033[35m" | |
| # Context color based on usage | |
| if [ "$USED_PCT" -lt 50 ]; then | |
| CTX_COLOR="$GREEN" | |
| elif [ "$USED_PCT" -lt 75 ]; then | |
| CTX_COLOR="$YELLOW" | |
| else | |
| CTX_COLOR="$RED" | |
| fi | |
| # Build the statusline | |
| # Format: [Model] dir (branch) | ctx: 45% 123k/200k | $0.42 | |
| printf "%b%b%s%b " "$BOLD" "$CYAN" "$MODEL" "$RESET" | |
| printf "%b%s%b" "$MAGENTA" "$DIR_DISPLAY" "$RESET" | |
| [ -n "$GIT_BRANCH" ] && printf "%b%b%s%b" "$DIM" "$GREEN" "$GIT_BRANCH" "$RESET" | |
| printf " %b|%b " "$DIM" "$RESET" | |
| printf "%b%d%%%b %b%dk/%dk%b" "$CTX_COLOR" "$USED_PCT" "$RESET" "$DIM" "$CONTENT_K" "$MAX_K" "$RESET" | |
| printf " %b|%b " "$DIM" "$RESET" | |
| printf "%b\$%b%s" "$DIM" "$RESET" "$COST_FMT" | |
| echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment