-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm
executable file
·65 lines (48 loc) · 1.31 KB
/
tm
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
#!/bin/bash
TM_ROOT="${TM_ROOT:-$HOME/Code}"
TM_DEPTH=${TM_DEPTH:-3}
TM_PROJECT_CONTAINS="${TM_PROJECT_CONTAINS:-.git}"
TM_CRITERIA="${TM_CRITERIA:--type d -name $TM_PROJECT_CONTAINS}"
TM_FILTER="${TM_FILTER:-cat}"
TM_EDITOR="${TM_EDITOR:-${EDITOR:-vi}}"
## Functions
err()
{
echo "ERROR: $(basename "$0"): $*" 1>&2
exit 1
}
running_within_tmux()
{
[ -n "$TMUX" ]
}
## Main program
[ -n "$DEBUG" ] && set -x
# It's easy to run tm when you really meant to type tmux (e.g. `tm a` to
# attach to a running session). tm takes no arguments itself, so to handle
# that, if we've been passed any arguments we pass them through to tmux,
# then exit.
[ $# -gt 0 ] && exec tmux "$@"
MAX_DEPTH=$((TM_DEPTH + 1))
DIR=$(
find "$TM_ROOT" -maxdepth $MAX_DEPTH $TM_CRITERIA 2>/dev/null | \
sed "s,^$TM_ROOT/,,; s,/${TM_PROJECT_CONTAINS}$,," | \
$TM_FILTER | \
fzf
)
FZF_RC=$?
if [ -z "$DIR" ]; then
if [ $FZF_RC -eq 1 ]; then
err "no matching path"
else
exit $FZF_RC
fi
fi
SESSION="$(basename "$DIR" | tr '[:punct:]' '-')"
if ! tmux has-session -t "=$SESSION" 2>/dev/null; then
tmux new-session -d -s "$SESSION" -c "$TM_ROOT/$DIR" -n editor "$TM_EDITOR"
fi
if running_within_tmux; then
tmux switch-client -t "$SESSION"
else
tmux attach-session -t "$SESSION"
fi