Skip to content

Instantly share code, notes, and snippets.

@p32929
Created December 16, 2025 18:39
Show Gist options
  • Select an option

  • Save p32929/151207c009e0d767813af1c5598a5cb3 to your computer and use it in GitHub Desktop.

Select an option

Save p32929/151207c009e0d767813af1c5598a5cb3 to your computer and use it in GitHub Desktop.
my claude code status line
#!/bin/bash
# Read JSON input from stdin
input=$(cat)
# Extract data from JSON
current_dir=$(echo "$input" | jq -r '.workspace.current_dir')
project_dir=$(echo "$input" | jq -r '.workspace.project_dir')
model_name=$(echo "$input" | jq -r '.model.display_name')
# Token usage from context_window
tokens_input=$(echo "$input" | jq -r '.context_window.total_input_tokens // 0')
tokens_output=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0')
tokens_total=$(echo "$input" | jq -r '.context_window.context_window_size // 0')
tokens_used=$((tokens_input + tokens_output))
tokens_remaining=$((tokens_total - tokens_used))
# Session info for second line
version=$(echo "$input" | jq -r '.version // "unknown"')
duration_ms=$(echo "$input" | jq -r '.cost.total_duration_ms // 0')
# Convert milliseconds to human readable format
duration_seconds=$((duration_ms / 1000))
duration_minutes=$((duration_seconds / 60))
duration_hours=$((duration_minutes / 60))
remaining_minutes=$((duration_minutes % 60))
remaining_seconds=$((duration_seconds % 60))
if [ $duration_hours -gt 0 ]; then
duration_str="${duration_hours}h ${remaining_minutes}m"
elif [ $duration_minutes -gt 0 ]; then
duration_str="${duration_minutes}m ${remaining_seconds}s"
else
duration_str="${duration_seconds}s"
fi
# Count background tasks (check for tasks in JSON or running background processes)
bg_tasks=$(echo "$input" | jq -r '.background_tasks // [] | length')
if [ "$bg_tasks" = "0" ] || [ "$bg_tasks" = "null" ]; then
# Fallback: count running claude-related background processes
bg_tasks=$(ps aux | grep -c "claude.*background" 2>/dev/null || echo "0")
# Subtract 1 for the grep process itself if greater than 0
if [ "$bg_tasks" -gt 0 ]; then
bg_tasks=$((bg_tasks - 1))
fi
fi
# Get project name from project directory
if [ "$project_dir" != "null" ] && [ -n "$project_dir" ]; then
project_name=$(basename "$project_dir")
else
project_name=$(basename "$current_dir")
fi
# Get relative path from project dir to current dir
if [ "$project_dir" != "null" ] && [ -n "$project_dir" ]; then
rel_dir="${current_dir#$project_dir}"
if [ -z "$rel_dir" ]; then
rel_dir="/"
fi
else
rel_dir=$(basename "$current_dir")
fi
# Git branch
git_branch=""
if [ -d "$current_dir/.git" ] || git -C "$current_dir" rev-parse --git-dir > /dev/null 2>&1; then
git_branch=$(git -C "$current_dir" --no-optional-locks branch --show-current 2>/dev/null)
if [ -z "$git_branch" ]; then
git_branch=$(git -C "$current_dir" --no-optional-locks rev-parse --short HEAD 2>/dev/null)
fi
fi
# Git status (clean or dirty)
git_status=""
if [ -n "$git_branch" ]; then
if git -C "$current_dir" --no-optional-locks diff-index --quiet HEAD -- 2>/dev/null; then
git_status="✓"
else
git_status="*"
fi
fi
# Build status line
status_parts=()
# Current Directory
status_parts+=("$rel_dir")
# Git Branch and Status
if [ -n "$git_branch" ]; then
status_parts+=("[$git_branch]")
status_parts+=("$git_status")
fi
# Model
status_parts+=("$model_name")
# Project Name
status_parts+=("[$project_name]")
# Context/Token usage
if [ "$tokens_total" -gt 0 ]; then
# Format numbers with commas for readability
tokens_used_fmt=$(printf "%'d" "$tokens_used" 2>/dev/null || echo "$tokens_used")
tokens_remaining_fmt=$(printf "%'d" "$tokens_remaining" 2>/dev/null || echo "$tokens_remaining")
tokens_total_fmt=$(printf "%'d" "$tokens_total" 2>/dev/null || echo "$tokens_total")
# Calculate percentage used
tokens_pct=$((tokens_used * 100 / tokens_total))
status_parts+=("${tokens_used_fmt}/${tokens_total_fmt} (${tokens_pct}% | ${tokens_remaining_fmt} left)")
fi
# Join parts with " | "
status_line=""
for i in "${!status_parts[@]}"; do
if [ $i -eq 0 ]; then
status_line="${status_parts[$i]}"
else
status_line="$status_line | ${status_parts[$i]}"
fi
done
# Build second line with session info
second_line_parts=()
second_line_parts+=("Session: ${duration_str}")
second_line_parts+=("Version: ${version}")
second_line_parts+=("Background Tasks: ${bg_tasks}")
second_line=""
for i in "${!second_line_parts[@]}"; do
if [ $i -eq 0 ]; then
second_line="${second_line_parts[$i]}"
else
second_line="$second_line | ${second_line_parts[$i]}"
fi
done
# Output both lines
printf "%s\n%s" "$status_line" "$second_line"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment