aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.build.yml6
-rw-r--r--.gitmodules9
-rw-r--r--README.md2
-rw-r--r--tests.sh63
-rwxr-xr-xtoggle-term.sh60
m---------vendor/bats0
m---------vendor/bats-assert0
m---------vendor/bats-support0
8 files changed, 128 insertions, 12 deletions
diff --git a/.build.yml b/.build.yml
new file mode 100644
index 0000000..a211aa9
--- /dev/null
+++ b/.build.yml
@@ -0,0 +1,6 @@
+image: alpine/3.22
+tasks:
+ - test: |
+ cd tmux-toggle-term
+ ./vendor/bats/bin/bats tests.sh
+
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..cb46bef
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,9 @@
+[submodule "vendor/bats-support"]
+ path = vendor/bats-support
+ url = https://github.com/bats-core/bats-support.git
+[submodule "vendor/bats-assert"]
+ path = vendor/bats-assert
+ url = https://github.com/bats-core/bats-assert.git
+[submodule "vendor/bats"]
+ path = vendor/bats
+ url = https://github.com/bats-core/bats-core.git
diff --git a/README.md b/README.md
index d38da9f..225f661 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# tmux-toggle-term
-A terminal toggle for tmux that supports a per-window split pane and global floating popups.
+A terminal toggle for tmux that supports a per-window split pane and a global floating popup.
## Installation
diff --git a/tests.sh b/tests.sh
new file mode 100644
index 0000000..09126f9
--- /dev/null
+++ b/tests.sh
@@ -0,0 +1,63 @@
+#!/usr/bin/env bats
+
+load 'vendor/bats-support/load'
+load 'vendor/bats-assert/load'
+
+setup() {
+ export TMUX_SOCKET="test$$"
+ export TEST_SESSION="test-$BATS_TEST_NUMBER-$$"
+ tmux_start_server
+}
+
+teardown() {
+ tmux_kill_server
+}
+
+tmux_start_server() {
+ tmux -L "$TMUX_SOCKET" new-session -d -s "$TEST_SESSION" 'sleep 1000'
+}
+
+tmux_kill_server() {
+ tmux -L "$TMUX_SOCKET" kill-server || true
+}
+
+tmux_pane_count() {
+ num_panes=$(tmux -L "$TMUX_SOCKET" list-panes | wc -l | bc)
+ echo "$num_panes"
+}
+
+tmux_active_panes() {
+ active_panes=$(tmux -L "$TMUX_SOCKET" list-panes -F '#{pane_active}' | tr '\n' '-' | sed 's/-$//g')
+ echo "$active_panes"
+}
+
+tmux_is_zoomed() {
+ is_zoomed=$(tmux -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'
+
+ ./toggle-term.sh split
+
+ 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'
+
+ ./toggle-term.sh split
+ ./toggle-term.sh split
+
+ 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
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 "$@"
diff --git a/vendor/bats b/vendor/bats
new file mode 160000
+Subproject 855844b8344e67d60dc0f43fa39817ed7787f14
diff --git a/vendor/bats-assert b/vendor/bats-assert
new file mode 160000
+Subproject 3be0fb7856791b4a64aef7a1336e965f5252e45
diff --git a/vendor/bats-support b/vendor/bats-support
new file mode 160000
+Subproject 0ad082d4590108684c68975ca517a90459f05cd