#!/bin/bash
#
# Claude Code status line
# 樣式仿 oh-my-zsh robbyrussell：➜ 資料夾 git:(分支) context: N% used · 模型
#
# context 顏色（提醒避免觸發 auto compact）：
#   < 50%   綠
#   50–69%  青
#   70–79%  黃
#   >= 80%  紅（粗體）
#

input="$(cat)"

# 從 JSON 取欄位（不依賴 jq）：get <key> <value 的 regex>
get() {
  printf '%s' "$input" | grep -o "\"$1\" *: *$2" | head -n 1 | sed 's/^[^:]*: *//; s/"//g'
}

model="$(get display_name '"[^"]*"')"
dir="$(get current_dir '"[^"]*"')"
dir="${dir:-$PWD}"
used="$(get used_percentage '[0-9][0-9.]*')"

GREEN=$'\033[32m'
CYAN=$'\033[36m'
YELLOW=$'\033[33m'
RED=$'\033[1;31m'
BLUE=$'\033[1;34m'
BRED=$'\033[31m'
DIM=$'\033[2m'
RESET=$'\033[0m'

out="${GREEN}➜${RESET} ${CYAN}$(basename "$dir")${RESET}"

branch="$(git -C "$dir" --no-optional-locks branch --show-current 2>/dev/null)"
if [ -n "$branch" ]; then
  out="$out ${BLUE}git:(${BRED}${branch}${BLUE})${RESET}"
fi

if [ -n "$used" ]; then
  pct="$(printf '%.0f' "$used")"
  if   [ "$pct" -ge 80 ]; then color="$RED"
  elif [ "$pct" -ge 70 ]; then color="$YELLOW"
  elif [ "$pct" -ge 50 ]; then color="$CYAN"
  else color="$GREEN"
  fi
  out="$out ${color}context: ${pct}% used${RESET}"
else
  out="$out ${DIM}context: n/a${RESET}"
fi

[ -n "$model" ] && out="$out ${DIM}· ${model}${RESET}"

printf '%s' "$out"
