-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm.sh
executable file
·222 lines (174 loc) · 3.79 KB
/
tm.sh
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env sh
set -e
err() {
printf >&2 "error: %s\n" "$@"
exit 1
}
require() {
for arg; do
if ! command -v "${arg}" >/dev/null; then
missing_commands="${missing_commands}\"${arg}\" "
fi
done
if [ -n "${missing_commands}" ]; then
err "cannot find command ${missing_commands}"
fi
}
require tmux fzf zoxide
help() {
cat <<EOF
tm
tmux session manager using fzf and zoxide. Requires tmux, zoxide and fzf to be
available in the system path.
USAGE
tm [<command>] [<args>] [-h | --help]
COMMANDS
open Open a directory to a new session or an existing session
close Close a session
rename Rename a session
OPTIONS
-h --help Show help
For additional help use tm <command> -h
EOF
exit 2
}
help_open() {
cat <<EOF
tm open
Open a fuzzy finder with all the existing sessions, including directories
recorded by zoxide. The session path consists of a list of directories returned
by 'zoxide query -l' and any existing tmux sessions, which are represented as
'session://<session_name>'.
USAGE
open <session_path>
OPTIONS
-h --help Show help
EOF
exit 2
}
help_close() {
cat <<EOF
tm close
Close the current session, or, if given a session name, close that session
instead.
USAGE
close [<session_name>]
OPTIONS
-h --help Show help
EOF
exit 2
}
help_rename() {
cat <<EOF
tm rename
Rename the current tmux session with the provided new name, or alternatively,
use the basename of the current working directory.
USAGE
rename [<new_session_name>]
OPTIONS
-h --help Show help
EOF
exit 2
}
new_session() {
tmux new-session -A -d -s "$1" -c "$2"
}
switch_session() {
tmux switch -t "$1"
}
has_session() {
tmux has-session -t "$1"
}
close_session() {
[ "$#" -gt 1 ] && err "Too many arguments provided, use \`tm close <session_name>\`"
for arg; do
case "${arg}" in
-h | --help) help_close ;;
-*) err "Unknown option $1" ;;
esac
done
if [ -z "$1" ]; then
tmux kill-session
else
tmux kill-session -t "$1"
fi
}
open_session() {
[ "$#" -gt 1 ] && err "Too many arguments provided, use \`tm open <session_path>\`"
[ -z "$1" ] && session="$(basename "$(pwd)")" || session="$(basename "$1")"
if ! has_session "${session}" 2>/dev/null; then
new_session "${session}" "$1"
fi
switch_session "${session}"
}
get_existing_sessions() {
tmux ls | sed 's/:.*$//' | sed 's/^/session:\/\//'
}
fzf_preview() {
fzf \
--padding=1,2 \
--pointer=" " \
--reverse \
--no-scrollbar \
--preview-window=right,60%,border-left \
--preview="printf '%s' {} | sed 's/session:\\/\\///' | xargs tmux capture-pane -ep -t | cat"
}
session_opener() {
for arg; do
case "${arg}" in
-h | --help) help_open ;;
-*) err "Unknown option $1" ;;
esac
done
session="$(printf "%s\n%s" "$(get_existing_sessions)" "$(zoxide query -l)" | fzf_preview)"
open_session "${session}"
exit 0
}
rename_session() {
new_session_name="$1"
[ -z "${new_session_name}" ] && {
working_directory="$(pwd)"
new_session_name="${working_directory##*/}"
}
for arg; do
case "${arg}" in
-h | --help) help_rename ;;
-*) err "Unknown option $1" ;;
esac
done
tmux rename-session "${new_session_name}"
exit 0
}
enum() {
enum_str="$1"
shift
for arg; do
enum_str="${enum_str}|${arg}"
done
printf "^(%s)$" "${enum_str}"
}
parse_command() {
if printf "%s" "$1" | grep -qE "$2"; then
readonly CMD="$1"
fi
}
tm() {
[ "$#" -eq 0 ] && help
commands="$(enum open rename close)"
for arg; do
if [ -z "${CMD}" ]; then
parse_command "${arg}" "${commands}"
shift
fi
done
case "${CMD}" in
open) session_opener "$@" ;;
close) close_session "$@" ;;
rename) rename_session "$@" ;;
*)
printf "error: %s\n" "Unknown command"
help
;;
esac
}
tm "$@"