Files
team/.claude/hooks/restore-title-via-tmux.sh
T
2026-04-19 21:47:08 +08:00

54 lines
1.5 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.
#!/bin/bash
# ============================================================================
# 后台脚本: /clear 后通过 tmux 自动恢复会话标题
#
# 被 save-title-on-clear.sh 以 nohup 方式启动,独立于 hook 进程运行。
# 等待新 session 就绪后发送 /rename <title>。
# ============================================================================
PANE_ID="${1:?用法: restore-title-via-tmux.sh <pane_id> <title_file>}"
TITLE_FILE="${2:?需要标题文件路径}"
# 清理函数
cleanup() {
rm -f "${TITLE_FILE}"
}
trap cleanup EXIT
[ ! -f "${TITLE_FILE}" ] && exit 1
TITLE=$(cat "${TITLE_FILE}")
# Phase 1: 等待 消失(当前 session 正在被清除)
sleep 2
for i in $(seq 1 15); do
if ! tmux capture-pane -t "${PANE_ID}" -p | tail -5 | grep -q ''; then
break
fi
sleep 1
done
# Phase 2: 等待 重新出现(新 session 就绪)
for i in $(seq 1 30); do
if tmux capture-pane -t "${PANE_ID}" -p | tail -5 | grep -q ''; then
sleep 1
# 发送 /rename 命令
tmux send-keys -t "${PANE_ID}" "/rename ${TITLE}"
sleep 1
tmux send-keys -t "${PANE_ID}" Enter
sleep 2
# 验证: 检查 ❯ 是否消失(命令已被接受)
for j in $(seq 1 5); do
if ! tmux capture-pane -t "${PANE_ID}" -p | tail -5 | grep -q ''; then
exit 0
fi
tmux send-keys -t "${PANE_ID}" Enter
sleep 2
done
exit 0
fi
sleep 1
done
# 超时退出
exit 1