Skip to content

Commit

Permalink
Add __lp_is_function and tests
Browse files Browse the repository at this point in the history
This will be used to detect if a theme is valid and loaded in memory
before activating it.
  • Loading branch information
Rycieos committed Nov 25, 2020
1 parent 09cfced commit 9b40ca1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,14 @@ _lp_color_map() {
ret="${LP_COLORMAP[_LP_FIRST_INDEX+value*${#LP_COLORMAP[*]}/scale]}"
}

__lp_is_function() {
if (( _LP_SHELL_bash )); then
[[ $(LC_ALL=C \type -t "$1") == function ]]
else
[[ $(LC_ALL=C \type -w "$1") == *function ]]
fi
}

# Count the number of lines in the input string. A faster subsitute for 'wc -l'.
# input: $1; a single string of input.
# return: $count; the number of newline characters in the string.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ function test_pwd_tilde {
assertEquals "shorted home path" "~/a/test/path" "$lp_pwd_tilde"
}

function test_is_function {
function my_function { :; }

# Ignore errors, we just really need this to not be a function
unset -f not_my_function >/dev/null 2>&1 || true

assertTrue "failed to find valid function" '__lp_is_function my_function'
assertFalse "claimed to find non-existent function" '__lp_is_function not_my_function'

alias not_my_function=my_function
assertFalse "claimed alias was a function" '__lp_is_function not_my_function'

unset -f my_function
unalias not_my_function
}

if [ -n "${ZSH_VERSION-}" ]; then
SHUNIT_PARENT="$0"
setopt shwordsplit
Expand Down

0 comments on commit 9b40ca1

Please sign in to comment.