Skip to content

Commit 1a590b2

Browse files
committed
fix menu selection logic
- Window selection should not span across sessions - When ton-prioritize-window is true, do not consider other windows in session. - When there is only one item to select in the menu, auto-select it. Fixes #7
1 parent 34da7ca commit 1a590b2

File tree

3 files changed

+19
-59
lines changed

3 files changed

+19
-59
lines changed

scripts/ton

+18-11
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ shopt -s globstar
44
get_tmux_option() {
55
local option="$1"
66
local default_value="$2"
7-
local option_value=$(tmux show-option -gqv "$option")
7+
local option_value=$(tmux show-option -gqv "@$option")
88
if [ -z "$option_value" ]; then
99
echo "$default_value"
1010
else
1111
echo "$option_value"
1212
fi
1313
}
1414

15-
OPEN_STRATEGY="$(get_tmux_option "$open_strategy" ":e")"
16-
MENU_STYLE="$(get_tmux_option "$menu_style")"
17-
MENU_SELECTED_STYLE="$(get_tmux_option "$menu_selected_style")"
18-
PRIORITIZE_WINDOW="$(get_tmux_option "$prioritize_window")"
15+
OPEN_STRATEGY="$(get_tmux_option "open-strategy" ":e")"
16+
MENU_STYLE="$(get_tmux_option "menu-style" "")"
17+
MENU_SELECTED_STYLE="$(get_tmux_option "menu-selected-style")"
18+
PRIORITIZE_WINDOW="$(get_tmux_option "ton-prioritize-window")"
1919

2020
if [ -n "$MENU_STYLE" ]; then
2121
tmux_menu_s="-s $MENU_STYLE"
@@ -44,13 +44,14 @@ LINE=${a[1]:-0}
4444
COLUMN=${a[2]:-0}
4545

4646
# Get all nvim listening sockets (default location)
47-
# TODO: don't use `ls` here. Maybe `find` instead
47+
# TODO: don't use $(ls) here. Maybe $(find) instead
4848
readarray -t LISTEN_SOCKS < <(ls "${XDG_RUNTIME_DIR:-${TMPDIR}nvim.${USER}}"/**/nvim.*.0 2>/dev/null)
4949

5050
CURRENT_WINDOW_INDEX=$(tmux display-message -p '#{window_index}')
5151
CURRENT_SESSION_NAME=$(tmux display-message -p '#{session_name}')
5252

5353
MENU_ARGS=()
54+
MENU_CHOICES=0
5455
SOCK_COUNT=${#LISTEN_SOCKS[@]}
5556
SOCK_INDEX=1
5657
for sock in "${LISTEN_SOCKS[@]}"; do
@@ -71,28 +72,34 @@ for sock in "${LISTEN_SOCKS[@]}"; do
7172
window_index=${ids[0]}
7273
window_name=${ids[1]}
7374
pane_index=${ids[2]}
74-
session_name=${ids[3]}
75+
session_name=${ids[3]//[$'\r\n']/}
7576

76-
[ "$CURRENT_SESSION_NAME" != "$session_name" ] || continue
77+
if [[ "$CURRENT_SESSION_NAME" != "$session_name" ]]; then
78+
continue
79+
fi
7780

7881
c1="nvim --server $sock --remote-send \"<esc>$OPEN_STRATEGY $FILE<cr>\""
7982
c2="nvim --server $sock --remote-send \"<esc>:call cursor($LINE, $COLUMN)<cr>\""
80-
c3="tmux selectw -t $window_index && tmux selectp -t $pane_index"
83+
c3="tmux selectw -t $session_name:$window_index && tmux selectp -t $pane_index"
8184
remote_open="$c1 && $c2 && $c3"
85+
echo "wincount: $SESSION_WINDOW_COUNT, $PRIORITIZE_WINDOW, $window_index, $CURRENT_WINDOW_INDEX"
8286
if [[ $SOCK_COUNT == 1 || ($window_index == "$CURRENT_WINDOW_INDEX" && $PRIORITIZE_WINDOW == true) ]]; then
8387
# we found the only nvim, or found the first one in our window when prioritizing current window
8488
eval "$remote_open"
8589
exit 0
8690
else
8791
# store this nvim instance for selection
92+
MENU_CHOICES=$((MENU_CHOICES + 1))
8893
MENU_ARGS+=("[$window_index $window_name]: pane $pane_index" "$SOCK_INDEX" "run '$remote_open'")
8994
fi
9095
SOCK_INDEX=$((SOCK_INDEX + 1))
9196
done
9297

93-
if [[ ${MENU_ARGS[0]} ]]; then
98+
if [[ $MENU_CHOICES == 1 ]]; then
99+
eval "$(echo "${MENU_ARGS[2]#'run '}" | tr -d "'")"
100+
elif [[ $MENU_CHOICES -gt 1 ]]; then
101+
echo "${MENU_ARGS[@]}"
94102
# open menu for selection
95-
echo "$tmux_menu_s $tmux_menu_H"
96103
# shellcheck disable=SC2086
97104
tmux menu $tmux_menu_s $tmux_menu_H -T "tmux-open-nvim: $FILE" "${MENU_ARGS[@]}"
98105
else

scripts/variables.sh

-10
This file was deleted.

tmux_open_nvim.tmux

+1-38
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,3 @@
11
#!/usr/bin/env bash
22

3-
get_tmux_option() {
4-
local option="$1"
5-
local default_value="$2"
6-
local option_value=$(tmux show-option -gqv "$option")
7-
if [ -z "$option_value" ]; then
8-
echo "$default_value"
9-
else
10-
echo "$option_value"
11-
fi
12-
}
13-
14-
set_open_strategy() {
15-
local strategy=$(get_tmux_option "$open_strategy" "$open_strategy_default")
16-
tmux set-option -gq "$open_strategy" "$strategy"
17-
}
18-
19-
set_menu_style() {
20-
local style=$(get_tmux_option "$menu_style")
21-
tmux set-option -gq "$open_strategy" "$style"
22-
}
23-
24-
set_menu_selected_style() {
25-
local style=$(get_tmux_option "$menu_selected_style")
26-
tmux set-option -gq "$menu_selected_style" "$style"
27-
}
28-
29-
set_prioritize_window() {
30-
local opt=$(get_tmux_option "$prioritize_window" "$prioritize_window_default")
31-
tmux set-option -gq "$prioritize_window" "$opt"
32-
}
33-
34-
main() {
35-
set_open_strategy
36-
set_menu_style
37-
set_menu_selected_style
38-
set_prioritize_window
39-
}
40-
main
3+
# There doesn't seem to be anything here.

0 commit comments

Comments
 (0)