-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux-sessionizer
executable file
·71 lines (59 loc) · 1.48 KB
/
tmux-sessionizer
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
#!/usr/bin/env bash
set -euo pipefail
log_error() {
local msg="$1"
if [ -z "$TMUX" ]; then
printf "%s\n" "$msg" >&2
else
tmux display-message -d 5000 "$msg"
fi
}
is_executable_installed() {
local cmd="$1"
if command -v "$cmd" >/dev/null; then
return 0
fi
return 1
}
select_directory_from_fzf() {
local root="$1"
local directories=("$root" "$root/dev" "$HOME/.dotfiles")
if is_executable_installed "fd"; then
fd --min-depth 1 --max-depth 1 --type directory . "${directories[@]}" | fzf --exit-0
elif is_executable_installed "find"; then
find "${directories[@]}" -mindepth 1 -maxdepth 1 -type d | fzf --exit-0
else
log_error "[ERROR:tmux-sessionizer]: neither \`fd\` nor \`find\` are executable"
return 1
fi
}
switch_to() {
if [ -z "$TMUX" ]; then
tmux attach-session -t "$1"
else
tmux switch-client -t "$1"
fi
}
main() {
local selected=""
local workspace
workspace=$(tmux showenv -g WORKSPACE | cut -d "=" -f2 || echo "$PERSONAL_WORKSPACE")
if [[ $# -eq 1 ]]; then
# Use the provided argument as the selected directory
selected=$1
elif [[ $# -eq 0 ]]; then
# Use fzf to select a directory
selected=$(select_directory_from_fzf "$workspace")
echo "$selected"
fi
# Exit the script if no directory is selected
if [[ -z "$selected" ]]; then
exit 0
fi
session_name=$(echo "$selected" | tr ".-:" "___")
if ! tmux has-session -t="$session_name" 2>/dev/null; then
tmux new-session -ds "$session_name" -c "$selected"
fi
switch_to "$session_name"
}
main "$@"