diff --git a/README.md b/README.md index 5f0a86e..42756fd 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ A custom status line for [Claude Code](https://claude.com/claude-code) that disp Usage percentages are color-coded: green (<50%) → yellow (≥50%) → orange (≥70%) → red (≥90%). +Pace compares usage with how much of the window has elapsed: `5h 30%(20%)` means usage is running 20% ahead of the clock (on track to hit the limit before the reset), and `(-15%)` means 15% behind it. It is omitted when it can't be computed, e.g. right at the start of a window. + ## Installation Ask Claude Code: diff --git a/statusline.ps1 b/statusline.ps1 index 3f82345..a72ed0c 100644 --- a/statusline.ps1 +++ b/statusline.ps1 @@ -50,6 +50,27 @@ function Get-UsageColor([int]$pct) { else { return $green } } +# Pace vs the window: "(23%)" = used 23% faster than time has elapsed toward +# the reset, "(-2%)" = 2% slower. Empty when undefined: usage outside 0..100, +# reset not in (now, now + window], or no time elapsed yet. +# $reset is a Unix epoch, an ISO 8601 string, or a DateTime (PS7's +# ConvertFrom-Json turns ISO strings into DateTime). +function Format-PaceSuffix([object]$usedPct, [object]$reset, [long]$windowSeconds) { + if ($null -eq $usedPct -or $null -eq $reset -or "$reset" -eq "null" -or "$reset" -eq "") { return "" } + try { + if ($reset -is [datetime]) { $resetEpoch = ([DateTimeOffset]$reset).ToUnixTimeSeconds() } + elseif ("$reset" -match '^\d+$') { $resetEpoch = [double]"$reset" } + else { $resetEpoch = [DateTimeOffset]::Parse("$reset").ToUnixTimeSeconds() } + $used = [double]$usedPct + $left = $resetEpoch - [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() + if ($used -lt 0 -or $used -gt 100 -or $left -le 0 -or $left -gt $windowSeconds) { return "" } + $elapsed = ($windowSeconds - $left) / $windowSeconds + if ($elapsed -le 0) { return "" } + $pace = [math]::Round(($used / 100 / $elapsed - 1) * 100) + return "(" + $pace.ToString([Globalization.CultureInfo]::InvariantCulture) + "%)" + } catch { return "" } +} + # Null coalescing helper for PowerShell 5 compatibility (?? is PS7+ only) function Coalesce($value, $default) { if ($null -ne $value) { return $value } else { return $default } @@ -375,7 +396,7 @@ if ($effectiveBuiltin) { $fiveHourColor = Get-UsageColor $fiveHourPct $out += "${sep}${white}5h${reset} ${fiveHourColor}${fiveHourPct}%${reset}" $fiveHourReset = Format-EpochResetTime $builtinFiveHourReset "time" - if ($fiveHourReset) { $out += " ${dim}@${fiveHourReset}${reset}" } + if ($fiveHourReset) { $out += (Format-PaceSuffix $builtinFiveHourPct $builtinFiveHourReset 18000) + " ${dim}@${fiveHourReset}${reset}" } } if ($null -ne $builtinSevenDayPct) { @@ -383,7 +404,7 @@ if ($effectiveBuiltin) { $sevenDayColor = Get-UsageColor $sevenDayPct $out += "${sep}${white}7d${reset} ${sevenDayColor}${sevenDayPct}%${reset}" $sevenDayReset = Format-EpochResetTime $builtinSevenDayReset "datetime" - if ($sevenDayReset) { $out += " ${dim}@${sevenDayReset}${reset}" } + if ($sevenDayReset) { $out += (Format-PaceSuffix $builtinSevenDayPct $builtinSevenDayReset 604800) + " ${dim}@${sevenDayReset}${reset}" } } # Render extra_usage from API cache (stdin rate_limits doesn't expose it) @@ -426,7 +447,7 @@ if ($effectiveBuiltin) { $fiveHourColor = Get-UsageColor $fiveHourPct $out += "${sep}${white}5h${reset} ${fiveHourColor}${fiveHourPct}%${reset}" - if ($fiveHourReset) { $out += " ${dim}@${fiveHourReset}${reset}" } + if ($fiveHourReset) { $out += (Format-PaceSuffix $parsedUsage.five_hour.utilization $fiveHourResetIso 18000) + " ${dim}@${fiveHourReset}${reset}" } # ---- 7-day (weekly) ---- $sevenDayPct = [math]::Floor([double](Coalesce $parsedUsage.seven_day.utilization 0)) @@ -435,7 +456,7 @@ if ($effectiveBuiltin) { $sevenDayColor = Get-UsageColor $sevenDayPct $out += "${sep}${white}7d${reset} ${sevenDayColor}${sevenDayPct}%${reset}" - if ($sevenDayReset) { $out += " ${dim}@${sevenDayReset}${reset}" } + if ($sevenDayReset) { $out += (Format-PaceSuffix $parsedUsage.seven_day.utilization $sevenDayResetIso 604800) + " ${dim}@${sevenDayReset}${reset}" } $out += Format-ExtraUsage $parsedUsage } catch {} diff --git a/statusline.sh b/statusline.sh index 31f75fe..d9322f8 100755 --- a/statusline.sh +++ b/statusline.sh @@ -52,6 +52,21 @@ usage_color() { fi } +# Pace vs the window: "(23%)" = used 23% faster than time has elapsed toward +# the reset, "(-2%)" = 2% slower. Prints nothing when it is undefined: usage +# outside 0..100, reset not in (now, now + window], or no time elapsed yet. +# Args: used_pct reset_epoch window_seconds +pace_suffix() { + [ -n "$1" ] && [ -n "$2" ] && [ "$2" != "null" ] || return 0 + awk -v used="$1" -v reset="$2" -v window="$3" -v now="$(date +%s)" 'BEGIN { + left = reset - now + if (used < 0 || used > 100 || left <= 0 || left > window) exit + elapsed = (window - left) / window + if (elapsed <= 0) exit + printf "(%.0f%%)", (used / 100 / elapsed - 1) * 100 + }' +} + # Resolve config directory: CLAUDE_CONFIG_DIR (set by alias) or default ~/.claude claude_config_dir="${CLAUDE_CONFIG_DIR:-$HOME/.claude}" @@ -402,7 +417,7 @@ if $effective_builtin; then out+="${sep}${white}5h${reset} ${five_hour_color}${five_hour_pct}%${reset}" if [ -n "$builtin_five_hour_reset" ] && [ "$builtin_five_hour_reset" != "null" ]; then five_hour_reset=$(date -j -r "$builtin_five_hour_reset" +"%H:%M" 2>/dev/null || date -d "@$builtin_five_hour_reset" +"%H:%M" 2>/dev/null) - [ -n "$five_hour_reset" ] && out+=" ${dim}@${five_hour_reset}${reset}" + [ -n "$five_hour_reset" ] && out+="$(pace_suffix "$builtin_five_hour_pct" "$builtin_five_hour_reset" 18000) ${dim}@${five_hour_reset}${reset}" fi fi @@ -412,7 +427,7 @@ if $effective_builtin; then out+="${sep}${white}7d${reset} ${seven_day_color}${seven_day_pct}%${reset}" if [ -n "$builtin_seven_day_reset" ] && [ "$builtin_seven_day_reset" != "null" ]; then seven_day_reset=$(date -j -r "$builtin_seven_day_reset" +"%a %b %-d, %H:%M" 2>/dev/null || date -d "@$builtin_seven_day_reset" +"%a %b %-d, %H:%M" 2>/dev/null) - [ -n "$seven_day_reset" ] && out+=" ${dim}@${seven_day_reset}${reset}" + [ -n "$seven_day_reset" ] && out+="$(pace_suffix "$builtin_seven_day_pct" "$builtin_seven_day_reset" 604800) ${dim}@${seven_day_reset}${reset}" fi fi @@ -449,7 +464,7 @@ elif [ -n "$usage_data" ] && echo "$usage_data" | jq -e '.five_hour' >/dev/null five_hour_color=$(usage_color "$five_hour_pct") out+="${sep}${white}5h${reset} ${five_hour_color}${five_hour_pct}%${reset}" - [ -n "$five_hour_reset" ] && out+=" ${dim}@${five_hour_reset}${reset}" + [ -n "$five_hour_reset" ] && out+="$(pace_suffix "$five_hour_pct" "$(iso_to_epoch "$five_hour_reset_iso")" 18000) ${dim}@${five_hour_reset}${reset}" # ---- 7-day (weekly) ---- seven_day_pct=$(echo "$usage_data" | jq -r '.seven_day.utilization // 0' | awk '{printf "%.0f", $1}') @@ -458,7 +473,7 @@ elif [ -n "$usage_data" ] && echo "$usage_data" | jq -e '.five_hour' >/dev/null seven_day_color=$(usage_color "$seven_day_pct") out+="${sep}${white}7d${reset} ${seven_day_color}${seven_day_pct}%${reset}" - [ -n "$seven_day_reset" ] && out+=" ${dim}@${seven_day_reset}${reset}" + [ -n "$seven_day_reset" ] && out+="$(pace_suffix "$seven_day_pct" "$(iso_to_epoch "$seven_day_reset_iso")" 604800) ${dim}@${seven_day_reset}${reset}" render_extra_usage "$usage_data" else