-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm
executable file
·135 lines (110 loc) · 3.69 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
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
#!/bin/sh
picker=${TM_PICKER:-"fzf"}
template_dir=${TM_TEMPLATES_DIR:-"${XDG_CONFIG_HOME:-$HOME/.config}/tmux-companion"}
: "${TM_TEMPLATES:=true}"
die() { printf '%s: %s.\n' "$0" "$1" exit 1 >&2; }
# Searches for all non hidden directories
scrs() {
tmux list-sessions 2>/dev/null
[ -n "$TM_SRCS" ] && find $TM_SRCS -maxdepth 1 -type d
}
help() {
less <<EOF
NAME
tm - tmux companion
SYNOPSIS
usage: tm [OPTIONS] [DIRECTORY|NAME]
OPTIONS
-h, --help
display help
-t, --template
create or edit a template file for a specified directory or arbitrary
name or, if no arguments are given, the current working directory will
be used
ENVIROMENT
The script is configured and customized through enviroment variables.
TM_SRCS: space separetad list of directories where tm's will let you choose
when called with no arguments (default:"")
TM_PICKER: fuzzy finder/picker for selecting directories from from your
TM_SRCS (default:"fzf")
TM_TEMPLATES: disable templates capabilities
TM_TEMPLATES_DIR: where tm searches for directory templates (default:"\$XDG_CONFIG_HOME/tmux-companion/")
EOF
}
template_get() { printf "%s/%s.template\n" "$template_dir" "$1"; }
template_gen() {
basename=$(basename "$1" | tr '[:lower:]' '[:upper:]' | tr '-' '_' | cut -d'.' -f1)
cat <<EOF >"$1"
#!/bin/sh
# Welcome to the default preset for tmux-companion. As you can see this is a
# POSIX shellscript but you can use whatever POSIX compatible shell language
# you'd like.
###############################################################################
# BE CAREFULL THIS CAN DESTROY YOUR SYSTEM IF YOU DON'T KNOW WHAT YOU ARE DOING
###############################################################################
# This is here to prevent re-executon when switching between sessions
if [ -z "\$TM_LOCK_$basename" ] && [ -n "\$TMUX" ]; then
export TM_LOCK_$basename=1
elif [ -n "\$TM_LOCK_$basename ]; then
return
fi
# You can run whatever tmux command you want:
# tmux new-window -d top
# tmux spilt-window python3
# tmux spilt-window ipython
EOF
}
template_run() {
target=$(template_get "$2")
"$TM_TEMPLATES" && [ -r "$target" ] && tmux send-keys -t "$1" ". $target" Enter
}
template_edit() {
target=$(template_get "${1:-$(basename "$PWD")}")
if [ -d "$template_dir" ]; then
! [ -f "$target" ] &&
template_gen "$target"
${EDITOR:-vi} "$target"
else
die "$template_dir directory doesn't exitst"
fi
}
# Getting first 8 caracters of name for preventing tmux to truncate the
# name label box.
# If you have the directory name "tmux-companion" tmux will truncate at the
# 9th line like so: '[tmux-comp'. If instead we take the first 8 characters
# will become something like this: '[tmux-com]'
session_name_fmt() { basename "$1" | sed "s/\./_/g; s/\ /-/g" | head -c8; }
main() {
case "$1" in
"")
sel=$(scrs | "$picker")
;;
-h | --help)
help
exit 0
;;
-t | --template)
template_edit "$2"
;;
.)
sel="$PWD"
;;
*)
sel="$*"
;;
esac
[ -z "$sel" ] && exit 0
session_name_bs=$(basename "$sel")
session_name=$(session_name_fmt "$sel")
if [ -n "$TMUX" ]; then # If in tmux
tmux has-session -t "$session_name" 2>/dev/null ||
tmux new-session -ds "$session_name" -c "$sel"
tmux switch-client -t "$session_name"
elif [ -z "$TMUX" ]; then # If outside of tmux
tmux has-session -t "$session_name" 2>/dev/null ||
tmux new-session -s "$session_name" -c "$sel"
tmux attach -t "$session_name"
fi
template_run "$session_name_bs" "$sel"
}
main "$@"