#!/bin/bash
# Claude Code Status Line — Kainos Compact (3 lines)
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:$PATH"
input=$(cat)
# --- Progress bar [====------] ---
bar() {
local pct=${1:-0} width=10
local filled=$(( pct * width / 100 ))
local empty=$(( width - filled ))
printf "["
for ((i=0; i<filled; i++)); do printf "="; done
for ((i=0; i<empty; i++)); do printf "-"; done
printf "]"
}
# --- Comma formatting ---
fmt_num() { printf "%d" "$1" | rev | sed 's/.\{3\}/&,/g' | sed 's/,$//' | rev; }
# --- Extract data ---
model_name=$(echo "$input" | jq -r '.model.display_name // "Claude"')
model_id=$(echo "$input" | jq -r '.model.id // ""')
version=$(echo "$input" | jq -r '.version // ""')
style=$(echo "$input" | jq -r '.output_style.name // "default"')
context_used=$(echo "$input" | jq -r '.context_window.used_percentage // 0')
context_size=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
rate_5h=$(echo "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
reset_5h_at=$(echo "$input" | jq -r '.rate_limits.five_hour.resets_at // empty')
rate_7d=$(echo "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
reset_7d_at=$(echo "$input" | jq -r '.rate_limits.seven_day.resets_at // empty')
total_cost=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')
total_duration=$(echo "$input" | jq -r '.cost.total_duration_ms // 0')
total_input=$(echo "$input" | jq -r '.context_window.total_input_tokens // 0')
total_output=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0')
transcript=$(echo "$input" | jq -r '.transcript_path // empty')
# Git branch
git_branch=""
if git rev-parse --git-dir >/dev/null 2>&1; then
git_branch=$(git --no-optional-locks branch --show-current 2>/dev/null)
fi
# Model version (claude-opus-4-6 -> 4.6)
model_ver=""
if [[ "$model_id" =~ ([0-9]+)-([0-9]+)$ ]]; then
model_ver=" ${BASH_REMATCH[1]}.${BASH_REMATCH[2]}"
fi
# Session timing
session_elapsed=""
if [ -n "$transcript" ] && [ -f "$transcript" ]; then
session_start=$(stat -f %B "$transcript" 2>/dev/null)
if [ -n "$session_start" ]; then
elapsed=$(($(date +%s) - session_start))
hours=$((elapsed / 3600))
minutes=$(((elapsed % 3600) / 60))
[ $hours -gt 0 ] && session_elapsed="${hours}h${minutes}m" || session_elapsed="${minutes}m"
fi
fi
# Reset time (5h)
reset_5h_ttl=""
if [ -n "$reset_5h_at" ]; then
remaining_sec=$((reset_5h_at - $(date +%s)))
if [ $remaining_sec -gt 0 ]; then
reset_5h_ttl="$((remaining_sec/3600))h$(((remaining_sec%3600)/60))m"
fi
fi
# Reset time (7d)
reset_7d_ttl=""
if [ -n "$reset_7d_at" ]; then
remaining_sec=$((reset_7d_at - $(date +%s)))
if [ $remaining_sec -gt 0 ]; then
rd=$((remaining_sec/86400))
rh=$(((remaining_sec%86400)/3600))
reset_7d_ttl="${rd}d${rh}h"
fi
fi
# Computed values
total_tokens=$((total_input + total_output))
cost_per_hour="0.00"
tokens_per_min=0
if [ "$total_duration" -gt 0 ]; then
cost_per_hour=$(awk "BEGIN { printf \"%.2f\", $total_cost / ($total_duration / 3600000) }")
tokens_per_min=$(awk "BEGIN { printf \"%.0f\", $total_tokens / ($total_duration / 60000) }")
fi
ctx_pct=$(printf "%.0f" "$context_used")
rate_5h_pct=0
[ -n "$rate_5h" ] && rate_5h_pct=$(printf "%.0f" "$rate_5h")
rate_7d_pct=0
[ -n "$rate_7d" ] && rate_7d_pct=$(printf "%.0f" "$rate_7d")
ctx_label="200K"
[ "$context_size" -ge 1000000 ] && ctx_label="1M"
total_cost_fmt=$(printf "%.2f" "$total_cost")
# --- Build 3 lines with aligned columns ---
# All labels: 14 terminal columns (emoji=2 + spaces + Korean/text + spaces + colon + space)
# Content after label is ASCII, so ${#} = terminal width
# Widest content is line 2: "100% [==========] 200K" = 22 chars
# PAD = 22 (content area), right column starts at terminal col 14+22 = 36
PAD=22
mkpad() {
local n=$(( PAD - ${#1} ))
[ "$n" -lt 2 ] && n=2
printf "%*s" "$n" ""
}
branch="${git_branch:-detached}"
ctx_bar=$(bar "$ctx_pct")
# Left content (ASCII only)
C1="${branch}"
C2="${ctx_pct}% ${ctx_bar} ${ctx_label}"
C3="\$${total_cost_fmt} (\$${cost_per_hour}/h)"
# Right content
R1="🤖 모델 : ${model_name}${model_ver} / v${version} / ${style}"
R2=""
if [ -n "$rate_5h" ]; then
r5_bar=$(bar "$rate_5h_pct")
R2="📈 사용 : ${rate_5h_pct}% ${r5_bar}"
[ -n "$reset_5h_ttl" ] && R2="${R2} (-${reset_5h_ttl})"
fi
if [ -n "$rate_7d" ]; then
r7_bar=$(bar "$rate_7d_pct")
[ -n "$R2" ] && R2="${R2} / ${rate_7d_pct}% ${r7_bar}"
[ -n "$reset_7d_ttl" ] && R2="${R2} (-${reset_7d_ttl})"
fi
R3="📊 토큰 : $(fmt_num $total_tokens) ($(fmt_num $tokens_per_min)/min)"
[ -n "$session_elapsed" ] && R3="${R3} 🕐 세션 : ${session_elapsed}"
# --- Output ---
echo "🌿 브랜치 : ${C1}$(mkpad "$C1")${R1}"
echo "🧠 Context : ${C2}$(mkpad "$C2")${R2}"
echo "💰 비용 : ${C3}$(mkpad "$C3")${R3}"