Skip to content

Commit

Permalink
change all echos where appropriate with printf
Browse files Browse the repository at this point in the history
also:
- update `secret` to put output in the correct locations
- remove `foreach_line.md` as there is now `echo-eval`
  • Loading branch information
balupton committed Feb 11, 2025
1 parent d3a6ca9 commit 91e1f66
Show file tree
Hide file tree
Showing 38 changed files with 146 additions and 153 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/dorothy-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ jobs:
setup-util-ksh
setup-util-carapace
dorothy --slug=${{ github.event.pull_request.head.repo.full_name || github.repository }} install
# nu -c 'echo $nu.loginshell-path'
- name: 'Dorothy Login Shell: bash'
shell: bash -leo pipefail {0}
run: |
Expand Down Expand Up @@ -140,7 +139,7 @@ jobs:
# commit changes, if any
cd "$DOROTHY"
if git diff --quiet &>/dev/null; then
echo 'Already formatted.'
printf '%s\n' 'Already formatted.'
else
git config --global user.name 'Continuous Integration'
git config --global user.email 'bot@bevry.me'
Expand Down
4 changes: 4 additions & 0 deletions commands.beta/echo-eval
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env bash

# [Stack Exchange: How to apply shell command to each line of a command output?](https://stackoverflow.com/a/68310927/130638)
# `ls -1 | xargs -I %s -- echo %s`
# ^ xargs is too complicated, hence why this now exists

