#!/usr/bin/env bash KLAUS_THEME="${KLAUS_THEME:-dark}" KLAUS_GIT_CACHE_TTL="${KLAUS_GIT_CACHE_TTL:-5}" KLAUS_USAGE_CACHE_TTL="${KLAUS_USAGE_CACHE_TTL:-60}" BAR_LABEL_WIDTH=3 mkdir -p /tmp/klaus RST='\033[0m' if [[ "$KLAUS_THEME" == "light" ]]; then RED='\033[38;2;157;0;6m' GREEN='\033[38;2;121;116;14m' EMPTY_FG='\033[38;2;251;241;199m' MODEL_BG='\033[48;2;143;63;113m' MODEL_FG='\033[38;2;251;241;199m' IN_BG='\033[48;2;7;102;120m' IN_FG='\033[38;2;251;241;199m' OUT_BG='\033[48;2;66;123;88m' OUT_FG='\033[38;2;251;241;199m' CTX_BG='\033[48;2;181;118;20m' CTX_FG='\033[38;2;251;241;199m' DAY_BG='\033[48;2;121;116;14m' DAY_FG='\033[38;2;251;241;199m' WK_BG='\033[48;2;157;0;6m' WK_FG='\033[38;2;251;241;199m' REPO_BG='\033[48;2;143;63;113m' REPO_FG='\033[38;2;251;241;199m' BRANCH_BG='\033[48;2;66;123;88m' BRANCH_FG='\033[38;2;251;241;199m' DIFF_BG='\033[48;2;60;56;54m' AGENT_BG='\033[48;2;143;63;113m' AGENT_FG='\033[38;2;251;241;199m' else RED='\033[38;2;204;36;29m' GREEN='\033[38;2;152;151;26m' EMPTY_FG='\033[38;2;235;219;178m' MODEL_BG='\033[48;2;177;98;134m' MODEL_FG='\033[38;2;40;40;40m' IN_BG='\033[48;2;69;133;136m' IN_FG='\033[38;2;40;40;40m' OUT_BG='\033[48;2;104;157;106m' OUT_FG='\033[38;2;40;40;40m' CTX_BG='\033[48;2;215;153;33m' CTX_FG='\033[38;2;40;40;40m' DAY_BG='\033[48;2;152;151;26m' DAY_FG='\033[38;2;40;40;40m' WK_BG='\033[48;2;204;36;29m' WK_FG='\033[38;2;40;40;40m' REPO_BG='\033[48;2;177;98;134m' REPO_FG='\033[38;2;40;40;40m' BRANCH_BG='\033[48;2;104;157;106m' BRANCH_FG='\033[38;2;40;40;40m' DIFF_BG='\033[48;2;80;73;69m' AGENT_BG='\033[48;2;177;98;134m' AGENT_FG='\033[38;2;40;40;40m' fi JSON="$(cat)" PARSED=$(echo "$JSON" | jq -r ' (.model.display_name // ""), (.workspace.current_dir // ""), (.context_window.used_percentage // 0), (.context_window.total_input_tokens // 0), (.context_window.total_output_tokens // 0), (.cost.total_lines_added // 0), (.cost.total_lines_removed // 0), (.agent.name // "") ') { read -r MODEL read -r DIR read -r PCT read -r IN_TOKENS read -r OUT_TOKENS read -r LINES_ADD read -r LINES_DEL read -r AGENT_NAME } <<<"$PARSED" get_usage() { local cache_file="/tmp/klaus/usage" local now=$(date +%s) if [[ -f "$cache_file" ]]; then local mtime if [[ "$(uname)" == "Darwin" ]]; then mtime=$(stat -f %m "$cache_file" 2>/dev/null || echo 0) else mtime=$(stat -c %Y "$cache_file" 2>/dev/null || echo 0) fi if [[ $((now - mtime)) -lt "$KLAUS_USAGE_CACHE_TTL" ]]; then cat "$cache_file" return fi fi local token token=$(security find-generic-password -s "Claude Code-credentials" -w 2>/dev/null | jq -r '.claudeAiOauth.accessToken // empty' 2>/dev/null) if [[ -z "$token" ]]; then echo "0|0" return fi local resp resp=$(curl -sf --max-time 3 \ -H "Authorization: Bearer $token" \ -H "anthropic-beta: oauth-2025-04-20" \ "https://api.anthropic.com/api/oauth/usage" 2>/dev/null) if [[ -z "$resp" ]] || echo "$resp" | jq -e '.error' &>/dev/null; then # fall back to stale cache if available [[ -f "$cache_file" ]] && cat "$cache_file" && return echo "0|0" return fi local result result=$(echo "$resp" | jq -r ' ((.five_hour.utilization // 0) | round), ((.seven_day.utilization // 0) | round) ' 2>/dev/null | paste -sd'|' -) if [[ -z "$result" || "$result" == "|" ]]; then [[ -f "$cache_file" ]] && cat "$cache_file" && return echo "0|0" return fi echo "$result" >"$cache_file" echo "$result" } USAGE_DATA=$(get_usage) IFS='|' read -r FIVE_HOUR_PCT SEVEN_DAY_PCT <<<"$USAGE_DATA" format_tokens() { echo "$1" | awk '{ if ($1 >= 1000000) printf "%.1fM", $1/1000000 else if ($1 >= 1000) printf "%.0fk", $1/1000 else printf "%s", $1 }' } osc_link() { printf '\033]8;;%s\033\\%s\033]8;;\033\\' "$1" "$2" } progress_bar() { local pct="$1" label="$2" bar_width="${3:-8}" bg="$4" fg="$5" local pct_int=${pct%.*} pct_int=${pct_int:-0} [[ "$pct_int" -gt 100 ]] && pct_int=100 local filled=$((pct_int * bar_width / 100)) local empty=$((bar_width - filled)) local filled_bar=$(printf "%${filled}s" | tr ' ' '▓') local empty_bar=$(printf "%${empty}s" | tr ' ' '░') local right_len=$((bar_width + 7)) local label_pad=$((W - right_len - 1)) [[ "$label_pad" -lt ${#label} ]] && label_pad=${#label} printf "${bg}${fg} %-${label_pad}s %s${EMPTY_FG}%s${fg} %3d%% ${RST}" "$label" "$filled_bar" "$empty_bar" "$pct_int" } strip_ansi() { printf '%s' "$1" | sed $'s/\033\\[[0-9;]*m//g' | sed $'s/\033\\]8;;[^\033]*\033\\\\//g' } pad_line() { local content="$1" bg="$2" fg="$3" local visible=$(strip_ansi "$content") local vlen=${#visible} local pad=$((W - vlen)) [[ "$pad" -lt 0 ]] && pad=0 local padding=$(printf "%${pad}s" "") printf '%s%b%s%b' "$content" "${bg}${fg}" "$padding" "$RST" } get_git_info() { local dir="$1" [[ -z "$dir" ]] && return local cache_file="/tmp/klaus/git" local now=$(date +%s) if [[ -f "$cache_file" ]]; then local mtime if [[ "$(uname)" == "Darwin" ]]; then mtime=$(stat -f %m "$cache_file" 2>/dev/null || echo 0) else mtime=$(stat -c %Y "$cache_file" 2>/dev/null || echo 0) fi if [[ $((now - mtime)) -lt "$KLAUS_GIT_CACHE_TTL" ]]; then cat "$cache_file" return fi fi local result="" if git -C "$dir" rev-parse --is-inside-work-tree &>/dev/null; then local branch=$(git -C "$dir" symbolic-ref --short HEAD 2>/dev/null || git -C "$dir" rev-parse --short HEAD 2>/dev/null) local staged=$(git -C "$dir" diff --cached --numstat 2>/dev/null | wc -l | tr -d ' ') local modified=$(git -C "$dir" diff --numstat 2>/dev/null | wc -l | tr -d ' ') local link_part="" local remote_url=$(git -C "$dir" remote get-url origin 2>/dev/null) if [[ -n "$remote_url" ]]; then local repo_name=$(echo "$remote_url" | sed -E 's#\.git$##; s#.*[:/]([^/]+/[^/]+)$#\1#') local web_url=$(echo "$remote_url" | sed -E 's#^git@([^:]+):#https://\1/#; s#\.git$##') link_part="${repo_name}|${web_url}" fi result="${branch}|${staged}|${modified}|${link_part}" fi echo "$result" >"$cache_file" echo "$result" } IN_FMT="$(format_tokens "$IN_TOKENS")" OUT_FMT="$(format_tokens "$OUT_TOKENS")" GIT_INFO=$(get_git_info "$DIR") branch_text="" diff_text="" diff_len=0 repo_label="" if [[ -n "$GIT_INFO" ]]; then IFS='|' read -r BRANCH STAGED MODIFIED REPO_NAME WEB_URL <<<"$GIT_INFO" branch_text=" ${BRANCH}" [[ "$STAGED" -gt 0 ]] && branch_text+=" +${STAGED}" [[ "$MODIFIED" -gt 0 ]] && branch_text+=" ~${MODIFIED}" if [[ "$LINES_ADD" -gt 0 || "$LINES_DEL" -gt 0 ]]; then diff_text=" +${LINES_ADD} -${LINES_DEL} " diff_len=${#diff_text} fi repo_label="${REPO_NAME:-$(basename "$DIR")}" fi model_min=$((${#MODEL} + ${#IN_FMT} + ${#OUT_FMT} + 8)) bar_min=$((BAR_LABEL_WIDTH + 8 + 4)) if [[ -n "$GIT_INFO" ]]; then git_min=$((${#repo_label} + 2 + ${#branch_text} + 1 + diff_len)) elif [[ -n "$DIR" ]]; then dn="$(basename "$DIR")" git_min=$((${#dn} + 2)) else git_min=0 fi agent_min=0 [[ -n "$AGENT_NAME" ]] && agent_min=$((${#AGENT_NAME} + 2)) W=$model_min [[ "$bar_min" -gt "$W" ]] && W=$bar_min [[ "$git_min" -gt "$W" ]] && W=$git_min [[ "$agent_min" -gt "$W" ]] && W=$agent_min # fill space with bar (label 3 chars + 8 fixed chars) BAR_WIDTH=$((W - 11)) [[ "$BAR_WIDTH" -lt 4 ]] && BAR_WIDTH=4 tokens_len=$((${#IN_FMT} + ${#OUT_FMT} + 6)) model_pad=$((W - tokens_len - 1)) [[ "$model_pad" -lt $((${#MODEL} + 1)) ]] && model_pad=$((${#MODEL} + 1)) line1="$(printf "${MODEL_BG}${MODEL_FG} %-${model_pad}s${RST}" "$MODEL")" line1+="$(printf "${IN_BG}${IN_FG} ↓%s ${RST}${OUT_BG}${OUT_FG} ↑%s ${RST}" "$IN_FMT" "$OUT_FMT")" line2="" if [[ -n "$GIT_INFO" ]]; then right_len=$((${#branch_text} + 1 + diff_len)) repo_pad=$((W - right_len - 1)) [[ "$repo_pad" -lt $((${#repo_label} + 1)) ]] && repo_pad=$((${#repo_label} + 1)) if [[ -n "$WEB_URL" ]]; then pad_spaces=$((repo_pad - ${#repo_label})) [[ "$pad_spaces" -lt 0 ]] && pad_spaces=0 line2+="$(printf "${REPO_BG}${REPO_FG} %s%${pad_spaces}s${RST}" "$(osc_link "$WEB_URL" "$repo_label")" "")" else line2+="$(printf "${REPO_BG}${REPO_FG} %-${repo_pad}s${RST}" "$repo_label")" fi line2+="$(printf "${BRANCH_BG}${BRANCH_FG}%s ${RST}" "$branch_text")" if [[ -n "$diff_text" ]]; then line2+="$(printf "${DIFF_BG}${GREEN} +%s ${RED}-%s ${RST}" "$LINES_ADD" "$LINES_DEL")" fi elif [[ -n "$DIR" ]]; then dir_name="$(basename "$DIR")" dir_pad=$((W - 1)) [[ "$dir_pad" -lt $((${#dir_name} + 1)) ]] && dir_pad=$((${#dir_name} + 1)) line2+="$(printf "${REPO_BG}${REPO_FG} %-${dir_pad}s${RST}" "$dir_name")" fi if [[ -n "$AGENT_NAME" ]]; then agent_label="$(echo "$AGENT_NAME" | tr '[:lower:]' '[:upper:]')" agent_pad=$((W - 1)) [[ "$agent_pad" -lt $((${#agent_label} + 1)) ]] && agent_pad=$((${#agent_label} + 1)) printf '%b\n' "$(printf "${AGENT_BG}${AGENT_FG} %-${agent_pad}s${RST}" "$agent_label")" fi printf '%b\n' "$line1" printf '%b\n' "$(progress_bar "$PCT" "ctx" "$BAR_WIDTH" "$CTX_BG" "$CTX_FG")" printf '%b\n' "$(progress_bar "$FIVE_HOUR_PCT" "5h" "$BAR_WIDTH" "$DAY_BG" "$DAY_FG")" printf '%b\n' "$(progress_bar "$SEVEN_DAY_PCT" "7d" "$BAR_WIDTH" "$WK_BG" "$WK_FG")" printf '%b\n' "$line2"