瀏覽代碼

Merge core.sh into pretty.sh

Alois Mahdal 9 年之前
父節點
當前提交
ba75f3a46d

+ 1
- 1
bin/ffdoc 查看文件

@@ -2,7 +2,7 @@
2 2
 
3 3
 . <(ffoom init)
4 4
 
5
-ffoo import core
5
+ffoo import pretty
6 6
 
7 7
 
8 8
 usage() {

+ 1
- 1
bin/fff 查看文件

@@ -2,7 +2,7 @@
2 2
 
3 3
 . <(ffoom init)
4 4
 
5
-ffoo import core
5
+ffoo import pretty
6 6
 
7 7
 
8 8
 usage() {

+ 1
- 1
bin/ffoom.skel 查看文件

@@ -2,7 +2,7 @@
2 2
 
3 3
 . __FFOO_DIR__/ffoo.sh
4 4
 
5
-ffoo import core
5
+ffoo import pretty
6 6
 
7 7
 
8 8
 usage() {

+ 1
- 1
doc/examples/template.sh 查看文件

@@ -2,7 +2,7 @@
2 2
 
3 3
 . <(ffoom init)
4 4
 
5
-ffoo import core
5
+ffoo import pretty
6 6
 
7 7
 FFOO_VERBOSE=true
8 8
 FFOO_DEBUG=true

+ 1
- 1
doc/examples/test_something.sh 查看文件

@@ -1,7 +1,7 @@
1 1
 #!/bin/bash
2 2
 . <(ffoom init)
3 3
 
4
-ffoo import core
4
+ffoo import pretty
5 5
 ffoo import testing
6 6
 FFOO_VERBOSE=true           ## to think loud
7 7
 FFOO_DEBUG=true             ## to stay on the safe side

+ 1
- 1
doc/ffoo.3.sh 查看文件

@@ -1,7 +1,7 @@
1 1
 #!/bin/bash
2 2
 # ~/bin/example
3 3
 . <(ffoom init)
4
-ffoo import core
4
+ffoo import pretty
5 5
 
6 6
 #
7 7
 # basic echo helpers

+ 1
- 1
include/charmenu.sh 查看文件

@@ -1,4 +1,4 @@
1
-ffoo import core
1
+ffoo import pretty
2 2
 
3 3
 
4 4
 FFOO_CHARMENU_FILE="${FFOO_CHARMENU_FILE:-}"

+ 1
- 1
include/config.sh 查看文件

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 
3
-ffoo import core
3
+ffoo import pretty
4 4
 
5 5
 
6 6
 #

+ 0
- 250
include/core.sh 查看文件

@@ -1,250 +0,0 @@
1
-#!/bin/bash
2
-
3
-ffoo import exits
4
-ffoo import pretty
5
-
6
-
7
-##--------------------##
8
-## PRINTING FRONT-END ##
9
-##--------------------##
10
-
11
-debug() {
12
-    #
13
-    # You already know what it does
14
-    #
15
-    # BTW, following are equivalent:
16
-    #
17
-    #     debug "var1=$var1" "var2=$var2" "result=$result"
18
-    #     debug -v var1 var2 result
19
-    #     debug -v "var@ result"
20
-    #
21
-    $FFOO_DEBUG || return 0
22
-    __echo "$@"
23
-}
24
-
25
-
26
-debug_pipe() {
27
-    #
28
-    # Debug the whole pipe.
29
-    #
30
-    grep . \
31
-        | while IFS= read line;
32
-          do
33
-              debug "|$1: '$line'"
34
-              echos "$line"
35
-          done
36
-}
37
-
38
-
39
-die() {
40
-    #
41
-    # A fatal error
42
-    #
43
-    __echo -t
44
-    __echo "$@"
45
-    exits_error
46
-}
47
-
48
-
49
-echos() {
50
-    #
51
-    # Safer version of echo able to echo "-n"
52
-    #
53
-    # Traditional echo is broken in that it does not
54
-    # distinguish between string to print and its own switches
55
-    # (-e, -E or -n), leading to unexpected behavior.
56
-    #
57
-    # This echo version circumvents this by using printf.
58
-    #
59
-    printf -- '%s\n' "$@"
60
-}
61
-
62
-
63
-usage_is() {
64
-    #
65
-    # Echo out usage patterns and (by default) exit 2
66
-    #
67
-    # Specify -e STATUS for exit status, or "-E" to prevent
68
-    # exiting.  Use "--" to delimit end of arguments processed
69
-    # by usage_is.
70
-    #
71
-    # Recommended usage is to define usage() in your script and
72
-    # use this in body.
73
-    #
74
-    local es=$FFOO_EXITS_OK
75
-    while true; do case "$1" in
76
-        -e) es="$2"; shift 2 ;;
77
-        -E) es="";   shift   ;;
78
-        *)           break ;;
79
-    esac done
80
-    __echo -u "$@";
81
-    test -n "$es" && exits "$es"
82
-}
83
-
84
-
85
-think() {
86
-    #
87
-    # If verose is on, think loud
88
-    #
89
-    # Use "-l" to split every parameter to separate line, (useful
90
-    # or longer warnings)
91
-    #
92
-    $FFOO_VERBOSE || return 0
93
-    __echo "$@"
94
-}
95
-
96
-
97
-warn() {
98
-    #
99
-    # Warn them
100
-    #
101
-    # Use "-l" to split every parameter to separate line, (useful
102
-    # or longer warnings)
103
-    #
104
-    # Resist  temptation to prefix "warning" or similar BS here.
105
-    # STDERR is *made* for warnings.
106
-    #
107
-    __echo "$@"
108
-}
109
-
110
-
111
-##----------##
112
-## BACK-END ##
113
-##----------##
114
-
115
-
116
-__cat() {
117
-    # cat without starting a process
118
-    while IFS= read line;
119
-    do echos "$line"; done
120
-}
121
-
122
-
123
-__cut_stack() {
124
-    #
125
-    # call stack except the head that fits pattern $1
126
-    #
127
-    local patt=${1:-"^__"}
128
-    echos "${FUNCNAME[@]}" \
129
-      | perl -ne "
130
-            my \$checking = 1;
131
-            while (<>) {
132
-                next if (\$checking and m/$patt/);
133
-                \$checking = 0;
134
-                print qq/\$_\\n/;
135
-            }
136
-        "
137
-}
138
-
139
-
140
-__echo() {
141
-    #
142
-    # A smarter echo backend
143
-    #
144
-    # A smarter backend for debug, warn, think, die and
145
-    # usage_is.
146
-    #
147
-    #   -c cmd      echo output of a command
148
-    #   -f file     echo output of a file (- for stdin)
149
-    #   -l line [line...] echo each line separately
150
-    #   -t          add stack trace to output
151
-    #   -u patt [patt...] convert each patt to usage pattern
152
-    #   -v var [var...] show contents of each var
153
-    #
154
-    local frontend  # who (of core.sh) was called (=> prettyprinter choice)
155
-    local caller    # which user's function (or script) called it
156
-    frontend="$(__cut_stack '^__echo' | head -1 )"
157
-    caller="$(
158
-        __cut_stack \
159
-            '^__echo|^warn$|^debug$|^die$|^usage$|^usage_is$|^think$' \
160
-            | head -1
161
-    )"
162
-    test "$frontend" == "main" && frontend=$(basename $0)
163
-    test "$caller" == "main" && caller=$(basename $0)
164
-    test "$frontend" == "usage" && frontend=usage_is
165
-    local prettyfn=__cat
166
-    local src       # which source, or "producer" (__echo_*()) to use
167
-    while true; do case $1 in
168
-        -c|--cmd)   src=cmd;   shift; break ;;
169
-        -f|--files) src=files; shift; break ;;
170
-        -l|--lines) src=lines; shift; break ;;
171
-        -t|--trace) src=trace; shift; break ;;
172
-        -u|--usage) src=usage; shift; break ;;
173
-        -v|--vars)  src=vars;  shift; break ;;
174
-        *)          src=args;         break ;;
175
-    esac done
176
-    prettyfn=_pretty_$frontend
177
-    case $frontend in
178
-        warn|debug|die|usage_is)    __echo_$src "$@" | $prettyfn >&2 ;;
179
-        think)                      __echo_$src "$@" | $prettyfn     ;;
180
-        *)      echo "do not call __echo* directly: $frontend" >&2;
181
-                exits_usage                                          ;;
182
-    esac
183
-}
184
-
185
-
186
-__echo_args() {
187
-    echos "$@"
188
-}
189
-
190
-
191
-__echo_cmd() {
192
-    local c=\$
193
-    test $(id -u) -eq 0 && c=\#
194
-    echo "$c $*"
195
-    $@
196
-    echo "^ exit status: $?"
197
-}
198
-
199
-
200
-__echo_files() {
201
-    local fp
202
-    for fp in "$@";
203
-    do
204
-        test -s "$fp" || continue
205
-        echo "-- $fp --"
206
-        cat $fp
207
-    done
208
-}
209
-
210
-
211
-__echo_lines() {
212
-    local l;
213
-    for l in "$@"; do __echo "$l"; done
214
-}
215
-
216
-
217
-__echo_trace() {
218
-    $FFOO_DEBUG || return 0
219
-    echo "== trace =="
220
-    caller | sed -e 's/^/-> /g'
221
-}
222
-
223
-
224
-__echo_usage() {
225
-    local u
226
-    for u in "$@";
227
-    do echo "usage: $caller $u"
228
-    done
229
-}
230
-
231
-
232
-__echo_vars() {
233
-    local vn
234
-    for vn in "$@";
235
-    do
236
-        local heel="${vn:0-1}"          # last char
237
-        local toes="${vn%%$heel}"       # all but last char
238
-        case "$heel" in
239
-            @)
240
-                # FIXME: review+fix the eval (even at cost of
241
-                #        dropping the feature)
242
-                local vars="$(eval "echos \"\${!$toes$heel}\"")"
243
-                __echo_vars "$vars"
244
-                ;;
245
-            *)
246
-                __echo "$vn='$(echos "${!vn}")'"
247
-                ;;
248
-        esac
249
-    done
250
-}

