Files
ai-setup/home-configs/claude/hooks/switch-account-hook.sh
vitaly 986abf5101 fix: resize через TIOCSWINSZ ioctl на TTY вместо kill -WINCH
SIGWINCH напрямую игнорируется. TIOCSWINSZ на TTY claude посылает
SIGWINCH через kernel к foreground process group.

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

62 lines
2.0 KiB
Bash
Executable File

#!/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
# Тригерим resize через TIOCSWINSZ на TTY claude (надёжнее чем kill -WINCH)
sh_pid=$PPID
claude_pid=$(awk '/PPid/{print $2}' /proc/$sh_pid/status 2>/dev/null)
if [ -n "$claude_pid" ]; then
tty_dev=$(ps -o tty= -p "$claude_pid" 2>/dev/null | tr -d ' ')
if [ -n "$tty_dev" ] && [ "$tty_dev" != "?" ]; then
( sleep 0.3 && python3 -c "
import fcntl, termios, struct, sys
try:
fd = open('/dev/$tty_dev', 'rb+', buffering=0)
ws = fcntl.ioctl(fd, termios.TIOCGWINSZ, bytes(8))
fcntl.ioctl(fd, termios.TIOCSWINSZ, ws)
fd.close()
except: pass
" ) &
fi
fi
exit 2