Skip to content

Commit

Permalink
small fixes with recent regexp changes:
Browse files Browse the repository at this point in the history
- ask, choose: use echo-wrap in arg form, no need for pipe
- get-devices, get-local-to-remote: fix echo-regexp regression, and fix sporadic nature of success which was due to missing `--stdin`
- bash.bash: simplify eval statements and support spaces in paths
  • Loading branch information
balupton committed Sep 16, 2024
1 parent e035a22 commit e9a955a
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions commands.beta/convert-helper
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function convert_helper() (
filename="$(fs-filename -- "$path")"
extension="$(fs-extension -- "$path")"
if test -n "$option_timestamp"; then
# prefix filename with timestamp
if test "$option_timestamp" = 'yes'; then
filename="$(date-helper --8601 | echo-regexp -g ':' '-') $filename"
else
Expand Down
4 changes: 2 additions & 2 deletions commands.beta/echo-if-empty
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ function echo_if_empty() (
my-fallback-value
# exit status: 0
echo | echo-if-empty 'my-fallback-value'
echo | echo-if-empty 'my-fallback-value' --stdin
my-fallback-value
# exit status: 0
echo 'a-value' | echo-if-empty 'my-fallback-value'
echo 'a-value' | echo-if-empty 'my-fallback-value' --stdin
'a-value'
# exit status: 0
Expand Down
2 changes: 1 addition & 1 deletion commands/ask
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ function ask_() (
fi # otherwise it has timed out. ctrl+c is not caught as we are not trapping it

# generate our erasure
result_prompt="$(__print_string "$result_prompt" | echo-wrap)"$'\n' # "$()" trims the trailing newline, so add it back
result_prompt="$(echo-wrap -- "$result_prompt")"$'\n' # "$()" trims the trailing newline, so add it back
result_prompt_row_count="$(echo-count-lines -- "$result_prompt")"
clear=$'\e['"$result_prompt_row_count"$'F\e[G\e[J'
fi
Expand Down
6 changes: 3 additions & 3 deletions commands/choose
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ function choose_() (

# shrink the content
shrunk="$content"
shrunk="$(__print_string "$shrunk" | echo-wrap --width="$content_columns")"
shrunk="$(echo-wrap --width="$content_columns" -- "$shrunk")"
rows="$(echo-count-lines -- "$shrunk")"
if test "$rows" -le 1; then
eval "${name}_content=\"\$content\""
Expand Down Expand Up @@ -979,7 +979,7 @@ function choose_() (
content_columns="$((terminal_columns - terminal_margin))"
# calculate menu header size
menu_header_shrunk="$menu_header"
menu_header_shrunk="$(__print_string "$menu_header_shrunk" | echo-wrap --width="$content_columns")"$'\n' # "$()" trims the trailing newline, so add it back
menu_header_shrunk="$(echo-wrap --width="$content_columns" -- "$menu_header_shrunk")"$'\n' # "$()" trims the trailing newline, so add it back
menu_header_rows="$(echo-count-lines -- "${menu_header_shrunk}")"
# refresh the legends
render_lengends
Expand Down Expand Up @@ -1496,7 +1496,7 @@ function choose_() (
# need to format item, as it is too big
item_rendered="$item_original"
item_rendered="${item_rendered//$'\n'/$'\n'"$style__indent_subsequent"}" # re-add the necessary indentation
item_rendered="$(__print_string "$item_rendered" | echo-wrap --width="$content_columns")"
item_rendered="$(echo-wrap --width="$content_columns" -- "$item_rendered")"
item_rows="$(echo-count-lines -- "$item_rendered")"
items_renders[item_index]="$item_rendered"
items_rows[item_index]="$item_rows"
Expand Down
4 changes: 3 additions & 1 deletion commands/echo-wrap
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ function echo_wrap_test() (
$(echo-style --green='1' --red='2' --blue='3' --yellow='4' --magenta='5' --cyan=$'6\n' --white='7' --black='8' --reset='9')
EOF
)"
eval_tester --name='echo-wrap is working as expected' --stdout="$expected" \
eval_tester --name='echo-wrap is working as expected with stdin' --stdout="$expected" \
-- echo-wrap --width=6 <<<"$input"
eval_tester --name='echo-wrap is working as expected with args' --stdout="$expected" \
-- echo-wrap --width=6 -- "$input"

echo-style --g1="TEST: $0"
return 0
Expand Down
4 changes: 2 additions & 2 deletions commands/get-devices
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ function get_devices() (
fi
if is-mac; then
MOUNT_RAID="$(sudo-helper -- diskutil appleRAID list)"
sudo-helper -- diskutil list | echo-regexp -og --regexp='/dev/[^ ]+'
sudo-helper -- diskutil list | echo-regexp -fong --regexp='/dev/[^ ]+'
else
sudo-helper -- fdisk -l | echo-regexp -ogm --regexp='Disk (/dev/.+): (.+?),.+\nDisk model: (.+)' --replace=$'$1\t$2\t$3'
sudo-helper -- fdisk -l | echo-regexp -fongm --regexp='Disk (/dev/.+): (.+?),.+\nDisk model: (.+)' --replace=$'$1\t$2\t$3'
fi
}
function get_one {
Expand Down
5 changes: 3 additions & 2 deletions commands/get-local-to-remote
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ function get_local_to_remote() (
if test -z "$option_target"; then
help "<locally-mounted-path> is required"
fi
option_target="$(fs-realpath -- "$option_target")"

# =====================================
# Action

local remotes result volume path server directory result
mapfile -t remotes < <(get-volumes --remote | echo-trim-empty-lines)
mapfile -t remotes < <(get-volumes --remote | echo-trim-empty-lines --stdin)
if test "${#remotes[@]}" -eq 0; then
help 'There were no remote volumes to convert to.'
help 'There were no remote volumes to convert ' --code="$option_target"
fi

result=''
Expand Down
6 changes: 3 additions & 3 deletions sources/bash.bash
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,17 @@ function eval_capture {

# save the stdout/stderr/output, and remove their temporary files
if test -n "$stdout_temp_file" -a -f "$stdout_temp_file"; then
eval "${stdout_variable}=\"\$(cat $stdout_temp_file)\""
eval "$stdout_variable"'="$(cat "$stdout_temp_file")"'
rm "$stdout_temp_file"
stdout_temp_file=''
fi
if test -n "$stderr_temp_file" -a -f "$stderr_temp_file"; then
eval "${stderr_variable}=\"\$(cat $stderr_temp_file)\""
eval "$stderr_variable"'="$(cat "$stderr_temp_file")"'
rm "$stderr_temp_file"
stderr_temp_file=''
fi
if test -n "$output_temp_file" -a -f "$output_temp_file"; then
eval "${output_variable}=\"\$(cat $output_temp_file)\""
eval "$output_variable"'="$(cat "$output_temp_file")"'
rm "$output_temp_file"
output_temp_file=''
fi
Expand Down

0 comments on commit e9a955a

Please sign in to comment.