nixos/utils/home-manager/claude-code/statusline-command.sh

94 lines
2.5 KiB
Bash

#!/usr/bin/env bash
# ANSI color codes
GREEN='\033[32m'
YELLOW='\033[33m'
RED='\033[31m'
CYAN='\033[36m'
GRAY='\033[90m'
MAGENTA='\033[35m'
RESET='\033[0m'
input=$(cat)
usage=$(echo "$input" | jq '.context_window.current_usage')
model=$(echo "$input" | jq -r '.model.display_name // "Claude"')
# Get message count and session duration from input
message_count=$(echo "$input" | jq '.message_count // 0')
session_start=$(echo "$input" | jq -r '.session_start_time // empty')
# Get git info (if in a git repo)
branch=$(git branch --show-current 2>/dev/null)
project=$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null)
dirty=$(git status --porcelain 2>/dev/null | head -1)
# Build output
output=""
# Model name
output+="${CYAN}${model}${RESET}"
# Context progress bar
if [ "$usage" != "null" ]; then
current=$(echo "$input" | jq '.context_window.current_usage | .input_tokens + .cache_creation_input_tokens + .cache_read_input_tokens')
size=$(echo "$input" | jq '.context_window.context_window_size')
pct=$((current * 100 / size))
bar_width=15
filled=$((pct * bar_width / 100))
empty=$((bar_width - filled))
# Choose color based on usage level
if [ "$pct" -lt 50 ]; then
COLOR="$GREEN"
elif [ "$pct" -lt 80 ]; then
COLOR="$YELLOW"
else
COLOR="$RED"
fi
# Build colored progress bar
filled_bar=""
empty_bar=""
for ((i=0; i<filled; i++)); do filled_bar+="█"; done
for ((i=0; i<empty; i++)); do empty_bar+="░"; done
output+=" ${GRAY}[${RESET}${COLOR}${filled_bar}${GRAY}${empty_bar}${RESET}${GRAY}]${RESET} ${COLOR}${pct}%${RESET}"
else
output+=" ${GRAY}[waiting...]${RESET}"
fi
# Message count
if [ "$message_count" -gt 0 ] 2>/dev/null; then
output+=" ${GRAY}|${RESET} ${MAGENTA}${message_count} msgs${RESET}"
fi
# Session duration
if [ -n "$session_start" ]; then
now=$(date +%s)
start_epoch=$(date -d "$session_start" +%s 2>/dev/null || echo "")
if [ -n "$start_epoch" ]; then
elapsed=$((now - start_epoch))
hours=$((elapsed / 3600))
minutes=$(((elapsed % 3600) / 60))
if [ "$hours" -gt 0 ]; then
output+=" ${GRAY}${hours}h${minutes}m${RESET}"
else
output+=" ${GRAY}${minutes}m${RESET}"
fi
fi
fi
# Git project and branch
if [ -n "$project" ]; then
output+=" ${GRAY}|${RESET} ${CYAN}${project}${RESET}"
if [ -n "$branch" ]; then
output+="${GRAY}:${RESET}${GREEN}${branch}${RESET}"
fi
# Git dirty indicator
if [ -n "$dirty" ]; then
output+=" ${YELLOW}${RESET}"
fi
fi
printf "%b" "$output"