@@ -1,142 +1,32 @@
1-#!/usr/bin/env bash
2-# Claude Code status line: context, cwd, git, and 5-hour and weekly plan usage.
3-#
4-# Hierarchy comes from weight, not hue. Claude Code paints the line in its own
5-# grey and ignores SGR 39, so the terminal's default fg can't be requested.
6-# Bright white is the nearest match on dark themes; change FG for a light one.
7-
8-export LC_ALL=C.UTF-8
9-
10-ESC=$'\e'
11-RESET="$ESC[0m" DIM="$ESC[2m"
12-FG="$ESC[97m"
13-STRONG="$ESC[1m$FG"
14-INVERT="$ESC[1;7m$FG"
15-
16-# Octicons from Nerd Fonts; these codepoints are the same in v2 and v3.
17-ICON_CONTEXT=$'' # cpu
18-ICON_DIR=$'' # file_directory
19-ICON_BRANCH=$'' # git_branch
20-ICON_DETACHED=$'' # git_commit
21-ICON_CLEAN=$'' # check
22-ICON_USAGE=$'' # dashboard
23-ICON_TIME=$'' # clock
24-
25-WARN_AT=80 # at or above this percentage a value flips to reverse video
26-GAP=' '
27-
28-settings="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/settings.json"
29-[[ -r $settings ]] || settings=/dev/null
30-
31-# Context is measured against the auto-compact window when one is configured,
32-# so 100% means "about to compact". used_percentage is relative to the full
33-# model window, which would read 30% at compaction with a 300k limit on 1M.
34-IFS=$'\x1f' read -r ctx cwd used_5h resets_5h used_7d resets_7d < <(
35- jq -r --rawfile cfg "$settings" '
36- def pct: if . == null then "" else round end;
37- def epoch: if . == null then "" else floor end;
38- (try ($cfg | fromjson) catch {}) as $s
39- | (.model.id // "") as $id
40- | ($s.modelSettings[$id].autoCompactWindow
41- // $s.modelSettings[$id | sub("\\[.*$"; "")].autoCompactWindow
42- // $s.autoCompactWindow) as $limit
43- | .context_window as $c
44- | [ (if $c.used_percentage == null then ""
45- elif $limit != null and $c.total_input_tokens != null
46- and $limit < ($c.context_window_size // 0)
47- then $c.total_input_tokens / $limit * 100 | round
48- else $c.used_percentage | round end),
49- (.workspace.current_dir // .cwd // ""),
50- (.rate_limits.five_hour.used_percentage | pct),
51- (.rate_limits.five_hour.resets_at | epoch),
52- (.rate_limits.seven_day.used_percentage | pct),
53- (.rate_limits.seven_day.resets_at | epoch)
54- ] | map(tostring) | join("\u001f")'
55-)
56-[[ -n $cwd ]] || cwd=$PWD
57-
58-# Glyphs paint two cells: the trailing space carries the dim for the second half.
59-icon() { printf '%s%s %s ' "$DIM" "$1" "$RESET"; }
60-level() { # level <pct>: the value, reversed once it crosses WARN_AT
61- if (( $1 >= WARN_AT )); then
62- printf '%s %s%% %s' "$INVERT" "$1" "$RESET"
63- else
64- printf '%s%s%%%s' "$STRONG" "$1" "$RESET"
65- fi
66-}
67-duration() { # duration <seconds>: the two largest units
68- local s=$1
69- if (( s >= 86400 )); then printf '%dd%02dh' $(( s / 86400 )) $(( s % 86400 / 3600 ))
70- elif (( s >= 3600 )); then printf '%dh%02dm' $(( s / 3600 )) $(( s % 3600 / 60 ))
71- elif (( s >= 60 )); then printf '%dm' $(( s / 60 ))
72- else printf '<1m'
73- fi
74-}
75-window() { # window <label> <pct> <resets_at>: absent until the first API response
76- [[ -n $2 ]] || return 0
77- local seg left
78- seg="$(icon "$ICON_USAGE")$DIM$1$RESET $(level "$2")"
79- left=$(( ${3:-0} - EPOCHSECONDS ))
80- (( left > 0 )) && seg+=" $(icon "$ICON_TIME")$(duration "$left")"
81- segments+=("$seg")
82-}
83-
84-segments=()
85-
86-# Context
87-[[ -n $ctx ]] && segments+=("$(icon "$ICON_CONTEXT")$(level "$ctx")")
88-
89-# Working directory: ~ for home, at most the last two components below it
90-dir=$cwd
91-if [[ $dir == "$HOME" || $dir == "$HOME"/* ]]; then
92- dir="~${dir#"$HOME"}"
93-fi
94-if [[ $dir =~ ^(~?)/.+/([^/]+/[^/]+)$ ]]; then
95- dir="${BASH_REMATCH[1]}/…/${BASH_REMATCH[2]}"
96-fi
97-segments+=("$(icon "$ICON_DIR")$dir")
98-
99-# Git: branch, ahead/behind and file counts from a single status call
100-read -r head detached staged modified untracked conflicts ahead behind < <(
101- git -C "$cwd" --no-optional-locks status --porcelain=v2 --branch 2>/dev/null | awk '
102- $1 == "#" && $2 == "branch.oid" { oid = $3 }
103- $1 == "#" && $2 == "branch.head" { head = $3 }
104- $1 == "#" && $2 == "branch.ab" { ahead = substr($3, 2) + 0; behind = substr($4, 2) + 0 }
105- $1 == "1" || $1 == "2" {
106- if (substr($2, 1, 1) != ".") staged++
107- if (substr($2, 2, 1) != ".") modified++
108- }
109- $1 == "u" { conflicts++ }
110- $1 == "?" { untracked++ }
111- END {
112- if (head == "") exit
113- detached = head == "(detached)"
114- if (detached) head = substr(oid, 1, 7)
115- print head, detached + 0, staged + 0, modified + 0, untracked + 0, conflicts + 0, ahead + 0, behind + 0
116- }'
117-)
118-if [[ -n $head ]]; then
119- branch_icon=$ICON_BRANCH
120- (( detached )) && branch_icon=$ICON_DETACHED
121- git_seg="$(icon "$branch_icon")$STRONG$head$RESET"
122-
123- summary=''
124- (( conflicts )) && summary+=" !$conflicts"
125- (( staged )) && summary+=" +$staged"
126- (( modified )) && summary+=" ~$modified"
127- (( untracked )) && summary+=" ?$untracked"
128- (( ahead )) && summary+=" ↑$ahead"
129- (( behind )) && summary+=" ↓$behind"
130- [[ -n $summary ]] || summary=" $DIM$ICON_CLEAN $RESET"
131- segments+=("$git_seg$summary")
132-fi
133-
134-# Plan usage
135-window 5h "$used_5h" "$resets_5h"
136-window 7d "$used_7d" "$resets_7d"
137-
138-out=''
139-for s in "${segments[@]}"; do
140- out+="${out:+$GAP}$s"
141-done
142-printf '%s\n' "$out"
1+#!/bin/bash
2+i=$(cat)
3+j() { jq -r "$1 // empty" <<<"$i"; }
4+d=$(j '.workspace.current_dir // .cwd')
5+[ "$d" = "$HOME" ] && n="~" || n=${d##*/}
6+b=$(git -C "$d" --no-optional-locks branch --show-current 2>/dev/null)
7+m=$(j .model.display_name)
8+c=$(j .context_window.used_percentage); c=${c%.*}; c=${c:-0}
9+h=$(j .rate_limits.five_hour.used_percentage); h=${h%.*}
10+w=$(j .rate_limits.seven_day.used_percentage); w=${w%.*}
11+TEXT='205;214;244' OK='166;173;200' WARN='249;226;175' HOT='243;139;168'
12+C1='88;91;112' C2='69;71;90' C3='49;50;68' C4='24;24;37' C5='17;17;27'
13+fg() { local col=$OK; (( $1 >= 50 )) && col=$WARN; (( $1 >= 80 )) && col=$HOT; printf '\e[38;2;%sm' "$col"; }
14+fit() { if (( ${#1} <= $2 )); then printf %s "$1"; elif (( $2 >= 6 )); then printf '%s…' "${1:0:$(($2 - 1))}"; fi; }
15+for ((k = 0; k < 5; k++)); do ((k < c / 20)) && bar+=▰ || bar+=▱; done
16+ctx=" $(fg "$c")$bar $c%"; cw=$((10 + ${#c}))
17+u= uw=0
18+[ -n "$h" ] && u+="$(fg "$h") 5h $h%" && uw=$((6 + ${#h}))
19+[ -n "$w" ] && u+="${u:+ }$(fg "$w") wk $w%" && uw=$((uw + (uw ? 2 : 0) + 6 + ${#w}))
20+room=$((${COLUMNS:-200} - 4 - 2 - (cw + 2)))
21+[ -n "$u" ] && room=$((room - uw - 3))
22+(( room >= ${#m} + 5 )) && room=$((room - ${#m} - 5)) || m=
23+n=$(fit "$n" $((room - 5))); [ -n "$n" ] && room=$((room - ${#n} - 5))
24+[ -n "$b" ] && b=$(fit "$b" $((room - 5)))
25+p=
26+seg() { [ -n "$p" ] && printf '\e[38;2;%s;48;2;%sm' "$p" "$1" || printf '\e[38;2;%sm' "$1"; printf '\e[38;2;%s;48;2;%sm %s ' "$TEXT" "$1" "$2"; p=$1; }
27+[ -n "$m" ] && seg "$C1" " $m"
28+[ -n "$n" ] && seg "$C2" " $n"
29+[ -n "$b" ] && seg "$C3" " $b"
30+seg "$C4" "$ctx"
31+[ -n "$u" ] && seg "$C5" $'\e]8;;https://claude.ai/settings/usage\a'"$u"$'\e]8;;\a'
32+printf '\e[0;38;2;%sm\e[0m\n' "$p"