diff options
Diffstat (limited to 'dotfiles/common')
| -rwxr-xr-x | dotfiles/common/.local/bin/aix | 97 |
1 files changed, 62 insertions, 35 deletions
diff --git a/dotfiles/common/.local/bin/aix b/dotfiles/common/.local/bin/aix index 0800cc1..a421139 100755 --- a/dotfiles/common/.local/bin/aix +++ b/dotfiles/common/.local/bin/aix @@ -3,6 +3,25 @@ SESSION="ai" +# Switch to or attach to the session +attach_session() { + if [ -n "$TMUX" ]; then + tmux switch-client -t "$SESSION" + else + tmux attach-session -t "$SESSION" + fi +} + +# Attach directly to existing session +if [ "$1" = "--attach" ]; then + if ! tmux has-session -t "$SESSION" 2>/dev/null; then + echo "No $SESSION session to attach to." >&2 + exit 1 + fi + attach_session + exit 0 +fi + # Dependency checks for cmd in fzf tmux claude; do if ! command -v "$cmd" &>/dev/null; then @@ -15,6 +34,23 @@ done AI_CMD="claude" AI_INSTRUCTIONS='Read docs/protocols.org and follow all instructions.' +# Create a window in the session and launch claude; prints window ID +create_window() { + local dir="$1" name="$2" wid + wid=$(tmux new-window -t "$SESSION" -n "$name" -c "$dir" -P -F '#{window_id}') + sleep 0.1 + tmux send-keys -t "$wid" "$AI_CMD \"$AI_INSTRUCTIONS\"" Enter + echo "$wid" +} + +# Read fzf selections into the 'selected' array +read_selections() { + selected=() + while IFS= read -r line; do + selected+=("$line") + done <<<"$1" +} + # Build candidate directory list build_candidates() { candidates=() @@ -33,25 +69,35 @@ build_candidates() { # Sort windows alphabetically by name, shells last sort_windows() { - local windows shells projects sorted + local windows shells projects windows=$(tmux list-windows -t "$SESSION" -F '#{window_name}'$'\t''#{window_id}') shells=$(echo "$windows" | grep -iE '^(bash|zsh|fish|sh)'$'\t' | sort -t$'\t' -k1,1f) projects=$(echo "$windows" | grep -viE '^(bash|zsh|fish|sh)'$'\t' | sort -t$'\t' -k1,1f) - sorted=$(printf '%s\n' "$projects" "$shells" | sed '/^$/d') + local all + all=$(printf '%s\n' "$shells" "$projects" | sed '/^$/d') # Move all to high temp indices to avoid collisions local i=900 while IFS=$'\t' read -r _name wid; do tmux move-window -s "$wid" -t "$SESSION:$i" ((i++)) - done <<<"$sorted" - - # Move to final sequential positions + done <<<"$all" + + # Place shells at 0, projects from 1 + i=0 + if [ -n "$shells" ]; then + while IFS=$'\t' read -r _name wid; do + tmux move-window -s "$wid" -t "$SESSION:$i" + ((i++)) + done <<<"$shells" + fi i=1 - while IFS=$'\t' read -r _name wid; do - tmux move-window -s "$wid" -t "$SESSION:$i" - ((i++)) - done <<<"$sorted" + if [ -n "$projects" ]; then + while IFS=$'\t' read -r _name wid; do + tmux move-window -s "$wid" -t "$SESSION:$i" + ((i++)) + done <<<"$projects" + fi } # If session exists, add new windows to it @@ -76,18 +122,13 @@ if tmux has-session -t "$SESSION" 2>/dev/null; then selections=$(printf '%s\n' "${filtered[@]}" | fzf --multi --height=70% --reverse) if [ -n "$selections" ]; then - selected=() - while IFS= read -r line; do - selected+=("$line") - done <<<"$selections" + read_selections "$selections" first_wid="" for entry in "${selected[@]}"; do dir="${entry/#\~/$HOME}" name="$(basename "$dir")" - wid=$(tmux new-window -t "$SESSION" -n "$name" -c "$dir" -P -F '#{window_id}') - sleep 0.1 - tmux send-keys -t "$wid" "$AI_CMD \"$AI_INSTRUCTIONS\"" Enter + wid=$(create_window "$dir" "$name") [ -z "$first_wid" ] && first_wid="$wid" done @@ -96,12 +137,7 @@ if tmux has-session -t "$SESSION" 2>/dev/null; then fi fi - # Attach or switch to session - if [ -n "$TMUX" ]; then - tmux switch-client -t "$SESSION" - else - tmux attach-session -t "$SESSION" - fi + attach_session exit 0 fi @@ -111,32 +147,23 @@ build_candidates selections=$(printf '%s\n' "${candidates[@]}" | fzf --multi --height=70% --reverse) [ -z "$selections" ] && exit 0 -selected=() -while IFS= read -r line; do - selected+=("$line") -done <<<"$selections" +read_selections "$selections" # Create session with first selection first="${selected[0]}" dir="${first/#\~/$HOME}" name="$(basename "$dir")" -tmux new-session -d -s "$SESSION" -n "$name" -c "$dir" -P -F '#{window_id}' > /dev/null -first_wid=$(tmux list-windows -t "$SESSION" -F '#{window_id}' | head -1) +first_wid=$(tmux new-session -d -s "$SESSION" -n "$name" -c "$dir" -P -F '#{window_id}') tmux send-keys -t "$first_wid" "$AI_CMD \"$AI_INSTRUCTIONS\"" Enter # Create remaining windows for entry in "${selected[@]:1}"; do dir="${entry/#\~/$HOME}" name="$(basename "$dir")" - wid=$(tmux new-window -t "$SESSION" -n "$name" -c "$dir" -P -F '#{window_id}') - tmux send-keys -t "$wid" "$AI_CMD \"$AI_INSTRUCTIONS\"" Enter + create_window "$dir" "$name" > /dev/null done # Sort windows and select first sort_windows tmux select-window -t "$SESSION:1" -if [ -n "$TMUX" ]; then - tmux switch-client -t "$SESSION" -else - tmux attach-session -t "$SESSION" -fi +attach_session |
