-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatusline.sh
More file actions
executable file
·168 lines (138 loc) · 6.51 KB
/
Copy pathstatusline.sh
File metadata and controls
executable file
·168 lines (138 loc) · 6.51 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
#!/bin/bash
set -f
# Read JSON input once
input=$(cat)
if [ -z "$input" ]; then
printf "Claude"
exit 0
fi
# ── Colors ──────────────────────────────────────────────
green='\033[0;32m'
yellow='\033[0;33m'
red='\033[0;31m'
dim='\033[2m'
white='\033[38;2;220;220;220m'
bold='\033[1m'
gray='\033[38;5;240m'
gray_dim='\033[38;5;242m'
reset='\033[0m'
# ── Helpers ─────────────────────────────────────────────
# Color based on percentage: green < 50, yellow 50-80, red > 80
color_for_pct() {
local pct=$1
if (( pct < 50 )); then printf "$green"
elif (( pct < 80 )); then printf "$yellow"
else printf "$red"
fi
}
# Build progress bar with filled/empty circles
build_bar() {
local pct=$1
local width=${2:-10}
[ "$pct" -lt 0 ] 2>/dev/null && pct=0
[ "$pct" -gt 100 ] 2>/dev/null && pct=100
local filled=$(( pct * width / 100 ))
local empty=$(( width - filled ))
local bar_color
bar_color=$(color_for_pct "$pct")
local filled_str="" empty_str=""
for ((i=0; i<filled; i++)); do filled_str+="●"; done
for ((i=0; i<empty; i++)); do empty_str+="○"; done
printf "${bar_color}${filled_str}${dim}${empty_str}${reset}"
}
# Format epoch timestamp for display
format_epoch() {
local epoch="$1"
local style="$2"
[ -z "$epoch" ] || [ "$epoch" = "null" ] || [ "$epoch" = "0" ] && return
case "$style" in
time) date -d "@$epoch" +"%H:%M" 2>/dev/null ;;
datetime) date -d "@$epoch" +"%b %-d, %H:%M" 2>/dev/null | sed 's/ / /g' ;;
esac
}
# ── Extract data ────────────────────────────────────────
MODEL=$(echo "$input" | jq -r '.model.display_name')
DIR=$(echo "$input" | jq -r '.workspace.current_dir')
COST=$(echo "$input" | jq -r '.cost.total_cost_usd')
COST_FORMATTED=$(LC_NUMERIC=C printf "%.2f" "$COST")
LINES_ADDED=$(echo "$input" | jq -r '.cost.total_lines_added // 0')
LINES_REMOVED=$(echo "$input" | jq -r '.cost.total_lines_removed // 0')
CONTEXT_PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0')
CONTEXT_PCT=${CONTEXT_PCT%.*}
CONTEXT_PCT=${CONTEXT_PCT:-0}
GIT_BRANCH=""
if git rev-parse --git-dir > /dev/null 2>&1; then
GIT_BRANCH=$(git branch --show-current 2>/dev/null)
fi
# v2.1.98: workspace.git_worktree is populated when cwd is inside a linked git worktree
GIT_WORKTREE=$(echo "$input" | jq -r '.workspace.git_worktree // empty')
# ── Line 1: model, dir, branch, context, diff, cost ────
CTX_COLOR=$(color_for_pct "$CONTEXT_PCT")
sep=" ${gray}│${reset} "
DIFF_SECTION=""
if [[ "$LINES_ADDED" -gt 0 || "$LINES_REMOVED" -gt 0 ]]; then
DIFF_SECTION="${sep}${green}+${LINES_ADDED}${reset}, ${red}-${LINES_REMOVED}${reset}"
fi
BRANCH_SECTION=""
if [ -n "$GIT_BRANCH" ]; then
BRANCH_SECTION="${sep}🌿 ${GIT_BRANCH}"
fi
WORKTREE_SECTION=""
if [ -n "$GIT_WORKTREE" ]; then
WORKTREE_SECTION="${sep}🌲 ${GIT_WORKTREE}"
fi
LINE1="[$MODEL]${sep}📁 ${DIR##*/}${BRANCH_SECTION}${WORKTREE_SECTION}${sep}🧠 ${CTX_COLOR}${bold}${CONTEXT_PCT}%${reset}${DIFF_SECTION}${sep}${green}\$${COST_FORMATTED}${reset}"
# ── Rate limits (v2.1.80: keys are five_hour/seven_day, resets_at is epoch) ──
rate_lines=""
five_hour_pct=$(echo "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
if [ -n "$five_hour_pct" ] && [ "$five_hour_pct" != "null" ]; then
five_hour_pct=${five_hour_pct%.*}
five_hour_reset=$(echo "$input" | jq -r '.rate_limits.five_hour.resets_at // empty')
five_hour_reset_fmt=$(format_epoch "$five_hour_reset" "time")
five_hour_bar=$(build_bar "$five_hour_pct" 10)
five_hour_pct_color=$(color_for_pct "$five_hour_pct")
rate_lines+="${white}current${reset} ${five_hour_bar} ${five_hour_pct_color}$(printf '%3d' "$five_hour_pct")%${reset} 🕐 ${gray_dim}${five_hour_reset_fmt}${reset}"
seven_day_pct=$(echo "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
if [ -n "$seven_day_pct" ] && [ "$seven_day_pct" != "null" ]; then
seven_day_pct=${seven_day_pct%.*}
seven_day_reset=$(echo "$input" | jq -r '.rate_limits.seven_day.resets_at // empty')
seven_day_reset_fmt=$(format_epoch "$seven_day_reset" "datetime")
seven_day_bar=$(build_bar "$seven_day_pct" 10)
seven_day_pct_color=$(color_for_pct "$seven_day_pct")
# Pace indicator: compare consumption rate vs ideal linear rate
pace_section=""
if [ -n "$seven_day_reset" ] && [ "$seven_day_reset" != "null" ] && (( seven_day_pct > 0 )); then
now=$(date +%s)
total_seconds=604800 # 7 days in seconds
remaining_seconds=$((seven_day_reset - now))
elapsed_seconds=$((total_seconds - remaining_seconds))
# Only show pace if at least 1h elapsed and reset is in the future
if (( elapsed_seconds > 3600 && remaining_seconds > 0 )); then
pace=$(awk "BEGIN { printf \"%.1f\", $seven_day_pct * $total_seconds / (100.0 * $elapsed_seconds) }")
pace_raw=$(awk "BEGIN { printf \"%.2f\", $seven_day_pct * $total_seconds / (100.0 * $elapsed_seconds) }")
# Arrow: under / on track / over budget
if awk "BEGIN { exit ($pace_raw > 1.1) ? 0 : 1 }"; then
arrow="↑"
elif awk "BEGIN { exit ($pace_raw < 0.9) ? 0 : 1 }"; then
arrow="↓"
else
arrow="→"
fi
# Color: green if <= 1.0, yellow if 1.0-1.3, red if >= 1.3
if awk "BEGIN { exit ($pace_raw >= 1.3) ? 0 : 1 }"; then
pace_color="$red"
elif awk "BEGIN { exit ($pace_raw > 1.0) ? 0 : 1 }"; then
pace_color="$yellow"
else
pace_color="$green"
fi
pace_section=" ${dim}(${reset}${pace_color}x${pace}${arrow}${reset}${dim})${reset}"
fi
fi
rate_lines+="\n${white}weekly${reset} ${seven_day_bar} ${seven_day_pct_color}$(printf '%3d' "$seven_day_pct")%${reset}${pace_section} 📅 ${gray_dim}${seven_day_reset_fmt}${reset}"
fi
fi
# ── Output ──────────────────────────────────────────────
echo -e "$LINE1"
[ -n "$rate_lines" ] && printf "%b\n" "$rate_lines"
exit 0