diff --git a/.bash_functions/bool_eval.sh b/.bash_functions/bool_eval.sh new file mode 100644 index 0000000..411bf8e --- /dev/null +++ b/.bash_functions/bool_eval.sh @@ -0,0 +1,28 @@ +# Basic safe boolean evaluation. See this Gist for details: +# https://gist.github.com/gliech/184dc7566821442202f21dfe15e2b7ff +function truthy { + if [[ "${1,,}" == @(y|yes|on|true|1) ]]; then + return 0 + fi + return 1 +} + +function falsy { + if [[ "${1,,}" == @(n|no|off|false|0) ]]; then + return 0 + fi + return 1 +} + +function true_false_default { + case "${1,,}" in + y | yes | on | true | 1) + return 0 ;; + n | no | off | false | 0) + return 1 ;; + esac + shift + eval "$@" + return 1 +} + diff --git a/.bashrc b/.bashrc index 9b8ccd8..3340b9c 100644 --- a/.bashrc +++ b/.bashrc @@ -21,43 +21,18 @@ if (( BASH_VERSINFO[0] < 4 )); then return 1 fi -# Basic safe boolean evaluation. See this Gist for details: -# https://gist.github.com/gliech/184dc7566821442202f21dfe15e2b7ff -function truthy { - if [[ "${1,,}" == @(y|yes|on|true|1) ]]; then - return 0 - fi - return 1 -} +# Determine if the rest of the configuration should be loaded. DOTFILES_ACTIVE +# is a environment variable that can be set in ~/.environment.d/dotfiles.env +if [[ "${DOTFILES_ACTIVE,,}" == @(y|yes|on|true|1) ]]; then -function falsy { - if [[ "${1,,}" == @(n|no|off|false|0) ]]; then - return 0 - fi - return 1 -} - -function true_false_default { - case "${1,,}" in - y | yes | on | true | 1) - return 0 ;; - n | no | off | false | 0) - return 1 ;; - esac - shift - eval "$@" - return 1 -} - -export -f truthy falsy true_false_default - -# Now that we have the bare essentials, determine if the rest of the configura- -# tion should be loaded. DOTFILES_ACTIVE is a environment variable that can be -# set in ~/.environment.d/dotfiles.env -if truthy $DOTFILES_ACTIVE; then + # Load a function that allows us to import other function definitions from + # the BASH_FUNC_PATH without providing their exact location and export it + . "$DOTFILES_DIR/bash_functions/autosource.sh" + export -f autosource # Load utilty functions that may be used by other configurations - . "$DOTFILES_DIR/bash_functions.sh" + autosource bool_eval + autosource dot_utility # Load the rest of the bash configs in ~/.basrc.d for file in ~/.bashrc.d/*.sh; do diff --git a/.dotfiles/bash_functions/autosource.sh b/.dotfiles/bash_functions/autosource.sh new file mode 100644 index 0000000..dc4514c --- /dev/null +++ b/.dotfiles/bash_functions/autosource.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +function autosource { + # Test if 1 argument was given + if (( $# == 0 )); then + echo "autosource: basename of a file in BASH_FUNC_PATH required" + echo "autosource: usage: autosource basename [arguments]" + return 1 + fi + + # Split the BASH_FUNC_PATH into an array of directory names + IFS=: read -ra bash_function_dirs <<< "${BASH_FUNC_PATH}" + + # Look for a .sh file with the basename given as first argument in each direc- + # tory in each directory in turn + for bash_function_dir in "${bash_function_dirs[@]}"; do + if [[ -r "${bash_function_dir}/${1}.sh" ]]; then + + # If we find a matching file, source it and skip the rest of this function + . "${bash_function_dir}/${1}.sh" ${@:2} + return + fi + done + + # If the loop completes no matching file is in BASH_FUNC_PATH + echo "autosource: no ${1} in (${BASH_FUNC_PATH})" + return 1 +} diff --git a/.dotfiles/bash_functions.sh b/.dotfiles/bash_functions/dot_utility.sh similarity index 100% rename from .dotfiles/bash_functions.sh rename to .dotfiles/bash_functions/dot_utility.sh diff --git a/.environment.d/dotfiles.env b/.environment.d/dotfiles.env index 10de78c..9d16607 100644 --- a/.environment.d/dotfiles.env +++ b/.environment.d/dotfiles.env @@ -1,4 +1,4 @@ DOTFILES_ACTIVE=true DOTFILES_DIR="${HOME}/.dotfiles" DOTFILES_GITDIR="${HOME}/.dotgit" - +BASH_FUNC_PATH="${HOME}/.bash_functions:${DOTFILES_DIR}/bash_functions"