Skip to content

Skills for Claude Code CLI such as full stack dev Cloudflare, React, Tailwind v4, and AI integrations.

feat(statusline): Add working directory + v2.1.6 percentage fields

Jez171fa66db2a7
diff --git a/tools/statusline-npm/package.json b/tools/statusline-npm/package.json
index de249e19..30a019d2 100644
--- a/tools/statusline-npm/package.json
+++ b/tools/statusline-npm/package.json
@@ -1,6 +1,6 @@
{
"name": "contextbricks",
- "version": "3.2.0",
+ "version": "3.4.0",
"description": "Git-aware statusline for Claude Code CLI with context brick visualization",
"keywords": [
"claude",
diff --git a/tools/statusline-npm/scripts/statusline.sh b/tools/statusline-npm/scripts/statusline.sh
index 45cab393..2ca35c0b 100755
--- a/tools/statusline-npm/scripts/statusline.sh
+++ b/tools/statusline-npm/scripts/statusline.sh
@@ -1,11 +1,12 @@
#!/bin/bash
# Claude Code Custom Status Line
-# v3.2.0 - Context tracking RE-ENABLED with current_usage API
-# Shows: Model | Repo:Branch [commit] message | GitHub | git status | lines changed
+# v3.4.0 - Working directory display + percentage fields (Claude Code 2.1.6+)
+# Shows: Model | Path | Repo:Branch [commit] message | GitHub | git status | lines changed
# Context bricks | percentage | duration | cost
#
-# Uses new current_usage field (Claude Code 2.0.70+) for accurate context tracking.
+# Uses new percentage fields (Claude Code 2.1.6+) for accurate context display.
+# Falls back to current_usage calculation for older versions.
# See: https://code.claude.com/docs/en/statusline#context-window-usage
# Read JSON from stdin
@@ -64,6 +65,10 @@ line1=""
# Model in brackets
line1+="\033[1;36m[$model]\033[0m "
+# Working directory (tilde-compressed)
+display_dir="${current_dir/#$HOME/\~}"
+line1+="\033[2;37m$display_dir\033[0m "
+
# Repo:Branch
if [[ -n "$repo_name" && "$repo_name" != "no-repo" ]]; then
line1+="\033[1;32m$repo_name\033[0m"
@@ -104,29 +109,40 @@ duration_min=$(((duration_ms % 3600000) / 60000))
# Get session cost (only show if > 0, for API users)
cost_usd=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')
-# Get context window data using new current_usage field (Claude Code 2.0.70+)
+# Get context window data - prefer v2.1.6+ percentage fields, fallback to calculation
total_tokens=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
-current_usage=$(echo "$input" | jq -r '.context_window.current_usage // null')
-if [[ "$current_usage" != "null" ]]; then
- # Use accurate current_usage data
- input_tokens=$(echo "$current_usage" | jq -r '.input_tokens // 0')
- cache_creation=$(echo "$current_usage" | jq -r '.cache_creation_input_tokens // 0')
- cache_read=$(echo "$current_usage" | jq -r '.cache_read_input_tokens // 0')
+# Try new percentage fields first (Claude Code 2.1.6+)
+used_pct_raw=$(echo "$input" | jq -r '.context_window.used_percentage // null')
+remaining_pct_raw=$(echo "$input" | jq -r '.context_window.remaining_percentage // null')
- # Calculate actual current context usage
- used_tokens=$((input_tokens + cache_creation + cache_read))
-else
- # Fallback: no current_usage available yet (first message or older version)
- used_tokens=0
-fi
+if [[ "$used_pct_raw" != "null" && -n "$used_pct_raw" ]]; then
+ # Use official percentage (more accurate)
+ usage_pct=${used_pct_raw%.*} # Truncate to integer for display
+ remaining_pct=${remaining_pct_raw%.*}
-# Calculate metrics
-free_tokens=$((total_tokens - used_tokens))
-if [[ $total_tokens -gt 0 ]]; then
- usage_pct=$(( (used_tokens * 100) / total_tokens ))
+ # Calculate tokens from percentages for display
+ used_tokens=$(( (total_tokens * usage_pct) / 100 ))
+ free_tokens=$(( (total_tokens * remaining_pct) / 100 ))
else
- usage_pct=0
+ # Fallback: Calculate from current_usage (Claude Code 2.0.70+)
+ current_usage=$(echo "$input" | jq -r '.context_window.current_usage // null')
+
+ if [[ "$current_usage" != "null" ]]; then
+ input_tokens=$(echo "$current_usage" | jq -r '.input_tokens // 0')
+ cache_creation=$(echo "$current_usage" | jq -r '.cache_creation_input_tokens // 0')
+ cache_read=$(echo "$current_usage" | jq -r '.cache_read_input_tokens // 0')
+ used_tokens=$((input_tokens + cache_creation + cache_read))
+ else
+ used_tokens=0
+ fi
+
+ free_tokens=$((total_tokens - used_tokens))
+ if [[ $total_tokens -gt 0 ]]; then
+ usage_pct=$(( (used_tokens * 100) / total_tokens ))
+ else
+ usage_pct=0
+ fi
fi
# Convert to 'k' format for display
diff --git a/tools/statusline/README.md b/tools/statusline/README.md
index 93ba0c61..c67a53a6 100644
--- a/tools/statusline/README.md
+++ b/tools/statusline/README.md
@@ -7,11 +7,15 @@ A custom status line for Claude Code CLI that displays real-time context trackin
## ✨ Features
### Real-Time Context Tracking
-- **Accurate context tracking** using `current_usage` API (Claude Code 2.0.70+)
+- **Official percentage fields** (Claude Code 2.1.6+) for most accurate display
+- **Fallback calculation** using `current_usage` API (Claude Code 2.0.70+)
- **Brick visualization** showing context usage at a glance
- **Session duration** - track how long you've been working
- **Session cost** - API users see spending (hidden for Max subscribers)
+### Workspace Context
+- **Working directory** (tilde-compressed, e.g., `~/Documents/project`)
+
### Git Integration
- Current **repo name and branch**
- Latest **commit hash and message**
@@ -75,9 +79,9 @@ The installer will:
## 🎨 What It Looks Like
-### Line 1: Git + Model + Changes
+### Line 1: Path + Git + Model + Changes
```
-[Sonnet 4.5] claude-skills:main [5f2ce67] Remove auth-js skill | jezweb/claude-skills *↑2 | +145/-23
+[Sonnet 4.5] ~/Documents/claude-skills claude-skills:main [5f2ce67] Remove auth-js skill | jezweb/claude-skills *↑2 | +145/-23
```
### Line 2: Context Bricks + Duration + Cost
@@ -111,9 +115,24 @@ For Max subscribers (no API cost):
## 📖 How It Works
-### Context Tracking (v3.1+)
+### Context Tracking (v3.3.0+)
+
+**Claude Code 2.1.6+** provides pre-calculated percentage fields for the most accurate display:
+```json
+{
+ "context_window": {
+ "context_window_size": 200000,
+ "used_percentage": 43.5,
+ "remaining_percentage": 56.5
+ }
+}
+```
+
+The status line uses these official percentages directly, avoiding any calculation drift.
+
+### Fallback: Token Calculation (v2.0.70+)
-**Claude Code 2.0.70+** provides accurate context data via the `current_usage` field:
+For Claude Code 2.0.70 - 2.1.5 without percentage fields, falls back to `current_usage`:
```json
{
"context_window": {
@@ -127,16 +146,14 @@ For Max subscribers (no API cost):
}
```
-The status line calculates current context as:
+Calculates as:
```
used_tokens = input_tokens + cache_creation_input_tokens + cache_read_input_tokens
```
-This provides accurate usage that properly reflects context state after compaction.
-
### Older Versions
-For Claude Code < 2.0.70 without `current_usage`, the status line shows 0% until data becomes available.
+For Claude Code < 2.0.70, the status line shows 0% until data becomes available.
### Git Information
@@ -159,7 +176,10 @@ Queries git commands in the current workspace:
# Check if jq is installed
which jq
-# Test with current_usage data (2.0.70+)
+# Test with percentage fields (2.1.6+)
+echo '{"context_window":{"context_window_size":200000,"used_percentage":43.5,"remaining_percentage":56.5},"model":{"display_name":"Sonnet 4.5"},"workspace":{"current_dir":"'"$PWD"'"},"cost":{"total_duration_ms":300000}}' | ~/.claude/statusline.sh
+
+# Test with current_usage data (2.0.70 - 2.1.5)
echo '{"context_window":{"context_window_size":200000,"current_usage":{"input_tokens":50000,"cache_creation_input_tokens":3000,"cache_read_input_tokens":2000}},"model":{"display_name":"Sonnet 4.5"},"workspace":{"current_dir":"'"$PWD"'"},"cost":{"total_duration_ms":300000}}' | ~/.claude/statusline.sh
```
@@ -231,6 +251,12 @@ rm ~/.claude/statusline.sh
## 📋 Changelog
+### v3.4.0 (2026-01-13)
+- **Working directory display** - Shows tilde-compressed path (e.g., `~/Documents/project`)
+- **Official percentage fields** - Uses `used_percentage` and `remaining_percentage` (Claude Code 2.1.6+)
+- **More accurate context display** - No calculation drift, uses official values directly
+- **Backwards compatible** - Falls back to token calculation for older versions
+
### v3.1.0 (2025-12-16)
- **Re-enabled context tracking** - Uses new `current_usage` API (Claude Code 2.0.70+)
- **Simplified code** - Removed complex compaction detection (~100 lines removed)
diff --git a/tools/statusline/statusline.sh b/tools/statusline/statusline.sh
index 45cab393..2ca35c0b 100755
--- a/tools/statusline/statusline.sh
+++ b/tools/statusline/statusline.sh
@@ -1,11 +1,12 @@
#!/bin/bash
# Claude Code Custom Status Line
-# v3.2.0 - Context tracking RE-ENABLED with current_usage API
-# Shows: Model | Repo:Branch [commit] message | GitHub | git status | lines changed
+# v3.4.0 - Working directory display + percentage fields (Claude Code 2.1.6+)
+# Shows: Model | Path | Repo:Branch [commit] message | GitHub | git status | lines changed
# Context bricks | percentage | duration | cost
#
-# Uses new current_usage field (Claude Code 2.0.70+) for accurate context tracking.
+# Uses new percentage fields (Claude Code 2.1.6+) for accurate context display.
+# Falls back to current_usage calculation for older versions.
# See: https://code.claude.com/docs/en/statusline#context-window-usage
# Read JSON from stdin
@@ -64,6 +65,10 @@ line1=""
# Model in brackets
line1+="\033[1;36m[$model]\033[0m "
+# Working directory (tilde-compressed)
+display_dir="${current_dir/#$HOME/\~}"
+line1+="\033[2;37m$display_dir\033[0m "
+
# Repo:Branch
if [[ -n "$repo_name" && "$repo_name" != "no-repo" ]]; then
line1+="\033[1;32m$repo_name\033[0m"
@@ -104,29 +109,40 @@ duration_min=$(((duration_ms % 3600000) / 60000))
# Get session cost (only show if > 0, for API users)
cost_usd=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')
-# Get context window data using new current_usage field (Claude Code 2.0.70+)
+# Get context window data - prefer v2.1.6+ percentage fields, fallback to calculation
total_tokens=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
-current_usage=$(echo "$input" | jq -r '.context_window.current_usage // null')
-if [[ "$current_usage" != "null" ]]; then
- # Use accurate current_usage data
- input_tokens=$(echo "$current_usage" | jq -r '.input_tokens // 0')
- cache_creation=$(echo "$current_usage" | jq -r '.cache_creation_input_tokens // 0')
- cache_read=$(echo "$current_usage" | jq -r '.cache_read_input_tokens // 0')
+# Try new percentage fields first (Claude Code 2.1.6+)
+used_pct_raw=$(echo "$input" | jq -r '.context_window.used_percentage // null')
+remaining_pct_raw=$(echo "$input" | jq -r '.context_window.remaining_percentage // null')
- # Calculate actual current context usage
- used_tokens=$((input_tokens + cache_creation + cache_read))
-else
- # Fallback: no current_usage available yet (first message or older version)
- used_tokens=0
-fi
+if [[ "$used_pct_raw" != "null" && -n "$used_pct_raw" ]]; then
+ # Use official percentage (more accurate)
+ usage_pct=${used_pct_raw%.*} # Truncate to integer for display
+ remaining_pct=${remaining_pct_raw%.*}
-# Calculate metrics
-free_tokens=$((total_tokens - used_tokens))
-if [[ $total_tokens -gt 0 ]]; then
- usage_pct=$(( (used_tokens * 100) / total_tokens ))
+ # Calculate tokens from percentages for display
+ used_tokens=$(( (total_tokens * usage_pct) / 100 ))
+ free_tokens=$(( (total_tokens * remaining_pct) / 100 ))
else
- usage_pct=0
+ # Fallback: Calculate from current_usage (Claude Code 2.0.70+)
+ current_usage=$(echo "$input" | jq -r '.context_window.current_usage // null')
+
+ if [[ "$current_usage" != "null" ]]; then
+ input_tokens=$(echo "$current_usage" | jq -r '.input_tokens // 0')
+ cache_creation=$(echo "$current_usage" | jq -r '.cache_creation_input_tokens // 0')
+ cache_read=$(echo "$current_usage" | jq -r '.cache_read_input_tokens // 0')
+ used_tokens=$((input_tokens + cache_creation + cache_read))
+ else
+ used_tokens=0
+ fi
+
+ free_tokens=$((total_tokens - used_tokens))
+ if [[ $total_tokens -gt 0 ]]; then
+ usage_pct=$(( (used_tokens * 100) / total_tokens ))
+ else
+ usage_pct=0
+ fi
fi
# Convert to 'k' format for display