Blogmark

Claude Code Status Line

via jbranchaud@gmail.com

I finally got around to customizing my Claude Code status line. I started by browsing through the statusline docs. Then I found this reddit post for "a nice compact status line that you will probably like". I liked a lot of the elements of that status line, but it was several hundred lines of shell code that someone else had written. Instead, I pointed Claude Code at it and asked it to make a minimal version of that script with many of the same elements. In a couple subsequent prompts, I asked for some formatting changes and then for it to emulate the Tokyo Night theme.

image

It is spread across two lines.

The first line has a bunch of model details including the current context window and usage over the 5 hour and 7 day windows.

The second line contains local repo details, so the current git short stat numbers, the current working directory (cwd) name, and the current git branch.

Here is the source which manages to be only 71 lines:

#!/bin/bash
# Line 1: Model (effort) | used/total (pct) | 5h pct @reset | 7d pct @reset | vX
# Line 2: +a -d dir@branch

eval "$(jq -r '
  @sh "model=\(.model.display_name // "Claude")",
  @sh "effort=\(.effort.level // env.CLAUDE_CODE_EFFORT_LEVEL // "medium")",
  @sh "cwd=\(.cwd // "")",
  @sh "size=\(.context_window.context_window_size // 200000)",
  @sh "used=\(.context_window.current_usage // {} | [.input_tokens, .cache_creation_input_tokens, .cache_read_input_tokens] | add // 0)",
  @sh "h5=\(.rate_limits.five_hour.used_percentage // "" | if . == "" then . else round end)",
  @sh "h5r=\(.rate_limits.five_hour.resets_at // "")",
  @sh "d7=\(.rate_limits.seven_day.used_percentage // "" | if . == "" then . else round end)",
  @sh "d7r=\(.rate_limits.seven_day.resets_at // "")",
  @sh "ver=\(.version // "")"
')"

c() { printf '\033[38;2;%sm' "$1"; }
# Tokyo Night palette
blue=$(c '122;162;247') orange=$(c '255;158;100') green=$(c '158;206;106') cyan=$(c '125;207;255')
red=$(c '247;118;142') yellow=$(c '224;175;104') purple=$(c '187;154;247') white=$(c '192;202;245')
teal=$(c '115;218;202') dim=$(c '86;95;137') reset=$'\033[0m'
sep=" ${dim}|${reset} "

pct_color() {
  if (($1 >= 90)); then echo "$red"; elif (($1 >= 70)); then echo "$orange"
  elif (($1 >= 50)); then echo "$yellow"; else echo "$green"; fi
}
tokens() {
  if (($1 >= 1000000)); then awk "BEGIN{printf \"%gm\", int($1/100000)/10}"
  elif (($1 >= 1000)); then echo "$(($1 / 1000))k"
  else echo "$1"; fi
}
fmt_epoch() { date -r "$1" "+$2" 2>/dev/null || date -d "@$1" "+$2"; }

# Model + effort
model=${model/ context)/)}  # "Opus (1M context)" -> "Opus (1M)"
case $effort in
  low) ec=$dim ;; medium) ec=$orange effort=med ;; xhigh) ec=$purple ;; max) ec=$red ;; *) ec=$green ;;
esac
out="${blue}${model}${reset} ${dim}(${reset}${ec}${effort}${reset}${dim})${reset}"

# Line 2: diff stat, dir, branch
line2=""
if [[ -n $cwd ]]; then
  line2="${cyan}${cwd##*/}${reset}"
  if branch=$(git -C "$cwd" branch --show-current 2>/dev/null) && [[ -n $branch ]]; then
    line2+="${dim}@${reset}${purple}${branch}${reset}"
    read -r a d < <(git -C "$cwd" diff --numstat 2>/dev/null | awk '{a+=$1; d+=$2} END{print a+0, d+0}')
    ((a + d)) && line2="${green}+${a}${reset} ${red}-${d}${reset} ${line2}"
  fi
fi

# Context window
((size > 0)) || size=200000
ctx=$((used * 100 / size))
out+="${sep}${teal}$(tokens "$used")/$(tokens "$size")${reset} ${dim}(${reset}$(pct_color "$ctx")${ctx}%${reset}${dim})${reset}"

# Rate limits
limit() { # label pct reset_epoch date_fmt
  if [[ -z $2 ]]; then out+="${sep}${white}$1${reset} ${dim}-${reset}"; return; fi
  out+="${sep}${white}$1${reset} $(pct_color "$2")$2%${reset}"
  [[ -n $3 ]] && out+=" ${dim}@$(fmt_epoch "$3" "$4")${reset}"
}
limit 5h "$h5" "$h5r" '%H:%M'
limit 7d "$d7" "$d7r" '%a %b %-d, %H:%M'

[[ -n $ver ]] && out+="${sep}${dim}v${ver}${reset}"

printf '%s' "$out"
[[ -n $line2 ]] && printf '\n%s' "$line2"