From 44a7151bbbceba398521ed1a723883ee59792873 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 17 Aug 2025 11:57:09 -0400 Subject: Update directory structure --- plugin.tmux | 4 +- scripts/toggle-term.sh | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ tests.sh | 67 ------------------------------- tests/tests.sh | 69 ++++++++++++++++++++++++++++++++ toggle-term.sh | 105 ------------------------------------------------- 5 files changed, 176 insertions(+), 174 deletions(-) create mode 100755 scripts/toggle-term.sh delete mode 100644 tests.sh create mode 100644 tests/tests.sh delete mode 100755 toggle-term.sh diff --git a/plugin.tmux b/plugin.tmux index c5c851b..c768ec9 100644 --- a/plugin.tmux +++ b/plugin.tmux @@ -3,5 +3,5 @@ if -n "#{==:#{@toggle_term_split_key},}" \ if -n "#{==:#{@toggle_term_float_key},}" \ set -g @toggle_term_float_key 'C-f' -bind-key -n "#{@toggle_term_split_key}" run-shell -b "#{sourcedir}/toggle-term.sh split" -bind-key -n "#{@toggle_term_float_key}" run-shell -b "#{sourcedir}/toggle-term.sh float" +bind-key -n "#{@toggle_term_split_key}" run-shell -b "#{sourcedir}/scripts/toggle-term.sh split" +bind-key -n "#{@toggle_term_float_key}" run-shell -b "#{sourcedir}/scripts/toggle-term.sh float" diff --git a/scripts/toggle-term.sh b/scripts/toggle-term.sh new file mode 100755 index 0000000..cd2e5c1 --- /dev/null +++ b/scripts/toggle-term.sh @@ -0,0 +1,105 @@ +#!/bin/sh +TMUX_CMD="tmux" +POPUP_SESSION_NAME="popup" + +main() { + # The script supports targeting a different tmux socket + # or configuration file solely for testing purposes. This + # allows isolation between the tmux installation on the host + # and the tmux session for the test run. + if [ -n "${TMUX_SOCKET:-}" ]; then + if [ -n "${TMUX_CONFIG_PATH:-}" ]; then + TMUX_CMD="tmux -f $TMUX_CONFIG_PATH -L $TMUX_SOCKET" + else + TMUX_CMD="tmux -L $TMUX_SOCKET" + fi + fi + + # 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. + 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:.- + 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 "$@" diff --git a/tests.sh b/tests.sh deleted file mode 100644 index ed883e3..0000000 --- a/tests.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env bats - -load 'vendor/bats-support/load' -load 'vendor/bats-assert/load' - -setup() { - export TMUX_SOCKET="test$$" - export TMUX_CONFIG_PATH="$TMUX_CONFIG_PATH" - export TEST_SESSION="test-$BATS_TEST_NUMBER-$$" - tmux_start_server -} - -teardown() { - tmux_kill_server -} - -tmux_start_server() { - tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" new-session -d -s "$TEST_SESSION" 'sleep 1000' -} - -tmux_kill_server() { - tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" kill-server || true -} - -tmux_pane_count() { - num_panes=$(tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" list-panes | wc -l | sed 's/^[[:space:]]*//g') - echo "$num_panes" -} - -tmux_active_panes() { - active_panes=$(tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" list-panes -F '#{pane_active}' | tr '\n' '-' | sed 's/-$//g') - echo "$active_panes" -} - -tmux_is_zoomed() { - is_zoomed=$(tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" list-panes -F '#{window_zoomed_flag}') - if echo "$is_zoomed" | grep -q 1; then - echo 'true' - else - echo 'false' - fi -} - -@test 'split creates a new pane' { - assert_equal "$(tmux_pane_count)" 1 - assert_equal "$(tmux_active_panes)" '1' - - run ./toggle-term.sh split - assert_success - - assert_equal "$(tmux_pane_count)" 2 - assert_equal "$(tmux_active_panes)" '0-1' -} - -@test 'toggling split changes back active pane' { - assert_equal "$(tmux_pane_count)" 1 - assert_equal "$(tmux_active_panes)" '1' - - run ./toggle-term.sh split - assert_success - run ./toggle-term.sh split - assert_success - - assert_equal "$(tmux_pane_count)" 2 - assert_equal "$(tmux_active_panes)" '1-0' - assert_equal "$(tmux_is_zoomed)" 'true' -} diff --git a/tests/tests.sh b/tests/tests.sh new file mode 100644 index 0000000..99706dd --- /dev/null +++ b/tests/tests.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bats + +load '../vendor/bats-support/load' +load '../vendor/bats-assert/load' + +TOGGLE_TERM_SCRIPT_PATH="$BATS_TEST_DIRNAME/../scripts/toggle-term.sh" + +setup() { + export TMUX_SOCKET="test$$" + export TMUX_CONFIG_PATH="$TMUX_CONFIG_PATH" + export TEST_SESSION="test-$BATS_TEST_NUMBER-$$" + tmux_start_server +} + +teardown() { + tmux_kill_server +} + +tmux_start_server() { + tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" new-session -d -s "$TEST_SESSION" 'sleep 1000' +} + +tmux_kill_server() { + tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" kill-server || true +} + +tmux_pane_count() { + num_panes=$(tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" list-panes | wc -l | sed 's/^[[:space:]]*//g') + echo "$num_panes" +} + +tmux_active_panes() { + active_panes=$(tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" list-panes -F '#{pane_active}' | tr '\n' '-' | sed 's/-$//g') + echo "$active_panes" +} + +tmux_is_zoomed() { + is_zoomed=$(tmux -f "$TMUX_CONFIG_PATH" -L "$TMUX_SOCKET" list-panes -F '#{window_zoomed_flag}') + if echo "$is_zoomed" | grep -q 1; then + echo 'true' + else + echo 'false' + fi +} + +@test 'split creates a new pane' { + assert_equal "$(tmux_pane_count)" 1 + assert_equal "$(tmux_active_panes)" '1' + + run "$TOGGLE_TERM_SCRIPT_PATH" split + assert_success + + assert_equal "$(tmux_pane_count)" 2 + assert_equal "$(tmux_active_panes)" '0-1' +} + +@test 'toggling split changes back active pane' { + assert_equal "$(tmux_pane_count)" 1 + assert_equal "$(tmux_active_panes)" '1' + + run "$TOGGLE_TERM_SCRIPT_PATH" split + assert_success + run "$TOGGLE_TERM_SCRIPT_PATH" split + assert_success + + assert_equal "$(tmux_pane_count)" 2 + assert_equal "$(tmux_active_panes)" '1-0' + assert_equal "$(tmux_is_zoomed)" 'true' +} diff --git a/toggle-term.sh b/toggle-term.sh deleted file mode 100755 index cd2e5c1..0000000 --- a/toggle-term.sh +++ /dev/null @@ -1,105 +0,0 @@ -#!/bin/sh -TMUX_CMD="tmux" -POPUP_SESSION_NAME="popup" - -main() { - # The script supports targeting a different tmux socket - # or configuration file solely for testing purposes. This - # allows isolation between the tmux installation on the host - # and the tmux session for the test run. - if [ -n "${TMUX_SOCKET:-}" ]; then - if [ -n "${TMUX_CONFIG_PATH:-}" ]; then - TMUX_CMD="tmux -f $TMUX_CONFIG_PATH -L $TMUX_SOCKET" - else - TMUX_CMD="tmux -L $TMUX_SOCKET" - fi - fi - - # 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. - 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:.- - 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 "$@" -- cgit v1.2.3