theniceboy/zsh/functions/opr.zsh
2026-03-27 21:04:47 -07:00

54 lines
1.4 KiB
Bash

opr() {
if [ -z "$TMUX" ]; then
print -u2 "opr: not in tmux"
return 1
fi
local pane_target="${TMUX_PANE:-}"
if [ -z "$pane_target" ]; then
pane_target=$(tmux display-message -p '#{pane_id}')
fi
if [ -z "$pane_target" ]; then
print -u2 "opr: unable to determine tmux pane id"
return 1
fi
local pane_locator
pane_locator=$(tmux display-message -p -t "$pane_target" '#{session_name}:#{window_index}.#{pane_index}')
if [ -z "$pane_locator" ]; then
print -u2 "opr: unable to determine tmux pane locator"
return 1
fi
local state_file="${XDG_STATE_HOME:-$HOME/.local/state}/op/loc_${pane_locator//[^a-zA-Z0-9_]/_}"
if [ ! -f "$state_file" ]; then
print -u2 "opr: no previous session for this tmux pane locator"
return 1
fi
local session_id
IFS= read -r session_id < "$state_file"
if [ -z "$session_id" ]; then
print -u2 "opr: no previous session to resume"
return 1
fi
print -u2 "opr: resuming session $session_id"
if ! typeset -f _op_run >/dev/null; then
local src=${funcfiletrace[1]%:*}
local dir=${src:h}
local common="$dir/_op_common.zsh"
if [ ! -f "$common" ]; then
common="${XDG_CONFIG_HOME:-$HOME/.config}/zsh/functions/_op_common.zsh"
fi
if [ ! -f "$common" ]; then
print -u2 "opr: missing _op_common.zsh at $common"
return 1
fi
source "$common" || return 1
fi
_op_run op --session "$session_id" "$@"
}