Files
ai-setup/home-configs/claude/hooks/switch-account-hook.sh
vitaly 7187aa6669 fix: SIGWINCH с задержкой 0.3s в фоне после exit 2
SIGWINCH до exit 2 игнорируется - claude ещё рисует блокировку.
Запускаем sleep+kill в фоне, они живут после завершения хука.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-09 21:18:29 +03:00

49 lines
1.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# UserPromptSubmit hook: перехватывает /switch-account без участия LLM
input=$(cat)
prompt=$(echo "$input" | jq -r '.user_prompt // .prompt // empty' 2>/dev/null)
# Нормализуем: убираем пробелы и слэш в начале
normalized=$(echo "$prompt" | sed 's|^[[:space:]]*/||; s|[[:space:]]*$||')
[ "$normalized" != "switch-account" ] && exit 0
# --- Переключаем аккаунт ---
ACCOUNTS_DIR="$HOME/.claude/accounts"
CREDS="$HOME/.claude/.credentials.json"
CURRENT_FILE="$ACCOUNTS_DIR/current"
mkdir -p "$ACCOUNTS_DIR"
mapfile -t accounts < <(ls "$ACCOUNTS_DIR"/*.credentials.json 2>/dev/null \
| xargs -I{} basename {} .credentials.json | sort)
if [ ${#accounts[@]} -eq 0 ]; then
echo "Аккаунты не настроены. Создай ~/.claude/accounts/<name>.credentials.json для каждого аккаунта." >&2
exit 2
fi
current=$(cat "$CURRENT_FILE" 2>/dev/null || echo "")
# Найти следующий по кругу
idx=-1
for i in "${!accounts[@]}"; do
[ "${accounts[$i]}" = "$current" ] && idx=$i && break
done
next_idx=$(( (idx + 1) % ${#accounts[@]} ))
next="${accounts[$next_idx]}"
cp "$ACCOUNTS_DIR/${next}.credentials.json" "$CREDS"
chmod 600 "$CREDS"
echo "$next" > "$CURRENT_FILE"
echo "Аккаунт: ${current:-?} -> ${next} (всего: ${#accounts[@]})" >&2
# Посылаем SIGWINCH процессу claude с задержкой в фоне (после отрисовки блокировки)
sh_pid=$PPID
claude_pid=$(awk '/PPid/{print $2}' /proc/$sh_pid/status 2>/dev/null)
[ -n "$claude_pid" ] && ( sleep 0.3 && kill -WINCH "$claude_pid" 2>/dev/null ) &
exit 2