Skip to content

Instantly share code, notes, and snippets.

@amb
Created August 19, 2025 13:11
Show Gist options
  • Select an option

  • Save amb/8086a33da5f0798f6cdda5d5863eb272 to your computer and use it in GitHub Desktop.

Select an option

Save amb/8086a33da5f0798f6cdda5d5863eb272 to your computer and use it in GitHub Desktop.
Claude Code statusline.py configuration
#!/usr/bin/env python3
import json
import sys
import os
import subprocess
from pathlib import Path
def get_git_info(current_dir):
"""Get git branch and status info."""
try:
os.chdir(current_dir)
# Get branch name
branch = subprocess.check_output(
['git', 'branch', '--show-current'],
stderr=subprocess.DEVNULL
).decode().strip()
if not branch:
branch = 'detached'
# Check for uncommitted changes
status_output = subprocess.check_output(
['git', 'status', '--porcelain'],
stderr=subprocess.DEVNULL
).decode().strip()
has_changes = len(status_output.splitlines()) > 0
if has_changes:
return f" \033[01;33m({branch}*)"
else:
return f" \033[01;32m({branch})"
except (subprocess.CalledProcessError, FileNotFoundError):
return ""
def get_last_user_message(transcript_path):
"""Extract the last user message from transcript file."""
try:
with open(transcript_path, 'r') as f:
# Read last 50 lines for efficiency
lines = f.readlines()[-50:]
# Search backwards for the last user message
for line in reversed(lines):
try:
entry = json.loads(line.strip())
if (entry.get('message', {}).get('role') == 'user' and
entry.get('message', {}).get('content')):
content = entry['message']['content']
# Handle both string and array content
if isinstance(content, str):
return content
elif isinstance(content, list) and content:
# Extract text from first text block
for item in content:
if isinstance(item, dict) and item.get('type') == 'text':
return item.get('text', '')
# Fallback to string representation
return str(content[0]) if content else ""
except (json.JSONDecodeError, KeyError):
continue
return ""
except (FileNotFoundError, IOError):
return ""
def main():
# Read JSON input from stdin
try:
input_data = json.loads(sys.stdin.read())
except json.JSONDecodeError:
input_data = {}
# Extract information
model = input_data.get('model', {}).get('display_name') or input_data.get('model', {}).get('id', 'Claude')
current_dir = input_data.get('workspace', {}).get('current_dir', os.getcwd())
transcript_path = input_data.get('transcript_path', '')
# Get git info
git_info = get_git_info(current_dir)
# Get last user message
# last_prompt = get_last_user_message(transcript_path)
# Process prompt - truncate if too long
# prompt_info = ""
# if last_prompt:
# if len(last_prompt) > 30:
# prompt_truncated = last_prompt[:30] + "..."
# else:
# prompt_truncated = last_prompt
# prompt_info = f' \033[01;36m"{prompt_truncated}"\033[00m'
# Get username and hostname
username = os.getenv('USER', 'user')
hostname = subprocess.check_output(['hostname', '-s']).decode().strip()
# Build status line
status_line = (
f"\033[01;35m[{model}]\033[00m "
f"\033[01;32m{username}@{hostname}\033[00m:"
f"\033[01;34m{os.path.basename(current_dir)}\033[00m"
f"{git_info}"
# f"{prompt_info}"
f"\033[00m"
)
print(status_line, end='')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment