From 36f7df56164df73c7af867d8ed478a9b572aee40 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 14 Nov 2026 11:08:59 -0800 Subject: Move source files to src/ directory and add test script to scripts/ --- README.md | 2 +- plugin.tmux | 79 ---------------------------------------- scripts/test.sh | 12 +++++++ scripts/tmux.sh | 17 --------- scripts/toggle-term.sh | 96 ------------------------------------------------- src/main.sh | 77 +++++++++++++++++++++++++++++++++++++++ src/tmux.sh | 19 ++++++++++ src/toggle-term.sh | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tests.sh | 4 +-- 9 files changed, 209 insertions(+), 195 deletions(-) delete mode 100755 plugin.tmux create mode 100755 scripts/test.sh delete mode 100755 scripts/tmux.sh delete mode 100755 scripts/toggle-term.sh create mode 100755 src/main.sh create mode 100755 src/tmux.sh create mode 100755 src/toggle-term.sh diff --git a/README.md b/README.md index d95f7c6..38f832d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ git clone https://git.eugenetriguba.com/tmux-toggle-term Add to your tmux configuration: ```tmux -run-shell '~/.tmux/plugins/tmux-toggle-term/plugin.tmux' +run-shell '~/.tmux/plugins/tmux-toggle-term/src/main.sh' ``` ## Usage diff --git a/plugin.tmux b/plugin.tmux deleted file mode 100755 index 43d587a..0000000 --- a/plugin.tmux +++ /dev/null @@ -1,79 +0,0 @@ -#!/bin/sh -# vi: ft=sh -set -e - -CURRENT_DIR="$(dirname "$0")" -. "$CURRENT_DIR/scripts/tmux.sh" - -SCRIPTS_DIR="${CURRENT_DIR}/scripts" - -FLOAT_KEYBINDING="C-f" -FLOAT_KEYBINDING_OPTION="@toggle-term-float-key" -FLOAT_USE_PREFIX_OPTION="@toggle-term-float-use-prefix" -FLOAT_ENABLED_OPTION="@toggle-term-float-enabled" - -SPLIT_KEYBINDING="C-\;" -SPLIT_KEYBINDING_OPTION="@toggle-term-split-key" -SPLIT_USE_PREFIX_OPTION="@toggle-term-split-use-prefix" -SPLIT_ENABLED_OPTION="@toggle-term-split-enabled" - -main() { - tmux_cmd="$(get_tmux_cmd)" - - if [ "$(float_enabled)" = "true" ]; then - float_key_binding="$(float_key)" - if [ "$(use_prefix_for_float)" = "true" ]; then - $tmux_cmd bind-key "$float_key_binding" run-shell -b "$SCRIPTS_DIR/toggle-term.sh float" - else - $tmux_cmd bind-key -n "$float_key_binding" run-shell -b "$SCRIPTS_DIR/toggle-term.sh float" - fi - fi - - if [ "$(split_enabled)" = "true" ]; then - split_key_binding="$(split_key)" - if [ "$(use_prefix_for_split)" = "true" ]; then - $tmux_cmd bind-key "$split_key_binding" run-shell -b "$SCRIPTS_DIR/toggle-term.sh split" - else - $tmux_cmd bind-key -n "$split_key_binding" run-shell -b "$SCRIPTS_DIR/toggle-term.sh split" - fi - fi -} - -float_key() { - get_tmux_option "$FLOAT_KEYBINDING_OPTION" "$FLOAT_KEYBINDING" -} - -use_prefix_for_float() { - get_tmux_option "$FLOAT_USE_PREFIX_OPTION" "false" -} - -split_key() { - get_tmux_option "$SPLIT_KEYBINDING_OPTION" "$SPLIT_KEYBINDING" -} - -use_prefix_for_split() { - get_tmux_option "$SPLIT_USE_PREFIX_OPTION" "false" -} - -float_enabled() { - get_tmux_option "$FLOAT_ENABLED_OPTION" "true" -} - -split_enabled() { - get_tmux_option "$SPLIT_ENABLED_OPTION" "true" -} - -get_tmux_option() { - option="$1" - default_value="$2" - tmux_cmd=$(get_tmux_cmd) - - option_value=$($tmux_cmd show-option -gqv "$option") - if [ -z "$option_value" ]; then - echo "$default_value" - else - echo "$option_value" - fi -} - -main diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..e123124 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -o errexit +set -o nounset + +DIR=$(dirname "$0") +ROOT_DIR="$DIR/../" + +main() { + "$ROOT_DIR/vendor/bats/bin/bats" "$ROOT_DIR/tests/tests.sh" +} + +main "$@" diff --git a/scripts/tmux.sh b/scripts/tmux.sh deleted file mode 100755 index b919043..0000000 --- a/scripts/tmux.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh - -get_tmux_cmd() { - TMUX_CMD="tmux" - # 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 - echo "$TMUX_CMD" -} diff --git a/scripts/toggle-term.sh b/scripts/toggle-term.sh deleted file mode 100755 index 0cc7414..0000000 --- a/scripts/toggle-term.sh +++ /dev/null @@ -1,96 +0,0 @@ -#!/bin/sh -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. - 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/src/main.sh b/src/main.sh new file mode 100755 index 0000000..4bc01c2 --- /dev/null +++ b/src/main.sh @@ -0,0 +1,77 @@ +#!/bin/sh +set -o errexit +set -o nounset + +CURRENT_DIR="$(dirname "$0")" +. "$CURRENT_DIR/tmux.sh" + +FLOAT_KEYBINDING="C-f" +FLOAT_KEYBINDING_OPTION="@toggle-term-float-key" +FLOAT_USE_PREFIX_OPTION="@toggle-term-float-use-prefix" +FLOAT_ENABLED_OPTION="@toggle-term-float-enabled" + +SPLIT_KEYBINDING="C-\;" +SPLIT_KEYBINDING_OPTION="@toggle-term-split-key" +SPLIT_USE_PREFIX_OPTION="@toggle-term-split-use-prefix" +SPLIT_ENABLED_OPTION="@toggle-term-split-enabled" + +main() { + tmux_cmd="$(get_tmux_cmd)" + + if [ "$(float_enabled)" = "true" ]; then + float_key_binding="$(float_key)" + if [ "$(use_prefix_for_float)" = "true" ]; then + $tmux_cmd bind-key "$float_key_binding" run-shell -b "$CURRENT_DIR/toggle-term.sh float" + else + $tmux_cmd bind-key -n "$float_key_binding" run-shell -b "$CURRENT_DIR/toggle-term.sh float" + fi + fi + + if [ "$(split_enabled)" = "true" ]; then + split_key_binding="$(split_key)" + if [ "$(use_prefix_for_split)" = "true" ]; then + $tmux_cmd bind-key "$split_key_binding" run-shell -b "$CURRENT_DIR/toggle-term.sh split" + else + $tmux_cmd bind-key -n "$split_key_binding" run-shell -b "$CURRENT_DIR/toggle-term.sh split" + fi + fi +} + +float_key() { + get_tmux_option "$FLOAT_KEYBINDING_OPTION" "$FLOAT_KEYBINDING" +} + +use_prefix_for_float() { + get_tmux_option "$FLOAT_USE_PREFIX_OPTION" "false" +} + +split_key() { + get_tmux_option "$SPLIT_KEYBINDING_OPTION" "$SPLIT_KEYBINDING" +} + +use_prefix_for_split() { + get_tmux_option "$SPLIT_USE_PREFIX_OPTION" "false" +} + +float_enabled() { + get_tmux_option "$FLOAT_ENABLED_OPTION" "true" +} + +split_enabled() { + get_tmux_option "$SPLIT_ENABLED_OPTION" "true" +} + +get_tmux_option() { + option="$1" + default_value="$2" + tmux_cmd=$(get_tmux_cmd) + + option_value=$($tmux_cmd show-option -gqv "$option") + if [ -z "$option_value" ]; then + echo "$default_value" + else + echo "$option_value" + fi +} + +main diff --git a/src/tmux.sh b/src/tmux.sh new file mode 100755 index 0000000..2be86de --- /dev/null +++ b/src/tmux.sh @@ -0,0 +1,19 @@ +#!/bin/sh +set -o errexit +set -o nounset + +get_tmux_cmd() { + TMUX_CMD="tmux" + # 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 + echo "$TMUX_CMD" +} diff --git a/src/toggle-term.sh b/src/toggle-term.sh new file mode 100755 index 0000000..3554ec0 --- /dev/null +++ b/src/toggle-term.sh @@ -0,0 +1,98 @@ +#!/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 "$@" diff --git a/tests/tests.sh b/tests/tests.sh index 88e34aa..ed4eaa8 100644 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -3,8 +3,8 @@ load '../vendor/bats-support/load' load '../vendor/bats-assert/load' -TOGGLE_TERM_SCRIPT_PATH="$BATS_TEST_DIRNAME/../scripts/toggle-term.sh" -PLUGIN_SCRIPT_PATH="$BATS_TEST_DIRNAME/../plugin.tmux" +TOGGLE_TERM_SCRIPT_PATH="$BATS_TEST_DIRNAME/../src/toggle-term.sh" +PLUGIN_SCRIPT_PATH="$BATS_TEST_DIRNAME/../src/main.sh" setup() { export TMUX_SOCKET="test$$" -- cgit v1.2.3