Merge pull request #3 from jaclu/dev

Fishify
This commit is contained in:
Jacob Lundqvist 2022-10-28 15:13:11 +02:00 committed by GitHub
commit dd73b505bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 131 additions and 65 deletions

View file

@ -19,13 +19,6 @@ SCRIPTS_DIR="$CURRENT_DIR/scripts"
. "$SCRIPTS_DIR/utils.sh"
#
# By using Z as default we don't overwrite the default zoom binding (z)
# unless the caller actually want this to happen.
#
default_key="Z"
#
# By printing a NL and date, its easier to keep separate runs apart
#
@ -80,5 +73,10 @@ if bool_param "$(get_tmux_option "@power_zoom_without_prefix" "No")"; then
log_it "Not using prefix"
fi
#
# Debug - clear list of zoomed panes
#
$TMUX_BIN set-option -U @power_zoom_state
# shellcheck disable=SC2086 #options cant be quoted
$TMUX_BIN bind $options "$trigger_key" run-shell "$SCRIPTS_DIR"/power_zoom.sh

View file

@ -8,84 +8,147 @@
# Tracking the placeholder pane by its pane title, this works regardless
# if pane titles are displayed or not.
#
# shellcheck disable=SC2154
# shellcheck disable=SC1007
CURRENT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
# shellcheck disable=SC1091
. "$CURRENT_DIR/utils.sh"
IS_ZOOMED="is_zoomed"
GET_PLACEHOLDER="get_placeholder"
GET_ZOOMED="get_zoomed"
set_pz_status() {
local value="$1"
log_it ">> new @power_zoom_state [$value]"
if [[ -n $value ]]; then
$TMUX_BIN set-option @power_zoom_state "$value"
else
$TMUX_BIN set-option -U @power_zoom_state
fi
}
read_pz_status() {
statuses="$($TMUX_BIN show-option -qv @power_zoom_state)"
echo "$statuses"
}
check_pz_status() {
local this_id
local updated_values
local do_update
local result
local pow_zoomed_panes
local placeholder
local zoomed
case $1 in
"$IS_ZOOMED" | "$GET_PLACEHOLDER" | "$GET_ZOOMED" ) ;;
*)
error_msg "ERROR: check_pz_status - invalid param: [$1]"
;;
esac
this_id="$($TMUX_BIN display -p '#D')"
updated_values=""
do_update=false
result=""
pow_zoomed_panes=( $(read_pz_status) )
for pzp in "${pow_zoomed_panes[@]}" ; do
placeholder="$(echo "$pzp" | cut -d= -f 1)"
zoomed="$(echo "$pzp" | cut -d= -f 2)"
if [[ $zoomed = "$this_id" ]]; then
if [[ $1 = "$IS_ZOOMED" ]]; then
# Since this check won't update the list of zoomed panes
# its ok to return early
return # implicit true
elif [[ $1 = "$GET_PLACEHOLDER" ]]; then
result=$placeholder
do_update=true
continue # dont save current pair in the update
fi
elif [[ $placeholder = "$this_id" ]] && [[ $1 = "$GET_ZOOMED" ]]; then
result=$zoomed
break
fi
updated_values="$updated_values $placeholder=$zoomed"
done
if $do_update; then
set_pz_status "$updated_values"
fi
if [[ -n "$result" ]]; then
# In this case a string is expected, so the implicit true return
# has no significance
echo "$result"
else
false
fi
}
power_zoom() {
recursion="$1"
[[ "$recursion" != "" ]] && log_it "power_zoom($recursion) triggered"
# shellcheck disable=SC2154
primary_pane_id="$($TMUX_BIN display -p '#D')"
primary_pane_title="$($TMUX_BIN display -p '#T')"
placeholder_stub="=== POWER ZOOM === place-holder for pane:"
placeholder_title="$placeholder_stub $primary_pane_id"
log_it "Checking for this place-holder: [$placeholder_title]"
placeholder_pane=$($TMUX_BIN list-panes -a -F "#D #T" | grep "$placeholder_title" | awk '{ print $1 }')
if [[ -n "$placeholder_pane" ]]; then
if check_pz_status "$IS_ZOOMED" ; then
log_it "was zoomed"
#
# Found a place-holder for current pane, move it there and delete
# the place-holder
# Is a zoomed pane, un-zoom it
#
log_it "Found a matching place-holder, move this pane there and delete place-holder"
$TMUX_BIN join-pane -b -t "$placeholder_pane"
$TMUX_BIN kill-pane -t "$placeholder_pane"
placeholder="$(check_pz_status "$GET_PLACEHOLDER")"
if [[ -z $placeholder ]]; then
error_msg "Placeholder for pane is not listed"
fi
$TMUX_BIN join-pane -b -t "$placeholder"
$TMUX_BIN kill-pane -t "$placeholder"
return
fi
zoomed="$(check_pz_status "$GET_ZOOMED")"
if [[ -n "$zoomed" ]]; then
log_it "was placeholder"
if [[ -n "$1" ]]; then
error_msg "Recursion detected when unzooming"
exit 1
fi
#
# Keep code simple, only use one unzoom procedure
#
$TMUX_BIN select-window -t "$zoomed"
power_zoom recursion
else
#
# Zoom this to new window
# Zoom it!
#
log_it "will zoom"
if [[ "$($TMUX_BIN list-panes | wc -l)" -eq 1 ]]; then
error_msg "Can't zoom only pane in a window"
return 0
fi
if [[ "$($TMUX_BIN display -p '#T' | grep "$placeholder_stub")" != "" ]]; then
# shellcheck disable=SC2154
log_it "This is a $plugin_name place-holder!"
#
# go to the referred pane, and run power_zoom again to restore it.
#
if [[ "$recursion" -ne "" ]]; then
error_msg "power_zoom is entering repeated recursion, aborting"
return 0
fi
pane_id="$($TMUX_BIN display -p '#T'| awk '{print $8}')"
log_it "pane_id: [$pane_id]"
if ! $TMUX_BIN select-window -t "$pane_id"; then
error_msg "Failed to find window with Zoomed pane: $pane_id"
return 0
fi
if ! $TMUX_BIN select-pane -t "$pane_id"; then
error_msg "Failed to find Zoomed pane: $pane_id"
return 0
fi
power_zoom recursion
return 0
error_msg "Can't zoom only pane in a window"
fi
this_id="$($TMUX_BIN display -p '#D')"
#
# the place-holder pane will close when it's process is terminated,
# so keep a long sleep going for ever in a loop.
# Ctrl-C would exit script and pane would close in case the zoomed pane
# is killed and the place-holder is left hanging.
#
log_it "Zoom active pane to new window"
# shellcheck disable=SC2154
trigger_key=$(get_tmux_option "@power_zoom_trigger" "$default_key")
$TMUX_BIN split-window -b "echo; echo \" $placeholder_title\n Press [<Prefix> $trigger_key] in this pane to restore it back here...\"; while true ; do sleep 30; done"
log_it ">> trigger: $trigger_key"
$TMUX_BIN split-window -b "echo; \
echo \" placeholder for zoomed pane ${this_id}\"; \
echo ; echo \" You can press <Prefix> $trigger_key\"; \
echo \" in this pane to restore it back here...\"; \
bash -c \"while true ; do sleep 6; done\""
$TMUX_BIN select-pane -T "$placeholder_title"
$TMUX_BIN select-pane -t "$primary_pane_id"
placholder_pane_id="$($TMUX_BIN display -p '#D')"
set_pz_status "$(read_pz_status) $placholder_pane_id=$this_id"
$TMUX_BIN select-pane -t "$this_id"
$TMUX_BIN break-pane # move it to new window
$TMUX_BIN rename-window "**POWER ZOOM** $primary_pane_title ($primary_pane_id)"
$TMUX_BIN rename-window "ZOOMED $this_id"
fi
return 0
}
power_zoom

