Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 80 additions & 3 deletions skills/phobos/hooks/statusline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
# Claude Code pipes session JSON on stdin and uses our stdout as the ENTIRE
# status line — statusLine is ONE command, not an array — so we render every
# badge here, including ponytail's if its flag is present.
# Must stay cheap: fires several times/second. One jq parse, no transcript reads.
# Must stay cheap: fires several times/second. One jq parse; the only transcript
# reads are size-guarded, so an unchanged transcript is never rescanned.
set -euo pipefail

in=$(cat)

# One jq pass. Unit-separator delimited: unlike tabs, \x1f isn't IFS whitespace,
# so empty fields (e.g. no transcript_path) don't shift the columns.
IFS=$'\x1f' read -r model dir effort added removed tp ctxpct rlpct < <(
IFS=$'\x1f' read -r model dir effort added removed tp ctxpct rlpct sid pdir < <(
printf '%s' "$in" | jq -r '[
(.model.display_name // .model.id // "?"),
((.workspace.current_dir // .cwd // ".") | split("/") | last | split("\\") | last),
Expand All @@ -19,7 +20,9 @@ IFS=$'\x1f' read -r model dir effort added removed tp ctxpct rlpct < <(
(.cost.total_lines_removed // 0),
(.transcript_path // ""),
(.context_window.used_percentage // -1 | floor),
(.rate_limits.five_hour.used_percentage // -1 | round)
(.rate_limits.five_hour.used_percentage // -1 | round),
(.session_id // ""),
(.workspace.project_dir // .cwd // "")
] | map(tostring) | join("")' 2>/dev/null
) || true

Expand All @@ -39,6 +42,80 @@ if [ -f "$pflag" ]; then
fi
fi

# Permission-mode badge — the Shift+Tab mode, so the status line follows it.
# It is NOT in the statusLine payload (see code.claude.com/docs/en/statusline),
# so read the transcript, which is the live source: Claude Code appends a
# `permission-mode` entry the moment the mode changes. Kept cheap per this
# file's contract — the resolved mode is cached against the transcript size, so
# an idle render costs one `wc -c` and a growing one scans only the new bytes.
if [ "${PHOBOS_STATUSLINE_MODE:-1}" != 0 ] && [ -n "${tp:-}" ] && [ -f "$tp" ]; then
permmode=; defmode=; csize=0; dirty=0
mcache="${TMPDIR:-/tmp}/phobos-mode-${sid:-nosession}"
# `wc -c` fstats rather than reading, so this stays ~1ms on a multi-MB file.
tsize=$(wc -c <"$tp" 2>/dev/null) || tsize=
tsize=$(( ${tsize:-0} + 0 )) # BSD wc pads with spaces; normalise to an int
if [ -r "$mcache" ]; then
# `|`, not a space: space is IFS whitespace, so bash collapses runs of it
# and an empty middle field would silently shift the later columns.
# Written without a trailing newline, so read reports EOF while still
# populating the fields — hence the `|| true`.
IFS='|' read -r csize permmode defmode <"$mcache" 2>/dev/null || true
case ${csize:-} in ''|*[!0-9]*) csize=0 ;; esac
fi

# The mode the session started in. The badge only earns space once you have
# switched away from it — in your configured mode Claude Code already prints
# its own indicator, so repeating it here is pure noise. Resolved once per
# session and cached: defaultMode applies at startup, so editing it later
# cannot change the mode this session is actually in.
if [ -z "${defmode:-}" ]; then
defmode=${PHOBOS_STATUSLINE_MODE_DEFAULT:-}
if [ -z "$defmode" ]; then
# Settings precedence, most specific first.
for sf in "${pdir:-.}/.claude/settings.local.json" \
"${pdir:-.}/.claude/settings.json" \
"${CLAUDE_CONFIG_DIR:-$HOME/.claude}/settings.json"; do
if [ -f "$sf" ]; then
defmode=$(jq -r '.permissions.defaultMode // empty' "$sf" 2>/dev/null) || defmode=
if [ -n "$defmode" ]; then break; fi
fi
done
fi
if [ -z "$defmode" ]; then defmode=default; fi
dirty=1
fi

if [ "$tsize" -gt 0 ] && [ "$csize" != "$tsize" ]; then
# Resume from the last known offset. With no cache — or after a rotation
# shrank the file — bootstrap from the last 1MiB rather than the whole
# thing: a full scan of a multi-MB transcript costs ~200ms, and Claude Code
# re-emits the entry every turn, so anything older is superseded shortly.
if [ "$csize" -gt 0 ] && [ "$csize" -le "$tsize" ]; then from=$(( csize + 1 ))
elif [ "$tsize" -gt 1048576 ]; then from=$(( tsize - 1048576 ))
else from=1
fi
found=$(tail -c "+$from" "$tp" 2>/dev/null \
| grep -o '"permissionMode":"[A-Za-z]*"' | tail -n1 | cut -d'"' -f4) || found=
if [ -n "${found:-}" ]; then permmode=$found; fi
dirty=1
fi
if [ "$dirty" = 1 ]; then
printf '%s|%s|%s' "$tsize" "${permmode:-}" "${defmode:-}" >"$mcache" 2>/dev/null || true
fi

# Only when you have switched away from your default. `default` has no arm of
# its own either way. Colour escalates with how much is being skipped.
if [ "${permmode:-}" != "${defmode:-}" ]; then
case "${permmode:-}" in
plan) out+=" ${blue}[⏸ plan]${rst}" ;;
acceptEdits) out+=" ${green}[⏵ accept edits]${rst}" ;;
auto) out+=" ${yellow}[⏵⏵ auto]${rst}" ;;
bypassPermissions) out+=" ${red}[⏵⏵ bypass]${rst}" ;;
dontAsk) out+=" ${red}[⏵⏵ dont ask]${rst}" ;;
esac
fi
fi

# model · dir
out+=" ${dim}${model:-?} · ${dir:-.}${rst}"

Expand Down
Loading