-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.qc.sh
executable file
·284 lines (248 loc) · 6.49 KB
/
.qc.sh
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
#!/bin/bash
#
# To enable, add the following 2 lines to .bashrc or .profile
#
# export QUICKCMDS=<dir>
# . $QUICKCMDS/.qc.sh
function findQC {
qcmd="$QUICKCMDS/$1"
local cxt
for cxt in . QC `cat $QUICKCMDS/.qccontexts`
do
local tcmd="$QUICKCMDS/$cxt/$1"
if [ -f $tcmd ]; then
qcmd=$tcmd
break
fi
done
}
# DESC x NAME [ARGS] : Execute a saved script
function x {
findQC $1.sh
[ $# == 0 ] && { echo usage: x NAME [ARGS]; return 1; }
[ ! -f $qcmd ] && { echo $1: command not found; return 1; }
shift
$qcmd "$@"
}
# exec mode. no need to load entire script
[ "$1" == "-x" ] && { shift; x $*; exit 0; }
# DESC xmv NAME CONTEXT : Move a script to a context
function xmv {
[ $# -ne 2 ] && { echo usage: xm NAME CONTEXT; return 1; }
_moveToContext $1.sh $2
}
# DESC xls [NAME] : List saved scripts
function xls {
echo Generating listing...
( local cxt
for cxt in . `cat $QUICKCMDS/.qccontexts` QC
do
local qcfiles=`ls $QUICKCMDS/${cxt}/*.sh 2>/dev/null`
if [ "$qcfiles" != "" ]; then
test "$cxt" != "." && echo ${cxt}
local qci
for qci in $qcfiles
do
echo "__"`basename $qci .sh` `grep '^# DESC' $qci | head -1 | sed 's/# DESC/ : /'`
done
fi
done ) | $colcmd | while read LINE
do
test "`echo "$LINE" | grep '^__'`" != "$LINE" && echo
echo "$LINE" | sed 's/__/ /'
done
}
# DESC xcat [NAME] : Display script
function xcat {
[ $# == 0 ] && { echo usage: xcat NAME ; return 1; }
findQC $1.sh
if [ -f $qcmd ]; then
echo -e '\nFile: '${qcmd}'\n'
cat ${qcmd}
else
echo Command not found
fi
}
# DESC xu [NAME] : Display command usage
function xu {
findQC `basename $1 .sh`.sh
[ $# == 0 ] && { echo usage: xu NAME ; return 1; }
[ ! -f ${qcmd} ] && { echo Command not found ; return 1; }
echo -e '\nFile: '${qcmd}'\n'
grep '^# DESC' $qcmd | sed 's/# DESC //' | sed 's/# DESC//'
}
# DESC xed NAME : Edit a script
function xed {
[ $# == 0 ] && { echo usage: xed NAME; return 1; }
findQC $1.sh
eval $qceditor $qcmd
test -f $qcmd && chmod 777 $qcmd
}
# DESC xp NAME : Edit a profile
function xp {
[ $# -ne 1 ] && { echo usage: xp CONTEXT; return 1; }
eval $qceditor $QUICKCMDS/${1}/profile
}
# DESC xr NAME COMMAND [ARGS] : Remember a script
function xr {
[ $# -lt 2 ] && { echo usage: xr NAME COMMAND [ARGS]; return 1; }
findQC $1.sh
local qcorig=$1
shift
echo $* > ${qcmd}
chmod 777 ${qcmd}
echo Saved: $qcorig
xcat ${qcorig}
}
# DESC xrm NAME : Remove a script
function xrm {
findQC $1.sh
[ $# == 0 ] && { echo usage: xrm NAME; return 1; }
[ ! -f $qcmd ] && { echo $1 not found; return 1; }
mv $qcmd ~/.local/share/Trash/files/ && echo Removed $qcmd
}
# DESC c NAME : Change to directory
function c {
findQC $1.cd
[ $# == 0 ] && { eval cd ; return; }
#echo usage: c NAME; return 1; }
[ -f $qcmd ] && { eval cd `cat $qcmd`; return; }
cd $*
}
# DESC ced NAME : Edit a directory
function ced {
findQC $1.cd
[ $# == 0 ] && { echo usage: c NAME; return 1; }
[ ! -f $qcmd ] && { echo $1 not found; return 1; }
eval $qceditor $qcmd
}
# DESC cr NAME : Remember a directory
function cr {
[ $# -ne 1 ] && { echo usage: cr NAME; return 1; }
findQC $1.cd
echo `pwd` > $qcmd && echo Remembered $1 : `pwd`
}
# DESC cmv NAME CONTEXT : Move a directory to a context
function cmv {
[ $# -ne 2 ] && { echo usage: xm NAME CONTEXT; return 1; }
_moveToContext $1.cd $2
}
# DESC crm NAME : Remove a directory
function crm {
findQC $1.cd
[ $# -ne 1 ] && { echo usage: crm NAME; return 1; }
[ ! -f $qcmd ] && { echo Directory not found; return 1; }
rm $qcmd && echo Removed $qcmd
}
# DESC cs : List directories
function cs {
( local cxt
for cxt in . `cat $QUICKCMDS/.qccontexts`
do
local qcfiles=`ls $QUICKCMDS/${cxt}/*.cd 2>/dev/null`
if [ "$qcfiles" != "" ]; then
test "$cxt" != "." && echo ${cxt}
local qci
for qci in $qcfiles
do
echo "__"`basename $qci .cd` : `cat $qci`
done
fi
done ) | $colcmd -t -s : | while read LINE
do
test "`echo "$LINE" | grep '^__'`" != "$LINE" && echo
echo "$LINE" | sed 's/__/ /'
done
}
# internal functions
function _moveToContext {
findQC $1
local cdir=$QUICKCMDS/$2
[ ! -f $qcmd ] && { echo $1: command not found; return 1; }
[ ! -d $cdir ] && { echo Context not found; return 1; }
mv $qcmd $cdir && echo `basename $1 .sh` moved to $2
}
function _loadProfiles {
local cxt
for cxt in `cat $QUICKCMDS/.qccontexts`
do
local profile=$QUICKCMDS/$cxt/profile
if [ -f $profile ]; then
# echo sourcing $cxt/profile
. $profile
fi
done
}
function _getEditor {
qceditor=$EDITOR
if [ "$qceditor" == "" ]; then
qceditor=vi
fi
}
# Bash completion
_x_completion()
{
local cur prev opts=""
cur="${COMP_WORDS[COMP_CWORD]}"
local cxt
for cxt in . `cat $QUICKCMDS/.qccontexts` QC
do
local qcfiles=`ls $QUICKCMDS/${cxt}/*.sh 2>/dev/null`
if [ "$qcfiles" != "" ]; then
test "$cxt" != "."
local qci
for qci in $qcfiles
do
opts="$opts `basename $qci .sh`"
done
fi
done
COMPREPLY=()
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ${cur} == * ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _x_completion x xed xrm xcat xmv xu
_c_completion()
{
local cxt opts=""
for cxt in . `cat $QUICKCMDS/.qccontexts`
do
local qcfiles=`ls $QUICKCMDS/${cxt}/*.cd 2>/dev/null`
if [ "$qcfiles" != "" ]; then
local qci
for qci in $qcfiles
do
opts="$opts `basename $qci .cd`"
done
fi
done
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
}
complete -F _c_completion c ced cr cmv crm
#echo Loading Quick Commands
colcmd="column -t -s :"
type -P column > /dev/null
[ $? -ne 0 ] && {
echo -e '\ncolumn command not found'
echo install for best display \(util-linux package in Cygwin\)
colcmd="cat"
}
#QUICKCMDS=`dirname ${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}`
#UICKCMDS=`realpath $QUICKCMDS`
if [ ! -d $QUICKCMDS ]; then
echo Creating directory $QUICKCMDS
mkdir $QUICKCMDS
fi
if [ ! -f $QUICKCMDS/.qccontexts ]; then
echo -e '\nContext file does not exist'
echo Creating $QUICKCMDS/.qccontexts
touch $QUICKCMDS/.qccontexts
echo Quick Commands loaded. For help type \'x help\'
fi
_getEditor
export -f x xmv xls xcat xu xed xr xrm c ced cr cmv crm cs findQC _moveToContext
export colcmd qceditor
_loadProfiles