2012-09-08 22:47:57 +00:00
|
|
|
#
|
|
|
|
# Defines helper functions.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
|
|
|
#
|
|
|
|
|
2012-09-08 23:08:54 +00:00
|
|
|
# Checks if a file can be autoloaded by trying to load it in a subshell.
|
|
|
|
function is-autoloadable {
|
|
|
|
( unfunction $1 ; autoload -U +X $1 ) &> /dev/null
|
2012-09-08 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2012-09-08 23:21:00 +00:00
|
|
|
# Checks if a name is a command, function, or alias.
|
2012-09-08 22:47:57 +00:00
|
|
|
function is-callable {
|
2017-04-13 11:05:16 +00:00
|
|
|
(( $+commands[$1] || $+functions[$1] || $+aliases[$1] || $+builtins[$1] ))
|
2012-09-08 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2012-09-08 23:08:54 +00:00
|
|
|
# Checks a boolean variable for "true".
|
|
|
|
# Case insensitive: "1", "y", "yes", "t", "true", "o", and "on".
|
|
|
|
function is-true {
|
|
|
|
[[ -n "$1" && "$1" == (1|[Yy]([Ee][Ss]|)|[Tt]([Rr][Uu][Ee]|)|[Oo]([Nn]|)) ]]
|
|
|
|
}
|
|
|
|
|
2012-09-08 22:47:57 +00:00
|
|
|
# Prints the first non-empty string in the arguments array.
|
|
|
|
function coalesce {
|
|
|
|
for arg in $argv; do
|
|
|
|
print "$arg"
|
|
|
|
return 0
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
2020-02-13 08:56:29 +00:00
|
|
|
|
2021-05-02 06:00:06 +00:00
|
|
|
# Checks if running on macOS Darwin.
|
2020-02-13 08:56:29 +00:00
|
|
|
function is-darwin {
|
|
|
|
[[ "$OSTYPE" == darwin* ]]
|
|
|
|
}
|
|
|
|
|
2021-05-02 06:00:06 +00:00
|
|
|
# Checks if running on Linux.
|
2020-02-13 08:56:29 +00:00
|
|
|
function is-linux {
|
|
|
|
[[ "$OSTYPE" == linux* ]]
|
|
|
|
}
|
|
|
|
|
2021-05-02 06:00:06 +00:00
|
|
|
# Checks if running on BSD.
|
2020-02-13 08:56:29 +00:00
|
|
|
function is-bsd {
|
|
|
|
[[ "$OSTYPE" == *bsd* ]]
|
|
|
|
}
|
|
|
|
|
2021-05-02 06:00:06 +00:00
|
|
|
# Checks if running on Cygwin (Windows).
|
2020-02-13 08:56:29 +00:00
|
|
|
function is-cygwin {
|
|
|
|
[[ "$OSTYPE" == cygwin* ]]
|
|
|
|
}
|
2020-07-20 23:07:53 +00:00
|
|
|
|
2021-05-02 06:00:06 +00:00
|
|
|
# Checks if running on termux (Android).
|
2020-07-20 23:07:53 +00:00
|
|
|
function is-termux {
|
|
|
|
[[ "$OSTYPE" == linux-android ]]
|
|
|
|
}
|