-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm
executable file
·59 lines (43 loc) · 1.05 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
#!/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
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