summaryrefslogtreecommitdiff
path: root/dotfiles/common/.local/bin/aix
blob: 0800cc1c9686a34fd8f180dabbac0db1a44a593a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
# Launch tmux session with an LLM in fzf-selected project directories

SESSION="ai"

# Dependency checks
for cmd in fzf tmux claude; do
  if ! command -v "$cmd" &>/dev/null; then
    echo "Error: $cmd is not installed" >&2
    exit 1
  fi
done

# Claude command — separated for safe editing
AI_CMD="claude"
AI_INSTRUCTIONS='Read docs/protocols.org and follow all instructions.'

# Build candidate directory list
build_candidates() {
  candidates=()
  [ -d "$HOME/.emacs.d" ] && candidates+=("~/.emacs.d")
  if [ -d "$HOME/code" ]; then
    while IFS= read -r d; do
      candidates+=("~/${d#"$HOME"/}")
    done < <(find "$HOME/code" -maxdepth 1 -mindepth 1 -type d | sort)
  fi
  if [ -d "$HOME/projects" ]; then
    while IFS= read -r d; do
      candidates+=("~/${d#"$HOME"/}")
    done < <(find "$HOME/projects" -maxdepth 1 -mindepth 1 -type d | sort)
  fi
}

# Sort windows alphabetically by name, shells last
sort_windows() {
  local windows shells projects sorted
  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')

  # 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
  i=1
  while IFS=$'\t' read -r _name wid; do
    tmux move-window -s "$wid" -t "$SESSION:$i"
    ((i++))
  done <<<"$sorted"
}

# If session exists, add new windows to it
if tmux has-session -t "$SESSION" 2>/dev/null; then
  # Get existing window names to filter duplicates
  existing=$(tmux list-windows -t "$SESSION" -F '#{window_name}')

  build_candidates

  # Filter out candidates that already have a window open
  filtered=()
  for c in "${candidates[@]}"; do
    name="$(basename "${c/#\~/$HOME}")"
    if ! echo "$existing" | grep -qxF "$name"; then
      filtered+=("$c")
    fi
  done

  if [ ${#filtered[@]} -eq 0 ]; then
    echo "All projects already have windows open."
  else
    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"

      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
        [ -z "$first_wid" ] && first_wid="$wid"
      done

      sort_windows
      tmux select-window -t "$first_wid"
    fi
  fi

  # Attach or switch to session
  if [ -n "$TMUX" ]; then
    tmux switch-client -t "$SESSION"
  else
    tmux attach-session -t "$SESSION"
  fi
  exit 0
fi

# New session: select projects and create session
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"

# 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)
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
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