#!/bin/sh # tigertty-claude-context: tells TigerTTY how full Claude Code's context is, so the session's tab shows it as its bar. # It goes on the machine where Claude Code runs (the far end of ssh, if that is where it runs) and is named as # Claude Code's status line in ~/.claude/settings.json there: # # "statusLine": { "type": "command", "command": "/path/to/tigertty-claude-context" } # # Claude Code gives a status line its state as JSON on standard input, with context_window.used_percentage in it # (seen in 2.1.329), but no terminal: the command has no controlling terminal and escape sequences in what it prints # are taken out. Claude Code itself has one, though, two processes up, and its device can be opened by name. So this # looks up the process tree for the first terminal and writes the percent there as iTerm2's user variable "context" # (OSC 1337 ; SetUserVar=context=), which TigerTTY's tab reads. A terminal that does not know the sequence # ignores it. # # It writes only when the percent changed, or once a minute, in one short write: what it sends goes between two of # Claude Code's own writes to the same terminal. # # The status line itself: with arguments, they are a status line command of your own, run with the same input, and # its output is the line (tigertty-claude-context ~/bin/my-statusline). Without, the line is "context 37%". # # tigertty-claude-context --clear takes the bar away again: name it as a SessionEnd hook, or the tab keeps the last # percent after Claude Code has quit. # # "hooks": { "SessionEnd": [ { "hooks": [ { "type": "command", "command": "/path/to/tigertty-claude-context --clear" } ] } ] } clear= if [ "$1" = "--clear" ]; then clear=1 shift fi input=$(cat) percent= if [ -z "$clear" ]; then if command -v jq >/dev/null 2>&1; then percent=$(printf '%s' "$input" | jq -r '.context_window.used_percentage // empty' 2>/dev/null) else # without jq: the last used_percentage inside context_window (rate_limits has one of its own) percent=$(printf '%s' "$input" | tr -d '\n' | sed -n 's/.*"context_window"//p' | sed 's/"rate_limits".*//' | sed -n 's/.*"used_percentage" *: *\([0-9][0-9.]*\).*/\1/p') fi percent=${percent%.*} case "$percent" in '' | *[!0-9]*) percent= ;; esac fi # the terminal Claude Code runs in: the first process above this one that has one terminal= pid=$PPID for step in 1 2 3 4 5 6; do [ -n "$pid" ] && [ "$pid" -gt 1 ] 2>/dev/null || break terminal=$(ps -o tty= -p "$pid" 2>/dev/null | tr -d ' ') case "$terminal" in '' | '?' | '??' | '-') terminal= ;; *) break ;; esac pid=$(ps -o ppid= -p "$pid" 2>/dev/null | tr -d ' ') done if [ -n "$terminal" ] && [ -w "/dev/$terminal" ] && { [ -n "$percent" ] || [ -n "$clear" ]; }; then # what was last sent to this terminal, and when state=${TMPDIR:-/tmp}/tigertty-context-$(id -u) mkdir -p "$state" 2>/dev/null && chmod 700 "$state" 2>/dev/null last=$state/$(printf '%s' "$terminal" | tr '/' '_') now=$(date +%s) sent= when=0 [ -r "$last" ] && read -r sent when < "$last" case "$when" in '' | *[!0-9]*) when=0 ;; esac if [ -n "$clear" ]; then printf '\033]1337;SetUserVar=context=\007' > "/dev/$terminal" 2>/dev/null rm -f "$last" elif [ "$sent" != "$percent" ] || [ $((now - when)) -ge 60 ]; then if command -v base64 >/dev/null 2>&1; then value=$(printf '%s' "$percent" | base64) else value=$(printf '%s' "$percent" | openssl base64) fi printf '\033]1337;SetUserVar=context=%s\007' "$value" > "/dev/$terminal" 2>/dev/null printf '%s %s\n' "$percent" "$now" > "$last" 2>/dev/null fi fi [ -n "$clear" ] && exit 0 if [ $# -gt 0 ]; then printf '%s' "$input" | "$@" elif [ -n "$percent" ]; then printf 'context %s%%\n' "$percent" fi