blob: b3a70a5939347b37f5d1187a3f14a6ccbc79b6c0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#!/usr/bin/env bash
# tmux file opener with fallback file manager (no preview)
# Mark this pane as the file manager immediately
tmux select-pane -T "FILE_MANAGER"
# Also set the option as backup
tmux set-option -pq @file_manager 1
orig_pane="$1"
chooser_file="$HOME/.cache/tmux-fm-selected"
rm -f "$chooser_file"
# Function: pick available file manager
pick_fm() {
if command -v lf >/dev/null 2>&1; then
echo "lf"
elif command -v nnn >/dev/null 2>&1; then
echo "nnn"
elif command -v ranger >/dev/null 2>&1; then
echo "ranger"
else
echo ""
fi
}
fm=$(pick_fm)
if [[ -z "$fm" ]]; then
echo "No file manager found (lf, nnn, ranger)." >&2
cleanup
exit 1
fi
# Cleanup function to reset both title and option
cleanup() {
tmux select-pane -T ""
tmux set-option -puq @file_manager
rm -f "$chooser_file"
}
# Set trap to cleanup on exit (including when user presses 'q')
trap cleanup EXIT INT TERM
# Launch the chosen file manager with no preview where possible
case "$fm" in
nnn)
# Disable preview completely and use picker mode
# -A: disable dir auto-select, -e: open text files in editor
# -o: open files with opener, -x: show only selection
NNN_OPENER="tee \"$chooser_file\"" nnn -Axo
;;
lf)
# Disable preview and set selection path
lf -command 'set preview false' -selection-path="$chooser_file"
;;
ranger)
# Disable all previews
ranger --choosefile="$chooser_file" \
--cmd='set preview_files false' \
--cmd='set preview_directories false' \
--cmd='set preview_images false'
;;
esac
# Exit if no file chosen (user pressed 'q' or cancelled)
if [[ ! -s "$chooser_file" ]]; then
exit 0
fi
file="$(head -n 1 "$chooser_file")"
rm -f "$chooser_file"
# Restrict to current window panes and exclude the file manager pane
current_window=$(tmux display-message -p '#I')
mapfile -t panes < <(
tmux list-panes -t "$current_window" -F '#S:#I.#P' |
grep -v "^$(tmux display-message -p '#S:#I').$(tmux display-message -p '#P')$"
)
# Choose target pane
if [[ ${#panes[@]} -eq 0 ]]; then
exit 1
elif [[ ${#panes[@]} -eq 1 ]]; then
target="${panes[0]}"
else
echo "Select target pane:"
for i in "${!panes[@]}"; do
letter=$(printf "\\$(printf '%03o' $((97 + i)))") # a, b, c...
echo "$letter) ${panes[$i]}"
done
read -n 1 -p "Choice: " choice
echo
idx=$(( $(printf "%d" "'$choice") - 97 ))
if [[ $idx -ge 0 && $idx -lt ${#panes[@]} ]]; then
target="${panes[$idx]}"
else
exit 1
fi
fi
# Decide if file is text or binary
if file --mime-type "$file" 2>/dev/null | grep -q 'text/'; then
opener="${EDITOR:-$(command -v nvim || command -v vim || echo 'vi')}"
else
opener="$(command -v xdg-open || command -v open || echo 'cat')"
fi
# Send open command to target pane
tmux send-keys -t "$target" "$opener \"$file\"" C-m
|