-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude_code.sh
More file actions
executable file
·113 lines (96 loc) · 4.03 KB
/
claude_code.sh
File metadata and controls
executable file
·113 lines (96 loc) · 4.03 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
#!/usr/bin/env bash
# Claude Code status line. Receives JSON on stdin.
input=$(cat)
model=$(echo "$input" | jq -r '.model.display_name // "Unknown model"')
session_name=$(echo "$input" | jq -r '.session_name // empty')
cwd=$(echo "$input" | jq -r '.workspace.current_dir // .cwd // ""')
worktree_branch=$(echo "$input" | jq -r '.worktree.branch // empty')
used_pct=$(echo "$input" | jq -r '.context_window.used_percentage // empty')
cost_usd=$(echo "$input" | jq -r '.cost.total_cost_usd // empty')
transcript=$(echo "$input" | jq -r '.transcript_path // ""')
tool_count=0
if [ -n "$transcript" ] && [ -f "$transcript" ]; then
tool_count=$(grep -c '"tool_use"' "$transcript" 2>/dev/null || echo 0)
fi
five_h=$(echo "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
five_h_resets=$(echo "$input" | jq -r '.rate_limits.five_hour.resets_at // empty')
seven_d=$(echo "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
seven_d_resets=$(echo "$input" | jq -r '.rate_limits.seven_day.resets_at // empty')
git_branch=""
if [ -n "$cwd" ] && git -C "$cwd" rev-parse --git-dir >/dev/null 2>&1; then
git_branch=$(git -C "$cwd" --no-optional-locks symbolic-ref --short HEAD 2>/dev/null \
|| git -C "$cwd" --no-optional-locks rev-parse --short HEAD 2>/dev/null)
fi
RESET='\033[0m'
BOLD='\033[1m'
CYAN='\033[38;5;30m'
YELLOW='\033[38;5;172m'
GREEN='\033[32m'
MAGENTA='\033[35m'
RED='\033[31m'
BLUE='\033[34m'
DIM='\033[2m'
sep="$(printf "${DIM}|${RESET}")"
line1=()
line1+=("$(printf "${CYAN}${BOLD}%s${RESET}" "$model")")
[ -n "$session_name" ] && line1+=("$(printf "${DIM}[%s]${RESET}" "$session_name")")
if [ -n "$cwd" ]; then
short_cwd="${cwd/#$HOME/~}"
line1+=("$(printf "${BLUE}%s${RESET}" "$short_cwd")")
fi
[ -n "$git_branch" ] && line1+=("$(printf "${MAGENTA} %s${RESET}" "$git_branch")")
[ -n "$worktree_branch" ] && line1+=("$(printf "${MAGENTA}[wt:%s]${RESET}" "$worktree_branch")")
line2=()
used_int=$(printf '%.0f' "${used_pct:-0}")
if [ "$used_int" -ge 80 ]; then ctx_color="$RED"
elif [ "$used_int" -ge 50 ]; then ctx_color="$YELLOW"
else ctx_color="$GREEN"; fi
line2+=("$(printf "${ctx_color}ctx:%d%%${RESET}" "$used_int")")
five_h_val=$(printf '%.0f' "${five_h:-0}")
seven_d_val=$(printf '%.0f' "${seven_d:-0}")
five_h_reset_str=""
if [ -n "$five_h_resets" ]; then
now=$(date +%s)
remaining=$((five_h_resets - now))
if [ "$remaining" -gt 0 ]; then
hrs=$((remaining / 3600)); mins=$(( (remaining % 3600) / 60 ))
five_h_reset_str=$(printf " (r:%dh%02dm)" "$hrs" "$mins")
fi
fi
seven_d_reset_str=""
if [ -n "$seven_d_resets" ]; then
now=${now:-$(date +%s)}
remaining=$((seven_d_resets - now))
if [ "$remaining" -gt 0 ]; then
days=$((remaining / 86400)); hrs=$(( (remaining % 86400) / 3600 ))
seven_d_reset_str=$(printf " (r:%dd%dh)" "$days" "$hrs")
fi
fi
line2+=("$(printf "${YELLOW}5h:%d%%%s 7d:%d%%%s${RESET}" "$five_h_val" "$five_h_reset_str" "$seven_d_val" "$seven_d_reset_str")")
# Requires the UserPromptSubmit hook in settings.json to write the timestamp file.
idle_str=""
session_id=$(echo "$input" | jq -r '.session_id // empty')
idle_file="$HOME/.claude/last-user-input-${session_id}"
if [ -n "$session_id" ] && [ -f "$idle_file" ]; then
last_input=$(cat "$idle_file")
now=${now:-$(date +%s)}
idle=$((now - last_input))
if [ "$idle" -ge 3600 ]; then
idle_str=$(printf "%dh%dm" $((idle/3600)) $((idle%3600/60)))
elif [ "$idle" -ge 60 ]; then
idle_str=$(printf "%dm%ds" $((idle/60)) $((idle%60)))
else
idle_str=$(printf "%ds" "$idle")
fi
fi
line2+=("$(printf "${CYAN}tools:%s${RESET}" "$tool_count")")
if [ -n "$idle_str" ]; then
if [ "$idle" -ge 300 ]; then idle_color="$YELLOW"
else idle_color="$DIM"; fi
line2+=("$(printf "${idle_color}idle:%s${RESET}" "$idle_str")")
fi
cost_rounded=$(printf '%.1f' "${cost_usd:-0}")
line2+=("$(printf "${GREEN}${DIM}est:\$%s${RESET}" "$cost_rounded")")
join() { local out=""; for p in "$@"; do [ -z "$out" ] && out="$p" || out="${out}${sep}${p}"; done; printf "%b" "$out"; }
printf "%b\n" "$(join "${line1[@]}")"
printf "%b\n" "$(join "${line2[@]}")"