-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathstatusline-command.sh
More file actions
executable file
·288 lines (252 loc) · 9.91 KB
/
statusline-command.sh
File metadata and controls
executable file
·288 lines (252 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env bash
set -euo pipefail
input=$(cat)
# --- Output cache: skip all work if recently computed (2s TTL) ---
OUTPUT_CACHE="/tmp/claude_sl_out"
FIRST_RUN_MARKER="/tmp/claude_sl_init"
now=$(date +%s)
if [ -f "$OUTPUT_CACHE" ]; then
output_mtime=$(stat -f %m "$OUTPUT_CACHE" 2>/dev/null) || output_mtime=0
if (( now - output_mtime < 5 )); then
cat "$OUTPUT_CACHE"
exit 0
fi
elif [ ! -f "$FIRST_RUN_MARKER" ]; then
# Very first invocation ever - return placeholder instantly, compute in background
touch "$FIRST_RUN_MARKER"
# Extract just the dir name with jq (already needed) for a minimal placeholder
_dir=$(jq -r '.workspace.current_dir // ""' <<< "$input" 2>/dev/null)
_dir="${_dir##*/}"
_model=$(jq -r '.model.display_name // .model.id // "unknown"' <<< "$input" 2>/dev/null)
printf "[\033[37m%s\033[0m] \033[2mloading...\033[0m" "$_dir"
# Run the full script in background so the cache is warm for the next call
(bash "$0" <<< "$input" > /dev/null 2>&1) &
disown 2>/dev/null
exit 0
fi
# Parse input JSON (single jq call)
used_pct=0 ctx_size=0 cwd="" model_name="unknown" session_id="default"
eval "$(jq -r '@sh "used_pct=\(.context_window.used_percentage // 0) ctx_size=\(.context_window.context_window_size // 0) cwd=\(.workspace.current_dir // "") model_name=\(.model.display_name // .model.id // "unknown") session_id=\(.session_id // "default")"' <<< "$input")"
used_tokens=$((used_pct * ctx_size / 100))
# Fetch 5-hour usage from Anthropic OAuth API (cached for 120s)
CACHE_FILE="/tmp/claude_usage_cache.json"
CACHE_MAX_AGE=120
session_pct=0
FETCH_LOCK="/tmp/claude_usage_fetch.lck"
ERROR_BACKOFF_FILE="/tmp/claude_usage_error"
ERROR_BACKOFF_SECS=300
fetch_usage() {
local creds token response
creds=$(security find-generic-password -s "Claude Code-credentials" -w 2>/dev/null) || { touch "$ERROR_BACKOFF_FILE"; return 1; }
token=$(jq -r '.claudeAiOauth.accessToken // empty' <<< "$creds" 2>/dev/null) || { touch "$ERROR_BACKOFF_FILE"; return 1; }
[ -z "$token" ] && { touch "$ERROR_BACKOFF_FILE"; return 1; }
response=$(curl -s --max-time 3 \
-H "Authorization: Bearer $token" \
-H "anthropic-beta: oauth-2025-04-20" \
-H "User-Agent: claude-code/2.0" \
"https://api.anthropic.com/api/oauth/usage" 2>/dev/null) || { touch "$ERROR_BACKOFF_FILE"; return 1; }
# Only cache successful, valid responses
if echo "$response" | jq -e '.error // empty' &>/dev/null; then
touch "$ERROR_BACKOFF_FILE"
return 1
fi
if echo "$response" | jq -e '.five_hour' &>/dev/null; then
echo "$response" > "$CACHE_FILE"
rm -f "$ERROR_BACKOFF_FILE"
else
touch "$ERROR_BACKOFF_FILE"
return 1
fi
}
# Check if a fetch is needed
_should_fetch=false
read_cache=false
if [ -f "$CACHE_FILE" ]; then
cache_mtime=$(stat -f %m "$CACHE_FILE" 2>/dev/null) || cache_mtime=0
if (( now - cache_mtime > CACHE_MAX_AGE )); then
_should_fetch=true
fi
read_cache=true
else
_should_fetch=true
fi
# Error backoff: after a failed fetch, wait longer before retrying
if [ "$_should_fetch" = true ] && [ -f "$ERROR_BACKOFF_FILE" ]; then
err_mtime=$(stat -f %m "$ERROR_BACKOFF_FILE" 2>/dev/null) || err_mtime=0
if (( now - err_mtime < ERROR_BACKOFF_SECS )); then
_should_fetch=false
fi
fi
if [ "$_should_fetch" = true ]; then
# Atomic lock: mkdir is atomic across processes, preventing request storms
# from multiple Claude Code sessions hitting the API simultaneously
if [ -d "$FETCH_LOCK" ]; then
lock_mtime=$(stat -f %m "$FETCH_LOCK" 2>/dev/null) || lock_mtime=0
if (( now - lock_mtime >= CACHE_MAX_AGE )); then
rmdir "$FETCH_LOCK" 2>/dev/null
fi
fi
# Only one process wins the mkdir race
if mkdir "$FETCH_LOCK" 2>/dev/null; then
fetch_usage &>/dev/null &
disown 2>/dev/null
fi
fi
# Read usage cache
resets_at=""
diff=0
if [ "$read_cache" = true ] && [ -f "$CACHE_FILE" ]; then
eval "$(jq -r '@sh "session_pct=\(.five_hour.utilization // 0) resets_iso=\(.five_hour.resets_at // "")"' "$CACHE_FILE" 2>/dev/null)"
session_pct=${session_pct%.*}
[ -z "$session_pct" ] && session_pct=0
if [ -n "$resets_iso" ]; then
clean_ts=$(sed -E 's/\.[0-9]+//; s/:([0-9]{2})$/\1/' <<< "$resets_iso")
reset_epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S%z" "$clean_ts" +%s 2>/dev/null) || reset_epoch=0
diff=$((reset_epoch - now))
if [ "$diff" -gt 0 ]; then
hrs=$((diff / 3600))
mins=$(( (diff % 3600) / 60 ))
if [ "$hrs" -gt 0 ]; then
resets_at="${hrs}h${mins}m"
else
resets_at="${mins}m"
fi
fi
fi
fi
# Format a token count for display (e.g. 125000 -> "125k", 1200000 -> "1.2M")
_fmt_count() {
local n=$1
if [ "$n" -ge 1000000 ]; then
local w=$((n / 1000000)) f=$(( (n % 1000000) / 100000 ))
if [ "$f" -gt 0 ]; then echo "${w}.${f}M"; else echo "${w}M"; fi
elif [ "$n" -ge 1000 ]; then
local w=$((n / 1000)) f=$(( (n % 1000) / 100 ))
if [ "$f" -gt 0 ]; then echo "${w}.${f}k"; else echo "${w}k"; fi
else
echo "$n"
fi
}
_fmt_tokens=""
if [ "$used_tokens" -gt 0 ]; then
_used=$(_fmt_count "$used_tokens")
_total=$(_fmt_count "$ctx_size")
_fmt_tokens="${_used}/${_total}"
elif [ "$ctx_size" -gt 0 ]; then
_fmt_tokens="0/$(_fmt_count "$ctx_size")"
fi
# Build progress bars inline (no subshells - functions set variables directly)
_build_bar() {
local filled=$(($1 * 10 / 100)) i
_bar=""
for ((i=0; i<filled; i++)); do _bar+="█"; done
for ((i=filled; i<10; i++)); do _bar+="░"; done
}
_build_bar "$used_pct"; context_bar="$_bar"
_build_bar "$session_pct"; session_bar="$_bar"
# Colour thresholds (no subshells, $'...' for real escape bytes)
if [ "$used_pct" -lt 50 ]; then context_colour=$'\033[32m'
elif [ "$used_pct" -lt 75 ]; then context_colour=$'\033[33m'
else context_colour=$'\033[31m'; fi
if [ "$session_pct" -lt 50 ]; then session_colour=$'\033[32m'
elif [ "$session_pct" -lt 75 ]; then session_colour=$'\033[33m'
else session_colour=$'\033[31m'; fi
# Parameter expansion instead of basename subprocess
dir_name="${cwd##*/}"
# --- Model usage tracking per session ---
MODEL_TRACK_DIR="/tmp/claude_model_tracking"
mkdir -p "$MODEL_TRACK_DIR"
MODEL_TRACK_FILE="${MODEL_TRACK_DIR}/${session_id}"
# Clean up stale session files in background (~5% of runs)
if (( RANDOM % 20 == 0 )); then
find "$MODEL_TRACK_DIR" -type f -mtime +1 -delete 2>/dev/null &
disown 2>/dev/null
fi
# Increment count for current model (single jq call)
if [ -f "$MODEL_TRACK_FILE" ]; then
jq --arg m "$model_name" '.[$m] = ((.[$m] // 0) + 1)' "$MODEL_TRACK_FILE" > "${MODEL_TRACK_FILE}.tmp" 2>/dev/null && \
mv "${MODEL_TRACK_FILE}.tmp" "$MODEL_TRACK_FILE"
else
printf '{\"%s\":%d}' "$model_name" 1 > "$MODEL_TRACK_FILE"
fi
# Model colour helper (sets _mcolour, no subshell)
_model_colour() {
case "$1" in
*Opus*) _mcolour=$'\033[35m' ;;
*Sonnet*) _mcolour=$'\033[34m' ;;
*Haiku*) _mcolour=$'\033[32m' ;;
*) _mcolour=$'\033[37m' ;;
esac
}
# Build segmented model bar from tracking data (single jq call)
model_bar_width=10
model_bar=""
model_legend=""
if [ -f "$MODEL_TRACK_FILE" ]; then
tracking_data=$(jq -r '
([.[]] | add // 0) as $total |
length as $count |
"\($total) \($count)",
(to_entries | sort_by(-.value) | .[] | "\(.key)=\(.value)")
' "$MODEL_TRACK_FILE" 2>/dev/null) || tracking_data=""
if [ -n "$tracking_data" ]; then
# Read header line without spawning head
IFS= read -r _header <<< "$tracking_data"
read -r total_ticks model_count <<< "$_header"
total_ticks=${total_ticks:-0}
model_count=${model_count:-0}
if [ "$total_ticks" -gt 0 ]; then
filled_so_far=0
model_idx=0
active_pct=0
# Read remaining lines without spawning tail
_rest="${tracking_data#*$'\n'}"
while IFS='=' read -r m_name m_ticks; do
[ -z "$m_name" ] && continue
model_idx=$((model_idx + 1))
_model_colour "$m_name"
if [ "$model_idx" -eq "$model_count" ]; then
blocks=$((model_bar_width - filled_so_far))
else
blocks=$((m_ticks * model_bar_width / total_ticks))
[ "$blocks" -lt 1 ] && blocks=1
fi
filled_so_far=$((filled_so_far + blocks))
segment=""
for ((i=0; i<blocks; i++)); do segment+="█"; done
model_bar="${model_bar}${_mcolour}${segment}"$'\033[0m'
if [ "$m_name" = "$model_name" ]; then
active_pct=$((m_ticks * 100 / total_ticks))
fi
done <<< "$_rest"
_model_colour "$model_name"
model_legend="${_mcolour}${model_name} ${active_pct}%"$'\033[0m'
fi
fi
fi
# Fallback if no tracking data yet
if [ -z "$model_bar" ]; then
_model_colour "$model_name"
model_bar="${_mcolour}"
for ((i=0; i<model_bar_width; i++)); do model_bar+="█"; done
model_bar+=$'\033[0m'
model_legend="${_mcolour}${model_name} 100%"$'\033[0m'
fi
reset_str=""
if [ -n "$resets_at" ]; then
if [ "$diff" -lt 1800 ]; then
reset_str=$' \033[94m'"${resets_at}"$'\033[0m'
else
reset_str=$' \033[97m'"${resets_at}"$'\033[0m'
fi
fi
# Write output to cache and display
token_str=""
if [ -n "$_fmt_tokens" ]; then
token_str=$' \033[97m'"${_fmt_tokens}"$'\033[0m'
fi
printf "[\033[37m%s\033[0m] 🧠 %s%s\033[0m%s%%%s | 📶 %s%s\033[0m%s%%%s | 🧑💻 %s %s" \
"$dir_name" "$context_colour" "$context_bar" "$used_pct" "$token_str" \
"$session_colour" "$session_bar" "$session_pct" "$reset_str" \
"$model_bar" "$model_legend" \
| tee "$OUTPUT_CACHE"