Skip to content

Commit

Permalink
echo-trim-empty-lines => echo-trim-zero-length
Browse files Browse the repository at this point in the history
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
  • Loading branch information
balupton committed Sep 1, 2023
1 parent 11a3705 commit 69fbc01
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 60 deletions.
6 changes: 4 additions & 2 deletions commands/echo-lines
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
4 changes: 2 additions & 2 deletions commands/echo-quiet
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 0 additions & 48 deletions commands/echo-trim-empty-lines

This file was deleted.

77 changes: 77 additions & 0 deletions commands/echo-trim-zero-length
Original file line number Diff line number Diff line change
@@ -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
28 changes: 25 additions & 3 deletions commands/expand-path
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion commands/expand-path-bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
2 changes: 1 addition & 1 deletion commands/expand-path-zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions commands/unziptar
Original file line number Diff line number Diff line change
Expand Up @@ -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 --'
Expand Down
3 changes: 2 additions & 1 deletion sources/stdinargs.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 69fbc01

Please sign in to comment.