diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-08-16 15:29:24 -0400 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-08-16 15:29:24 -0400 |
| commit | fc2c4f173673fce90cfa16dfe0d425bf1c399b54 (patch) | |
| tree | 8bf6ccc3138d0695e509a527309b2869e9c8de27 /toggle-term.sh | |
| parent | Fix plugin hotkey (diff) | |
| download | tmux-toggle-term-fc2c4f173673fce90cfa16dfe0d425bf1c399b54.zip | |
Add bats to test splitting panes
Diffstat (limited to 'toggle-term.sh')
| -rwxr-xr-x | toggle-term.sh | 60 |
1 files changed, 49 insertions, 11 deletions
diff --git a/toggle-term.sh b/toggle-term.sh index b80ef97..ab5f87f 100755 --- a/toggle-term.sh +++ b/toggle-term.sh @@ -4,6 +4,7 @@ # float → global floating window TMUX_CMD="tmux" +POPUP_SESSION_NAME="popup" main() { if [ -n "${TMUX_SOCKET:-}" ]; then @@ -13,7 +14,7 @@ main() { mode="$1" case "${mode}" in float) - toggle_popup "popup" + toggle_popup ;; split) toggle_split @@ -26,32 +27,69 @@ main() { } toggle_popup() { - term_session_name="$1" - if $TMUX_CMD has-session -t "$term_session_name" 2>/dev/null; then + 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" = "$term_session_name" ]; then + 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 $term_session_name" + -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 $term_session_name" + -E "$TMUX_CMD new -s $POPUP_SESSION_NAME" fi } toggle_split() { - panes_list="$($TMUX_CMD list-panes -F '#F')" - zoomed_panes="$(echo "${panes_list}" | grep Z)" + # 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 | bc)" 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}" - elif [ -n "${zoomed_panes}" ]; then + 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. + zoomed_panes="$(echo "${panes_list}" | grep Z)" + if [ -n "${zoomed_panes}" ]; 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:.- - else - $TMUX_CMD resize-pane -Z -t1 + return fi + + # If we have more than 1 pane and none are zoomed in, we can assume the + # screen has the split pane open. + # + # To simulate 'toggling' between the main pane and the split + # pane, we 'zoom' the main pane so it takes up the full screen. + $TMUX_CMD resize-pane -Z -t1 } main "$@" |