Browse Source

Switch to in-line `do`/`then` style

Turns out to be concise and safe enough to become the preferred one.
Alois Mahdal 6 years ago
parent
commit
7ece3484f9

+ 9
- 16
notes/style.md View File

@@ -31,16 +31,14 @@ Indentation is done exclusively by *space*.
31 31
 
32 32
 Indentation level is *almost exclusively* 4 spaces.
33 33
 
34
-    for i in $(seq 1 5);
35
-    do
34
+    for i in $(seq 1 5); do
36 35
         something_with "$i"
37 36
     done
38 37
 
39 38
 One exception is *vertical pipelines*:
40 39
 
41 40
     cat list \
42
-      | while read line;
43
-        do
41
+      | while read line; do
44 42
             something_with "$line"
45 43
             ...
46 44
         done \
@@ -254,8 +252,7 @@ declarative line (the one ending with `{`):
254 252
         #
255 253
         local bar=$1    # barrator
256 254
         local baz       # bazate
257
-        for baz in "$@";
258
-        do
255
+        for baz in "$@"; do
259 256
             something_with "$bar" "$baz"
260 257
         done
261 258
     }
@@ -353,32 +350,28 @@ That is,
353 350
 
354 351
 ### `if`, `while`, `for` ###
355 352
 
356
-    if foo;
357
-    then
353
+    if foo; then
358 354
         bar
359
-    elif baz;
360
-    then
355
+    elif baz; then
361 356
         quux
362 357
     else
363 358
         idk
364 359
     fi
365 360
 
366
-    while foo;
367
-    do
361
+    while foo; do
368 362
         bar
369 363
         baz
370 364
         quux
371 365
     done
372 366
 
373
-    for foo in bar baz;
374
-    do
367
+    for foo in bar baz; do
375 368
         quux $foo
376 369
     done
377 370
 
378 371
 That is,
379 372
 
380
- *  `then`, `do` and `done` are always alone, aligned with opening keyword
381
-    (`if`, `while`, `for`) or intermediary keyword (`elif`, `else`);
373
+ *  `then` and `do` are always on same line as opening keyword (`if`,
374
+    `while`, `for`) or intermediary keyword (`elif`);
382 375
 
383 376
  *  command part (i.e. condition for `if`/`elif`/`while`, or expansion in
384 377
     `for`) is terminated by semicolon;

+ 6
- 12
src/include-bash/inigrep.sh View File

@@ -129,8 +129,7 @@ _inigrep__fltkey() {
129 129
     local line      # every line
130 130
     local key       # key name part
131 131
     local value     # name part
132
-    while IFS= read -r line;                        # '  key  =  val  '
133
-    do
132
+    while IFS= read -r line; do                     # '  key  =  val  '
134 133
         line="${line##*([[:space:]])}"              # line='key  =  val  '
135 134
         key="${line%%*([[:space:]])=*}"             # key='key'
136 135
         value="${line##$key*([[:space:]])=}"        # value='  val  '
@@ -153,14 +152,12 @@ _inigrep__fltsct() {
153 152
     local line          # current line
154 153
     while IFS= read -r line;
155 154
     do
156
-        if [[ $line =~ ^[[:space:]]*\[[^]]*\].* ]];     # IOW: 'spaces[name]anything'
157
-        then
155
+        if [[ $line =~ ^[[:space:]]*\[[^]]*\].* ]]; then    # IOW: 'spaces[name]anything'
158 156
             # hack out only the name between '[' and ']'
159 157
             sct_name=$line
160 158
             sct_name=${sct_name##*([[:space:]])[}   # drop leading space
161 159
             sct_name=${sct_name%%]*}                # and trailing anything
162
-            if test "$sct_name" == "$WantedSection";
163
-            then
160
+            if test "$sct_name" == "$WantedSection"; then
164 161
                 sct_ok=true;                            # our section
165 162
                 $ListingMode && echos "[$sct_name]";    # inform lister
166 163
             else
@@ -183,8 +180,7 @@ _inigrep__fltlst() {
183 180
     local line=""           # current line
184 181
     local key="__NOKEY__"   # current key
185 182
     local sct="__NOSCT__"   # current section
186
-    while read -r line;
187
-    do
183
+    while read -r line; do
188 184
         case "$line" in
189 185
             \[*\]) sct=${line#[}; sct=${sct%]}; key=__NOKEY__   ;;
190 186
             *=*)   key="${line%%*([[:space:]])=*}" ;;   # key='key'
