Refactoring: Replace shellquote() with printf %q

I didn't know about printf's capability when I introduced quoting 10 years ago. The %q format will do the quoting, and "-v VAR" can be used to reassign to the variable.

Note: The shellquote() function has been exported for reuse by add-ons. I don't think anyone has ever used that (it was mostly intended for my own, extensive extensions, and I never used it), and is trivial to move away from, anyway.
This commit is contained in:
Ingo Karkat
2022-04-12 07:53:07 +02:00
parent ea32af34e6
commit aef7d8b9e5

11
todo.sh
View File

@@ -824,11 +824,6 @@ _addto() {
fi fi
} }
shellquote()
{
typeset -r qq=\'; printf %s\\n "'${1//\'/${qq}\\${qq}${qq}}'";
}
filtercommand() filtercommand()
{ {
filter=${1:-} filter=${1:-}
@@ -843,13 +838,13 @@ filtercommand()
then then
## First character isn't a dash: hide lines that don't match ## First character isn't a dash: hide lines that don't match
## this $search_term ## this $search_term
filter="${filter:-}${filter:+ | }grep -i $(shellquote "$search_term")" printf -v filter '%sgrep -i %q' "${filter:-}${filter:+ | }" "$search_term"
else else
## First character is a dash: hide lines that match this ## First character is a dash: hide lines that match this
## $search_term ## $search_term
# #
## Remove the first character (-) before adding to our filter command ## Remove the first character (-) before adding to our filter command
filter="${filter:-}${filter:+ | }grep -v -i $(shellquote "${search_term:1}")" printf -v filter '%sgrep -v -i %q' "${filter:-}${filter:+ | }" "${search_term:1}"
fi fi
done done
@@ -1036,7 +1031,7 @@ listWordsWithSigil()
| sort -u | sort -u
} }
export -f cleaninput getPrefix getTodo getNewtodo shellquote filtercommand _list listWordsWithSigil getPadding _format die export -f cleaninput getPrefix getTodo getNewtodo filtercommand _list listWordsWithSigil getPadding _format die
# == HANDLE ACTION == # == HANDLE ACTION ==
action=$( printf "%s\n" "$ACTION" | tr '[:upper:]' '[:lower:]' ) action=$( printf "%s\n" "$ACTION" | tr '[:upper:]' '[:lower:]' )