aboutsummaryrefslogtreecommitdiff
path: root/tests/tmux-util/fake-tmux
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tmux-util/fake-tmux')
-rwxr-xr-xtests/tmux-util/fake-tmux65
1 files changed, 50 insertions, 15 deletions
diff --git a/tests/tmux-util/fake-tmux b/tests/tmux-util/fake-tmux
index f6217bb..ba2d5cc 100755
--- a/tests/tmux-util/fake-tmux
+++ b/tests/tmux-util/fake-tmux
@@ -1,9 +1,11 @@
-#!/bin/sh
+#!/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> [last_activity_epoch]
+# 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>
@@ -19,19 +21,36 @@ printf 'tmux %s\n' "$*" >> "$LOG"
cmd="$1"
shift
-# Helper: read state file, ignoring blank lines
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"
+}
+
case "$cmd" in
list-sessions)
- # Format string is ignored; we always emit "<name> <attached>" because
- # that's the only format tmux-util uses against list-sessions.
- read_state | while IFS=' ' read -r name attached pids _rest; do
+ 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
- echo "$name $attached"
+ render_format "$fmt" "$name" "$attached" "${activity:-0}" "${windows:-1}" "${cwd:-/tmp}"
done
;;
list-panes)
@@ -51,6 +70,25 @@ case "$cmd" in
fi
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
@@ -76,14 +114,11 @@ case "$cmd" in
done
tmp="$STATE.tmp"
: > "$tmp"
- while IFS=' ' read -r name attached pids rest; do
- [ -n "$name" ] || continue
- if [ "$name" != "$session" ]; then
- if [ -n "$rest" ]; then
- printf '%s %s %s %s\n' "$name" "$attached" "$pids" "$rest" >> "$tmp"
- else
- printf '%s %s %s\n' "$name" "$attached" "$pids" >> "$tmp"
- fi
+ 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"