-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatusline.sh
More file actions
executable file
·284 lines (250 loc) · 10.1 KB
/
statusline.sh
File metadata and controls
executable file
·284 lines (250 loc) · 10.1 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
#!/usr/bin/env bash
# Claude Code Statusline — standalone installer
# Usage: curl -fsSL https://raw.githubusercontent.com/dr5hn/ccm/main/statusline.sh | bash
#
# Shows context, tokens, cost, duration, burn rate, rate limits,
# directory, branch, version, and CCM account (if multi-account).
set -euo pipefail
SCRIPT_PATH="$HOME/.claude/ccm-statusline.sh"
SETTINGS="$HOME/.claude/settings.json"
# Colors
if [[ -t 1 ]]; then
GREEN='\033[0;32m'
CYAN='\033[0;36m'
DIM='\033[0;90m'
RESET='\033[0m'
else
GREEN='' CYAN='' DIM='' RESET=''
fi
echo ""
echo -e "${CYAN}Claude Code Statusline Installer${RESET}"
echo ""
# Create the statusline script
mkdir -p "$HOME/.claude"
cat > "$SCRIPT_PATH" << 'EOF'
#!/usr/bin/env bash
# CCM Statusline — smart multi-line display for Claude Code
input=$(cat)
# ── Colors ──
R="\033[0m" C="\033[36m" D="\033[90m" G="\033[32m" Y="\033[33m" RED="\033[31m"
# ── Extract all session data in a single jq call for performance ──
# Uses ASCII Unit Separator (\x1f) so empty fields are preserved.
# Tab is a whitespace IFS char — bash read collapses consecutive tabs, which
# would corrupt parsing whenever an optional field (e.g. rate limits) is empty.
IFS=$'\x1f' read -r PCT IN_TOK CC_TOK CR_TOK COST DUR_MS API_MS CWD RL5_PCT RL5_RESET RL7_PCT RL7_RESET CC_VER MODEL_NAME EFFORT SESSION_NAME LINES_ADD LINES_DEL < <(
echo "$input" | jq -r '[
(.context_window.used_percentage // 0 | floor | tostring),
(.context_window.current_usage.input_tokens // 0 | tostring),
(.context_window.current_usage.cache_creation_input_tokens // 0 | tostring),
(.context_window.current_usage.cache_read_input_tokens // 0 | tostring),
(.cost.total_cost_usd // 0 | tostring),
(.cost.total_duration_ms // 0 | tostring),
(.cost.total_api_duration_ms // 0 | tostring),
(.cwd // ""),
(.rate_limits.five_hour.used_percentage // "" | tostring),
(.rate_limits.five_hour.resets_at // "" | tostring),
(.rate_limits.seven_day.used_percentage // "" | tostring),
(.rate_limits.seven_day.resets_at // "" | tostring),
(.version // ""),
(.model.display_name // .model.id // ""),
(.effort.level // ""),
(.session_name // ""),
(.cost.total_lines_added // 0 | tostring),
(.cost.total_lines_removed // 0 | tostring)
] | join("\u001f")' 2>/dev/null
)
TOKENS=$((IN_TOK + CC_TOK + CR_TOK))
# ── Context bar (10 chars) ──
PCT_NUM=${PCT:-0}
FILLED=$((PCT_NUM / 10))
EMPTY=$((10 - FILLED))
BAR=""
for ((i=0; i<FILLED; i++)); do BAR+="▓"; done
for ((i=0; i<EMPTY; i++)); do BAR+="░"; done
if [[ "$PCT_NUM" -ge 90 ]]; then BAR_C="$RED"
elif [[ "$PCT_NUM" -ge 70 ]]; then BAR_C="$Y"
else BAR_C="$G"; fi
# ── Format tokens (K/M) ──
if [[ "$TOKENS" -ge 1000000 ]]; then
TOK_FMT="$(awk -v t="$TOKENS" 'BEGIN{printf "%.1fM", t/1000000}')"
elif [[ "$TOKENS" -ge 1000 ]]; then
TOK_FMT="$(awk -v t="$TOKENS" 'BEGIN{printf "%.0fK", t/1000}')"
else
TOK_FMT="${TOKENS}"
fi
# ── Format cost ──
COST_FMT=$(awk -v c="$COST" 'BEGIN{printf "$%.2f", c}' 2>/dev/null || echo "\$$COST")
# ── Format session duration ──
DUR_S=$((DUR_MS / 1000))
if [[ "$DUR_S" -ge 3600 ]]; then
DUR_FMT="$((DUR_S / 3600))h$((DUR_S % 3600 / 60))m"
elif [[ "$DUR_S" -ge 60 ]]; then
DUR_FMT="$((DUR_S / 60))m"
else
DUR_FMT="${DUR_S}s"
fi
# ── Format API latency ──
API_S=$((API_MS / 1000))
if [[ "$API_S" -ge 3600 ]]; then
API_FMT="$((API_S / 3600))h$((API_S % 3600 / 60))m"
elif [[ "$API_S" -ge 60 ]]; then
API_FMT="$((API_S / 60))m"
elif [[ "$API_S" -gt 0 ]]; then
API_FMT="${API_S}s"
else
API_FMT=""
fi
# ── Token burn rate (tokens per minute) ──
BURN_FMT=""
if [[ "$DUR_S" -gt 60 ]] && [[ "$TOKENS" -gt 0 ]]; then
BURN=$((TOKENS / (DUR_S / 60)))
if [[ "$BURN" -ge 1000000 ]]; then
BURN_FMT="$(awk -v b="$BURN" 'BEGIN{printf "%.1fM", b/1000000}')/m"
elif [[ "$BURN" -ge 1000 ]]; then
BURN_FMT="$(awk -v b="$BURN" 'BEGIN{printf "%.0fK", b/1000}')/m"
else
BURN_FMT="${BURN}/m"
fi
fi
# ── Format rate limits (5hr + 7day) ──
_fmt_rl() {
local pct="$1" reset="$2" label="$3"
[[ -z "$pct" || "$pct" == "null" ]] && return
local pint=$(echo "$pct" | cut -d. -f1)
local rc="$G"
[[ "$pint" -ge 80 ]] && rc="$RED"
[[ "$pint" -ge 60 ]] && [[ "$pint" -lt 80 ]] && rc="$Y"
local rfmt=""
if [[ -n "$reset" ]] && [[ "$reset" != "null" ]]; then
case "$(uname)" in
Darwin) rfmt=$(date -r "$reset" +%H:%M 2>/dev/null) ;;
*) rfmt=$(date -d "@$reset" +%H:%M 2>/dev/null) ;;
esac
fi
printf " ${D}·${R} ${rc}${label}: ${pint}%%${R}"
[[ -n "$rfmt" ]] && printf "${D} ↻${rfmt}${R}"
}
RL_FMT=""
RL_FMT+="$(_fmt_rl "$RL5_PCT" "$RL5_RESET" "5hr")"
RL_FMT+="$(_fmt_rl "$RL7_PCT" "$RL7_RESET" "7d")"
# ── Write rate limits to shared file for ccm watch ──
RL_FILE="$HOME/.claude-switch-backup/rate-limits.json"
if [[ -n "$RL5_PCT" && "$RL5_PCT" != "null" ]] || [[ -n "$RL7_PCT" && "$RL7_PCT" != "null" ]]; then
mkdir -p "$(dirname "$RL_FILE")" 2>/dev/null
cat > "${RL_FILE}.tmp" 2>/dev/null << RLJSON
{"five_hour":{"used_percentage":${RL5_PCT:-0},"resets_at":${RL5_RESET:-0}},"seven_day":{"used_percentage":${RL7_PCT:-0},"resets_at":${RL7_RESET:-0}},"updated_at":"$(date -u +%Y-%m-%dT%H:%M:%SZ)"}
RLJSON
mv "${RL_FILE}.tmp" "$RL_FILE" 2>/dev/null
fi
# ── Git branch ──
BRANCH=""
if [[ -n "$CWD" ]] && command -v git &>/dev/null; then
BRANCH=$(git -C "$CWD" branch --show-current 2>/dev/null)
fi
# ── Directory (short) ──
DIR_SHORT=""
if [[ -n "$CWD" ]]; then
DIR_SHORT="${CWD/#$HOME/~}"
[[ ${#DIR_SHORT} -gt 30 ]] && DIR_SHORT="…${DIR_SHORT: -29}"
fi
# ── CCM account data (direct file read + env var fallback) ──
SEQ="$HOME/.claude-switch-backup/sequence.json"
CONF="$HOME/.claude/.claude.json"
[[ -f "$CONF" ]] || CONF="$HOME/.claude.json"
ALIAS="" EMAIL_SHORT="" HEALTH="" TOTAL_ACCTS=0
if [[ -f "$SEQ" ]] && [[ -f "$CONF" ]]; then
# Use CLAUDE_CODE_USER_EMAIL env var if available (v2.1.51+), fall back to config file
EMAIL="${CLAUDE_CODE_USER_EMAIL:-}"
[[ -z "$EMAIL" ]] && EMAIL=$(jq -r '.oauthAccount.emailAddress // empty' "$CONF" 2>/dev/null)
if [[ -n "$EMAIL" ]]; then
EMAIL_SHORT="$EMAIL"
ACCT_DATA=$(jq -r --arg e "$EMAIL" '
.accounts | to_entries[] | select(.value.email == $e) |
"\(.value.alias // "")\t\(.value.healthStatus // "unknown")"
' "$SEQ" 2>/dev/null)
if [[ -n "$ACCT_DATA" ]]; then
ALIAS=$(echo "$ACCT_DATA" | cut -f1)
HEALTH=$(echo "$ACCT_DATA" | cut -f2)
fi
TOTAL_ACCTS=$(jq '.accounts | length' "$SEQ" 2>/dev/null || echo "0")
fi
fi
# ── Compact warning ──
COMPACT_WARN=""
if [[ "$PCT_NUM" -ge 80 ]]; then
COMPACT_WARN=" ${D}·${R} ${Y}⚠ /compact${R}"
fi
# ── Model + reasoning effort + session name (each segment hidden when absent) ──
MODEL_FMT=""
if [[ -n "$MODEL_NAME" ]]; then
MODEL_FMT="${C}${MODEL_NAME}${R}"
if [[ -n "$EFFORT" ]]; then
case "$EFFORT" in
low) EC="$D" ;;
medium) EC="$G" ;;
high) EC="$Y" ;;
xhigh|max) EC="$RED" ;;
*) EC="$D" ;;
esac
MODEL_FMT+=" ${D}·${R} ${EC}${EFFORT}${R}"
fi
fi
if [[ -n "$SESSION_NAME" ]]; then
[[ -n "$MODEL_FMT" ]] && MODEL_FMT+=" ${D}·${R} "
MODEL_FMT+="${C}${SESSION_NAME}${R}"
fi
# ── Lines added/removed (only shown when either is non-zero) ──
LINES_FMT=""
if [[ "${LINES_ADD:-0}" -gt 0 ]] || [[ "${LINES_DEL:-0}" -gt 0 ]]; then
LINES_FMT=" ${D}·${R} ${G}+${LINES_ADD}${R} ${RED}-${LINES_DEL}${R}"
fi
# ── LINE 1: Model + effort + session (own line, omitted when all are absent) ──
[[ -n "$MODEL_FMT" ]] && echo -e "$MODEL_FMT"
# ── LINE 2: Context + tokens + cost + duration + burn + lines diff + rate limits ──
L1="${BAR_C}${BAR}${R} ${PCT_NUM}% ${D}·${R} ${TOK_FMT} tokens ${D}·${R} ${COST_FMT} ${D}·${R} ${DUR_FMT}"
[[ -n "$BURN_FMT" ]] && L1+=" ${D}·${R} ${BURN_FMT}"
L1+="${LINES_FMT}${RL_FMT}"
echo -e "$L1"
# ── LINE 2: Directory + branch + version + compact warning ──
L2="${C}${DIR_SHORT}${R}"
[[ -n "$BRANCH" ]] && L2+=" ${D}·${R} ${G}${BRANCH}${R}"
[[ -n "$CC_VER" ]] && L2+=" ${D}· v${CC_VER}${R}"
L2+="${COMPACT_WARN}"
echo -e "$L2"
# ── LINE 3: Account info (only if 2+ accounts managed) ──
if [[ "$TOTAL_ACCTS" -ge 2 ]]; then
ACCT_LABEL="${ALIAS:-$EMAIL_SHORT}"
case "$HEALTH" in
healthy) H="${G}●${R}" ;;
degraded) H="${Y}●${R}" ;;
*) H="${RED}●${R}" ;;
esac
# Check for bound account in current directory
BOUND=""
if [[ -n "$CWD" ]] && [[ -f "$SEQ" ]]; then
BOUND_ACCT=$(jq -r --arg p "$CWD" '.bindings[$p] // empty' "$SEQ" 2>/dev/null)
if [[ -n "$BOUND_ACCT" ]]; then
BOUND_ALIAS=$(jq -r --arg n "$BOUND_ACCT" '.accounts[$n].alias // ("acct-" + $n)' "$SEQ" 2>/dev/null)
BOUND=" ${D}· bound:${R} ${C}${BOUND_ALIAS}${R}"
fi
fi
echo -e "${C}${ACCT_LABEL}${R} ${D}(${EMAIL_SHORT})${R} ${D}·${R} ${TOTAL_ACCTS} accounts ${D}·${R} ${H}${BOUND}"
fi
EOF
chmod +x "$SCRIPT_PATH"
echo -e "${GREEN}✓${RESET} Created $SCRIPT_PATH"
# Update settings.json
if [[ -f "$SETTINGS" ]]; then
ORIG_PERMS=$(stat -f '%Lp' "$SETTINGS" 2>/dev/null || stat -c '%a' "$SETTINGS" 2>/dev/null || echo "644")
UPDATED=$(jq --arg cmd "$SCRIPT_PATH" '.statusLine = {type: "command", command: $cmd, padding: 2}' "$SETTINGS")
echo "$UPDATED" > "${SETTINGS}.tmp" && mv "${SETTINGS}.tmp" "$SETTINGS"
chmod "$ORIG_PERMS" "$SETTINGS"
else
echo "{}" | jq --arg cmd "$SCRIPT_PATH" '.statusLine = {type: "command", command: $cmd, padding: 2}' > "$SETTINGS"
fi
echo -e "${GREEN}✓${RESET} Updated settings.json"
echo ""
echo -e "${GREEN}Done!${RESET} Restart Claude Code to see the statusline."
echo ""
echo -e " ${DIM}Uninstall: rm ~/.claude/ccm-statusline.sh${RESET}"
echo -e " ${DIM}Guide: https://github.com/dr5hn/ccm#statusline${RESET}"