diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-01 18:06:21 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-01 18:06:21 +0200 |
| commit | 507e3cae3d4e3939dad1af33feff945ab850cb72 (patch) | |
| tree | b0e1dc62649819f23468b4a001a6974002ba0f34 | |
| parent | b75af00ee44263c3d3ae4101c720aa6277e39974 (diff) | |
Add tmux editor popup and deploy helper script.
This enables an overlay editor binding and installs the send-keys script on frontends.
| -rw-r--r-- | frontends/Rexfile | 6 | ||||
| -rw-r--r-- | frontends/etc/tmux.conf | 2 | ||||
| -rw-r--r-- | frontends/scripts/tmux-edit-send | 87 |
3 files changed, 95 insertions, 0 deletions
diff --git a/frontends/Rexfile b/frontends/Rexfile index 430c11d..3a84648 100644 --- a/frontends/Rexfile +++ b/frontends/Rexfile @@ -139,6 +139,12 @@ task 'base', owner => 'root', group => 'wheel', mode => '644'; + + file '/usr/local/bin/tmux-edit-send', + source => './scripts/tmux-edit-send', + owner => 'root', + group => 'wheel', + mode => '755'; }; desc 'Setup uptimed'; diff --git a/frontends/etc/tmux.conf b/frontends/etc/tmux.conf index 1449326..0f4e306 100644 --- a/frontends/etc/tmux.conf +++ b/frontends/etc/tmux.conf @@ -16,6 +16,8 @@ bind-key J resize-pane -D 5 bind-key K resize-pane -U 5 bind-key L resize-pane -R 5 +bind-key e run-shell -b "tmux display-message -p '#{pane_id}' > /tmp/tmux-edit-target-#{client_pid} \; tmux popup -E -w 90% -h 35% -x 5% -y 65% -d '#{pane_current_path}' \"~/scripts/tmux-edit-send /tmp/tmux-edit-target-#{client_pid}\"" + bind-key b break-pane -d bind-key c new-window -c '#{pane_current_path}' bind-key p setw synchronize-panes off diff --git a/frontends/scripts/tmux-edit-send b/frontends/scripts/tmux-edit-send new file mode 100644 index 0000000..5edafe1 --- /dev/null +++ b/frontends/scripts/tmux-edit-send @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +set -u -o pipefail + +log_file="${TMPDIR:-/tmp}/tmux-edit-send.log" +log() { + printf '%s\n' "$*" >> "$log_file" +} + +target_file="${1:-}" +target="" +if [ -n "$target_file" ] && [ -f "$target_file" ]; then + target="$(sed -n '1p' "$target_file" | tr -d '[:space:]')" + log "file target=${target:-<empty>}" + rm -f "$target_file" +fi + +if [ -z "$target" ]; then + target="${TMUX_EDIT_TARGET:-}" +fi +log "env target=${target:-<empty>}" +if [ -z "$target" ]; then + env_line="$(tmux show-environment -g TMUX_EDIT_TARGET 2>/dev/null || true)" + case "$env_line" in + TMUX_EDIT_TARGET=*) target="${env_line#TMUX_EDIT_TARGET=}" ;; + esac +fi +log "tmux env target=${target:-<empty>}" + +current_pane="$(tmux display-message -p "#{pane_id}" 2>/dev/null || true)" +log "current pane=${current_pane:-<empty>}" +if [ -n "$target" ] && [[ "$target" == *"#{"* ]]; then + log "format target detected, clearing" + target="" +fi +if [ -z "$target" ]; then + target="$(tmux display-message -p "#{last_pane}" 2>/dev/null || true)" +elif [ "$target" = "$current_pane" ]; then + last_pane="$(tmux display-message -p "#{last_pane}" 2>/dev/null || true)" + if [ -n "$last_pane" ]; then + target="$last_pane" + fi +fi +log "fallback target=${target:-<empty>}" + +editor="${EDITOR:-vi}" +tmpfile="$(mktemp "./.tmux-edit-send.XXXXXX.md")" + +cleanup() { + rm -f "$tmpfile" +} +trap cleanup EXIT + +"$editor" "$tmpfile" +log "editor exited with status $?" + +if [ ! -s "$tmpfile" ]; then + log "empty file, nothing sent" + exit 0 +fi + +# Validate target after editor so popup stays open. +if [ -z "$target" ]; then + log "error: no target pane determined" + echo "Could not determine target pane." >&2 + exit 1 +fi + +target_found=0 +for pane in $(tmux list-panes -a -F "#{pane_id}" 2>/dev/null || true); do + if [ "$pane" = "$target" ]; then + target_found=1 + break + fi +done + +if [ "$target_found" -ne 1 ]; then + log "error: target pane not found: $target" + echo "Target pane not found: $target" >&2 + exit 1 +fi + +# Send line by line to preserve newlines reliably. +while IFS= read -r line || [ -n "$line" ]; do + tmux send-keys -t "$target" -l "$line" + tmux send-keys -t "$target" Enter +done < "$tmpfile" +log "sent content to $target" |
