Skip to content

Instantly share code, notes, and snippets.

@z333d
Created April 23, 2026 03:14
Show Gist options
  • Select an option

  • Save z333d/e8fac075c5f1599d3b55b52e33c7074f to your computer and use it in GitHub Desktop.

Select an option

Save z333d/e8fac075c5f1599d3b55b52e33c7074f to your computer and use it in GitHub Desktop.
Claude Code statusline: model, branch, context, cost, rate limits with reset times
#!/bin/sh
input=$(cat)
model=$(echo "$input" | jq -r '.model.display_name // "Claude"')
cwd=$(echo "$input" | jq -r '.workspace.current_dir // .cwd // ""')
used=$(echo "$input" | jq -r '.context_window.used_percentage // empty')
cost_usd=$(echo "$input" | jq -r '.cost.total_cost_usd // empty')
lines_added=$(echo "$input" | jq -r '.cost.total_lines_added // empty')
lines_removed=$(echo "$input" | jq -r '.cost.total_lines_removed // empty')
five_hour=$(echo "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
five_hour_reset=$(echo "$input" | jq -r '.rate_limits.five_hour.resets_at // empty')
seven_day=$(echo "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
seven_day_reset=$(echo "$input" | jq -r '.rate_limits.seven_day.resets_at // empty')
now=$(date +%s)
# Format seconds as "2d4h" / "3h12m" / "45m" / "30s"
fmt_duration() {
secs=$1
if [ "$secs" -le 0 ]; then
echo "now"
return
fi
days=$(( secs / 86400 ))
hours=$(( (secs % 86400) / 3600 ))
mins=$(( (secs % 3600) / 60 ))
if [ "$days" -gt 0 ]; then
echo "${days}d${hours}h"
elif [ "$hours" -gt 0 ]; then
echo "${hours}h${mins}m"
elif [ "$mins" -gt 0 ]; then
echo "${mins}m"
else
echo "${secs}s"
fi
}
# Git branch
branch=""
if [ -n "$cwd" ]; then
branch=$(git -C "$cwd" --no-optional-locks symbolic-ref --short HEAD 2>/dev/null)
fi
# Context progress bar
if [ -n "$used" ]; then
used_int=$(printf '%.0f' "$used")
filled=$(( used_int * 10 / 100 ))
empty=$(( 10 - filled ))
bar=""
i=0
while [ $i -lt $filled ]; do
bar="${bar}█"
i=$(( i + 1 ))
done
i=0
while [ $i -lt $empty ]; do
bar="${bar}░"
i=$(( i + 1 ))
done
ctx_part="[${bar}] ${used_int}%"
else
ctx_part="[░░░░░░░░░░] -"
fi
# Line 1: model + branch + context
if [ -n "$branch" ]; then
line1="${model} | ${branch} ${ctx_part}"
else
line1="${model} | ${ctx_part}"
fi
# Line 2: cost + lines changed
line2=""
if [ -n "$cost_usd" ]; then
line2=$(printf '$%.2f' "$cost_usd")
fi
if [ -n "$lines_added" ] || [ -n "$lines_removed" ]; then
added=${lines_added:-0}
removed=${lines_removed:-0}
diff_part="+${added}/-${removed}"
if [ -n "$line2" ]; then
line2="${line2} ${diff_part}"
else
line2="${diff_part}"
fi
fi
# Line 3: rate limits with time-until-reset
line3=""
if [ -n "$five_hour" ]; then
five_int=$(printf '%.0f' "$five_hour")
part="5h: ${five_int}%"
if [ -n "$five_hour_reset" ]; then
remaining=$(( five_hour_reset - now ))
part="${part} ($(fmt_duration $remaining))"
fi
line3="$part"
fi
if [ -n "$seven_day" ]; then
week_int=$(printf '%.0f' "$seven_day")
part="7d: ${week_int}%"
if [ -n "$seven_day_reset" ]; then
remaining=$(( seven_day_reset - now ))
part="${part} ($(fmt_duration $remaining))"
fi
if [ -n "$line3" ]; then
line3="${line3} | ${part}"
else
line3="$part"
fi
fi
# Assemble output, skipping empty lines
out="$line1"
[ -n "$line2" ] && out="${out}
${line2}"
[ -n "$line3" ] && out="${out}
${line3}"
printf "%s" "$out"

Claude Code Statusline

三行 statusline,展示模型、分支、上下文用量、会话花费、改动行数、5h / 7d 限额(含距离重置时间)。

Opus 4.7 (1M context) | feat/my-branch [███░░░░░░░] 32%
$0.12 +156/-23
5h: 32% (3h12m) | 7d: 81% (2d6h)

安装

一行装好(需要 jqbrew install jq):

mkdir -p ~/.claude && \
  curl -fsSL https://gist.githubusercontent.com/z333d/e8fac075c5f1599d3b55b52e33c7074f/raw/statusline-command.sh \
    -o ~/.claude/statusline-command.sh && \
  chmod +x ~/.claude/statusline-command.sh

然后在 Claude Code 里配置 statusline:

方式 A:用 /statusline 命令

在 Claude Code 里输入 /statusline,填入:

sh ~/.claude/statusline-command.sh

方式 B:手动编辑 ~/.claude/settings.json

{
  "statusLine": {
    "type": "command",
    "command": "sh ~/.claude/statusline-command.sh"
  }
}

重启 Claude Code 或下次刷新即可。

说明

  • rate_limits 只对 Pro / Max 订阅账户可用,免费额度不显示第三行
  • cost 是客户端估算值,跟实际账单可能有出入
  • 字段缺失时对应行/段自动隐藏
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment