diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-02-02 23:47:19 +0100 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-02-02 23:47:19 +0100 |
commit | 54b7de4d1f1a73f212f176e477f02afec54e74cc (patch) | |
tree | cc677107bda5bb5b109849b4ad481d1883423077 /modules/by-name/lf | |
parent | fix(modules/lf/commands/trash_restore): Depend on `gawk` not `gaw` (diff) | |
download | nixos-config-54b7de4d1f1a73f212f176e477f02afec54e74cc.zip |
fix(modules/lf/commands/): Correctly modify `$@` when in a while read loop
Previously, I just piped into the loop which resulted in it becoming a subshell (i.e., all variable assignments were local to that loop.) The only way to work around this in POSIX shell is via temporary files, like implemented in the `base.sh` `tmp` function. I'd hoped to finally rid myself of these, when migrating away from the sysLib, but I see no other way to achieve the desired result.
Diffstat (limited to 'modules/by-name/lf')
9 files changed, 38 insertions, 15 deletions
diff --git a/modules/by-name/lf/lf/commands/base.sh b/modules/by-name/lf/lf/commands/base.sh index 1203bc13..61b59a7b 100755 --- a/modules/by-name/lf/lf/commands/base.sh +++ b/modules/by-name/lf/lf/commands/base.sh @@ -16,6 +16,28 @@ prompt() { printf "=> %s" "$1" } +# Reads its Stdin into a temporary file and returns the path of the temporary file. +# This is only really useful, if you want to pipe something into an while read loop that +# should modify global variables. Piping directly into it will not work, because the shell +# would just run it in a subshell, so you need this workaround. +# +# # Type +# tmp :: <stdin> -> Path +# +# # Arguments +# +# # Examples +# while read -r file; do +# set -- "$@" "$file" +# done < "$(echo "$fx" | tmp)" +tmp() { + __base_tmp_temporary_file="$(mktemp --tmpdir="$__base_tmp_temporary_directory")" + cat >"$__base_tmp_temporary_file" + echo "$__base_tmp_temporary_file" +} +__base_tmp_temporary_directory="$(mktemp --directory)" +trap 'rm --recursive "$__base_tmp_temporary_directory"' EXIT + # Run a lf command on the current lf client # All arguments will run in like they were typed directly into lf. # # TODO(@bpeetz): Escape the single quotes in the input arguments. <2025-02-02> diff --git a/modules/by-name/lf/lf/commands/run.sh b/modules/by-name/lf/lf/commands/run.sh index fba3653d..6a9b8cab 100755 --- a/modules/by-name/lf/lf/commands/run.sh +++ b/modules/by-name/lf/lf/commands/run.sh @@ -20,6 +20,7 @@ for arg in "$@"; do done export f="$1" +set -- # shellcheck source=/dev/null . ./base.sh diff --git a/modules/by-name/lf/lf/commands/scripts/archive_compress.sh b/modules/by-name/lf/lf/commands/scripts/archive_compress.sh index 5f93de7f..0d53af2a 100755 --- a/modules/by-name/lf/lf/commands/scripts/archive_compress.sh +++ b/modules/by-name/lf/lf/commands/scripts/archive_compress.sh @@ -48,9 +48,9 @@ while [ -z "$name" ] || [ -e "$name" ]; do done # fx contains all selected file name separated by a newline -echo "$fx" | while read -r file; do +while read -r file; do set -- "$@" "$file" -done +done <"$(echo "$fx" | tmp)" case "$archiver" in "gzip") diff --git a/modules/by-name/lf/lf/commands/scripts/dragon.sh b/modules/by-name/lf/lf/commands/scripts/dragon.sh index f6653ed3..0e6123e1 100755 --- a/modules/by-name/lf/lf/commands/scripts/dragon.sh +++ b/modules/by-name/lf/lf/commands/scripts/dragon.sh @@ -9,9 +9,9 @@ fs="$fs" # shellcheck disable=SC2269 id="$id" -echo "$fx" | while read -r file; do +while read -r file; do set -- "$@" "$file" -done +done < "$(echo "$fx" | tmp)" dragon --all --and-exit "$@" # vim: ft=sh diff --git a/modules/by-name/lf/lf/commands/scripts/dragon_individual.sh b/modules/by-name/lf/lf/commands/scripts/dragon_individual.sh index 27e57354..580b0cd0 100755 --- a/modules/by-name/lf/lf/commands/scripts/dragon_individual.sh +++ b/modules/by-name/lf/lf/commands/scripts/dragon_individual.sh @@ -9,9 +9,9 @@ fs="$fs" # shellcheck disable=SC2269 id="$id" -echo "$fx" | while read -r file; do +while read -r file; do set -- "$@" "$file" -done +done < "$(echo "$fx" | tmp)" dragon "$@" # vim: ft=sh diff --git a/modules/by-name/lf/lf/commands/scripts/dragon_stay.sh b/modules/by-name/lf/lf/commands/scripts/dragon_stay.sh index 54d60cb4..4a16e802 100755 --- a/modules/by-name/lf/lf/commands/scripts/dragon_stay.sh +++ b/modules/by-name/lf/lf/commands/scripts/dragon_stay.sh @@ -9,9 +9,9 @@ fs="$fs" # shellcheck disable=SC2269 id="$id" -echo "$fx" | while read -r file; do +while read -r file; do set -- "$@" "$file" -done +done <"$(echo "$fx" | tmp)" dragon --all "$@" # vim: ft=sh diff --git a/modules/by-name/lf/lf/commands/scripts/trash.sh b/modules/by-name/lf/lf/commands/scripts/trash.sh index d35c23ce..958bc3f9 100755 --- a/modules/by-name/lf/lf/commands/scripts/trash.sh +++ b/modules/by-name/lf/lf/commands/scripts/trash.sh @@ -9,9 +9,9 @@ fs="$fs" # shellcheck disable=SC2269 id="$id" -echo "$fx" | while read -r file; do +while read -r file; do set -- "$@" "$file" -done +done <"$(echo "$fx" | tmp)" -trash-put "$@" +trash-put -- "$@" # vim: ft=sh diff --git a/modules/by-name/lf/lf/commands/scripts/trash_clear.sh b/modules/by-name/lf/lf/commands/scripts/trash_clear.sh index 647d1b6e..e23bcd0b 100755 --- a/modules/by-name/lf/lf/commands/scripts/trash_clear.sh +++ b/modules/by-name/lf/lf/commands/scripts/trash_clear.sh @@ -1,8 +1,8 @@ # shellcheck shell=sh -conceal list | fzf --multi | awk '{for(i=3; i<=NF; i++) {print $i}}' | while read -r file; do +while read -r file; do set -- "$@" "$file" -done +done < "$(conceal list | fzf --multi | awk '{for(i=3; i<=NF; i++) {print $i}}' | tmp)" trash empty --match=exact "$@" diff --git a/modules/by-name/lf/lf/commands/scripts/trash_restore.sh b/modules/by-name/lf/lf/commands/scripts/trash_restore.sh index 81aeba01..f0ddfda4 100755 --- a/modules/by-name/lf/lf/commands/scripts/trash_restore.sh +++ b/modules/by-name/lf/lf/commands/scripts/trash_restore.sh @@ -9,9 +9,9 @@ fs="$fs" # shellcheck disable=SC2269 id="$id" -conceal list | fzf --multi | awk '{for(i=3; i<=NF; i++) {print $i}}' | while read -r file; do +while read -r file; do set -- "$@" "$file" -done +done <"$(conceal list | fzf --multi | awk '{for(i=3; i<=NF; i++) {print $i}}' | tmp)" trash restore --match=exact "$@" # vim: ft=sh |