forked from ykdojo/claude-code-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext-bar.sh
More file actions
executable file
·211 lines (188 loc) · 7.63 KB
/
context-bar.sh
File metadata and controls
executable file
·211 lines (188 loc) · 7.63 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
#!/bin/bash
# Color theme: gray, orange, blue, teal, green, lavender, rose, gold, slate, cyan
# Preview colors with: bash scripts/color-preview.sh
COLOR="blue"
# Color codes
C_RESET='\033[0m'
C_GRAY='\033[38;5;245m' # explicit gray for default text
C_BAR_EMPTY='\033[38;5;238m'
case "$COLOR" in
orange) C_ACCENT='\033[38;5;173m' ;;
blue) C_ACCENT='\033[38;5;74m' ;;
teal) C_ACCENT='\033[38;5;66m' ;;
green) C_ACCENT='\033[38;5;71m' ;;
lavender) C_ACCENT='\033[38;5;139m' ;;
rose) C_ACCENT='\033[38;5;132m' ;;
gold) C_ACCENT='\033[38;5;136m' ;;
slate) C_ACCENT='\033[38;5;60m' ;;
cyan) C_ACCENT='\033[38;5;37m' ;;
*) C_ACCENT="$C_GRAY" ;; # gray: all same color
esac
input=$(cat)
# Extract model, directory, and cwd
model=$(echo "$input" | jq -r '.model.display_name // .model.id // "?"')
cwd=$(echo "$input" | jq -r '.cwd // empty')
dir=$(basename "$cwd" 2>/dev/null || echo "?")
# Get git branch, uncommitted file count, and sync status
branch=""
git_status=""
if [[ -n "$cwd" && -d "$cwd" ]]; then
branch=$(git -C "$cwd" branch --show-current 2>/dev/null)
if [[ -n "$branch" ]]; then
# Count uncommitted files
file_count=$(git -C "$cwd" --no-optional-locks status --porcelain -uall 2>/dev/null | wc -l | tr -d ' ')
# Check sync status with upstream
sync_status=""
upstream=$(git -C "$cwd" rev-parse --abbrev-ref @{upstream} 2>/dev/null)
if [[ -n "$upstream" ]]; then
# Get last fetch time
fetch_head="$cwd/.git/FETCH_HEAD"
fetch_ago=""
if [[ -f "$fetch_head" ]]; then
fetch_time=$(stat -f %m "$fetch_head" 2>/dev/null || stat -c %Y "$fetch_head" 2>/dev/null)
if [[ -n "$fetch_time" ]]; then
now=$(date +%s)
diff=$((now - fetch_time))
if [[ $diff -lt 60 ]]; then
fetch_ago="<1m ago"
elif [[ $diff -lt 3600 ]]; then
fetch_ago="$((diff / 60))m ago"
elif [[ $diff -lt 86400 ]]; then
fetch_ago="$((diff / 3600))h ago"
else
fetch_ago="$((diff / 86400))d ago"
fi
fi
fi
counts=$(git -C "$cwd" rev-list --left-right --count HEAD...@{upstream} 2>/dev/null)
ahead=$(echo "$counts" | cut -f1)
behind=$(echo "$counts" | cut -f2)
if [[ "$ahead" -eq 0 && "$behind" -eq 0 ]]; then
if [[ -n "$fetch_ago" ]]; then
sync_status="synced ${fetch_ago}"
else
sync_status="synced"
fi
elif [[ "$ahead" -gt 0 && "$behind" -eq 0 ]]; then
sync_status="${ahead} ahead"
elif [[ "$ahead" -eq 0 && "$behind" -gt 0 ]]; then
sync_status="${behind} behind"
else
sync_status="${ahead} ahead, ${behind} behind"
fi
else
sync_status="no upstream"
fi
# Build git status string
if [[ "$file_count" -eq 0 ]]; then
git_status="(0 files uncommitted, ${sync_status})"
elif [[ "$file_count" -eq 1 ]]; then
# Show the actual filename when only one file is uncommitted
single_file=$(git -C "$cwd" --no-optional-locks status --porcelain -uall 2>/dev/null | head -1 | sed 's/^...//')
git_status="(${single_file} uncommitted, ${sync_status})"
else
git_status="(${file_count} files uncommitted, ${sync_status})"
fi
fi
fi
# Get transcript path for context calculation and last message feature
transcript_path=$(echo "$input" | jq -r '.transcript_path // empty')
# Get context window size from JSON (accurate), but calculate tokens from transcript
# (more accurate than total_input_tokens which excludes system prompt/tools/memory)
# See: github.com/anthropics/claude-code/issues/13652
max_context=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
max_k=$((max_context / 1000))
# Calculate context bar from transcript
if [[ -n "$transcript_path" && -f "$transcript_path" ]]; then
context_length=$(jq -s '
map(select(.message.usage and .isSidechain != true and .isApiErrorMessage != true)) |
last |
if . then
(.message.usage.input_tokens // 0) +
(.message.usage.cache_read_input_tokens // 0) +
(.message.usage.cache_creation_input_tokens // 0)
else 0 end
' < "$transcript_path")
# 20k baseline: includes system prompt (~3k), tools (~15k), memory (~300),
# plus ~2k for git status, env block, XML framing, and other dynamic context
baseline=20000
bar_width=10
if [[ "$context_length" -gt 0 ]]; then
pct=$((context_length * 100 / max_context))
pct_prefix=""
else
# At conversation start, ~20k baseline is already loaded
pct=$((baseline * 100 / max_context))
pct_prefix="~"
fi
[[ $pct -gt 100 ]] && pct=100
bar=""
for ((i=0; i<bar_width; i++)); do
bar_start=$((i * 10))
progress=$((pct - bar_start))
if [[ $progress -ge 8 ]]; then
bar+="${C_ACCENT}█${C_RESET}"
elif [[ $progress -ge 3 ]]; then
bar+="${C_ACCENT}▄${C_RESET}"
else
bar+="${C_BAR_EMPTY}░${C_RESET}"
fi
done
ctx="${bar} ${C_GRAY}${pct_prefix}${pct}% of ${max_k}k tokens"
else
# Transcript not available yet - show baseline estimate
baseline=20000
bar_width=10
pct=$((baseline * 100 / max_context))
[[ $pct -gt 100 ]] && pct=100
bar=""
for ((i=0; i<bar_width; i++)); do
bar_start=$((i * 10))
progress=$((pct - bar_start))
if [[ $progress -ge 8 ]]; then
bar+="${C_ACCENT}█${C_RESET}"
elif [[ $progress -ge 3 ]]; then
bar+="${C_ACCENT}▄${C_RESET}"
else
bar+="${C_BAR_EMPTY}░${C_RESET}"
fi
done
ctx="${bar} ${C_GRAY}~${pct}% of ${max_k}k tokens"
fi
# Build output: Model | Dir | Branch (uncommitted) | Context
output="${C_ACCENT}${model}${C_GRAY} | 📁${dir}"
[[ -n "$branch" ]] && output+=" | 🔀${branch} ${git_status}"
output+=" | ${ctx}${C_RESET}"
printf '%b\n' "$output"
# Get user's last message (text only, not tool results, skip unhelpful messages)
if [[ -n "$transcript_path" && -f "$transcript_path" ]]; then
# Calculate visible length (without ANSI codes) - 10 chars for bar + content
plain_output="${model} | 📁${dir}"
[[ -n "$branch" ]] && plain_output+=" | 🔀${branch} ${git_status}"
plain_output+=" | xxxxxxxxxx ${pct}% of ${max_k}k tokens"
max_len=${#plain_output}
last_user_msg=$(jq -rs '
# Messages to skip (not useful as context)
def is_unhelpful:
startswith("[Request interrupted") or
startswith("[Request cancelled") or
. == "";
[.[] | select(.type == "user") |
select(.message.content | type == "string" or
(type == "array" and any(.[]; .type == "text")))] |
reverse |
map(.message.content |
if type == "string" then .
else [.[] | select(.type == "text") | .text] | join(" ") end |
gsub("\n"; " ") | gsub(" +"; " ")) |
map(select(is_unhelpful | not)) |
first // ""
' < "$transcript_path" 2>/dev/null)
if [[ -n "$last_user_msg" ]]; then
if [[ ${#last_user_msg} -gt $max_len ]]; then
echo "💬 ${last_user_msg:0:$((max_len - 3))}..."
else
echo "💬 ${last_user_msg}"
fi
fi
fi