function echo_eval() (
source "$DOROTHY/sources/stdinargs.bash"

Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-nonflags
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function echo_nonflags() (
[edit --dry | echo-nonflags --stdin] will output [code -w -n] because of the following:
echo 'code -a --b c' | echo-nonflags --stdin
printf '%s\n' 'code -a --b c' | echo-nonflags --stdin
code -a --b c
# exit status: 0
Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-nothing-or-fail
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function echo_nothing_or_fail() (
echo-nothing-or-fail ''
echo-nothing-or-fail -- ''
echo-nothing-or-fail -- 'sup'
echo 'sup' | echo-nothing-or-fail --stdin
printf '%s\n' 'sup' | echo-nothing-or-fail --stdin
# exit status: 1
Expand Down
6 changes: 3 additions & 3 deletions commands.beta/eval-on-empty-stdin
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ function eval_on_empty_stdin() (
# Action

# test cases:
# echo sup | ( eval-on-empty-stdin echo no )
# true | ( eval-on-empty-stdin echo no )
# true | ( eval-on-empty-stdin cat )
# printf '%s\n' sup | ( eval-on-empty-stdin -- printf '%s\n' no )
# true | ( eval-on-empty-stdin -- printf '%s\n' no )
# true | ( eval-on-empty-stdin -- cat )

# doesn't work: read -r
# doesn't work: [[ -t 0 ]]
Expand Down
4 changes: 2 additions & 2 deletions commands.beta/eval-on-not-empty-stdin
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function eval_on_not_empty_stdin() (
# Action

# test cases:
# echo-lines -- 1 2 | ( eval-on-not-empty-stdin cat )
# true | ( eval-on-not-empty-stdin echo sup )
# echo-lines -- 1 2 | ( eval-on-not-empty-stdin -- cat )
# true | ( eval-on-not-empty-stdin -- printf '%s\n' sup )

# doesn't work: grep --regexp='^' | "${option_cmd[@]}" # eval

Expand Down
1 change: 0 additions & 1 deletion commands.beta/researchgate-rename
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function researchgate_rename() (
f="$(mktemp)"
pdftotext -layout -eol unix "$file" "$f"
while read -r line; do
# echo $line
if [[ -z $id ]]; then
# find id
id="$(__print_lines "$line" | echo-regexp -on "$regex" '$1')"
Expand Down
2 changes: 1 addition & 1 deletion commands/ask
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ function ask_() (
if [[ $ASKED == 'no' ]]; then
# do we want to confirm the default value
# adding a [&& -t 0] will cause stdin input to always be respected, which is inconsistent with expectations, e.g.
# echo 'overwrite' | { ask --linger --default='d1' --skip-default --question='q1'; ask --linger --default='d2' --skip-default --question='q2' )
# __print_lines 'overwrite' | { ask --linger --default='d1' --skip-default --question='q1'; ask --linger --default='d2' --skip-default --question='q2' )
# should output 'd1' and 'overwrite', not 'overwrite' and 'd2', as the goal of skip-default is to use the default preference for performance and intuition reasons
if [[ $option_confirm_default == 'no' ]] && is-value -- "$RESULT"; then
return 0
Expand Down
2 changes: 1 addition & 1 deletion commands/dorothy-config
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function dorothy_config() (
if [[ -n $default_filepath ]]; then
# start with the header of the default configuration file
echo-lines-before --needle='' --stdin <"$default_filepath" >"$temp_filepath"
echo >>"$temp_filepath"
__print_line >>"$temp_filepath"

# inject the sourcing of the default configuration file
if [[ $extension == 'nu' ]]; then
Expand Down
8 changes: 4 additions & 4 deletions commands/echo-clear-lines
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ function echo_clear_lines() (
EXAMPLE:
echo 'sup'
printf '%s\n' 'sup'
printf 'a\nb\nc'
echo-clear-lines -- $'a\nb\nc'
# ^ retains: sup
# ^ Note that the argument technique should be discouraged, as can cause [Argument list too long] error:
# https://github.com/bevry/dorothy/actions/runs/7622089094/job/20759535555#step:4:3259
echo 'sup'
printf '%s\n' 'sup'
printf 'a\nb\nc'
echo-clear-lines --stdin < <(printf 'a\nb\nc')
# ^ retains: sup
# ^ Use this instead of the arguments option, and do not use <<< as it prints a trailing newline, which would erase 'sup'.
echo 'sup'
printf '%s\n' 'sup'
printf 'a\nb\nc'
printf 'a\nb\nc' | echo-clear-lines
# ^ retains: sup
file="\$(mktemp)"
echo 'sup'
printf '%s\n' 'sup'
printf 'a\nb\nc' | tee -- "\$file
echo-clear-lines < "\$file"
# ^ retains: sup
Expand Down
4 changes: 2 additions & 2 deletions commands/echo-escape-bash
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function echo_escape_bash() (
# bash-5.2$ printf '%q\n' " a'"
# \ a\'
# bash-5.2$ b=\ a\'
# bash-5.2$ echo "[$b]"
# bash-5.2$ printf '%s\n' "[$b]"
# [ a']
# bash-5.2$ echo "${b@Q}"
# bash-5.2$ printf '%s\n' "${b@Q}"
# ' a'\'''

function on_input {
Expand Down
2 changes: 1 addition & 1 deletion commands/echo-or-fail
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function echo_or_fail() (
# success cases
echo-or-fail -- 'sup'
echo 'sup' | echo-or-fail --stdin
printf '%s\n' 'sup' | echo-or-fail --stdin
sup
# exit status: 0
Expand Down
4 changes: 2 additions & 2 deletions commands/echo-style
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ function echo_style() (
Use [--nocolor=<value>] to indicate the value should only be outputted if colors are not.
value="\$(echo-style --bold='first' --nocolor=' ' --color+dim=' → ' --bold='second')"; echo "\$value"
value="\$(echo-style --bold='first' --nocolor=' ' --color+dim=' → ' --bold='second')"; printf '%s\n' "\$value"
# outputs with colors: first → second
value="\$(env NO_COLOR=yes echo-style --bold='first' --nocolor=' ' --color+dim=' → ' --bold='second')"; echo "\$value"
value="\$(env NO_COLOR=yes echo-style --bold='first' --nocolor=' ' --color+dim=' → ' --bold='second')"; printf '%s\n' "\$value"
# outputs without colors: first second
EXPLANATION:
Expand Down
2 changes: 1 addition & 1 deletion commands/fs-realpath
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ function fs_realpath() (
fi
}
function __fish_capable {
__command_exists -- fish && [[ "$(version-compare "$(fish -c 'echo $FISH_VERSION')" 3.3.0)" -ge 0 ]]
__command_exists -- fish && [[ "$(version-compare "$(fish -c "printf '%s\n' \$FISH_VERSION")" 3.3.0)" -ge 0 ]]
return # explicit return with [[ required for bash v3
}
function do_fish_realpath {
Expand Down
4 changes: 2 additions & 2 deletions commands/get-terminal-theme
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ function get_terminal_theme() (
# as terminal theme is really only important for TTY use cases, detecting TTY support solves vscode unable to ssh session into a machine
if get-terminal-tty-support --quiet; then
# </dev/tty is used to ensure data = book in this example (don't need >/dev/tty as -s flag prevents output):
# echo 'book' | {
# __print_lines 'book' | {
# IFS=: read -s -t 1 -d $'\a' -p $'\e]11;?\a' _ color </dev/tty;
# read -t 1 data
# echo data = $data
# __print_lines "data = $data"
# }
#
# use a timeout of 1 seconds, as 0.01 is too slow for vscode tunnels, which causes the respond to be to tty and not captured, causing a false guess to them... as [get-terminal-theme] now has a cache, the larger timeout is not a big deal
Expand Down
8 changes: 4 additions & 4 deletions commands/git-helper
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,10 @@ function git_helper() (
__print_lines "$GIT_DEFAULT_BRANCH"
return 0
elif __does_remote_branch_exist main; then
echo main
__print_lines main
return 0
elif __does_remote_branch_exist master; then
echo master
__print_lines master
return 0
fi

Expand All @@ -502,10 +502,10 @@ function git_helper() (
__print_lines "$GIT_DEFAULT_BRANCH"
return 0
elif __does_local_branch_exist main; then
echo main
__print_lines main
return 0
elif __does_local_branch_exist master; then
echo master
__print_lines master
return 0
fi

Expand Down
1 change: 1 addition & 0 deletions commands/mount-helper
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ function mount_helper() (
)
if [[ -n $option_password ]]; then
# --extpass stringArray Use external program for the password prompt
# @todo figure out the escaping to convert this to printf, such as passwords can begin with a dash
gocryptfs_options+=(
"--extpass=echo $option_password"
)
Expand Down
47 changes: 24 additions & 23 deletions commands/secret
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function secret_() (
Once done, this enables:
secret get MY_SECRET
secret env MY_SECRET -- echo 'my secret is: \$MY_SECRET'
secret env MY_SECRET -- printf '%s\n' 'my secret is: \$MY_SECRET'
get <secret>
Expand All @@ -63,12 +63,12 @@ function secret_() (
For example:
secret env MY_SECRET -- echo 'my secret is: \$MY_SECRET'
secret env MY_SECRET -- printf '%s\n' 'my secret is: \$MY_SECRET'
Will get the value of MY_SECRET, and provide it to the echo command's environment
Will get the value of MY_SECRET, and provide it to the printf command's environment
such that when it is interpolated into
echo my secret is: \$MY_SECRET
printf '%s\n' "my secret is: \$MY_SECRET"
\$MY_SECRET will be exist in that environment, and be outputted accordingly.
Expand Down Expand Up @@ -208,7 +208,7 @@ function secret_() (
}
if ! sudo-helper --reason="$sudo_reason" -- is-file.bash -- "$database"; then
fs-rm --quiet --no-confirm -- "$database"
__print_lines '{}' | sudo tee -- "$database" &>/dev/null
__print_lines '{}' | echo-wait --sudo -- "$database"
fi

# move old state location to new state location
Expand Down Expand Up @@ -604,26 +604,26 @@ function secret_() (
return 1
fi
if [[ -z $website ]]; then
echo-style --dim="[$item] no urls"
echo-style --dim="[$item] no url to upgrade" >/dev/stderr
return 0
fi

# upgrade
result="$(get-url-upgrade "$website" 2>/dev/null || :)"
if [[ -z $result ]]; then
# echo to stdout, as is actionable for user if they are piping
# log
echo-style --warning="[$item] [$website] unresponsive, consider archiving" >/dev/stderr
return 1
fi

# check if changed
if [[ $result == "$website" ]]; then
echo-style --dim="[$item] [$website] already upgraded"
echo-style --dim="[$item] [$website] already upgraded" >/dev/stderr
return 0
fi

# echo to stdout, as is actionable for user if they are piping
echo-style --success="[$item] [$website] => [$result]"
# log
echo-style --success="[$item] [$website] => [$result]" >/dev/stderr

# apply
if [[ $op_version -eq 2 ]]; then
Expand Down Expand Up @@ -667,7 +667,7 @@ function secret_() (
last=''
else
# continue skipping
echo-style --dim="[$item] skipping" >"$terminal_device_file"
echo-style --dim="[$item] skipping" >/dev/stderr
continue
fi
fi
Expand All @@ -680,11 +680,11 @@ function secret_() (

# wait for completed jobs, then note completion
while [[ "$(get_job_count)" -ne 0 ]]; do
__print_lines "Waiting on [$(get_job_count)] jobs..." >"$terminal_device_file"
echo-style --notice="Waiting on [$(get_job_count)] jobs..." >"$terminal_device_file"
sleep 3
jobs &>/dev/null # for some reason, this seems necessary to prevent hung jobs
done
__print_lines 'All done.' >"$terminal_device_file"
echo-style --success='All done.' >/dev/stderr
}

function secret_fetch {
Expand All @@ -711,10 +711,10 @@ function secret_() (
--arg field "$field" \
'.[$secret] = [$vault, $item, $field]' \
"$database")"
__print_lines "$result" | sudo tee -- "$database" &>/dev/null
__print_lines "$result" | echo-wait --sudo -- "$database"
__print_lines \
"Successfully updated [$database] so that [$secret] fetches [$vault].[$item].[$field]" \
"Fetching [$secret]..." >"$terminal_device_file"
"Fetching [$secret]..." >/dev/stderr
cache_delete "$secret"
secret_get "$secret"
}
Expand All @@ -726,14 +726,13 @@ function secret_() (
--question='What environment variable (secret) name do you wish to unmap?' \
--default="${1-}"
)"
__print_lines 'Enter your sudo password to update the database with the correct permissions...' >"$terminal_device_file"
result="$(jq -r \
--arg secret "$secret" \
'del(.[$secret])' \
"$database")"
__print_lines "$result" | sudo tee -- "$database"
__print_lines "$result" | echo-wait --sudo -- "$database"
cache_delete "$secret"
__print_lines "Successfully unmapped [$secret]" >"$terminal_device_file"
__print_lines "Successfully unmapped [$secret]" >/dev/stderr
}

function secret_env {
Expand Down Expand Up @@ -762,11 +761,13 @@ function secret_() (
keys=("${before[@]}")
args=("${after[@]}")
else
__print_lines "found = $found"
__print_lines 'before ='
echo-verbose -- "${before[@]}"
__print_lines 'after ='
echo-verbose -- "${after[@]}"
{
__print_lines "found = $found"
__print_lines 'before ='
echo-verbose -- "${before[@]}"
__print_lines 'after ='
echo-verbose -- "${after[@]}"
} >/dev/stderr
help "env action requires [--] separator"
fi

Expand Down
2 changes: 1 addition & 1 deletion commands/setup-util-gum
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# gum_0.11.0_x86_64.apk

# rpm repo support requires the following, which isn't important as they provide .rpm files anyway
# echo '[charm]
# printf '%s\n' '[charm]
# name=Charm
# baseurl=https://repo.charm.sh/yum/
# enabled=1
Expand Down
4 changes: 2 additions & 2 deletions commands/setup-util-vscode
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function setup_util_vscode() (
if is-linux; then
# https://docs.microsoft.com/en-us/visualstudio/liveshare/reference/linux#install-linux-prerequisites
# optional as currently not completely supported on ubuntu 22.04
# `echo |` to skip the `Press enter to dismiss this message' prompt
echo | setup-util --name='Visual Studio Code: Linux Dependencies' --optional "$@" \
# `__print_line |` to skip the `Press enter to dismiss this message' prompt
__print_line | setup-util --name='Visual Studio Code: Linux Dependencies' --optional "$@" \
INSTALLER='https://aka.ms/vsls-linux-prereq-script'
fi

Expand Down
Loading

0 comments on commit 91e1f66

Please sign in to comment.