From 2249cf8d1be87c0b1be9546b40eead70733e68c0 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 20 Jun 2014 15:35:52 +0200 Subject: [PATCH 1/4] new_window: only set $window and pass `-n` if a name is provided Otherwise `new_window` after `new_window foo` would re-use the same name, resulting in an error. --- lib/layout-helpers.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/layout-helpers.sh b/lib/layout-helpers.sh index bac4cd9..703f8c8 100644 --- a/lib/layout-helpers.sh +++ b/lib/layout-helpers.sh @@ -18,9 +18,11 @@ tmux() { # - $2: (optional) Shell command to execute when window is created. # new_window() { - if [ -n "$1" ]; then window="$1"; fi + if [ -n "$1" ]; then + window="$1" + local winarg=(-n "$window") + fi if [ -n "$2" ]; then local command=("$2"); fi - if [ -n "$window" ]; then local winarg=(-n "$window"); fi tmuxifier-tmux new-window -t "$session:" "${winarg[@]}" "${command[@]}" __go_to_window_or_session_path From 0ae0bc84e331a016faa0d8da88913b02c24e7dfc Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 29 Aug 2014 22:28:25 +0100 Subject: [PATCH 2/4] Add __get_current_window_index internal helper function --- lib/layout-helpers.sh | 9 ++++ .../__get_current_window_index.test.sh | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100755 test/lib/layout-helpers/__get_current_window_index.test.sh diff --git a/lib/layout-helpers.sh b/lib/layout-helpers.sh index 703f8c8..dfa2673 100644 --- a/lib/layout-helpers.sh +++ b/lib/layout-helpers.sh @@ -313,6 +313,15 @@ __get_first_window_index() { fi } +__get_current_window_index() { + local lookup=$(tmuxifier-tmux list-windows -t "$session:" \ + -F "#{window_active}:#{window_index}" 2>/dev/null | grep "^1:") + + if [ -n "$lookup" ]; then + echo "${lookup/1:}" + fi +} + __go_to_session() { if [ -z "$TMUX" ]; then tmuxifier-tmux -u attach-session -t "$session:" diff --git a/test/lib/layout-helpers/__get_current_window_index.test.sh b/test/lib/layout-helpers/__get_current_window_index.test.sh new file mode 100755 index 0000000..cf4371c --- /dev/null +++ b/test/lib/layout-helpers/__get_current_window_index.test.sh @@ -0,0 +1,48 @@ +#! /usr/bin/env bash +source "../../test-helper.sh" +source "${root}/lib/layout-helpers.sh" + +# +# __get_current_window_index() tests. +# + +# When current window is the first and only window. +create-test-session +assert "__get_current_window_index" "0" +kill-test-session + +# When creating a second window. +create-test-session +test-socket-tmux new-window -t "$session:1" +assert "__get_current_window_index" "1" +kill-test-session + +# When creating a second window and then switching back to the first window. +create-test-session +test-socket-tmux new-window -t "$session:1" +test-socket-tmux select-window -t "$session:0" +assert "__get_current_window_index" "0" +kill-test-session + +# When creating multiples windows and switching between them randomly. +create-test-session +assert "__get_current_window_index" "0" +test-socket-tmux new-window -t "$session:1" +assert "__get_current_window_index" "1" +test-socket-tmux new-window -t "$session:2" +assert "__get_current_window_index" "2" +test-socket-tmux new-window -t "$session:3" +assert "__get_current_window_index" "3" +test-socket-tmux select-window -t "$session:1" +assert "__get_current_window_index" "1" +test-socket-tmux select-window -t "$session:0" +assert "__get_current_window_index" "0" +test-socket-tmux select-window -t "$session:3" +assert "__get_current_window_index" "3" +test-socket-tmux select-window -t "$session:2" +assert "__get_current_window_index" "2" +kill-test-session + + +# End of tests. +assert_end "__get_current_window_index()" From ef0f25591cbb07048e8f37708f851543210b591d Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 29 Aug 2014 22:29:03 +0100 Subject: [PATCH 3/4] Set $window var based on currently active window Previously $window was only set by the new_window() helper when it was passed a name argument. This caused weird behavior cause if it was called without a name it would simply attempt to recreate the same window and fail. That was until @blueyed's fix in 2249cf8. However, the $window var still wasn't being set correctly, leaving split_v(), split_h() and other helpers to operate on the wrong window. This is no longer an issue as $window is always set to the index of the currently active window at the end of new_window() and select_window(). --- lib/layout-helpers.sh | 7 +-- test/lib/layout-helpers/new_window.test.sh | 17 +++++- test/lib/layout-helpers/select_window.test.sh | 55 +++++++++++++++++++ 3 files changed, 72 insertions(+), 7 deletions(-) create mode 100755 test/lib/layout-helpers/select_window.test.sh diff --git a/lib/layout-helpers.sh b/lib/layout-helpers.sh index dfa2673..dbbcb9a 100644 --- a/lib/layout-helpers.sh +++ b/lib/layout-helpers.sh @@ -18,13 +18,11 @@ tmux() { # - $2: (optional) Shell command to execute when window is created. # new_window() { - if [ -n "$1" ]; then - window="$1" - local winarg=(-n "$window") - fi + if [ -n "$1" ]; then local winarg=(-n "$1"); fi if [ -n "$2" ]; then local command=("$2"); fi tmuxifier-tmux new-window -t "$session:" "${winarg[@]}" "${command[@]}" + window="$(__get_current_window_index)" __go_to_window_or_session_path } @@ -91,6 +89,7 @@ clock() { # select_window() { tmuxifier-tmux select-window -t "$session:$1" + window="$(__get_current_window_index)" } # Select a specific pane in the current window. diff --git a/test/lib/layout-helpers/new_window.test.sh b/test/lib/layout-helpers/new_window.test.sh index 5b162b7..4817ce2 100755 --- a/test/lib/layout-helpers/new_window.test.sh +++ b/test/lib/layout-helpers/new_window.test.sh @@ -39,9 +39,20 @@ restore __go_to_window_or_session_path kill-test-session rm "/tmp/tmuxifier-new_window-test" &> /dev/null - -# Tear down. -kill-test-server +# When called ensure it sets the $window variable to the index of the newly +# created window. +unset window +create-test-session +stub __go_to_window_or_session_path +new_window "foo" +assert "echo $window" "1" +new_window +assert "echo $window" "2" +new_window "bar" +assert "echo $window" "3" +restore __go_to_window_or_session_path +kill-test-session +unset window # End of tests. assert_end "new_window()" diff --git a/test/lib/layout-helpers/select_window.test.sh b/test/lib/layout-helpers/select_window.test.sh new file mode 100755 index 0000000..0ba4977 --- /dev/null +++ b/test/lib/layout-helpers/select_window.test.sh @@ -0,0 +1,55 @@ +#! /usr/bin/env bash +source "../../test-helper.sh" +source "${root}/lib/layout-helpers.sh" + +# +# select_window() tests. +# + +window_list() { + test-socket-tmux list-windows -t "$session:" \ + -F "#{window_active}:#{window_index}" 2>/dev/null +} + +# Selects given window when passed a window index +create-test-session +test-socket-tmux new-window -t "$session:1" +test-socket-tmux new-window -t "$session:2" +select_window 0 +assert "window_list | grep '^1:'" "1:0" +select_window 1 +assert "window_list | grep '^1:'" "1:1" +select_window 2 +assert "window_list | grep '^1:'" "1:2" +kill-test-session + +# Selects given window when passed a window name +create-test-session +test-socket-tmux new-window -t "$session:1" -n "foo" +test-socket-tmux new-window -t "$session:2" -n "bar" +select_window foo +assert "window_list | grep '^1:'" "1:1" +select_window bar +assert "window_list | grep '^1:'" "1:2" +kill-test-session + + +# When called ensure it sets the $window variable to the index of the newly +# created window. +unset window +create-test-session +test-socket-tmux new-window -t "$session:1" -n "foo" +test-socket-tmux new-window -t "$session:2" -n "bar" +select_window "foo" +assert "echo $window" "1" +select_window "bar" +assert "echo $window" "2" +select_window 1 +assert "echo $window" "1" +select_window 2 +assert "echo $window" "2" +kill-test-session +unset window + +# End of tests. +assert_end "select_window()" From fe3beeac295cd01571afbaef5d69488e105f05b4 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 29 Aug 2014 22:46:41 +0100 Subject: [PATCH 4/4] Attempt to avoid timing issue which sometimes causes test to fail --- test/lib/layout-helpers/new_window.test.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/lib/layout-helpers/new_window.test.sh b/test/lib/layout-helpers/new_window.test.sh index 4817ce2..21a601f 100755 --- a/test/lib/layout-helpers/new_window.test.sh +++ b/test/lib/layout-helpers/new_window.test.sh @@ -34,6 +34,7 @@ stub __go_to_window_or_session_path new_window "foobardoo" "touch /tmp/tmuxifier-new_window-test; bash" assert "test-socket-window-count" "2" assert "test-socket-window-count foobardoo" "1" +sleep 0.1 # attempt to avoid timing issue causing flicker assert_raises 'test -f "/tmp/tmuxifier-new_window-test"' 0 restore __go_to_window_or_session_path kill-test-session