-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.cfg
487 lines (408 loc) · 9.72 KB
/
library.cfg
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#https://launchpad.net/~kamalmostafa/+archive/ubuntu/linux-kamal-mjgbacklight
#https://wiki.archlinux.org/index.php/backlight
#kill bg job:
#https://wiki.ubuntu.com/NotifyOSD
#travis syntax: http://lint.travis-ci.org/
#http://www.grymoire.com/Unix/Sed.html
#http://www.grymoire.com/Unix/Sed.html
#defult: load where_amn_i
unset command_not_found_handle
shopt -s expand_aliases
set -u
#set +e
soft(){
trap - ERR
trap - EXIT
trap - SIGTERM
set +u
set +e
#return 0
true
}
############################################### quick mode modification
export LIBRARY_LOADED=${LIBRARY_LOADED:-false}
export MODE_SET=false
export MODE_TEST=${MODE_TEST:-false}
if [ $MODE_TEST = false ];then
( test $LIBRARY_LOADED = true ) && { echo 1>&2 [\$LIBRARY_LOADED] library already loaded; return 0; }
soft
fi
############################################## user - keep shell alive when error occurs
################################################ func for testing
assertEquals(){
print_func
[ "$1" = "$2" ] && ( print ok ) || ( print error )
}
lock_set(){
local num=${1:-0}
if [ $num -eq 0 ];then
if [ $MODE_SET = true ];then
alias set='true'
fi
else
unalias set
fi
#echo '[set-not-enable!]'; $cmd_caller'
}
set_env_for_testing(){
#export MODE_TEST=false
#type $FUNCNAME
export cmd_WTF='eval echo "Error ! $LINENO $BASH_LINENO"; sleep 2'
export delay_testing=1
}
############################################# debug /print
library_about(){
print func
print color 33 FUNCS
print_g pv '----------------------------------'
commander "cat $BASH_SOURCE | grep 'export -f' | sed 's/export -f//g'"
print line
print color 33 VARS
print_g pv '----------------------------------'
commander "cat $BASH_SOURCE | grep 'export' | grep -v 'export -f' | sed 's/export//g'"
print line
print color 33 USE
print_g pv '----------------------------------'
cat $BASH_SOURCE | grep use | grep -v grep
}
expose_funcs(){
echo 1>&2 $FUNCNAME
cat $BASH_SOURCE | grep '(){' | sed 's/(){//g' | grep -v grep
}
################################################# debug/ traps
trap_exit1(){
# print_line0
local num=$?
echo "$FUNCNAME: $num"
# commander0 cowsay
echo "[CALLER] $( $cmd_caller )"
test -f /tmp/err && { cat /tmp/err; rm /tmp/err; } || true
return $num
}
export -f trap_exit1
#trap trap_exit1 EXIT
trap_err1(){
# print_line0
local num=$?
echo "$FUNCNAME: $num"
# commander0 cowsay
echo "[CALLER] $( $cmd_caller )"
test -f /tmp/err && { cat /tmp/err; rm /tmp/err; } || true
return $num
}
export -f trap_err1
#trap trap_err1 ERR
###############################################
#################################################### validate / ensure
ensure_use(){
export file_use="$dir_CODE/use.cfg"
#register $file
source $file_use
export file_use_sh="$dir_CODE/use_sh.cfg"
#register $file
source $file_use_sh
}
################################################# aware
where_am_i ()
{
local file=${1:-"${BASH_SOURCE[0]}"};
local rpath=$(readlink -m $file);
local rcommand=${rpath##*/};
local str_res=${rpath%/*};
echo $str_res
#local dir_self=$( cd $str_res && echo $PWD )
#echo "$dir_self"
}
#################################################
set_args(){
#https://wiki.ubuntu.com/DashAsBinSh
#commander "assert left_bigger $# 1"
local arg0=$1
local opts="${@:2}"
print color 34 "arg0: $arg0"
print color 34 "opts: $opts"
}
set_args_print(){
#https://wiki.ubuntu.com/DashAsBinSh
#commander "assert left_bigger $# 1"
local arg0=$1
local opts="${@:2}"
echo "[ $arg0 ] $opts"
}
export -f set_args_print
switch_to(){
#useful when we need to visit again then same url we may already opened using the browser
local switch_name="$1"
local cmd="$2"
print func
depend wmctrl
use dialog_confirm
local cmd1="open_with $cmd"
( commander "wmctrl -a $switch_name" ) || ( dialog_confirm 'switch to:' "$cmd1" )
}
broadcast(){
notify-send "$1" "$2" -u critical -t 7000
}
set_env_basic(){
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/games
TERM=xterm
#DISPLAY=:0
}
log(){
local line="$(date +%H:%M) | $0 | $@"
echo "$line" >> /tmp/library.log
}
#alias idea='vim ~/idea.yaml'
######################################## traps #############
reminder(){
set +e
clear
print_g pv $FUNCNAME
#type $FUNCNAME
#echo find . -name "*.[ch]" -exec grep -i -H ßearch pharse {} ;
#echo find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1
#echo find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
library_about
}
#############################################################
subshell(){
clear
print color 33 '[jail] run safely:'
print line
set +e
( commander $@ )
echo
print line
}
finder_cd(){
#goto dir which has this filename/funcname
print func
local str=$( commander finder $@ )
commander "cd $( dirname $str )"
}
finder(){
local util="$dir_CODE/find.sh"
local str="$1"
local type="${2:-cfg}"
local cmd="$util $str $type"
eval "$cmd" 2>/tmp/err
local res=$?
# test $res -eq 0 || ( echo cat /tmp/err; )
return $res
}
install_anchor(){
#export
local filename file_self file_to
local filename=$( basename $BASH_SOURCE )
local file_self="$dir_library/$filename"
local file_to=/tmp/$filename
local dir_to=/tmp/dir_library
#install an anchor for any other projects which may use this library
if [ ! -L $file_to ] ;then
ln -s $file_self $file_to
fi
if [ ! -d $dir_to ];then
ln -s $dir_library $dir_target
fi
}
ensure_workspace(){
if [ ! -d $dir_workspace ];then
mkdir -p $dir_workspace
fi
}
export_vars(){
export LOGNAME=${LOGNAME:-user1}
if [ $LOGNAME = travis ];then
echo export AUDIODEV=null #travis - fix: no audio device
fi
export PATH=$PATH:/usr/local/bin
export gxmessage1='gxmessage -file /tmp/err -title cmd_err -sticky -ontop -timeout 10'
export pidof1='pidof -x $( basename $0 )'
}
struct1(){
test -d $dir_library || { $cmd_caller; return 1; }
export dir_CODE=$dir_library/BANK #shorter name
export dir_workspace=$HOME/.config/do_for_others_first
export file_updatedb="$dir_CODE/updatedb.sh" #index of files under the parent dir
export file_log=/tmp/do_for_others_first.log
test -d $dir_CODE
}
export_funcs(){
local file
export -f finder
export -f set_args
export -f hotkey_overide
export -f finder_cd
export -f where_am_i
export -f activate_important
export -f switch_to
export -f broadcast
export -f log321
export -f log
export -f subshell
}
aliases(){
alias vi=vim
alias step2='activate_important'
alias libraryE="vi $BASH_SOURCE"
alias libraryS="source $BASH_SOURCE"
alias cd_library='cd $dir_library'
alias cd_service='cd $dir_service'
alias updatedb2='$file_updatedb'
############# other method for aliasing:
}
activate_important(){
#print func
echo $FUNCNAME
set -u
#echo 1>&2 $BASH_SOURCE
###############################################
#printing nicer
type use 1>/dev/null || { echo 1>&2 "[ERR] func not found: use()"; return 1; }
#use ps1
use open_with
use where_am_i
use who_am_i
use ps4
use indicator
use print
use point
use python1
use disown1
use sanitize
use traps
use trace
use expose_var
###############################################
#workflow
use commander
use exiting
use dialog_optional
###############################################
#useful for reminding every X times
use random
###############################################
#test operator wrapper
use assert
use ensure
###############################################
#shell navigation
use history_sync
use quick_follow
use quick_remember
use register
###############################################
#user custom parameters
use vars
use mode
use alias
###############################################
#file print/update
use cat1
use file_update
###############################################
#start using what sourced
register $file_use
register $file_use_sh
print ok Activated Important stuff
set +e
set +o nounset
}
ensure_index(){
if [ ! -f /tmp/target ] || [ ! -s /tmp/target ];then
echo 1>&2 creating index of library files..
$dir_CODE/updatedb.sh
fi
}
step_11(){
local cmd="$@"
cmd="$@"
local delay=${delay_testing:-0.1}
if [ $MODE_TEST = true ];then
echo 1>&2 "$cmd"
sleep $delay
fi
set +e
{ eval "$cmd" || $cmd_WTF; }
}
hotkey_overide(){
commander "assert left_bigger $# 1"
set +e
(
local desc="$1"
shift
local cmd="$@"
#xcowsay "hotkey: ctrl+F2: $desc" &
local file=/tmp/hotkey_overide.sh
commander touch $file
commander "echo \"xcowsay '$desc';$cmd\" > $file"
commander chmod u+x $file
cat1 $file true
commander "assert file_has_content $file"
)
echo
}
step(){
local step=$1
#echo "[step] $step"
eval "$step" 1>/tmp/out 2>/tmp/err || { cat /tmp/err; return 1; }
}
steps_for_lib(){
#type $FUNCNAME
step export_vars #dir_library, file_library , dir_CODE( the library BANK/)
step struct1
# assertEquals $dir_CODE ''
step ensure_use
step install_anchor #/tmp/library.cfg
step ensure_workspace
step ensure_index #index of library files: /tmp/target
step export_funcs #
#step aliases #updatedb+edit self+cd self
#set_env_basic
#echo step using #utilize: use()
#using #utilize: use()
}
######################################################### testing
testing_ex3(){
( blaaah || $cmd_WTF )
steps_for_lib || $cmd_WTF
activate_important || $cmd_WTF
( testing_ex1 ) || $cmd_WTF
#( test ( random 20 ) -eq 0 ) && reminder
}
testing_ex4(){
cd /tmp
echo "where_am_i $( where_am_i )"
}
#test_something
#expose_funcs
testing_ex2(){
steps_for_lib
activate_important
}
testing_ex1(){
#test -v MODE_TEST
local file
if [ "$MODE_TEST" = true ];then
file=$(mktemp)
print func
use register
subshell register $file_use
register $file_use_s
else
print color 34 'skip testing'
fi
}
################################################# start
export str_caller='eval echo $( caller )'
export cmd_caller='eval echo $( caller )'
export dir_library=$(where_am_i $BASH_SOURCE)
export dir_service=$dir_root/SCRIPT/SERVICE
echo 1>&2 dir_library: $dir_library
set_env_for_testing
lock_set
testing_ex2 || { echo 1>&2 "[ERROR]"; }
export LIBRARY_LOADED=true
soft
indicator