@@ -235,8 +231,7 @@ _inigrep__query() {
235 231
     #
236 232
     restore_shopt=$(shopt -p extglob)
237 233
     shopt -s extglob
238
-    for ifile in "$@";
239
-    do
234
+    for ifile in "$@"; do
240 235
         case "$ifile" in
241 236
             -) eval "$pipe" ;;
242 237
             *) <"$ifile" eval "$pipe" ;;
@@ -335,8 +330,7 @@ inigrep() {
335 330
     WantedSection="${kpath%.$WantedKey}"
336 331
     _inigrep__validate_ks || return 2
337 332
     debug -v WantedKey WantedSection
338
-    if $one_line;
339
-    then
333
+    if $one_line; then
340 334
         _inigrep__query "$@" | head -1
341 335
     else
342 336
         _inigrep__query "$@"

+ 15
- 30
src/include-bash/pretty.sh View File

@@ -52,8 +52,7 @@ __shellfu_pretty__init() {
52 52
     #
53 53
     # Import proper submodule
54 54
     #
55
-    if shellfu try_import "_pretty_${PRETTY}";
56
-    then
55
+    if shellfu try_import "_pretty_${PRETTY}"; then
57 56
         shellfu import "_pretty_${PRETTY}"
58 57
         return 0
59 58
     else
@@ -86,8 +85,7 @@ debug_pipe() {
86 85
     #
87 86
     # Debug the whole pipe.
88 87
     #
89
-    while IFS= read -r line;
90
-    do
88
+    while IFS= read -r line; do
91 89
         debug "|$1: '$line'"
92 90
         echos "$line"
93 91
     done
@@ -160,8 +158,7 @@ mkusage() {
160 158
         *)                  break ;;
161 159
     esac done
162 160
     _pretty__echo -u "$@";
163
-    if test -n "$cmsg";
164
-    then
161
+    if test -n "$cmsg"; then
165 162
         test -n "$*" && echo >&2
166 163
         warn "bad usage: $cmsg"
167 164
     fi
@@ -245,8 +242,7 @@ _pretty__get_caller() {
245 242
     #
246 243
     local fname
247 244
     local nidx="${#FUNCNAME[@]}"
248
-    for fname in "${FUNCNAME[@]}";
249
-    do
245
+    for fname in "${FUNCNAME[@]}"; do
250 246
         (( nidx-- ))
251 247
         _pretty__is_internal && continue
252 248
         _pretty__is_frontend && continue
@@ -261,8 +257,7 @@ _pretty__get_frontend() {
261 257
     # Get entry point function name from stack
262 258
     #
263 259
     local fname
264
-    for fname in "${FUNCNAME[@]}";
265
-    do
260
+    for fname in "${FUNCNAME[@]}"; do
266 261
         _pretty__is_internal && continue
267 262
         _pretty__is_frontend && echos "$fname" && return 0
268 263
         echo "do not call _pretty_* directly: $fname" >&2
@@ -287,15 +282,13 @@ _pretty__is_excluded() {
287 282
         *__*)   qualified=true  ;;
288 283
         *)      qualified=false ;;
289 284
     esac
290
-    if $qualified;
291
-    then
285
+    if $qualified; then
292 286
         # we'll use only the module part of the name
293 287
         name=${name#_}      # drop one "internal" prefix
294 288
         name=${name#_}      # drop yet another one
295 289
         name=${name%%__*}   # drop funcname
296 290
     fi
297
-    for listed in ${PRETTY_DEBUG_EXCLUDE//,/ };
298
-    do
291
+    for listed in ${PRETTY_DEBUG_EXCLUDE//,/ }; do
299 292
         test "$name" = "$listed" && return 0
300 293
     done
301 294
     return 1
@@ -398,13 +391,10 @@ _pretty__echo_files() {
398 391
     # Print names and contents of existing files
399 392
     #
400 393
     local fp
401
-    for fp in "$@";
402
-    do
403
-        if test "$fp" = "-";
404
-        then
394
+    for fp in "$@"; do
395
+        if test "$fp" = "-"; then
405 396
             cat
406
-        elif test -s "$fp" || test "$fp" = "/dev/stdin";
407
-        then
397
+        elif test -s "$fp" || test "$fp" = "/dev/stdin"; then
408 398
             echo "-- $fp --"
409 399
             cat "$fp"
410 400
         fi
@@ -428,8 +418,7 @@ _pretty__echo_trace() {
428 418
     $PRETTY_DEBUG || return 0
429 419
     local depth
430 420
     echo "== trace =="
431
-    for depth in $(seq 0 ${#FUNCNAME});
432
-    do
421
+    for depth in $(seq 0 ${#FUNCNAME}); do
433 422
         caller "$depth" || break
434 423
     done \
435 424
       | tail -n +3 \
@@ -498,8 +487,7 @@ _pretty__echo_usage() {
498 487
         1)  echo "usage: $self $1"; return 0 ;;
499 488
     esac
500 489
     echo usage:
501
-    for arg in "$@";
502
-    do
490
+    for arg in "$@"; do
503 491
         case $esc:$arg in
504 492
             0:--) shift; mode=plain;  esc=1; echo                ;;
505 493
             0:-c) shift; mode=indent; esc=1; echo; echo commands: ;;
@@ -524,15 +512,12 @@ _pretty__echo_vars() {
524 512
     #
525 513
     local varname
526 514
     local declare_str
527
-    for varname in "$@";
528
-    do
529
-        if ! _pretty__is_word "$varname";
530
-        then
515
+    for varname in "$@"; do
516
+        if ! _pretty__is_word "$varname"; then
531 517
             warn "unsafe value skipped: $varname";
532 518
             continue
533 519
         fi
534
-        if declare_str=$(declare -p "$varname" 2>/dev/null);
535
-        then
520
+        if declare_str=$(declare -p "$varname" 2>/dev/null); then
536 521
             _pretty__echo "${declare_str#declare ?? }"
537 522
         else
538 523
             _pretty__echo "$varname #Unset"

+ 7
- 14
src/include-bash/sfdoc.sh View File

@@ -130,8 +130,7 @@ __sfdoc__export_as_markdown() {
130 130
     echo
131 131
     __sfdoc__get_doc | __sfdoc__md_escapes
132 132
     echo
133
-    for variable in $(sfdoc__ls v "$MPath" | cut -d: -f2);
134
-    do
133
+    for variable in $(sfdoc__ls v "$MPath" | cut -d: -f2); do
135 134
         debug -v variable
136 135
         $vheader_done || {
137 136
             echo
@@ -145,8 +144,7 @@ __sfdoc__export_as_markdown() {
145 144
         __sfdoc__get_doc "v:$variable" | __sfdoc__md_escapes
146 145
         echo
147 146
     done
148
-    for function in $(sfdoc__ls f "$MPath" | cut -d: -f2);
149
-    do
147
+    for function in $(sfdoc__ls f "$MPath" | cut -d: -f2); do
150 148
         debug -v function
151 149
         $fheader_done || {
152 150
             echo
@@ -184,8 +182,7 @@ __sfdoc__export_as_pod() {
184 182
     echo
185 183
     __sfdoc__get_doc
186 184
     echo
187
-    for variable in $(sfdoc__ls v "$MPath" | cut -d: -f2);
188
-    do
185
+    for variable in $(sfdoc__ls v "$MPath" | cut -d: -f2); do
189 186
         debug -v variable
190 187
         $vheader_done || {
191 188
             echo
@@ -206,8 +203,7 @@ __sfdoc__export_as_pod() {
206 203
         echo "=back"
207 204
         echo
208 205
     }
209
-    for function in $(sfdoc__ls f "$MPath" | cut -d: -f2);
210
-    do
206
+    for function in $(sfdoc__ls f "$MPath" | cut -d: -f2); do
211 207
         debug -v function
212 208
         $fheader_done || {
213 209
             echo
@@ -451,8 +447,7 @@ __sfdoc__filter_var() {
451 447
     cache=$(mktemp -t shellfu_doc.__sfdoc__filter_var.XXXXXXXX)
452 448
     state=junk
453 449
     o_end=false
454
-    while read -r line;
455
-    do
450
+    while read -r line; do
456 451
         case $state:$line in
457 452
             # looks like void
458 453
             void:)          state=void; o_end=false ;;
@@ -490,10 +485,8 @@ __sfdoc__filter_var() {
490 485
         case $state in
491 486
             edge|dstr|vstr) echo "$line" >> "$cache" ;;
492 487
         esac
493
-        if $o_end;
494
-        then
495
-            if grep -q "^$oname=" "$cache";   # it's our wanted object
496
-            then
488
+        if $o_end; then
489
+            if grep -q "^$oname=" "$cache"; then    # it's our wanted object
497 490
                 cat "$cache"
498 491
                 rm "$cache"
499 492
                 return 0

+ 1
- 2
src/include-bash/sfpi.sh View File

@@ -188,8 +188,7 @@ sfpi__key() {
188 188
     local key=$2    # meta-data key
189 189
     local value     # ^^ value
190 190
     local fun="${SFPI__PREFIX}_${plg}__sfpimeta" # plg getter function name
191
-    if type -t "$fun" >/dev/null;
192
-    then
191
+    if type -t "$fun" >/dev/null; then
193 192
         value=$("$fun" "$key")
194 193
         debug -v fun key value
195 194
         test -n "$value" || value="(none)"

+ 1
- 2
src/include-sh/mdfmt.sh View File

@@ -78,8 +78,7 @@ _mdfmt__indent() {
78 78
     test "$Elem" == h2 && { cat; return; }
79 79
     test "$Elem" == h3 && { cat; return; }
80 80
     pfx=$(
81
-        for n in $(seq 1 "$Nest");
82
-        do
81
+        for n in $(seq 1 "$Nest"); do
83 82
             printf '    '
84 83
         done
85 84
     )

+ 6
- 12
src/shellfu.sh.skel View File

@@ -139,22 +139,19 @@ shellfu() {
139 139
     case $cmd in
140 140
         __debug)
141 141
             $SHELLFU_DEBUGINIT || return 0
142
-            for msg in "$@";
143
-            do
142
+            for msg in "$@"; do
144 143
                 echo "shellfu:debug: $msg" >&2
145 144
             done
146 145
             ;;
147 146
         __die)
148
-            for msg in "$@";
149
-            do
147
+            for msg in "$@"; do
150 148
                 echo "shellfu:fatal: $msg" >&2
151 149
             done
152 150
             test -z "$PS1" && exit 3
153 151
             return 3
154 152
             ;;
155 153
         __warn)
156
-            for msg in "$@";
157
-            do
154
+            for msg in "$@"; do
158 155
                 echo "shellfu: $msg" >&2
159 156
             done
160 157
             ;;
@@ -170,8 +167,7 @@ shellfu() {
170 167
             #
171 168
             local mname=$1
172 169
             shellfu _is_imported "$mname" && return 0
173
-            if shellfu _do_import "$mname";
174
-            then
170
+            if shellfu _do_import "$mname"; then
175 171
                 __SHELLFU_IMPORTED="$__SHELLFU_IMPORTED${__SHELLFU_IMPORTED:+:}$mname"
176 172
             else
177 173
                 shellfu __die "cannot import module: $mname"
@@ -222,8 +218,7 @@ shellfu() {
222 218
             es=$?
223 219
             test -n "$SHELLFU_EMBEDTMP" && echo "$mpath" >> "$SHELLFU_EMBEDTMP"
224 220
             local init=__shellfu_${mname}__init
225
-            if test "$(command -v "$init")" = "$init";
226
-            then
221
+            if test "$(command -v "$init")" = "$init"; then
227 222
                 shellfu __debug "launching init function: $init"
228 223
                 $init
229 224
                 es=$?
@@ -247,8 +242,7 @@ shellfu() {
247 242
             echo "$SHELLFU_PATH:$SHELLFU_INCLUDE-sh:$SHELLFU_INCLUDE-$SHELLFU_COMPAT" \
248 243
               | tr ":" '\n' \
249 244
               | awk '!seen[$0]++' \
250
-              | while read -r incdir;
251
-                do
245
+              | while read -r incdir; do
252 246
                     shellfu __debug "checking: $incdir"
253 247
                     test -d "$incdir" || continue
254 248
                     shellfu __debug "looking into: $incdir"