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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#!/bin/bash
# Fake tmux for testing tmux-util.
#
# State file: $FAKE_TMUX_DIR/sessions.txt
# One line per session, space-separated:
# <name> <attached> <pids_csv> [<activity_epoch> [<windows> [<cwd>]]]
# pids_csv is a comma-separated list of pane PIDs (or '-' for none)
# cwd cannot contain spaces in test data.
#
# Log file: $FAKE_TMUX_DIR/calls.log
# Each invocation appended as a single line: tmux <args>
: "${FAKE_TMUX_DIR:?FAKE_TMUX_DIR must be set}"
STATE="$FAKE_TMUX_DIR/sessions.txt"
LOG="$FAKE_TMUX_DIR/calls.log"
# Log every invocation
printf 'tmux %s\n' "$*" >> "$LOG"
cmd="$1"
shift
read_state() {
[ -f "$STATE" ] || return 0
grep -v '^[[:space:]]*$' "$STATE" || true
}
# Render a tmux format string against one session's fields.
# Args: format, name, attached, activity, windows, cwd
render_format() {
local out="$1" name="$2" attached="$3" activity="$4" windows="$5" cwd="$6"
out="${out//\#\{session_name\}/$name}"
out="${out//\#\{session_attached\}/$attached}"
out="${out//\#\{session_activity\}/$activity}"
out="${out//\#\{session_windows\}/$windows}"
out="${out//\#\{pane_current_path\}/$cwd}"
echo "$out"
}
# Render a tmux format string against one pane's fields.
# Args: format, name, pid, cmd, idx, cwd
render_pane_format() {
local out="$1" name="$2" pid="$3" cmd="$4" idx="$5" cwd="$6"
out="${out//\#\{pane_pid\}/$pid}"
out="${out//\#\{pane_current_command\}/$cmd}"
out="${out//\#\{pane_current_path\}/$cwd}"
out="${out//\#\{session_name\}/$name}"
out="${out//\#\{window_index\}/0}"
out="${out//\#\{pane_index\}/$idx}"
echo "$out"
}
case "$cmd" in
list-sessions)
fmt=""
while [ "$#" -gt 0 ]; do
case "$1" in
-F) shift; fmt="${1:-}"; shift ;;
*) shift ;;
esac
done
[ -n "$fmt" ] || fmt='#{session_name} #{session_attached}'
read_state | while IFS=' ' read -r name attached pids activity windows cwd; do
[ -n "$name" ] || continue
render_format "$fmt" "$name" "$attached" "${activity:-0}" "${windows:-1}" "${cwd:-/tmp}"
done
;;
list-panes)
all=0
fmt=""
session=""
while [ "$#" -gt 0 ]; do
case "$1" in
-t) shift; session="$1"; shift ;;
-F) shift; fmt="${1:-}"; shift ;;
-s) shift ;;
-a) all=1; shift ;;
*) shift ;;
esac
done
[ -n "$fmt" ] || fmt='#{pane_pid}'
read_state | while IFS=' ' read -r name attached pids activity windows cwd; do
[ -n "$name" ] || continue
if [ "$all" -eq 0 ] && [ "$name" != "$session" ]; then
continue
fi
[ "$pids" = "-" ] && continue
idx=0
for entry in $(echo "$pids" | tr ',' ' '); do
pid="${entry%%:*}"
cmd="${entry##*:}"
[ "$cmd" = "$entry" ] && cmd="shell"
render_pane_format "$fmt" "$name" "$pid" "$cmd" "$idx" "${cwd:-/tmp}"
idx=$((idx + 1))
done
done
;;
display)
fmt=""
target=""
while [ "$#" -gt 0 ]; do
case "$1" in
-p) shift ;;
-t) shift; target="$1"; shift ;;
*) fmt="$1"; shift ;;
esac
done
while IFS=' ' read -r name attached pids activity windows cwd; do
[ -n "$name" ] || continue
if [ "$name" = "$target" ]; then
render_format "$fmt" "$name" "$attached" "${activity:-0}" "${windows:-1}" "${cwd:-/tmp}"
exit 0
fi
done < "$STATE"
exit 1
;;
has-session)
session=""
while [ "$#" -gt 0 ]; do
case "$1" in
-t) shift; session="$1"; shift ;;
*) shift ;;
esac
done
# tmux accepts a `=name` form to force exact match; strip the prefix.
session="${session#=}"
while IFS=' ' read -r name attached pids _rest; do
if [ "$name" = "$session" ]; then
exit 0
fi
done < "$STATE"
exit 1
;;
new-session)
detached=0
name=""
cwd=""
while [ "$#" -gt 0 ]; do
case "$1" in
-s) shift; name="$1"; shift ;;
-c) shift; cwd="$1"; shift ;;
-d) detached=1; shift ;;
*) shift ;;
esac
done
attached=1
[ "$detached" -eq 1 ] && attached=0
printf '%s %s - 0 1 %s\n' "$name" "$attached" "${cwd:-/tmp}" >> "$STATE"
;;
attach-session|switch-client)
# No state mutation needed — the call log already records intent.
;;
rename-session)
# Forms: rename-session -t <old> <new> OR rename-session <new>
old=""
new=""
while [ "$#" -gt 0 ]; do
case "$1" in
-t) shift; old="$1"; shift ;;
*) new="$1"; shift ;;
esac
done
if [ -z "$old" ] || [ -z "$new" ]; then
echo "fake-tmux rename-session: need both -t <old> and <new>" >&2
exit 1
fi
tmp="$STATE.tmp"
: > "$tmp"
while IFS= read -r line; do
[ -n "$line" ] || continue
first="${line%% *}"
rest="${line#* }"
if [ "$first" = "$old" ]; then
printf '%s %s\n' "$new" "$rest" >> "$tmp"
else
printf '%s\n' "$line" >> "$tmp"
fi
done < "$STATE"
mv "$tmp" "$STATE"
;;
kill-session)
session=""
while [ "$#" -gt 0 ]; do
case "$1" in
-t) shift; session="$1"; shift ;;
*) shift ;;
esac
done
tmp="$STATE.tmp"
: > "$tmp"
while IFS= read -r line; do
[ -n "$line" ] || continue
first="${line%% *}"
if [ "$first" != "$session" ]; then
printf '%s\n' "$line" >> "$tmp"
fi
done < "$STATE"
mv "$tmp" "$STATE"
;;
*)
echo "fake-tmux: unknown command '$cmd'" >&2
exit 1
;;
esac
|