View file

@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
#
# Copyright (c) 2022: Jacob.Lundqvist@gmail.com
# License: MIT
@ -19,29 +19,34 @@
plugin_name="tmux-power-zoom"
#
# By using Z as default we don't overwrite the default zoom binding (z)
# unless the caller actually want this to happen.
#
default_key="Z"
#
# I use an env var TMUX_BIN to point at the current tmux, defined in my
# tmux.conf, in order to pick the version matching the server running.
# This is needed when checking backwards compatability with various versions.
# If not found, it is set to whatever is in path, so should have no negative
# impact. In all calls to tmux I use $TMUX_BIN instead in the rest of this
# plugin.
#
[ -z "$TMUX_BIN" ] && TMUX_BIN="tmux"
[[ -z "$TMUX_BIN" ]] && TMUX_BIN="tmux"
#
# If log_file is empty or undefined, no logging will occur,
# so comment it out for normal usage.
#
# log_file="/tmp/$plugin_name.log" # Trigger LF to separate runs of this script
log_file="/tmp/$plugin_name.log" # Trigger LF to separate runs of this script
#
# If $log_file is empty or undefined, no logging will occur.
#
log_it() {
if [ -z "$log_file" ]; then
if [[ -z "$log_file" ]]; then
return
fi
printf "[%s] %s\n" "$(date '+%H:%M:%S')" "$@" >> "$log_file"
@ -58,7 +63,7 @@ error_msg() {
log_it "$msg"
$TMUX_BIN display-message "$plugin_name $msg"
[ "$exit_code" -ne 0 ] && exit "$exit_code"
[[ "$exit_code" -ne 0 ]] && exit "$exit_code"
}
@ -97,7 +102,7 @@ get_tmux_option() {
gto_option=$1
gto_default_value=$2
gto_value=$($TMUX_BIN show-option -gqv "$gto_option")
if [ -z "$gto_value" ]; then
if [[ -z "$gto_value" ]]; then
echo "$gto_default_value"
else
echo "$gto_value"