+ 1
- 1
include/misc.sh 查看文件

@@ -1,7 +1,7 @@
1 1
 #!/bin/bash
2 2
 
3
-ffoo import core
4 3
 ffoo import config
4
+ffoo import pretty
5 5
 
6 6
 
7 7
 append_if_missing() {

+ 248
- 0
include/pretty.sh 查看文件

@@ -1,5 +1,7 @@
1 1
 #!/bin/bash
2 2
 
3
+ffoo import exits
4
+
3 5
 #
4 6
 # Name of prerry-printer module
5 7
 #
@@ -25,3 +27,249 @@ __ffoo_pretty__init() {
25 27
         ffoo import _pretty_${FFOO_PRETTY}
26 28
     fi
27 29
 }
30
+
31
+
32
+##--------------------##
33
+## PRINTING FRONT-END ##
34
+##--------------------##
35
+
36
+debug() {
37
+    #
38
+    # You already know what it does
39
+    #
40
+    # BTW, following are equivalent:
41
+    #
42
+    #     debug "var1=$var1" "var2=$var2" "result=$result"
43
+    #     debug -v var1 var2 result
44
+    #     debug -v "var@ result"
45
+    #
46
+    $FFOO_DEBUG || return 0
47
+    __echo "$@"
48
+}
49
+
50
+
51
+debug_pipe() {
52
+    #
53
+    # Debug the whole pipe.
54
+    #
55
+    grep . \
56
+        | while IFS= read line;
57
+          do
58
+              debug "|$1: '$line'"
59
+              echos "$line"
60
+          done
61
+}
62
+
63
+
64
+die() {
65
+    #
66
+    # A fatal error
67
+    #
68
+    __echo -t
69
+    __echo "$@"
70
+    exits_error
71
+}
72
+
73
+
74
+echos() {
75
+    #
76
+    # Safer version of echo able to echo "-n"
77
+    #
78
+    # Traditional echo is broken in that it does not
79
+    # distinguish between string to print and its own switches
80
+    # (-e, -E or -n), leading to unexpected behavior.
81
+    #
82
+    # This echo version circumvents this by using printf.
83
+    #
84
+    printf -- '%s\n' "$@"
85
+}
86
+
87
+
88
+usage_is() {
89
+    #
90
+    # Echo out usage patterns and (by default) exit 2
91
+    #
92
+    # Specify -e STATUS for exit status, or "-E" to prevent
93
+    # exiting.  Use "--" to delimit end of arguments processed
94
+    # by usage_is.
95
+    #
96
+    # Recommended usage is to define usage() in your script and
97
+    # use this in body.
98
+    #
99
+    local es=$FFOO_EXITS_OK
100
+    while true; do case "$1" in
101
+        -e) es="$2"; shift 2 ;;
102
+        -E) es="";   shift   ;;
103
+        *)           break ;;
104
+    esac done
105
+    __echo -u "$@";
106
+    test -n "$es" && exits "$es"
107
+}
108
+
109
+
110
+think() {
111
+    #
112
+    # If verose is on, think loud
113
+    #
114
+    # Use "-l" to split every parameter to separate line, (useful
115
+    # or longer warnings)
116
+    #
117
+    $FFOO_VERBOSE || return 0
118
+    __echo "$@"
119
+}
120
+
121
+
122
+warn() {
123
+    #
124
+    # Warn them
125
+    #
126
+    # Use "-l" to split every parameter to separate line, (useful
127
+    # or longer warnings)
128
+    #
129
+    # Resist  temptation to prefix "warning" or similar BS here.
130
+    # STDERR is *made* for warnings.
131
+    #
132
+    __echo "$@"
133
+}
134
+
135
+
136
+##----------##
137
+## BACK-END ##
138
+##----------##
139
+
140
+
141
+__cat() {
142
+    # cat without starting a process
143
+    while IFS= read line;
144
+    do echos "$line"; done
145
+}
146
+
147
+
148
+__cut_stack() {
149
+    #
150
+    # call stack except the head that fits pattern $1
151
+    #
152
+    local patt=${1:-"^__"}
153
+    echos "${FUNCNAME[@]}" \
154
+      | perl -ne "
155
+            my \$checking = 1;
156
+            while (<>) {
157
+                next if (\$checking and m/$patt/);
158
+                \$checking = 0;
159
+                print qq/\$_\\n/;
160
+            }
161
+        "
162
+}
163
+
164
+
165
+__echo() {
166
+    #
167
+    # A smarter echo backend
168
+    #
169
+    # A smarter backend for debug, warn, think, die and
170
+    # usage_is.
171
+    #
172
+    #   -c cmd      echo output of a command
173
+    #   -f file     echo output of a file (- for stdin)
174
+    #   -l line [line...] echo each line separately
175
+    #   -t          add stack trace to output
176
+    #   -u patt [patt...] convert each patt to usage pattern
177
+    #   -v var [var...] show contents of each var
178
+    #
179
+    local frontend  # who (of pretty.sh) was called (=> prettyprinter choice)
180
+    local caller    # which user's function (or script) called it
181
+    frontend="$(__cut_stack '^__echo' | head -1 )"
182
+    caller="$(
183
+        __cut_stack \
184
+            '^__echo|^warn$|^debug$|^die$|^usage$|^usage_is$|^think$' \
185
+            | head -1
186
+    )"
187
+    test "$frontend" == "main" && frontend=$(basename $0)
188
+    test "$caller" == "main" && caller=$(basename $0)
189
+    test "$frontend" == "usage" && frontend=usage_is
190
+    local prettyfn=__cat
191
+    local src       # which source, or "producer" (__echo_*()) to use
192
+    while true; do case $1 in
193
+        -c|--cmd)   src=cmd;   shift; break ;;
194
+        -f|--files) src=files; shift; break ;;
195
+        -l|--lines) src=lines; shift; break ;;
196
+        -t|--trace) src=trace; shift; break ;;
197
+        -u|--usage) src=usage; shift; break ;;
198
+        -v|--vars)  src=vars;  shift; break ;;
199
+        *)          src=args;         break ;;
200
+    esac done
201
+    prettyfn=_pretty_$frontend
202
+    case $frontend in
203
+        warn|debug|die|usage_is)    __echo_$src "$@" | $prettyfn >&2 ;;
204
+        think)                      __echo_$src "$@" | $prettyfn     ;;
205
+        *)      echo "do not call __echo* directly: $frontend" >&2;
206
+                exits_usage                                          ;;
207
+    esac
208
+}
209
+
210
+
211
+__echo_args() {
212
+    echos "$@"
213
+}
214
+
215
+
216
+__echo_cmd() {
217
+    local c=\$
218
+    test $(id -u) -eq 0 && c=\#
219
+    echo "$c $*"
220
+    $@
221
+    echo "^ exit status: $?"
222
+}
223
+
224
+
225
+__echo_files() {
226
+    local fp
227
+    for fp in "$@";
228
+    do
229
+        test -s "$fp" || continue
230
+        echo "-- $fp --"
231
+        cat $fp
232
+    done
233
+}
234
+
235
+
236
+__echo_lines() {
237
+    local l;
238
+    for l in "$@"; do __echo "$l"; done
239
+}
240
+
241
+
242
+__echo_trace() {
243
+    $FFOO_DEBUG || return 0
244
+    echo "== trace =="
245
+    caller | sed -e 's/^/-> /g'
246
+}
247
+
248
+
249
+__echo_usage() {
250
+    local u
251
+    for u in "$@";
252
+    do echo "usage: $caller $u"
253
+    done
254
+}
255
+
256
+
257
+__echo_vars() {
258
+    local vn
259
+    for vn in "$@";
260
+    do
261
+        local heel="${vn:0-1}"          # last char
262
+        local toes="${vn%%$heel}"       # all but last char
263
+        case "$heel" in
264
+            @)
265
+                # FIXME: review+fix the eval (even at cost of
266
+                #        dropping the feature)
267
+                local vars="$(eval "echos \"\${!$toes$heel}\"")"
268
+                __echo_vars "$vars"
269
+                ;;
270
+            *)
271
+                __echo "$vn='$(echos "${!vn}")'"
272
+                ;;
273
+        esac
274
+    done
275
+}

