-
Notifications
You must be signed in to change notification settings - Fork 56
/
smartcd_template
220 lines (210 loc) · 10.2 KB
/
smartcd_template
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
function smartcd_template() {
local action=$1; shift
# Going to cheat here a bit because doing this right is _hard_
# temporarily set nonomatch in zsh who will barf on the following ls without it
if [[ -n $ZSH_VERSION ]]; then
setopt localoptions
setopt nonomatch
fi
local base=$(_smartcd_base)
# Force an empty argument to ls in order to gracefully handle nullglob
case $(command ls '' $base/template_* 2>/dev/null) in
'') ;;
*) smartcd_upgrade_database;;
esac
case $action in
_init) local name="$1"; shift
local from="$1"
if [[ -n "$from" ]]; then
if [[ -f "$base/scripts$from/bash_enter" ]]; then
local enter_orig="$(eval command cat \"$base/scripts$from/bash_enter\")"
enter_orig="${enter_orig//$from/__PATH__}"
fi
if [[ -f "$base/scripts$from/bash_leave" ]]; then
local leave_orig="$(eval command cat \"$base/scripts$from/bash_leave\")"
leave_orig="${leave_orig//$from/__PATH__}"
fi
fi
command mkdir -p "$base/templates"
if [[ ! -f "$base/templates/$name" ]]; then
command cat <<EOF > "$base/templates/$name"
########################################################################
# This is a smartcd template. Edit this file to create a named
# configuration you can copy to any number of directories. This is
# useful if you often use the same settings in different directories
# but with minor differences.
#
# The string __PATH__ will be replaced with the directory being
# configured when this template is installed, so you can set variables:
#
# autostash PATH=__PATH__/bin:\$PATH
#
# This template is named '$name'
########################################################################
# Enter any bash_enter commands below here: (leave this line!)
$enter_orig
# Enter any bash_leave commands below here: (leave this line!)
$leave_orig
EOF
fi
;;
edit|-e) local name="$1"; shift
if [[ -n $name ]]; then
smartcd_template _init "$name"
${EDITOR:-vi} "$base/templates/$name"
else
echo "Usage: smartcd template edit <name>"
fi
;;
create|-c) local name="$1"; shift
if [[ -n $name ]]; then
if [[ -f "$base/templates/$name" ]]; then
echo "Template $name already exists, run \"smartcd template edit $name\" to modify it"
else
smartcd_template _init "$name" "$PWD"
smartcd_template edit "$name"
fi
else
echo "Usage: smartcd template create <name>"
fi
;;
list|-l) for file in $base/templates/*; do
# if the user has shell options that leave us with a literal asterisk here
# in the event of no matches, we'll want to skip it. rather than check and
# twiddle shell options, let's just see if that's what happened
if [[ "$file" == "$base/templates/*" ]]; then
break
fi
echo ${file#*templates/}
done
;;
show|-s) local name="$1"; shift
if [[ -n $name ]]; then
if [[ -f "$base/templates/$name" ]]; then
command cat "$base/templates/$name"
else
echo "Template $name not found"
fi
else
echo "Usage: smartcd template show <name>"
fi
;;
show_part) local name="$1"; shift
if [[ -n $name ]]; then
if [[ -f "$base/templates/$name" ]]; then
echo "# begin smartcd template $name $1"
local saving line=
while builtin read -r line; do
case $line in
"# Enter any bash_$1 commands"*) saving=1;;
"# Enter any bash"*) saving=;;
*) if [[ -n $saving ]]; then echo "# $line"; fi;;
esac
done < "$base/templates/$name"
echo "# end smartcd template $name $1"
else
echo "Template $name not found"
fi
else
echo "Usage: smartcd template show <name>"
fi
;;
install|-i) local whence="global"
if [[ -n $SMARTCD_DEFAULT_LOCATION ]]; then
whence=$SMARTCD_DEFAULT_LOCATION
fi
local system=
while [[ $1 =~ ^-- ]]; do
opt="$1"
case $opt in
--host) whence="host";;
--global) whence="global";;
--system) system="true";;
*) echo "$opt unknown"
return
;;
esac
shift
done
local name="$1"
if [[ -n $name ]]; then
if [[ -f "$base/templates/$name" ]]; then
local current_dir=$PWD
local mode= line=
while builtin read -r line; do
if [[ $line =~ "Enter any bash_enter commands" ]]; then
mode=enter
continue
fi
if [[ $line =~ "Enter any bash_leave commands" ]]; then
mode=leave
continue
fi
if [[ "$mode" == "enter" ]]; then
if [[ -z "$notified_enter" ]]; then
echo "Configuring $whence enter script from template $name"
local notified_enter=1
fi
echo "$line" | _smartcd_file_check "enter" "$whence" "$system" "append" "$current_dir"
elif [[ "$mode" == "leave" ]]; then
if [[ -z "$notified_leave" ]]; then
echo "Configuring $whence leave script from template $name"
local notified_leave=1
fi
echo "$line" | _smartcd_file_check "leave" "$whence" "$system" "append" "$current_dir"
fi
done < "$base/templates/$name"
else
echo "Template $name not found"
fi
else
echo "Usage: smartcd template install <name>"
fi
;;
delete|rm|-d) local name="$1"; shift
if [[ -n $name ]]; then
if [[ -f "$base/templates/$name" ]]; then
command rm "$base/templates/$name"
else
echo "Template $name not found"
fi
else
echo "Usage: smartcd template delete <name>"
fi
;;
run) local name="$1"; shift
local mode="$1"; shift
if [[ -z $mode ]]; then
mode="$smartcd_runmode"
fi
if [[ -n $name ]]; then
if [[ -f "$base/templates/$name" ]]; then
local contents saving line= _old_ifs="$IFS"
IFS=$'\n'
while builtin read -r line; do
case $line in
"# Enter any bash_$mode commands"*) saving=1;;
"# Enter any bash"*) saving=;;
*) if [[ -n $saving ]]; then
contents="$contents"$'\n'"$line"
fi
;;
esac
done < "$base/templates/$name"
IFS="$_old_ifs"
_smartcd_exec "$contents" "$PWD"
else
echo "Template $name not found"
fi
else
echo "Usage: smartcd template run <name> (enter|leave)"
fi
;;
'') echo "Usage: smartcd template (edit|create|list|show|run|install [--host|--global] [--system]| delete) [args]"
;;
*) echo "Unknown action $action."
echo "Usage: smartcd template (edit|create|list|show|run|install [--host|--global] [--system]|delete ) [args]"
;;
esac
}
# vim: filetype=sh autoindent expandtab shiftwidth=4 softtabstop=4