update cc statusline

This commit is contained in:
David Chen 2025-09-09 17:48:34 -07:00
parent 02eea13aa6
commit 0487e8b3ba
4 changed files with 171 additions and 14 deletions

View file

@ -0,0 +1,72 @@
#!/usr/bin/env python3
import json
import sys
# Constant
CONTEXT_LIMIT = int(168000) # CC triggers /compact at ~78% context utilization
# Read JSON from stdin
data = json.load(sys.stdin)
transcript_path = data["transcript_path"]
# Parse transcript file to calculate context usage
context_used_token = 0
try:
with open(transcript_path, "r") as f:
lines = f.readlines()
# Iterate from last line to first line
for line in reversed(lines):
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
# Check if this line contains the required token usage fields
if (
obj.get("type") == "assistant"
and "message" in obj
and "usage" in obj["message"]
and all(
key in obj["message"]["usage"]
for key in [
"input_tokens",
"cache_creation_input_tokens",
"cache_read_input_tokens",
"output_tokens",
]
)
):
usage = obj["message"]["usage"]
input_tokens = usage["input_tokens"]
cache_creation_input_tokens = usage["cache_creation_input_tokens"]
cache_read_input_tokens = usage["cache_read_input_tokens"]
output_tokens = usage["output_tokens"]
context_used_token = (
input_tokens
+ cache_creation_input_tokens
+ cache_read_input_tokens
+ output_tokens
)
break # Break after finding the first occurrence
except json.JSONDecodeError:
# Skip malformed JSON lines
continue
except FileNotFoundError:
# If transcript file doesn't exist, keep context_used_token as 0
pass
context_used_rate = (context_used_token / CONTEXT_LIMIT) * 100
# Create progress bar
bar_length = 20
filled_length = int(bar_length * context_used_token // CONTEXT_LIMIT)
bar = "" * filled_length + "" * (bar_length - filled_length)
print(f"[{bar}] {context_used_rate:.1f}% ({context_used_token:,})")

50
claude/scripts/usage-line.sh Executable file
View file

@ -0,0 +1,50 @@
#!/bin/bash
# Get current session cost (assuming current working directory as session)
session_data=$(ccusage session --json | jq -r '.sessions[] | select(.sessionId == "'"$(pwd | sed 's|/|-|g')"'") | .totalCost')
session_cost=${session_data:-0}
# Get today's cost
today_cost=$(ccusage daily --json | jq -r '.daily[0].totalCost // 0')
# Get current active block info
block_info=$(ccusage blocks --json | jq -r '
.blocks[] |
select(.isActive == true) |
"\(.costUSD // 0)|\(.startTime)|\(.endTime)"
')
if [[ -n "$block_info" ]]; then
block_cost=$(echo "$block_info" | cut -d'|' -f1)
start_time=$(echo "$block_info" | cut -d'|' -f2)
end_time=$(echo "$block_info" | cut -d'|' -f3)
# Calculate time left in block (5 hours from start)
start_epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S.%fZ" "$start_time" +%s 2>/dev/null || date -d "$start_time" +%s 2>/dev/null || echo "0")
current_epoch=$(date +%s)
elapsed_seconds=$((current_epoch - start_epoch))
remaining_seconds=$((18000 - elapsed_seconds)) # 5 hours = 18000 seconds
if [[ $remaining_seconds -gt 0 ]]; then
hours=$((remaining_seconds / 3600))
minutes=$(((remaining_seconds % 3600) / 60))
time_left="${hours}h ${minutes}m left"
else
time_left="expired"
fi
# Calculate hourly rate
if [[ $elapsed_seconds -gt 0 ]]; then
hourly_rate=$(echo "scale=2; $block_cost * 3600 / $elapsed_seconds" | bc -l 2>/dev/null || echo "0.00")
else
hourly_rate="0.00"
fi
else
block_cost="0.00"
time_left="no active block"
hourly_rate="0.00"
fi
# Format output
printf "💰 \$%.2f session / \$%.2f today / \$%.2f block (%s) | 🔥 \$%.2f/hr\n" \
"$session_cost" "$today_cost" "$block_cost" "$time_left" "$hourly_rate"