diff options
Diffstat (limited to 'tmuxrdr/plugin/bin/command-prompt')
| -rwxr-xr-x | tmuxrdr/plugin/bin/command-prompt | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/tmuxrdr/plugin/bin/command-prompt b/tmuxrdr/plugin/bin/command-prompt index 8c4a4e4f..a97d6702 100755 --- a/tmuxrdr/plugin/bin/command-prompt +++ b/tmuxrdr/plugin/bin/command-prompt @@ -25,6 +25,15 @@ opt() { # opt -t ARGS...: the value after -t, if any done } +# tmux's sessions are herdr's workspaces. A target (-t) is a workspace's name +# or id, as tmux takes "name", "name:" or "=name"; prints the id. +session_id() { + _t="${1#=}"; _t="${_t%%:*}" + "$H" workspace list | jq -r --arg t "$_t" \ + '[.result.workspaces[] | select(.label == $t or .workspace_id == $t)][0].workspace_id // empty' +} +current_session() { pane_info "$p" workspace_id; } + # The case lives in a function: bash 3.2, which is macOS /bin/sh, misparses a # case statement written inside $(...) -- its pattern parentheses end the # substitution early. @@ -32,7 +41,37 @@ run() { case "$cmd" in kill-pane | killp) "$H" pane close "$p" ;; kill-window | killw) "$H" tab close "$(pane_info "$p" tab_id)" ;; - kill-session) "$H" workspace close "$(pane_info "$p" workspace_id)" ;; + kill-session) + t="$(opt -t "$@")" + if [ -n "$t" ]; then w="$(session_id "$t")"; [ -n "$w" ] || { echo "can't find session: $t"; exit 1; } + else w="$(current_session)"; fi + "$H" workspace close "$w" ;; + new-session | new) + # new-session [-A] [-d] [-s name] [-c dir]: a workspace, switched to + # unless -d; -A switches to an existing one of that name instead. + name="$(opt -s "$@")"; dir="$(opt -c "$@")" + flag=-A; attach=""; has "$@" && attach=1 + flag=-d; focus=--focus; has "$@" && focus=--no-focus + if [ -n "$name" ]; then + w="$(session_id "$name")" + if [ -n "$w" ]; then + [ -n "$attach" ] || { echo "duplicate session: $name"; exit 1; } + "$H" workspace focus "$w"; exit + fi + fi + set -- workspace create "$focus" + [ -n "$name" ] && set -- "$@" --label "$name" + [ -n "$dir" ] && set -- "$@" --cwd "$dir" + "$H" "$@" ;; + attach-session | attach | a) + t="$(opt -t "$@")"; w="$(session_id "${t:-}")" + [ -n "$w" ] || { echo "can't find session: ${t:-}"; exit 1; } + "$H" workspace focus "$w" ;; + list-sessions | ls) + # tmux: "name: N windows (attached)"; herdr keeps no creation time. + "$H" workspace list | jq -r '.result.workspaces[] + | "\(.label): \(.tab_count) windows\(if .focused then " (attached)" else "" end)"' + exit 2 ;; kill-server) "$H" server stop ;; new-window | neww) "$H" tab create --workspace "$(pane_info "$p" workspace_id)" --focus ;; split-window | splitw | new-pane | newp) @@ -48,7 +87,21 @@ run() { else flag=-D; if has "$@"; then sh "$BIN/swap-pane" next; else action swap-marked; fi; fi ;; rotate-window | rotatew) flag=-D; if has "$@"; then sh "$BIN/rotate-window" down; else sh "$BIN/rotate-window" up; fi ;; last-window | last) action last-window ;; - switch-client | switchc) flag=-l; if has "$@"; then action last-session; else echo "switch-client: only -l is supported"; exit 1; fi ;; + switch-client | switchc) + flag=-l; if has "$@"; then action last-session; exit; fi + t="$(opt -t "$@")" + if [ -n "$t" ]; then + w="$(session_id "$t")"; [ -n "$w" ] || { echo "can't find session: $t"; exit 1; } + "$H" workspace focus "$w"; exit + fi + # -n / -p: the next / previous workspace in the sidebar's order, wrapping. + flag=-n; step=0; has "$@" && step=1 + flag=-p; has "$@" && step=-1 + [ "$step" != 0 ] || { echo "switch-client: -t, -n, -p or -l"; exit 1; } + w="$("$H" workspace list | jq -r --argjson s "$step" '.result.workspaces as $w + | ($w | map(.focused) | index(true)) as $i + | $w[(($i + $s) % ($w | length) + ($w | length)) % ($w | length)].workspace_id')" + "$H" workspace focus "$w" ;; select-pane | selectp) flag=-m; if has "$@"; then action mark; exit; fi flag=-M; if has "$@"; then action clear-mark; exit; fi |
