#!/bin/sh set -o errexit set -o nounset CURRENT_DIR="$(dirname "$0")" . "$CURRENT_DIR/tmux.sh" POPUP_SESSION_NAME="popup" main() { TMUX_CMD=$(get_tmux_cmd) # Modes: # float → global floating window # split → per-window split pane mode="$1" case "${mode}" in float) toggle_popup ;; split) toggle_split ;; *) echo "Usage: $0 [split|float]" >&2 exit 1 ;; esac } toggle_popup() { if $TMUX_CMD has-session -t "$POPUP_SESSION_NAME" 2>/dev/null; then current_session="$($TMUX_CMD display-message -p '#{session_name}')" if [ "$current_session" = "$POPUP_SESSION_NAME" ]; then $TMUX_CMD detach-client else $TMUX_CMD display-popup -d '#{pane_current_path}' -xC -yC -w90% -h80% \ -E "$TMUX_CMD attach -t $POPUP_SESSION_NAME" fi else $TMUX_CMD display-popup -d '#{pane_current_path}' -xC -yC -w90% -h80% \ -E "$TMUX_CMD new -s $POPUP_SESSION_NAME" fi } toggle_split() { # When we list the tmux panes out with the window flags, # when there are no special flags for the panes, the output # looks like the following for 3 panes: # * # * # * panes_list="$($TMUX_CMD list-panes -F '#{window_flags}')" pane_count="$(echo "${panes_list}" | wc -l | sed 's/^[[:space:]]*//g')" if [ "${pane_count}" = 1 ]; then # And when we only have 1 pane, we want to split it into two # and open a new one at the current path of the first pane. # This is what initially creates the 'split'. $TMUX_CMD split-window -c "#{pane_current_path}" return fi # However, if we've zoomed in a pane, all the panes will have # a 'Z' to indicate the window is zoomed in: # *Z # *Z # # Note that we're printing the 'window_flags' per pane. There # is no concept of 'pane flags', so every pane here has a Z to # indicate the window is zoomed, but this output doesn't indicate # which pane is the one zoomed in. # # If we have a zoomed in pane, that means we opened our split pane # and toggled it, so it is closed, and now we want to open it again. # In this case, our single main zoomed in window would be what is on # the screen and we want to open back up our split pane. if echo "${panes_list}" | grep -q Z; then # To open back up our split pane, we can switch focus to the previously # active pane in the current window of the current session. # # : = current session # . = current window # - = previously active pane $TMUX_CMD select-pane -t:.- return fi # If we have more than 1 pane and none are zoomed in, we can assume the # screen has the split pane open. # # So, to simulate 'toggling' between the main (first) pane and the split # pane, we 'zoom' the main pane so it takes up the full screen. Because # the initial pane index is configurable in tmux, we grab the first pane # index dynamically. first_pane_index="$($TMUX_CMD list-panes -F '#{pane_index}' | head -n1)" $TMUX_CMD resize-pane -Z -t"$first_pane_index" } main "$@"