-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsp
More file actions
executable file
·376 lines (322 loc) · 12.3 KB
/
csp
File metadata and controls
executable file
·376 lines (322 loc) · 12.3 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/env bash
# Regular Colors=
FG_R_Black="\e[0;30m"
FG_R_Red="\e[0;31m"
FG_R_Green="\e[0;32m"
FG_R_Yellow="\e[0;33m"
FG_R_Blue="\e[0;34m"
FG_R_Purple="\e[0;35m"
FG_R_Cyan="\e[0;36m"
FG_R_White="\e[0;37m"
# Bold=
FG_B_Black="\e[1;30m"
FG_B_Red="\e[1;31m"
FG_B_Green="\e[1;32m"
FG_B_Yellow="\e[1;33m"
FG_B_Blue="\e[1;34m"
FG_B_Purple="\e[1;35m"
FG_B_Cyan="\e[1;36m"
FG_B_White="\e[1;37m"
# Underline=
FG_U_Black="\e[4;30m"
FG_U_Red="\e[4;31m"
FG_U_Green="\e[4;32m"
FG_U_Yellow="\e[4;33m"
FG_U_Blue="\e[4;34m"
FG_U_Purple="\e[4;35m"
FG_U_Cyan="\e[4;36m"
FG_U_White="\e[4;37m"
# Background=
BG_R_Black="\e[40m"
BG_R_Red="\e[41m"
BG_R_Green="\e[42m"
BG_R_Yellow="\e[43m"
BG_R_Blue="\e[44m"
BG_R_Purple="\e[45m"
BG_R_Cyan="\e[46m"
BG_R_White="\e[47m"
# High Intensty=
FG_HI_Black="\e[0;90m"
FG_HI_Red="\e[0;91m"
FG_HI_Green="\e[0;92m"
FG_HI_Yellow="\e[0;93m"
FG_HI_Blue="\e[0;94m"
FG_HI_Purple="\e[0;95m"
FG_HI_Cyan="\e[0;96m"
FG_HI_White="\e[0;97m"
# Bold High Intensty=
FG_BHI_Black="\e[1;90m"
FG_BHI_Red="\e[1;91m"
FG_BHI_Green="\e[1;92m"
FG_BHI_Yellow="\e[1;93m"
FG_BHI_Blue="\e[1;94m"
FG_BHI_Purple="\e[1;95m"
FG_BHI_Cyan="\e[1;96m"
FG_BHI_White="\e[1;97m"
# High Intensty backgrounds=
BG_HI_Black="\e[0;100m"
BG_HI_Red="\e[0;101m"
BG_HI_Green="\e[0;102m"
BG_HI_Yellow="\e[0;103m"
BG_HI_Blue="\e[0;104m"
BG_HI_Purple="\e[0;105m"
BG_HI_Cyan="\e[0;106m"
BG_HI_White="\e[0;107m"
# Reset=
ClearColor="\e[0m"
while getopts hie:s:f: option; do
case $option in
h)
echo -e ""
echo -e "${FG_R_White}${BG_R_Black}This is${ClearColor} ${FG_B_Black}${BG_R_Red}Command${BG_R_Green}Scrach${BG_R_White}Pad${ClearColor} ${FG_B_White}${BG_R_Black}or${ClearColor} ${FG_B_Black}${BG_R_Red}c${BG_R_Green}s${BG_R_White}p${ClearColor}\n"
echo -e "${FG_B_Cyan}${BG_R_Black}This script lets you create a script and execute it on the fly.\n"
echo -e "${FG_R_Green}-f${ClearColor} ${FG_R_White}file/script/path${ClearColor}"
echo -e "${FG_R_Green}-s${ClearColor} ${FG_R_White}script_in_your_path${ClearColor}"
echo -e "${FG_R_Green}-i${ClearColor} ${FG_R_White}edit dirrectly \(use with -f or -s\)"
# get from history
# echo $OPTARG
# history=$(sk ~/.zsh_history)
# echo $history >> c$t.c.sh ;;
echo -e ""
;;
s)
loc=$OPTARG
s=true
;;
f)
loc=$OPTARG
f=true
;;
i)
i=true
;;
e)
lvim $(which $OPTARG | head -n 1)
# n) # Enter a name
# Name=$OPTARG;;
# \?) # Invalid option
# echo "Error: Invalid option"
# exit;;
esac
done
parsed_options=$(
getopt -n "$0" -o hisRef -- "$@"
) || exit
eval "set -- $parsed_options"
while [ "$#" -gt 0 ]; do
case $1 in
(-[Rsif]) shift;;
(-t) shift 2;;
(--) shift; break;;
(*) exit 1 # should never be reached.
esac
done
# echo "Now, the arguments are $*"
addESC() { sed 's/[][ \~`!@#$%^&*()=+{}|;:'"'"'",<>/?-]/\\&/g'; }
getnamedir () {
t=$(date +%s)
cdr=$(pwd)
# pipe=$(cat -)
}
inject () {
echo "#!/bin/env bash" > c$t.c.sh &&
echo $pipe >> c$t.c.sh &&
echo '' >> c$t.c.sh &&
tail ~/.zsh_history | sed 's/:.*;/# /' >> c$t.c.sh &&
echo '' >> c$t.c.sh &&
echo '# Tip: To get a increamental sequence in numbers select with Ctrl-v and g-v-g and then Ctrl-a' >> c$t.c.sh &&
echo '' >> c$t.c.sh &&
echo "$comm" >> c$t.c.sh
}
makekscript () {
${EDITOR:-vi} +\$ c$t.c.sh &&
# cat $cdr/c$t.c.sh | grep "^[^#]" | grep -v "^$" &> /dev/null && echo "Nothing to Execute" || rm $cdr/c$t.c.sh && exit
scripttxt=$(cat $cdr/c$t.c.sh | grep "^[^#]" | grep -v "^$")
if [[ $scripttxt == "" ]] ; then
echo "Nothing to Execute" && rm $cdr/c$t.c.sh && exit
fi
echo Executuing: &&
tput setaf 198
command -v bat >/dev/null && bat -P c$t.c.sh || cat .c.sh &&
tput setaf 7
# cat c$t.c.sh | zsh
chmod +x c$t.c.sh
mkdir -p ~/c
}
ifpiped () {
echo "Data was piped to this script!"
# echo -e "\e[0mIf you are sure Press \e[30m\e[42mEnter\e[0m otherwise Press \e[30m\e[41mCtrl-c\e[0m\"
echo -e "# Warning : Piping is being used.Therefore, script will execute on save and exit.If you do not want it executing do not save just quit." >> c$t.c.sh &&
# echo -e "read" >> c$t.c.sh
echo -e "" >> c$t.c.sh
# If we want to read the input line by line
while IFS= read pipe; do
echo "${pipe}" >> c$t.c.sh
done
# Or if we want to simply grab all the data, we can simply use cat instead
# cat
}
ask () {
echo -e "${ClearColor}Do you want to ${FG_B_Black}${BG_R_Green}Execute${ClearColor} it ? Press ${FG_B_Black}${BG_R_Green}y${ClearColor}"
echo -e "${ClearColor}Do you want to ${FG_B_Black}${BG_R_Red}Discard${ClearColor} it ? Press ${FG_B_Black}${BG_R_Red}n${ClearColor} or ${FG_B_Black}${BG_R_Red}d${ClearColor}"
echo -e "${ClearColor}Or perhaps you want to save the script with a ${FG_B_Black}${BG_R_Purple}new name${ClearColor} ? Press ${FG_B_Black}${BG_R_Purple}r${ClearColor}"
echo -e "${ClearColor}Or maybe you want to save the script without a ${FG_B_Black}${BG_R_Yellow}new name${ClearColor} ? Press ${FG_B_Black}${BG_R_Yellow}c${ClearColor}"
echo -e "${ClearColor}Oh ,and if you end up pressing enter or anything else except Ctrl-c ,it will count like c\n"
# echo $(echo $(echo $(read) >> /dev/null) >> /dev/null) >> /dev/null
read -s -n1 confirmation
if [[ $confirmation == "y" ]] ; then
run=true
elif [[ $confirmation == "n" || $confirmation == "d" ]] ; then
rm $cdr/c$t.c.sh && echo removed
exit
elif [[ $confirmation == "r" ]] ; then
echo -e "What\'s the new name ? It will be saved in ~/c btw !\n"
read -r newname
mv $cdr/c$t.c.sh ~/c/$(echo $newname | addESC) && echo "saved ~/c/$newname"
exit
elif [[ $confirmation == "c" ]] ; then
mv $cdr/c$t.c.sh ~/c && echo "saved ~c/c$t.c.sh"
exit
else
mv $cdr/c$t.c.sh ~/c && echo "saved ~c/c$t.c.sh"
exit
fi
}
executeandsave () {
./c$t.c.sh && echo -e "\n\e[30m\e[42mExecuted\e[0m\n"
scripttxt=$(cat $cdr/c$t.c.sh | grep "^[^#]" | grep -v "^$")
# echo $scripttxt
if [[ $scripttxt == "" ]] ; then
rm $cdr/c$t.c.sh
else
echo -e ""
echo -e "${ClearColor}Do you want to ${FG_B_Black}${BG_R_Green}save${ClearColor} it ? Press ${FG_B_Black}${BG_R_Green}y${ClearColor}"
echo -e "${ClearColor}Or perhaps you want to ${FG_R_Black}${BG_R_Red}delete${ClearColor} the script ? Press ${FG_R_Black}${BG_R_Red}d${ClearColor} or ${FG_R_Black}${BG_R_Red}n${ClearColor}"
echo -e "${ClearColor}Do you want to save it with a ${FG_R_Black}${BG_R_Purple}custom name${ClearColor} ? Press ${FG_R_Black}${BG_R_Purple}r${ClearColor}"
echo -e "${ClearColor}Oh ,and if you end up pressing enter or anything else ,it will count like y"
read -s -n1 confirmation
echo -e ""
if [[ $confirmation == "y" ]] ; then
mv $cdr/c$t.c.sh ~/c && echo "saved ~c/c$t.c.sh"
elif [[ $confirmation == "n" || $confirmation == "d" ]] ; then
rm $cdr/c$t.c.sh && echo removed
elif [[ $confirmation == "r" ]] ; then
echo -e "What\'s the new name ? It will be saved in ~/c btw !\n"
read -r newname
mv $cdr/c$t.c.sh ~/c/$(echo $newname | addESC) && echo "saved ~/c/$newname"
else
mv $cdr/c$t.c.sh ~/c && echo "saved ~c/c$t.c.sh"
fi
fi
}
s () {
if [[ $s == "true" ]] ; then
if [[ $i == "true" ]] ; then
cat $(which $loc | head -n 1) >> $cdr/c$t.c.sh
else
cp $(which $loc) $cdr/c$t.c.sh
fi
fi
}
f () {
if [[ $f == "true" ]] ; then
if [[ $i == "true" ]] ; then
cat $(realpath $loc) >> $cdr/c$t.c.sh
else
cp $(realpath $loc) $cdr/c$t.c.sh
fi
fi
}
comm=$@
mainscript () {
getnamedir
inject
if [ -p /dev/stdin ]; then
ifpiped
makekscript
executeandsave
else
s
f
makekscript
ask
executeandsave
fi
}
mainscript
# t=$(date +%s)
# cdr=$(pwd)
# # pipe=$(cat -)
# while getopts ":h:" option; do
# case $option in
# h) # get from history
# echo hi
# history=$(sk ~/.zsh_history)
# echo $history >> c$t.c.sh ;;
# # n) # Enter a name
# # Name=$OPTARG;;
# # \?) # Invalid option
# # echo "Error: Invalid option"
# # exit;;
# esac
# done
# echo "#!/bin/env bash" > c$t.c.sh &&
# echo $pipe >> c$t.c.sh &&
# echo '' >> c$t.c.sh &&
# tail ~/.zsh_history | sed 's/:.*;/# /' >> c$t.c.sh &&
# echo '' >> c$t.c.sh &&
# echo '# Tip: To get a increamental sequence in numbers select with Ctrl-v and g-v-g and then Ctrl-a' >> c$t.c.sh &&
# echo '' >> c$t.c.sh &&
# echo "$@" >> c$t.c.sh &&
# if [ -p /dev/stdin ]; then
# echo "Data was piped to this script!"
# # echo -e "\e[0mIf you are sure Press \e[30m\e[42mEnter\e[0m otherwise Press \e[30m\e[41mCtrl-c\e[0m\"
# echo -e "# Warning : Piping is being used.Therefore, script will execute on save and exit.If you do not want it executing do not save just quit." >> c$t.c.sh &&
# # echo -e "read" >> c$t.c.sh
# echo -e "" >> c$t.c.sh
# # If we want to read the input line by line
# while IFS= read pipe; do
# echo "${pipe}" >> c$t.c.sh
# done
# # Or if we want to simply grab all the data, we can simply use cat instead
# # cat
#
# ${EDITOR:-vi} +\$ c$t.c.sh &&
# echo Executuing: &&
# tput setaf 198
# command -v bat >/dev/null && bat -P c$t.c.sh || cat .c.sh &&
# tput setaf 7
# # echo -e "\e[0mIf you are sure Press \e[30m\e[42mEnter\e[0m otherwise Press \e[30m\e[41mCtrl-c\e[0m" &&
# # echo $(echo $(echo $(read) >> /dev/null) >> /dev/null) >> /dev/null
# # cat c$t.c.sh | zsh
# chmod +x c$t.c.sh
# ./c$t.c.sh && echo -e "\e[30m\e[42mExecuted\e[0m"
# mkdir -p ~/c
# # cat $cdr/c$t.c.sh | grep -v "#\!/bin/env bash" | grep -v "\# Warning \: Piping is being used.Therefore, script will execute on save and exit.If you do not want it executing do not save just quit." | grep -v "^$" &> /dev/null && mv $cdr/c$t.c.sh ~/c && echo "saved ~c/c$t.c.sh" || rm $cdr/c$t.c.sh
# cat $cdr/c$t.c.sh | grep "^[^#]" | grep -v "^$" &> /dev/null && mv $cdr/c$t.c.sh ~/c && echo "saved ~c/c$t.c.sh" || rm $cdr/c$t.c.sh
# else
# # echo "No input was found on stdin, skipping!"
# # # Checking to ensure a filename was specified and that it exists
# # if [ -f "$1" ]; then
# # echo "Filename specified: ${1}"
# # echo "Doing things now.."
# # else
# # echo "No input given!"
# # fi
# # echo "hello" >> /dev/null
#
# ${EDITOR:-vi} +\$ c$t.c.sh &&
# echo Executuing: &&
# tput setaf 198
# command -v bat >/dev/null && bat -P c$t.c.sh || cat .c.sh &&
# tput setaf 7
# # cat c$t.c.sh | zsh
# chmod +x c$t.c.sh
# ./c$t.c.sh && echo -e "\e[30m\e[42mExecuted\e[0m"
# mkdir -p ~/c
# echo -e "\e[0mIf you are sure Press \e[30m\e[42mEnter\e[0m otherwise Press \e[30m\e[41mCtrl-c\e[0m" &&
# echo $(echo $(echo $(read) >> /dev/null) >> /dev/null) >> /dev/null
# # cat $cdr/c$t.c.sh | grep -v "#\!/bin/env bash" | grep -v "^$" &> /dev/null && mv $cdr/c$t.c.sh ~/c && echo "saved ~c/c$t.c.sh" || rm $cdr/c$t.c.sh
# cat $cdr/c$t.c.sh | grep "^[^#]" | grep -v "^$" &> /dev/null && mv $cdr/c$t.c.sh ~/c && echo "saved ~c/c$t.c.sh" || rm $cdr/c$t.c.sh
# fi
# #comannd