-
Notifications
You must be signed in to change notification settings - Fork 0
/
msrc.zshrc
275 lines (243 loc) · 8.66 KB
/
msrc.zshrc
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/zsh
# =========================================
# Modularized Shell Runtime Configuration
# GitHub: https://github.com/ChristianSilvermoon/Modularized-Shell-Runtime-Config
# =================================================================================
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <https://unlicense.org>
# =========================================
# Enable required ZSH Modules
zmodload zsh/datetime
# MSRC Directory Setup
[ "$ZSH_MSRC_DIR" ] || export ZSH_MSRC_DIR="$HOME/.zshrc.d"
[ -d "$ZSH_MSRC_DIR" ] || mkdir -p "$ZSH_MSRC_DIR"
# MSRC Function
msrc() {
case "$1" in
"new"|"-n")
if [ "$(echo "$2" | grep ".zshrc$")" ]; then
echo "The extension \".zshrc\" is automatically appended, please don't include it." 1>&2
return 1
elif [ "$(echo "$2" | grep "/")" ]; then
echo "You cannot use \"/\" in file names." 1>&2
return 1
elif [ -e "$ZSH_MSRC_DIR/$2.zshrc" ]; then
echo "The file \"$2.zshrc\" already exists! Please specify a different name." 1>&2
return 1
elif [ ! "$EDITOR" ]||[ ! "$(command -v "$EDITOR")" ]; then
echo "Ensure \"\$EDITOR\" is set to a valid command." 1>&2
return 1
else
echo -e "#!/bin/zsh\n# New zshrc\n" > "$ZSH_MSRC_DIR/$2.zshrc"
$EDITOR "$ZSH_MSRC_DIR/$2.zshrc"
echo "Remember to enable your new file!" 1>&2
return 0
fi
;;
"remove"|"rm"|"-r")
if [ "$(echo "$2" | grep ".zshrc$")" ]; then
echo "The extension \".zshrc\" is automatically appended, please don't include it." 1>&2
return 1
elif [ "$(echo "$2" | grep "/")" ]; then
echo "You cannot use \"/\" in file names." 1>&2
return 1
elif [ ! -e "$ZSH_MSRC_DIR/$2.zshrc" ]; then
echo "The file \"$2.zshrc\" does not exist! Please specify a different name." 1>&2
return 1
else
rm -iv "$ZSH_MSRC_DIR/$2.zshrc"
return 0
fi
;;
"rename"|"mv"|"-m")
if [ "$(echo "$2" | grep ".zshrc$")" -o "$(echo "$3" | grep ".zshrc$")" ]; then
echo "The extension \".zshrc\" is automatically appended, please don't include it." 1>&2
return 1
elif [ "$(echo "$2" | grep "/")" -o "$(echo "$3" | grep "/")" ]; then
echo "You cannot use \"/\" in file names." 1>&2
return 1
elif [ ! -e "$ZSH_MSRC_DIR/$2.zshrc" ]; then
echo "The file \"$2.zshrc\" does not exist! Please specify a different name." 1>&2
return 1
else
mv -iv "$ZSH_MSRC_DIR/$2.zshrc" "$ZSH_MSRC_DIR/$3.zshrc"
return 0
fi
;;
"edit"|"-e")
if [ ! "$EDITOR" ]||[ ! "$(command -v "$EDITOR")" ]; then
echo "Ensure \"\$EDITOR\" is set to a valid command." 1>&2
return 1
elif [ ! "$2" ]; then
echo -e "\e[1mSelect a config file to edit:\e[22m"
local editable_files=()
local i
for i in $ZSH_MSRC_DIR/*.zshrc; do
editable_files+=( "$(basename "$i" | sed 's/\.zshrc$//g')" )
done
local PS3="$(echo -e "\e[1mEnter a #: \e[22m")"
select i in "${editable_files[@]}"; do
if [ "$i" ]; then
$EDITOR "$ZSH_MSRC_DIR/$i.zshrc"
return 0
else
echo "No valid file was selected, aborting." 1>&2
return 1
fi
done
return 0
elif [ "$(echo "$2" | grep ".zshrc$")" ]; then
echo "The extension \".zshrc\" is automatically appended, please don't include it." 1>&2
return 1
elif [ "$(echo "$2" | grep "/")" ]; then
echo "You cannot use \"/\" in file names." 1>&2
return 1
elif [ ! -e "$ZSH_MSRC_DIR/$2.zshrc" ]; then
echo "The file \"$2.zshrc\" does not exist! Please specify a different name." 1>&2
return 1
else
$EDITOR "$ZSH_MSRC_DIR/$2.zshrc"
return 0
fi
;;
"check"|"-c")
echo "Shell check does not support ZSH."
;;
"enable"|"+x")
if [ -e "$ZSH_MSRC_DIR/$2.zshrc" ]; then
if [ ! -x "$ZSH_MSRC_DIR/$2.zshrc" ]; then
chmod +x "$ZSH_MSRC_DIR/$2.zshrc"
echo "Set \"$2.zshrc\" as executable" 1>&2
return 0
else
echo "The zshrc file \"$2.zshrc\" is already executable." 1>&2
return 1
fi
else
echo "The zshrc file \"$2.zshrc\" does not exist." 1>&2
return 1
fi
return 0
;;
"disable"|"-x")
if [ -e "$ZSH_MSRC_DIR/$2.zshrc" ]; then
if [ -x "$ZSH_MSRC_DIR/$2.zshrc" ]; then
chmod -x "$ZSH_MSRC_DIR/$2.zshrc"
echo "Set \"$2.zshrc\" as non-executable" 1>&2
return 0
else
echo "The zshrc file \"$2.zshrc\" is already not executable." 1>&2
return 1
fi
else
echo "The zshrc file \"$2.zshrc\" does not exist." 1>&2
return 1
fi
return 0
;;
"list"|"ls"|"-l")
ls --color=always -p "$ZSH_MSRC_DIR" | grep -v "/$" | sed 's/\.zshrc//g'
return 0
;;
"source"|"-s")
MSRC_LOAD_TIMES=""
MSRC_LOAD_TIME_TOTAL=""
local sloadtime
local eloadtime
local tstime="$EPOCHREALTIME"
local MSRC_FILE
for MSRC_FILE in $ZSH_MSRC_DIR/*.zshrc; do
# Skip Iteration if not executable
[ ! -x "$MSRC_FILE" ] && continue
# Store time before execution
sloadtime="$EPOCHREALTIME"
# Source if executable
source "$MSRC_FILE"
# Store time After Execution
eloadtime="$EPOCHREALTIME"
MSRC_LOAD_TIMES+="${sloadtime} ${eloadtime} ${MSRC_FILE}"$'\n'
done
# Store total load time
MSRC_LOAD_TIME_TOTAL="$((EPOCHREALTIME - tstime))"
return 0
;;
"-S"|"restart")
local jcount="$(jobs -p | wc -l)"
if [ "$jcount" = "0" ]; then
echo "Restarting Shell..." >&2
exec zsh
else
echo -e "Your shell has $jcount stopped job$([ "$jcount" = "1" ] || echo -ne 's').\nPlease \e[1mterminate\e[22m or \e[1mdisown\e[22m any jobs before restarting to avoid losing work.\nYou can see your jobs with \"jobs -l\"" 1>&2
return 1
fi
return
;;
"-C"|"cd")
cd "$ZSH_MSRC_DIR"
;;
"-t"|"times")
echo -e "Total: ${MSRC_LOAD_TIME_TOTAL}s\n\n" 1>&2
local list
for f in ${(@f)MSRC_LOAD_TIMES}; do
local stime="$(echo "$f" | cut -d ' ' -f 1)"
local etime="$(echo "$f" | cut -d ' ' -f 2)"
local file="$(echo "$f" | cut -d ' ' -f 3-)"
list+="$((etime - stime))s $file"$'\n'
done
echo "$list" | sort -r
;;
"-o"|"order")
echo "$MSRC_LOAD_TIMES" | cut -d ' ' -f 3-
;;
"--help"|"-?")
echo -e "\e[1mInfo:\e[0m"
echo -e "MSRC - Modularized Shell Runtime Configuration"
echo -e "https://github.com/ChristianSilvermoon/Modularized-Shell-Runtime-Config\n"
echo -e "\e[1mUSAGE:\e[0m\nmsrc <options>\n"
echo -e "\e[1mARGUMENTS:\e[0m"
printf " %-28s %s\n" "-?, --help" "Display this message"
printf " %-28s %s\n" "-l, ls, list" "List config files"
printf " %-28s %s\n" "-s, source" "Source Executable config files from Config Path"
printf " %-28s %s\n" "-S, restart" "Restart Shell, potentially losing work"
printf " %-28s %s\n" "-t, times" "Print (Rough) Time It Took Sourcing Files"
printf " %-28s %s\n" "-o, order" "Print Order each file was sourced"
printf " %-28s %s\n" "+x, enable <file>" "Set a config file as executable"
printf " %-28s %s\n" "-x, disable <file>" "Set a config file as non-executable"
printf " %-28s %s\n" "-n, new <file>" "Create and edit new non-executable config file"
printf " %-28s %s\n" "-r, rm, remove <file>" "Delete a config file"
printf " %-28s %s\n" "-m, mv, rename <old> <new>" "Rename a config file"
#printf " %-28s %s\n" "-c, check" "Check a config file with shellcheck"
printf " %-28s %s\n" "-C, cd" "cd to Config Path"
printf " %-28s %s\n" "-e, edit" "Edit an existing config file using \$EDITOR"
echo -e "\n\e[1mConfig Path:\e[0m\n$ZSH_MSRC_DIR"
return 0
;;
*)
echo "Invalid Option: See \"msrc --help\"" 1>&2
return 1
;;
esac
}
msrc -s