-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux-sessionizer
executable file
·67 lines (59 loc) · 1.77 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
#!/usr/bin/env bash
# NOTES:
# - Remember to make this file executable
#
set -euo pipefail
__test_executable_cmd__() {
if [[ ! $# -eq 1 ]]; then
echo "[ERROR]: invalid number of arguments: \`$#\` (expected \`1\`)"
exit 1
fi
if [[ -x $(command -v "$1") ]]; then
return 0
fi
return 1
}
__tmux_fzf_get_session__() {
cmd="tmux list-sessions -F \"#{session_name}\" 2>/dev/null | fzf --exit-0"
if __test_executable_cmd__ "bat"; then
cmd="${cmd} --preview='tmux_tree {} | bat --style plain'"
fi
session=$(eval "$cmd")
echo "$session"
}
tms() {
local selected
WORKSPACE=$(tmux showenv -g WORKSPACE | cut -d "=" -f2 || echo "$WORKSPACE")
if [[ $# -eq 1 ]]; then
# Use the provided argument as the selected directory
selected=$1
fzf
elif [[ $# -eq 0 ]]; then
# Determine `find` command
directories=("$HOME/.config" "$WORKSPACE" "$WORKSPACE/dev" "$HOME/dotfiles")
if __test_executable_cmd__ "fd"; then
# shellcheck disable=SC2207
IFS=$'\n' search=($(fd --min-depth 1 --max-depth 1 -t d . "${directories[@]}"))
selected=$(printf "%s\n" "${search[@]}" | fzf --exit-0)
elif __test_executable_cmd__ "find"; then
# shellcheck disable=SC2207
IFS=$'\n' search=($(find "${directories[@]}" -mindepth 1 -maxdepth 1 -type d))
selected=$(printf "%s\n" "${search[@]}" | fzf --exit-0)
else
echo "[ERROR]: neither \`fd\` nor \`find\` are executable"
exit 1
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
tmux switch-client -t "$session_name"
else
echo "[ERROR]: invalid number of arguments: \`$#\` (expected \`0\` or \`1\`)"
fi
}
tms "$@"