1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-06-25 16:57:12 -07:00

rewrite 'has' function

This commit is contained in:
Connor Olding 2021-09-30 08:29:28 -07:00
parent 6013eb84ac
commit 1196225de8
2 changed files with 22 additions and 15 deletions

View File

@ -4,13 +4,17 @@
# {{{1 utilities
if [ -z "$ZSH_VERSION" ]; then
has() { # hardcoded here for convenience.
which "$1" >/dev/null 2>&1 && which "$1"
}
fi
has() ( # hardcoded here for convenience. refer to ~/sh/has for documentation.
if [ "${1:--}" = "${1#*[!A-Za-z_]}" ]; then
alias "$1"=
unalias "$1"
eval "$1() (:)"
unset -f "$1"
fi
which -- "$1" >/dev/null 2>&1 && which -- "$1"
)
if which sudo >/dev/null 2>&1; then
if has sudo >/dev/null; then
maybesudo() { sudo "$@"; }
else
maybesudo() { maybesudo_ "$@"; }

21
sh/has
View File

@ -4,17 +4,20 @@
# YES_DASH
# YES_ASH
has() { ### @-
### print the result of `which` if the program is found, else simply return 1.
###
has() ( ### @- print the result of `which` if the program is found, else simply return 1.
### ```
### export CC="$(has clang || has clang-3.8 || has gcc)"
### export SOLVER="$(has kissat || has picosat || has cadical || has minisat)"
### ```
if [ -n "$ZSH_VERSION" ]; then
whence -p "$1"
else
which "$1" >/dev/null 2>&1 && which "$1"
if [ "${1:--}" = "${1#*[!A-Za-z_]}" ]; then # valid alias name?
# forcefully unalias it (regardless of if it even was an alias)
alias "$1"=
unalias "$1"
# forcefully unset it (regardless of if it even was a function)
eval "$1() (:)"
unset -f "$1"
fi
}
which -- "$1" >/dev/null 2>&1 && which -- "$1"
) # this uses parentheses instead of braces so that the function is always run
# in a subshell. otherwise, the user's aliases and functions may be overwritten.
[ -n "${preload+-}" ] || has "$@"