Browse Source

global maps, logfile and misc cleanup

Jaromil 4 years ago
parent
commit
41d24810d3
2 changed files with 63 additions and 24 deletions
  1. 40 20
      zuper
  2. 23 4
      zuper.init

+ 40 - 20
zuper

@@ -24,24 +24,18 @@
24 24
 ##########################
25 25
 typeset -aU vars
26 26
 typeset -aU arrs
27
-vars=(DEBUG QUIET ztmpfile)
28
-arrs=(req freq)
29
-
30
-# reset list of destructors
31
-destruens=()
27
+typeset -aU maps
32 28
 
33
-# global execution flags
34
-DEBUG=${DEBUG:-0}
35
-QUIET=${QUIET:-0}
29
+vars=(DEBUG QUIET LOG)
30
+arrs=(req freq)
36 31
 
37 32
 vars+=(zuper_version)
38
-zuper_version=0.1
33
+zuper_version=0.2
39 34
 
40 35
 # Messaging function with pretty coloring
41 36
 autoload colors
42 37
 colors
43 38
 
44
-
45 39
 vars+=(last_act last_func last_notice)
46 40
 
47 41
 function _msg() {
@@ -91,6 +85,13 @@ function _msg() {
91 85
             ;;
92 86
     esac
93 87
     ${=command} "${progname} $fg_bold[$pcolor]$pchars$reset_color ${message}$color[reset_color]" >&2
88
+
89
+    # write the log if its configured
90
+    [[ "$LOG" = "" ]] || {
91
+        touch $LOG || return $?
92
+        ${=command} "${progname} $fg_bold[$pcolor]$pchars$reset_color ${message}$color[reset_color]" >> $LOG
93
+    }
94
+
94 95
     return $returncode
95 96
 }
96 97
 
@@ -157,23 +158,35 @@ ckreq reqck() {
157 158
 
158 159
 zerr() {
159 160
     error "error in: ${fun:-$last_notice}"
160
-
161
-    [[ "$last_func" = "" ]] || warn "called in: $last_func"
162
-    [[ "$last_act" = "" ]] || warn "called in: $last_act"
161
+    [[ "$last_func"   = "" ]] || warn "called in: $last_func"
162
+    [[ "$last_act"    = "" ]] || warn "called in: $last_act"
163 163
     [[ "$last_notice" = "" ]] || warn "called in: $last_notice"
164
-    [[ "$fun" = "" ]] || warn "called in: $fun"
164
+    [[ "$fun"         = "" ]] || warn "called in: $fun"
165 165
     error "error reported, operation aborted."
166 166
     return 1
167 167
 }
168 168
 
169 169
 zdump() {
170 170
     fn zdump
171
-    for v in $vars; do
172
-        print "$v \t ${(P)v}"
173
-    done
174
-    for a in $arrs; do
175
-        print "$a \t ${(P)a}"
176
-    done
171
+    [[ ${#vars} -gt 0 ]] && {
172
+        print "Global variables:"
173
+        for _v in $vars; do
174
+            print " $_v = \t ${(P)_v}"
175
+        done
176
+    }
177
+    [[ ${#arrs} -gt 0 ]] && {
178
+        print "Global arrays:"
179
+        for _a in $arrs; do
180
+        print " $_a \t ( ${(P)_a} )"
181
+        done
182
+    }
183
+    [[ ${#maps} -gt 0 ]] && {
184
+        print "Global maps:"
185
+        for _m in $maps; do
186
+            print " $_m [key] \t ( ${(Pk)_m} )"
187
+            print " $_m [val] \t ( ${(Pv)_m} )"
188
+        done
189
+    }
177 190
 }
178 191
 
179 192
 # handy wrappers for throw/catch execution of blocks where we need the
@@ -185,6 +198,7 @@ catch() { function TRAPZERR() { } }
185 198
 # Endgame handling
186 199
 
187 200
 arrs+=(destruens)
201
+destruens=()
188 202
 
189 203
 # Trap functions for the endgame event
190 204
 TRAPINT()  { endgame INT;   return $? }
@@ -220,6 +234,7 @@ zshexit() { endgame EXIT; return $? }
220 234
 ##########################
221 235
 # Temp file handling
222 236
 
237
+vars+=(ztmpfile)
223 238
 # ztmp() fills in $ztmpfile global. Caller must copy that variable as
224 239
 # it will be overwritten at every call.
225 240
 ztmp() {
@@ -413,6 +428,11 @@ EOF
413 428
 # {{{ Helpers
414 429
 [[ "$helpers" = "" ]] || {
415 430
 
431
+    function helper.isfound isfound() {
432
+        command -v $1   1>/dev/null 2>/dev/null
433
+        return $?
434
+    }
435
+
416 436
     # remote leading and trailing spaces in a string taken from stdin
417 437
     function helper.trim trim() {
418 438
         sed -e 's/^[[:space:]]*//g ; s/[[:space:]]*\$//g'

+ 23 - 4
zuper.init

@@ -3,14 +3,33 @@
3 3
 
4 4
 # initialize globals only after sourcing everything
5 5
 # since zlibs may contain more variable declarations
6
-for v in $vars; do
7
-    typeset -h $v
6
+for _v in $vars; do
7
+    typeset -h $_v
8 8
 done
9
-for a in $arrs; do
10
-    typeset -a $a
9
+for _a in $arrs; do
10
+    typeset -aU $_a
11 11
 done
12
+for _m in $maps; do
13
+    typeset -A $_m
14
+done
15
+
16
+# reset defaults
17
+DEBUG=${DEBUG:-0}
18
+QUIET=${QUIET:-0}
19
+LOG=${LOG:-""}
20
+req=()
21
+freq=()
22
+last_act=()
23
+last_func=()
24
+last_notice=()
25
+tmpfiles=()
26
+config_section=()
27
+config_section_type=${config_section_type:-org-mode}
28
+
29
+
12 30
 func "Zuper $zuper_version initialized"
13 31
 func "${#vars} global variables registered"
14 32
 func "${#arrs} global arrays registered"
33
+func "${#maps} global maps registered"
15 34
 
16 35