|
#!/bin/bash |
|
|
|
# Enhanced status line for Claude Code with ccusage integration |
|
# This script generates a colorful and informative status line with usage metrics |
|
|
|
# Read Claude Code context from stdin |
|
input=$(cat) |
|
|
|
# Color codes - Enhanced palette |
|
RED='\033[31m' |
|
GREEN='\033[32m' |
|
YELLOW='\033[33m' |
|
BLUE='\033[34m' |
|
MAGENTA='\033[35m' |
|
CYAN='\033[36m' |
|
WHITE='\033[37m' |
|
BRIGHT_RED='\033[91m' |
|
BRIGHT_GREEN='\033[92m' |
|
BRIGHT_YELLOW='\033[93m' |
|
BRIGHT_BLUE='\033[94m' |
|
BRIGHT_MAGENTA='\033[95m' |
|
BRIGHT_CYAN='\033[96m' |
|
BRIGHT_WHITE='\033[97m' |
|
DIM='\033[2m' |
|
BOLD='\033[1m' |
|
RESET='\033[0m' |
|
BG_BLUE='\033[44m' |
|
BG_GREEN='\033[42m' |
|
BG_YELLOW='\033[43m' |
|
BG_RED='\033[41m' |
|
|
|
# Get model name with nice formatting |
|
model=$(echo "$input" | jq -r '.model.display_name // .model.id // "unknown"') |
|
model_formatted="${BRIGHT_CYAN}${BOLD}${model}${RESET}" |
|
|
|
# Get ccusage data and format it with enhanced colors |
|
if command -v ccusage &> /dev/null; then |
|
ccusage_data=$(ccusage blocks --json 2>/dev/null | jq -r '.blocks[] | select(.isActive == true)' 2>/dev/null) |
|
|
|
if [[ -n "$ccusage_data" ]]; then |
|
# Extract values |
|
total_tokens=$(echo "$ccusage_data" | jq -r '.totalTokens // 0') |
|
cost_usd=$(echo "$ccusage_data" | jq -r '.costUSD // 0') |
|
remaining_minutes=$(echo "$ccusage_data" | jq -r '.projection.remainingMinutes // 0') |
|
burn_rate=$(echo "$ccusage_data" | jq -r '.burnRate.costPerHour // 0') |
|
|
|
# Calculate percentage (17,213,778 is the daily token limit) |
|
daily_limit=17213778 |
|
percentage=$(echo "$total_tokens $daily_limit" | awk '{printf "%.0f", ($1 / $2) * 100}') |
|
|
|
# Format cost with proper rounding |
|
cost_formatted=$(echo "$cost_usd" | awk '{printf "%.2f", $1}') |
|
burn_rate_formatted=$(echo "$burn_rate" | awk '{printf "%.2f", $1}') |
|
|
|
# Calculate hours and minutes from remaining minutes |
|
hours=$((remaining_minutes / 60)) |
|
minutes=$((remaining_minutes % 60)) |
|
|
|
# Color code based on usage percentage |
|
if [[ $percentage -lt 50 ]]; then |
|
usage_color="${BRIGHT_GREEN}" |
|
bar_char="▰" |
|
elif [[ $percentage -lt 75 ]]; then |
|
usage_color="${BRIGHT_YELLOW}" |
|
bar_char="▰" |
|
else |
|
usage_color="${BRIGHT_RED}" |
|
bar_char="▰" |
|
fi |
|
|
|
# Create mini progress bar (10 chars width) |
|
bar_filled=$((percentage / 10)) |
|
bar_empty=$((10 - bar_filled)) |
|
progress_bar="${usage_color}" |
|
for ((i=0; i<bar_filled; i++)); do progress_bar+="$bar_char"; done |
|
progress_bar+="${DIM}" |
|
for ((i=0; i<bar_empty; i++)); do progress_bar+="▱"; done |
|
progress_bar+="${RESET}" |
|
|
|
# Format the usage segment with enhanced colors |
|
usage_segment="${BRIGHT_BLUE}📊${RESET} ${progress_bar} ${usage_color}${BOLD}${percentage}%${RESET}" |
|
cost_segment="${BRIGHT_GREEN}💵${RESET} ${BRIGHT_WHITE}\$${cost_formatted}${RESET}" |
|
time_segment="${BRIGHT_MAGENTA}⏱${RESET} ${BRIGHT_YELLOW}${hours}h ${minutes}m${RESET}" |
|
burn_segment="${BRIGHT_RED}🔥${RESET} ${YELLOW}\$${burn_rate_formatted}/hr${RESET}" |
|
|
|
ccusage_formatted="${usage_segment} ${DIM}|${RESET} ${cost_segment} ${DIM}|${RESET} ${time_segment} ${DIM}|${RESET} ${burn_segment}" |
|
else |
|
# Fallback if no active block |
|
ccusage_formatted="${DIM}No active usage data${RESET}" |
|
fi |
|
else |
|
# Fallback if ccusage is not available |
|
ccusage_formatted="${DIM}ccusage not available${RESET}" |
|
fi |
|
|
|
# Output the formatted status line |
|
printf "%b ${DIM}|${RESET} %b\n" "$model_formatted" "$ccusage_formatted" |