-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmuxspace
executable file
·225 lines (183 loc) · 5.27 KB
/
tmuxspace
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
223
224
225
TMUXSPACE=tmuxspace
VERSION=0.0.1
if [ $# -ne 1 ]
then
echo "$0: missing file operand"
echo "Try '$0 --help' for more information."
exit 1
fi
if [ "$1" = '-h' ] || [ "$1" = '--help' ]
then
cat <<-USAGE
Usage: $0 [FILE]
Start a new tmux workspace (session). It uses tmux-up under
the hood.
The FILE argument is used to bootstrap the new tmux session
if it doesn't already exist. It must end with .tmuxspace.
It must contain lines in the following format:
path(name)[setup]
path: The path to the directory
name: The name of the window
setup: The way you want that window to be setup. It must be 3
characters long.
[ ]: One pane
[ | ]: Two panes split vertically
[---]: Two panes split horizontally
[-| ]: Three panes - two on the left and one on the right
[ |-]: Three panes - one on the left and two on the right
[-'-]: Three panes - two on the top and one on the bottom
[-,-]: Three panes - one on the top and two on the bottom
[-|-]: Four tiled panes
You can have mutiple windows by adding another line
--help display this help and exit
--version output version information and exit
USAGE
exit 0
fi
if [ "$1" = '-v' ] || [ "$1" = '--version' ]
then
cat <<-EOF
$TMUXSPACE $VERSION
Copyright (C) 2020 Francesco Ferraioli
License MIT: <http://opensource.org/licenses/MIT>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
EOF
exit 0
fi
INPUT_FILE=${1}
if [[ "$INPUT_FILE" != *.tmuxspace ]]
then
echo "$0: ‘${INPUT_FILE}’: Needs to end with .tmuxspace"
exit 1
fi
if [ ! -f "$INPUT_FILE" ]
then
echo "$0: ‘${INPUT_FILE}’: No such file"
exit 1
fi
OUTPUT_FILE="$(basename "$INPUT_FILE" .tmuxspace).conf"
tmuxspace_run() {
if [ -f "$OUTPUT_FILE" ]
then
rm $OUTPUT_FILE
fi
touch $OUTPUT_FILE
IFS=$'\n' read -d '' -r -a windows < $INPUT_FILE
create_window "${windows[0]}"
for window in "${windows[@]:1:(${#windows[@]}-1)}"; do
if [[ "${window}" =~ ^execute:[0-3]:.* ]]; then
execute_command "${window}"
elif [[ "${window}" =~ ^write:[0-3]:.* ]]; then
write_command "${window}"
else
new_window
create_window "${window}"
fi
done
tmux-up $OUTPUT_FILE
rm $OUTPUT_FILE
}
create_window() {
DIR=$(echo "${1}" | sed 's/\(.*\)(.*)\[.*\]/\1/')
NAME=$(echo "${1}" | sed 's/.*(\(.*\))\[.*\]/\1/')
SETUP=$(echo "${1}" | sed 's/.*(.*)\(\[.*\]\)/\1/')
rename_window "${NAME}"
case "${SETUP}" in
"[ | ]")
go_to_dir "${DIR}"
split_vertically
go_to_dir "${DIR}"
select_pane "L"
;;
"[---]")
go_to_dir "${DIR}"
split_horizonally
go_to_dir "${DIR}"
select_pane "U"
;;
"[-| ]")
go_to_dir "${DIR}"
split_vertically
go_to_dir "${DIR}"
select_pane "L"
split_horizonally
go_to_dir "${DIR}"
select_pane "U"
;;
"[ |-]")
go_to_dir "${DIR}"
split_vertically
go_to_dir "${DIR}"
split_horizonally
go_to_dir "${DIR}"
select_pane "L"
;;
"[-'-]")
go_to_dir "${DIR}"
split_horizonally
go_to_dir "${DIR}"
select_pane "U"
split_vertically
go_to_dir "${DIR}"
select_pane "L"
;;
"[-,-]")
go_to_dir "${DIR}"
split_horizonally
go_to_dir "${DIR}"
split_vertically
go_to_dir "${DIR}"
select_pane "U"
;;
"[-|-]")
go_to_dir "${DIR}"
split_vertically
go_to_dir "${DIR}"
split_horizonally
go_to_dir "${DIR}"
select_pane "L"
split_horizonally
go_to_dir "${DIR}"
select_pane "U"
;;
*)
go_to_dir "${DIR}"
;;
esac
}
execute_command() {
PANE=$(echo "${1}" | sed 's/execute:\([0-3]\):.*/\1/')
COMMAND=$(echo "${1}" | sed 's/execute:[0-3]:\(.*\)/\1/')
add_line_to_output_file "select-pane -t $PANE"
add_line_to_output_file "send-keys '${COMMAND}' C-m"
}
write_command() {
PANE=$(echo "${1}" | sed 's/write:\([0-3]\):.*/\1/')
COMMAND=$(echo "${1}" | sed 's/write:[0-3]:\(.*\)/\1/')
add_line_to_output_file "select-pane -t $PANE"
add_line_to_output_file "send-keys '${COMMAND}'"
}
new_window() {
add_line_to_output_file "new-window"
}
rename_window() {
add_line_to_output_file "rename-window '$1'"
}
select_pane() {
add_line_to_output_file "select-pane -$1"
}
go_to_dir() {
add_line_to_output_file "send-keys 'cd $1' C-m"
add_line_to_output_file "send-keys 'clear' C-m"
}
split_vertically() {
add_line_to_output_file "split-window -h"
}
split_horizonally() {
add_line_to_output_file "split-window -v"
}
add_line_to_output_file() {
echo "$1" >> $OUTPUT_FILE
}
tmuxspace_run