+ 1
- 1
include/recon.sh 查看文件

@@ -1,7 +1,7 @@
1 1
 #!/bin/bash
2 2
 
3
-ffoo import core
4 3
 ffoo import misc
4
+ffoo import pretty
5 5
 
6 6
 
7 7
 age_of_leaf() {

+ 1
- 1
include/testing.sh 查看文件

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 
3
-ffoo import core
3
+ffoo import pretty
4 4
 ffoo import tmp
5 5
 
6 6
 

+ 1
- 1
include/types.sh 查看文件

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 
3
-ffoo import core
3
+ffoo import pretty
4 4
 
5 5
 
6 6
 is_integer() {

+ 2
- 2
notes/TODO.md 查看文件

@@ -49,8 +49,8 @@ test
49 49
 *   write some tests!
50 50
 
51 51
 
52
-core.sh
53
--------
52
+pretty.sh
53
+---------
54 54
 
55 55
 *   add -name arg to debug, e.g. for pipe/cmd/file and use it in
56 56
     pretty

+ 1
- 1
notes/guidelines.md 查看文件

@@ -50,7 +50,7 @@ API
50 50
     success or error type
51 51
 
52 52
 *   use 1 for "no", 2 for syntax error (if applicable), more
53
-    otherwise;  note that ffoo core.die uses 9 as a generic
53
+    otherwise;  note that ffoo pretty.die uses 9 as a generic
54 54
     status
55 55
 
56 56
 

+ 0
- 1
notes/packaging.md 查看文件

@@ -36,7 +36,6 @@ No modules, just ffoo.sh and supporting binaries.
36 36
 
37 37
 ### ffoo-core ###
38 38
 
39
- *  core
40 39
  *  error
41 40
  *  pretty
42 41
  *  _pretty_color

+ 17
- 17
test/data/ffoo_api/functions.oracle.stdout 查看文件

@@ -23,23 +23,6 @@ config.__cfgrep__fltsct
23 23
 config.__cfgrep__load
24 24
 config.__cfgrep__merge
25 25
 config.cfgrep
26
-core.__cat
27
-core.__cut_stack
28
-core.__echo
29
-core.__echo_args
30
-core.__echo_cmd
31
-core.__echo_files
32
-core.__echo_lines
33
-core.__echo_trace
34
-core.__echo_usage
35
-core.__echo_vars
36
-core.debug
37
-core.debug_pipe
38
-core.die
39
-core.echos
40
-core.think
41
-core.usage_is
42
-core.warn
43 26
 exits.exit_error
44 27
 exits.exit_no
45 28
 exits.exit_ok
@@ -58,7 +41,24 @@ misc.expand_tilde
58 41
 misc.filter
59 42
 misc.mute_known
60 43
 misc.select_args
44
+pretty.__cat
45
+pretty.__cut_stack
46
+pretty.__echo
47
+pretty.__echo_args
48
+pretty.__echo_cmd
49
+pretty.__echo_files
50
+pretty.__echo_lines
51
+pretty.__echo_trace
52
+pretty.__echo_usage
53
+pretty.__echo_vars
61 54
 pretty.__ffoo_pretty__init
55
+pretty.debug
56
+pretty.debug_pipe
57
+pretty.die
58
+pretty.echos
59
+pretty.think
60
+pretty.usage_is
61
+pretty.warn
62 62
 recon.age_of_leaf
63 63
 recon.age_of_tree
64 64
 recon.cat_uri

+ 0
- 1
test/data/ffoo_api/modules.oracle.stdout 查看文件

@@ -3,7 +3,6 @@ _pretty_html
3 3
 _pretty_plain
4 4
 charmenu
5 5
 config
6
-core
7 6
 exits
8 7
 misc
9 8
 pretty