From 69fbc01963e7e3abf72530d34fd8699b6add5cbf Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Fri, 1 Sep 2023 17:34:23 +0800 Subject: [PATCH] echo-trim-empty-lines => echo-trim-zero-length echo-trim-zero-length: only eliminates zero length lines, which is all that it was needed for echo-lines: don't output if there is no input expand-path: add tests, move echo-trim-zero-length usage into expand-path-* expand-path-*: add echo-trim-zero-length unziptar: remove debug-bash usage, as we know where the issue was stdinargs: add IFS='' so that non-zero-length but otherwise empty lines are captured --- commands/echo-lines | 6 ++- commands/echo-quiet | 4 +- commands/echo-trim-empty-lines | 48 --------------------- commands/echo-trim-zero-length | 77 ++++++++++++++++++++++++++++++++++ commands/expand-path | 28 +++++++++++-- commands/expand-path-bash | 2 +- commands/expand-path-zsh | 2 +- commands/unziptar | 4 +- sources/stdinargs.bash | 3 +- 9 files changed, 114 insertions(+), 60 deletions(-) delete mode 100755 commands/echo-trim-empty-lines create mode 100755 commands/echo-trim-zero-length diff --git a/commands/echo-lines b/commands/echo-lines index 86e676cb7..fbf233cda 100755 --- a/commands/echo-lines +++ b/commands/echo-lines @@ -114,8 +114,10 @@ function echo_lines() ( } fi function on_finish { - # trunk-ignore(shellcheck/SC2059) - printf "$format" "${lines[@]}" + if test "${#lines[@]}" -ne 0; then + # trunk-ignore(shellcheck/SC2059) + printf "$format" "${lines[@]}" + fi } source "$DOROTHY/sources/stdinargs.bash" ) diff --git a/commands/echo-quiet b/commands/echo-quiet index 6fdb24e0b..6ee1d0707 100755 --- a/commands/echo-quiet +++ b/commands/echo-quiet @@ -49,10 +49,10 @@ function echo_quiet() ( shift case "$item" in '--no-verbose'* | '--verbose'*) - verbose="$(get-flag-value verbose -- "$item" | echo-trim-empty-lines | echo-affirmative)" + verbose="$(get-flag-value verbose -- "$item" | echo-trim-zero-length | echo-affirmative)" ;; '--no-quiet'* | '--quiet'*) - quiet="$(get-flag-value quiet -- "$item" | echo-trim-empty-lines | echo-affirmative)" + quiet="$(get-flag-value quiet -- "$item" | echo-trim-zero-length | echo-affirmative)" ;; *) ;; # ignore esac diff --git a/commands/echo-trim-empty-lines b/commands/echo-trim-empty-lines deleted file mode 100755 index 642c70ea2..000000000 --- a/commands/echo-trim-empty-lines +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env bash - -function echo_trim_empty_lines() ( - source "$DOROTHY/sources/bash.bash" - - function help { - cat <<-EOF >/dev/stderr - ABOUT: - If an input is empty, do not output it. - Uses [is-empty-value] for the check. - - USAGE: - echo-trim-empty-lines <...input> - echo-lines <...input> | echo-trim-empty-lines - - EXAMPLE: - - echo-trim-empty-lines '' 'a' ' ' 'b' 'null' 'c' 'false' - - a - b - c - false - - echo-lines '' 'a' ' ' 'b' 'null' 'c' 'false' | echo-trim-empty-lines - - a - b - c - false - - EOF - return 22 # EINVAL 22 Invalid argument - } - - function on_input { - if ! is-empty-value "$1"; then - print_line "$1" - fi - } - - source "$DOROTHY/sources/stdinargs.bash" -) - -# fire if invoked standalone -if test "$0" = "${BASH_SOURCE[0]}"; then - echo_trim_empty_lines "$@" -fi diff --git a/commands/echo-trim-zero-length b/commands/echo-trim-zero-length new file mode 100755 index 000000000..e4ad9867a --- /dev/null +++ b/commands/echo-trim-zero-length @@ -0,0 +1,77 @@ +#!/usr/bin/env bash + +function echo_trim_zero_length_test() ( + source "$DOROTHY/sources/bash.bash" + source "$(type -P eval-tester)" + echo-segment --h1="TEST: $0" + + local inputs=('a' '' 'b' ' ' 'c' 'null' 'd' 'false') + local expected=$'a\nb\n \nc\nnull\nd\nfalse' + + eval_tester --name='trimming arguments' --stdout="$expected" \ + -- echo-trim-zero-length "${inputs[@]}" + + function trim_stdin { + echo-lines "${inputs[@]}" | echo-trim-zero-length + } + eval_tester --name='trimming arguments' --stdout="$expected" \ + -- trim_stdin + + echo-segment --g1="TEST: $0" + return 0 +) +function echo_trim_zero_length() ( + source "$DOROTHY/sources/bash.bash" + + function help { + cat <<-EOF >/dev/stderr + ABOUT: + Trim inputs that are zero-length. + + USAGE: + echo-trim-zero-length <...input> + echo-lines <...input> | echo-trim-zero-length + + EXAMPLE: + + echo-trim-zero-length 'a' '' 'b' ' ' 'c' 'null' 'd' 'false' + + a + b + + c + null + d + false + + echo-lines 'a' '' 'b' ' ' 'c' 'null' 'd' 'false' | echo-trim-zero-length + + a + b + + c + null + d + false + + EOF + return 22 # EINVAL 22 Invalid argument + } + + function on_input { + if test -n "$1"; then + print_line "$1" + fi + } + + source "$DOROTHY/sources/stdinargs.bash" +) + +# fire if invoked standalone +if test "$0" = "${BASH_SOURCE[0]}"; then + if test "$*" = '--test'; then + echo_trim_zero_length_test + else + echo_trim_zero_length "$@" + fi +fi diff --git a/commands/expand-path b/commands/expand-path index b4ec89a8f..81d01ffcf 100755 --- a/commands/expand-path +++ b/commands/expand-path @@ -1,18 +1,40 @@ #!/usr/bin/env bash +function expand_path_test() ( + source "$DOROTHY/sources/bash.bash" + source "$(type -P eval-tester)" + echo-segment --h1="TEST: $0" + + # test fix for: https://github.com/bevry/dorothy/commit/fcaee87405eccc1b6cfa4b78c2bd3215c74537f0#diff-c2bcdf7f33a51745a999d7b87b788dedbad76901c10e74511e21463f5815a84f + function test_nonexistent_path { + local paths + mapfile -t paths < <(expand-path '/i/do/not exist*') + print_line "${#paths[@]}" + } + + eval_tester --name='test non-existent path' --stdout='0' \ + -- test_nonexistent_path + + echo-segment --g1="TEST: $0" + return 0 +) function expand_path() ( source "$DOROTHY/sources/bash.bash" # using zsh then bash, works around macos which has old bash, but new zsh # so this is needed so setup-paths-commands can do its thing if type -P zsh &>/dev/null; then - expand-path-zsh "$@" 2>/dev/null | echo-wait | echo-trim-empty-lines + expand-path-zsh "$@" 2>/dev/null else - expand-path-bash "$@" 2>/dev/null | echo-wait | echo-trim-empty-lines + expand-path-bash "$@" 2>/dev/null fi ) # fire if invoked standalone if test "$0" = "${BASH_SOURCE[0]}"; then - expand_path "$@" + if test "$*" = '--test'; then + expand_path_test + else + expand_path "$@" + fi fi diff --git a/commands/expand-path-bash b/commands/expand-path-bash index 628b26909..6f191ad79 100755 --- a/commands/expand-path-bash +++ b/commands/expand-path-bash @@ -12,7 +12,7 @@ function expand_path_bash() ( require_globstar fi for arg in "$@"; do - eval echo-lines -- "$(echo-escape-spaces "$arg")" + eval echo-lines -- "$(echo-escape-spaces "$arg")" | echo-trim-zero-length done ) diff --git a/commands/expand-path-zsh b/commands/expand-path-zsh index fc9e600dd..fed9bd597 100755 --- a/commands/expand-path-zsh +++ b/commands/expand-path-zsh @@ -21,5 +21,5 @@ unsetopt nomatch # Anonymice Nerd Font Complete.ttf for arg in "$@"; do - eval echo-lines -- "$(echo-escape-spaces "$arg")" + eval echo-lines -- "$(echo-escape-spaces "$arg")" | echo-trim-zero-length done diff --git a/commands/unziptar b/commands/unziptar index a8e6c4cfe..dc3ed3ebb 100755 --- a/commands/unziptar +++ b/commands/unziptar @@ -201,11 +201,11 @@ function unziptar() ( echo-style --bold='Filter:' ' ' "$filter" echo-style --bold='File:' ' ' "$file" echo-style --bold='-- expanded paths --' - debug-bash expand-path "$archive_directory/$filter" + expand-path "$archive_directory/$filter" echo-style --bold='-- expanded paths (zsh) --' expand-path-zsh "$archive_directory/$filter" echo-style --bold='-- expanded paths (bash) --' - debug-bash expand-path-bash "$archive_directory/$filter" + expand-path-bash "$archive_directory/$filter" echo-style --bold='-- filtered paths --' echo-verbose "${filtered_paths[@]}" echo-style --bold='-- ls --' diff --git a/sources/stdinargs.bash b/sources/stdinargs.bash index d27321396..b49941229 100755 --- a/sources/stdinargs.bash +++ b/sources/stdinargs.bash @@ -77,11 +77,12 @@ if test "$HAD_ARGS" != 'yes' -o "$REQUIRE_STDIN" = 'yes'; then # for each line, call `on_line` or `on_input` # for each inline, call `on_inline` or `on_line` or `on_input` # [read -t 0 item] will not read anything, so it must be done seperately + # IFS='' to not trim whitespace lines (e.g. ' ' would otherwise become '') if test -n "$TIMEOUT" -a "$TIMEOUT" -ne 0; then read_args+=("-t" "$TIMEOUT") fi item='' - while (test -z "$TIMEOUT" -o "$TIMEOUT" -ne 0 || read -t 0) && read -r "${read_args[@]}" item; do + while (test -z "$TIMEOUT" -o "$TIMEOUT" -ne 0 || read -t 0) && IFS='' read -r "${read_args[@]}" item; do HAD_STDIN='yes' if test "$BREAK" = 'yes'; then break