Skip to content

Instantly share code, notes, and snippets.

@darkwebdev
Created August 22, 2026 12:28
Show Gist options
  • Select an option

  • Save darkwebdev/fb22d3de848748359885065b27ee2b8d to your computer and use it in GitHub Desktop.

Select an option

Save darkwebdev/fb22d3de848748359885065b27ee2b8d to your computer and use it in GitHub Desktop.
Claude Code status line: model + stacked 5h/7d rate-limit bars (usage/time-elapsed) + context usage bar
#!/bin/bash
# Status line: model + progress bars for 5h/7d rate-limit usage and context
# window usage. Reads the JSON payload Claude Code pipes on stdin (see
# `claude` status line docs).
# Force C numeric formatting: a locale with a comma decimal separator (e.g.
# de_DE) makes printf/awk's %f reject or mis-render plain decimal input.
export LC_NUMERIC=C
input=$(cat)
model=$(echo "$input" | jq -r '.model.display_name // "?"')
cwd=$(echo "$input" | jq -r '.cwd // .workspace.current_dir // empty')
cwd="${cwd##*/}"
BAR_WIDTH=10
RED='\033[31m'
YELLOW='\033[33m'
RESET='\033[0m'
# Renders just the block bar (no percentage) for a 0-100 value, as a solid
# background-color fill: dark gray by default, red (>=90%) or yellow
# (>=70%, <90%) for the filled portion, light gray for the empty portion
# (matching stacked_bar's palette). Empty if $1 is blank (field absent
# from the payload).
bar_only() {
local pct="$1"
[ -z "$pct" ] && return
awk -v p="$pct" -v w="$BAR_WIDTH" 'BEGIN {
filled = int(p / 100 * w + 0.5)
if (filled > w) filled = w
if (filled < 0) filled = 0
color = (p >= 90) ? "\033[48;5;196m" : (p >= 70) ? "\033[48;5;220m" : "\033[48;5;238m"
empty_color = "\033[48;5;252m"
printf "%s", color
for (i = 0; i < filled; i++) printf " "
printf "\033[0m%s", empty_color
for (i = filled; i < w; i++) printf " "
printf "\033[0m"
}'
}
five=$(echo "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
week=$(echo "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
five_reset=$(echo "$input" | jq -r '.rate_limits.five_hour.resets_at // empty')
week_reset=$(echo "$input" | jq -r '.rate_limits.seven_day.resets_at // empty')
used_pct=$(echo "$input" | jq -r '.context_window.used_percentage // empty')
FIVE_WINDOW_SEC=18000
WEEK_WINDOW_SEC=604800
# Formats seconds-until a Unix epoch timestamp as "H:MM" (under a day) or
# "D:HH:MM" (a day or more).
time_until() {
local ts="$1"
[ -z "$ts" ] && return
local diff=$(( ${ts%.*} - $(date +%s) ))
[ "$diff" -lt 0 ] && diff=0
local days=$(( diff / 86400 ))
local hours=$(( (diff % 86400) / 3600 ))
local mins=$(( (diff % 3600) / 60 ))
if [ "$days" -gt 0 ]; then
printf "%d:%02d:%02d" "$days" "$hours" "$mins"
else
printf "%d:%02d" "$hours" "$mins"
fi
}
# Percentage (0-100) of the window's duration that has already elapsed,
# based on the reset timestamp and the window's total length in seconds.
time_elapsed_pct() {
local ts="$1" window="$2"
[ -z "$ts" ] && return
local diff=$(( ${ts%.*} - $(date +%s) ))
[ "$diff" -lt 0 ] && diff=0
[ "$diff" -gt "$window" ] && diff="$window"
awk -v d="$diff" -v w="$window" 'BEGIN { printf "%.2f", (1 - d / w) * 100 }'
}
# Renders one character-row that visually packs two half-height bars: the
# top half shows $1 (usage %, gray/yellow(>=70%)/red(>=90%)) and the
# bottom half shows $2 (time-elapsed % of the window, plain gray) using
# the upper-half-block glyph (▀) with independent foreground/background
# color per column. Empty cells (top and bottom) are light gray so filled
# portions read as darker gray/yellow/red against a light-gray track.
stacked_bar() {
local top_pct="$1" bottom_pct="$2"
[ -z "$top_pct" ] && return
awk -v top="$top_pct" -v bottom="$bottom_pct" -v w="$BAR_WIDTH" 'BEGIN {
ft = int(top / 100 * w + 0.5); if (ft > w) ft = w; if (ft < 0) ft = 0
fb = int(bottom / 100 * w + 0.5); if (fb > w) fb = w; if (fb < 0) fb = 0
for (i = 0; i < w; i++) {
if (i < ft) {
fg = (top >= 90) ? "\033[38;5;196m" : (top >= 70) ? "\033[38;5;220m" : "\033[38;5;238m"
} else {
fg = "\033[38;5;252m"
}
bg = (i < fb) ? "\033[48;5;244m" : "\033[48;5;252m"
printf "%s%s\xe2\x96\x80", fg, bg
}
printf "\033[0m"
}'
}
five_time=$(time_until "$five_reset")
week_time=$(time_until "$week_reset")
five_elapsed=$(time_elapsed_pct "$five_reset" "$FIVE_WINDOW_SEC")
week_elapsed=$(time_elapsed_pct "$week_reset" "$WEEK_WINDOW_SEC")
five_stack=$(stacked_bar "$five" "$five_elapsed")
week_stack=$(stacked_bar "$week" "$week_elapsed")
ctx_bar=$(bar_only "$used_pct")
five_seg=""
[ -n "$five_stack" ] && five_seg="5h ${five_stack} $(printf '%.0f' "$five")%/${five_time}"
week_seg=""
[ -n "$week_stack" ] && week_seg="7d ${week_stack} $(printf '%.0f' "$week")%/${week_time}"
ctx_seg=""
[ -n "$ctx_bar" ] && ctx_seg="ctx ${ctx_bar} $(printf '%.0f' "$used_pct")%"
# --- Assemble (normal text color; only bar fills are colored above) -----
if [ -n "$cwd" ]; then
line="${cwd} | ${model}"
else
line="${model}"
fi
[ -n "$five_seg" ] && line="${line} | ${five_seg}"
[ -n "$week_seg" ] && line="${line} | ${week_seg}"
[ -n "$ctx_seg" ] && line="${line} | ${ctx_seg}"
printf "%b" "$line"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment