diff --git a/aliases/available/git.aliases.bash b/aliases/available/git.aliases.bash index 8cafa82b..08254dd5 100644 --- a/aliases/available/git.aliases.bash +++ b/aliases/available/git.aliases.bash @@ -194,11 +194,13 @@ case $OSTYPE in esac # functions -function gdv() { +function gdv() +{ git diff --ignore-all-space "$@" | vim -R - } -function get_default_branch() { +function get_default_branch() +{ if git branch | grep -q main; then echo main else diff --git a/aliases/available/vim.aliases.bash b/aliases/available/vim.aliases.bash index b426d270..91490f68 100644 --- a/aliases/available/vim.aliases.bash +++ b/aliases/available/vim.aliases.bash @@ -11,5 +11,11 @@ alias vimh='vim -c ":h | only"' # open vim in new tab is taken from # http://stackoverflow.com/questions/936501/let-gvim-always-run-a-single-instancek -_command_exists mvim && function mvimt { command mvim --remote-tab-silent "$@" || command mvim "$@"; } -_command_exists gvim && function gvimt { command gvim --remote-tab-silent "$@" || command gvim "$@"; } +_command_exists mvim && function mvimt +{ + command mvim --remote-tab-silent "$@" || command mvim "$@" +} +_command_exists gvim && function gvimt +{ + command gvim --remote-tab-silent "$@" || command gvim "$@" +} diff --git a/completion/available/composer.completion.bash b/completion/available/composer.completion.bash index 176f0832..f5ea133d 100644 --- a/completion/available/composer.completion.bash +++ b/completion/available/composer.completion.bash @@ -2,7 +2,8 @@ cite "about-completion" about-completion "composer completion" -function __composer_completion() { +function __composer_completion() +{ local cur coms opts com words COMPREPLY=() _get_comp_words_by_ref -n : cur words diff --git a/completion/available/dart.completion.bash b/completion/available/dart.completion.bash index b7563443..c5a809a3 100644 --- a/completion/available/dart.completion.bash +++ b/completion/available/dart.completion.bash @@ -1,6 +1,7 @@ # shellcheck shell=bash -__dart_completion() { +__dart_completion() +{ # shellcheck disable=SC2155 local prev=$(_get_pword) # shellcheck disable=SC2155 diff --git a/completion/available/dmidecode.completion.bash b/completion/available/dmidecode.completion.bash index 4a884524..4c11c170 100644 --- a/completion/available/dmidecode.completion.bash +++ b/completion/available/dmidecode.completion.bash @@ -1,6 +1,7 @@ # shellcheck shell=bash -function __dmidecode_completion() { +function __dmidecode_completion() +{ # shellcheck disable=SC2155 local prev=$(_get_pword) # shellcheck disable=SC2155 diff --git a/completion/available/gem.completion.bash b/completion/available/gem.completion.bash index ab07a133..a4c34843 100644 --- a/completion/available/gem.completion.bash +++ b/completion/available/gem.completion.bash @@ -2,7 +2,8 @@ cite "about-completion" about-completion "gem completion" -__gem_completion() { +__gem_completion() +{ local cur=${COMP_WORDS[COMP_CWORD]} local prev=${COMP_WORDS[COMP_CWORD - 1]} case $prev in diff --git a/completion/available/knife.completion.bash b/completion/available/knife.completion.bash index 4b9950ed..4f632859 100644 --- a/completion/available/knife.completion.bash +++ b/completion/available/knife.completion.bash @@ -33,13 +33,15 @@ stat -c %Y /dev/null > /dev/null 2>&1 && _KAC_STAT_COMMAND="stat -c %Y" || _KAC_ # returns 0 iff the file whose path is given as 1st argument # exists and has last been modified in the last $2 seconds # returns 1 otherwise -_KAC_is_file_newer_than() { +_KAC_is_file_newer_than() +{ [ -f "$1" ] || return 1 [ $(($(date +%s) - $($_KAC_STAT_COMMAND "$1"))) -gt "$2" ] && return 1 || return 0 } # helper function for _KAC_get_and_regen_cache, see doc below -_KAC_regen_cache() { +_KAC_regen_cache() +{ local CACHE_NAME=$1 local CACHE_PATH="$_KNIFE_AUTOCOMPLETE_CACHE_DIR/$CACHE_NAME" # shellcheck disable=SC2155 @@ -54,12 +56,14 @@ _KAC_regen_cache() { } # cached files can't have spaces in their names -_KAC_get_cache_name_from_command() { +_KAC_get_cache_name_from_command() +{ echo "${@/ /_SPACE_}" } # the reverse operation from the function above -_KAC_get_command_from_cache_name() { +_KAC_get_command_from_cache_name() +{ echo "${@/_SPACE_/ }" } @@ -68,7 +72,8 @@ _KAC_get_command_from_cache_name() { # in either case, it regenerates the cache, and sets the _KAC_CACHE_PATH env variable # for obvious reason, do NOT call that in a sub-shell (in particular, no piping) # shellcheck disable=SC2155 -_KAC_get_and_regen_cache() { +_KAC_get_and_regen_cache() +{ # the cache name can't have space in it local CACHE_NAME=$(_KAC_get_cache_name_from_command "$@") local REGEN_CMD="_KAC_regen_cache $CACHE_NAME $*" @@ -83,15 +88,16 @@ _KAC_get_and_regen_cache() { # performs two things: first, deletes all obsolete temp files # then refreshes stale caches that haven't been called in a long time -_KAC_clean_cache() { +_KAC_clean_cache() +{ local FILE CMD # delete all obsolete temp files, could be lingering there for any kind of crash in the caching process for FILE in "$_KAC_CACHE_TMP_DIR"/*; do _KAC_is_file_newer_than "$FILE" "$_KNIFE_AUTOCOMPLETE_MAX_CACHE_AGE" || rm -f "$FILE" done # refresh really stale caches - find "$_KNIFE_AUTOCOMPLETE_CACHE_DIR" -maxdepth 1 -type f -not -name '.*' \ - | while read -r FILE; do + find "$_KNIFE_AUTOCOMPLETE_CACHE_DIR" -maxdepth 1 -type f -not -name '.*' | + while read -r FILE; do _KAC_is_file_newer_than "$FILE" "$_KNIFE_AUTOCOMPLETE_MAX_CACHE_AGE" && continue # first let's get the original command CMD=$(_KAC_get_command_from_cache_name "$(basename "$FILE")") @@ -109,14 +115,16 @@ _KAC_clean_cache() { ##################################### # returns all the possible knife sub-commands -_KAC_knife_commands() { +_KAC_knife_commands() +{ knife --help | grep -E "^knife" | sed -E 's/ \(options\)//g' } # rebuilds the knife base command currently being completed, and assigns it to $_KAC_CURRENT_COMMAND # additionnally, returns 1 iff the current base command is not complete, 0 otherwise # also sets $_KAC_CURRENT_COMMAND_NB_WORDS if the base command is complete -_KAC_get_current_base_command() { +_KAC_get_current_base_command() +{ local PREVIOUS="knife" local I=1 local CURRENT @@ -136,7 +144,8 @@ _KAC_get_current_base_command() { # (i.e. handles "plural" arguments such as knife cookbook upload cookbook1 cookbook2 and so on...) # assumes the current base command is complete # shellcheck disable=SC2155 -_KAC_get_current_arg_position() { +_KAC_get_current_arg_position() +{ local CURRENT_ARG_POS=$((_KAC_CURRENT_COMMAND_NB_WORDS + 1)) local COMPLETE_COMMAND=$(grep -E "^$_KAC_CURRENT_COMMAND" "$_KAC_CACHE_PATH") local CURRENT_ARG @@ -150,7 +159,8 @@ _KAC_get_current_arg_position() { } # the actual auto-complete function -_knife() { +_knife() +{ _KAC_get_and_regen_cache _KAC_knife_commands local RAW_LIST ITEM REGEN_CMD ARG_POSITION # shellcheck disable=SC2034 diff --git a/completion/available/laravel.completion.bash b/completion/available/laravel.completion.bash index 8f032568..728f9038 100644 --- a/completion/available/laravel.completion.bash +++ b/completion/available/laravel.completion.bash @@ -2,7 +2,8 @@ _command_exists laravel || return -function __laravel_completion() { +function __laravel_completion() +{ local OPTS=('-h' '--help' '-q' '--quiet' '--ansi' '--no-ansi' '-n' '--no-interaction' '-v' '-vv' '-vvv' '--verbose' 'help' 'list' 'new') local _opt_ COMPREPLY=() diff --git a/completion/available/lerna.completion.bash b/completion/available/lerna.completion.bash index f83f1125..d380f59a 100644 --- a/completion/available/lerna.completion.bash +++ b/completion/available/lerna.completion.bash @@ -2,7 +2,8 @@ cite "about-completion" about-completion "lerna(javascript project manager tool) completion" -function __lerna_completion() { +function __lerna_completion() +{ local cur compls # The currently-being-completed word. diff --git a/completion/available/ngrok.completion.bash b/completion/available/ngrok.completion.bash index ca50a16f..57d50b8c 100644 --- a/completion/available/ngrok.completion.bash +++ b/completion/available/ngrok.completion.bash @@ -1,6 +1,7 @@ # shellcheck shell=bash -__ngrok_completion() { +__ngrok_completion() +{ # shellcheck disable=SC2155 local prev=$(_get_pword) # shellcheck disable=SC2155 diff --git a/completion/available/notify-send.completion.bash b/completion/available/notify-send.completion.bash index 676485f8..f8aaef29 100644 --- a/completion/available/notify-send.completion.bash +++ b/completion/available/notify-send.completion.bash @@ -1,6 +1,7 @@ # shellcheck shell=bash -function __notify-send_completions() { +function __notify-send_completions() +{ # shellcheck disable=SC2155 local curr=$(_get_cword) # shellcheck disable=SC2155 diff --git a/completion/available/pip.completion.bash b/completion/available/pip.completion.bash index f094d6ed..8456158d 100644 --- a/completion/available/pip.completion.bash +++ b/completion/available/pip.completion.bash @@ -8,7 +8,8 @@ # So that pip is in the system's path. _command_exists pip || return -function __bash_it_complete_pip() { +function __bash_it_complete_pip() +{ if _command_exists _pip_completion; then complete -o default -F _pip_completion pip _pip_completion "$@" diff --git a/completion/available/pip3.completion.bash b/completion/available/pip3.completion.bash index 34abc053..95a37546 100644 --- a/completion/available/pip3.completion.bash +++ b/completion/available/pip3.completion.bash @@ -8,7 +8,8 @@ # So that pip3 is in the system's path. _command_exists pip3 || return -function __bash_it_complete_pip3() { +function __bash_it_complete_pip3() +{ if _command_exists _pip_completion; then complete -o default -F _pip_completion pip3 _pip_completion "$@" diff --git a/completion/available/sdkman.completion.bash b/completion/available/sdkman.completion.bash index 2dc09088..16a031af 100644 --- a/completion/available/sdkman.completion.bash +++ b/completion/available/sdkman.completion.bash @@ -1,6 +1,7 @@ # shellcheck shell=bash -function _sdkman_complete() { +function _sdkman_complete() +{ local CANDIDATES local CANDIDATE_VERSIONS local SDKMAN_CANDIDATES_CSV="${SDKMAN_CANDIDATES_CSV:-}" @@ -48,13 +49,15 @@ function _sdkman_complete() { return 0 } -function _sdkman_candidate_local_versions() { +function _sdkman_candidate_local_versions() +{ CANDIDATE_VERSIONS=$(__sdkman_cleanup_local_versions "$1") } -function _sdkman_candidate_all_versions() { +function _sdkman_candidate_all_versions() +{ candidate="$1" CANDIDATE_LOCAL_VERSIONS=$(__sdkman_cleanup_local_versions "$candidate") @@ -77,7 +80,8 @@ function _sdkman_candidate_all_versions() { } -function __sdkman_cleanup_local_versions() { +function __sdkman_cleanup_local_versions() +{ __sdkman_build_version_csv "$1" | tr ',' ' ' diff --git a/completion/available/vuejs.completion.bash b/completion/available/vuejs.completion.bash index 751658f0..e2b776cf 100644 --- a/completion/available/vuejs.completion.bash +++ b/completion/available/vuejs.completion.bash @@ -1,6 +1,7 @@ # shellcheck shell=bash -__vuejs_completion() { +__vuejs_completion() +{ # shellcheck disable=SC2155 local prev=$(_get_pword) # shellcheck disable=SC2155 diff --git a/completion/available/wpscan.completion.bash b/completion/available/wpscan.completion.bash index 105468a3..c7c9f584 100644 --- a/completion/available/wpscan.completion.bash +++ b/completion/available/wpscan.completion.bash @@ -2,7 +2,8 @@ _command_exists wpscan || return -function __wpscan_completion() { +function __wpscan_completion() +{ local _opt_ local OPTS=('--help' '--hh' '--version' '--url' '--ignore-main-redirect' '--verbose' '--output' '--format' '--detection-mode' '--scope' '--headers' '--user-agent' '--vhost' '--random-user-agent' '--user-agents-list' '--http-auth' '--max-threads' '--throttle' '--request-timeout' '--connect-timeout' '--disable-tlc-checks' '--proxy' '--proxy-auth' '--cookie-string' '--cookie-jar' '--cache-ttl' '--clear-cache' '--server' '--cache-dir' '--update' '--no-update' '--wp-content-dir' '--wp-plugins-dir' '--wp-version-detection' '--main-theme-detection' '--enumerate' '--exclude-content-based' '--plugins-list' '--plugins-detection' '--plugins-version-all' '--plugins-version-detection' '--themes-list' '--themes-detection' '--themes-version-all' '--themes-version-detection' '--timthumbs-list' '--timthumbs-detection' '--config-backups-list' '--config-backups-detection' '--db-exports-list' '--db-exports-detection' '--medias-detection' '--users-list' '--users-detection' '--passwords' '--usernames' '--multicall-max-passwords' '--password-attack' '--stealthy') COMPREPLY=() diff --git a/hooks/check-clean-files-txt.sh b/hooks/check-clean-files-txt.sh index a37ee777..f2ffc1f5 100755 --- a/hooks/check-clean-files-txt.sh +++ b/hooks/check-clean-files-txt.sh @@ -7,7 +7,8 @@ if [ "$file" != "clean_files.txt" ]; then exit 1 fi -function compare_lines() { +function compare_lines() +{ prev="" while read -r line; do # Skip unimportant lines diff --git a/install.sh b/install.sh index 5d2f883e..831072db 100755 --- a/install.sh +++ b/install.sh @@ -2,7 +2,8 @@ # bash-it installer # Show how to use this installer -function _bash-it_show_usage() { +function _bash-it_show_usage() +{ echo -e "\n$0 : Install bash-it" echo -e "Usage:\n$0 [arguments] \n" echo "Arguments:" @@ -16,7 +17,8 @@ function _bash-it_show_usage() { } # enable a thing -function _bash-it_load_one() { +function _bash-it_load_one() +{ file_type=$1 file_to_enable=$2 mkdir -p "$BASH_IT/${file_type}/enabled" @@ -30,7 +32,8 @@ function _bash-it_load_one() { } # Interactively enable several things -function _bash-it_load_some() { +function _bash-it_load_some() +{ file_type=$1 single_type=$(echo "$file_type" | sed -e "s/aliases$/alias/g" | sed -e "s/plugins$/plugin/g") enable_func="_enable-$single_type" @@ -57,27 +60,31 @@ function _bash-it_load_some() { } # Back up existing profile -function _bash-it_backup() { - test -w "$HOME/$CONFIG_FILE" \ - && cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" \ - && echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m" +function _bash-it_backup() +{ + test -w "$HOME/$CONFIG_FILE" && + cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && + echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m" } # Back up existing profile and create new one for bash-it -function _bash-it_backup_new() { +function _bash-it_backup_new() +{ _bash-it_backup sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" echo -e "\033[0;32mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE, edit this file to customize bash-it\033[0m" } # Back up existing profile and append bash-it templates at the end -function _bash-it_backup_append() { +function _bash-it_backup_append() +{ _bash-it_backup (sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" | tail -n +2) >> "$HOME/$CONFIG_FILE" echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m" } -function _bash-it_check_for_backup() { +function _bash-it_check_for_backup() +{ if ! [[ -e "$HOME/$BACKUP_FILE" ]]; then return fi @@ -111,7 +118,8 @@ function _bash-it_check_for_backup() { fi } -function _bash-it_modify_config_files() { +function _bash-it_modify_config_files() +{ _bash-it_check_for_backup if [[ -z "${silent}" ]]; then diff --git a/lib/helpers.bash b/lib/helpers.bash index 94df885d..1031d171 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -12,72 +12,69 @@ BASH_IT_LOAD_PRIORITY_SEPARATOR="---" # sed "${BASH_IT_SED_I_PARAMETERS[@]}" -e "..." file BASH_IT_SED_I_PARAMETERS=(-i) case "$OSTYPE" in - 'darwin'*) BASH_IT_SED_I_PARAMETERS=(-i "") + 'darwin'*) BASH_IT_SED_I_PARAMETERS=(-i "") ;; esac -function _command_exists () +function _command_exists() { - _about 'checks for existence of a command' - _param '1: command to check' - _param '2: (optional) log message to include when command not found' - _example '$ _command_exists ls && echo exists' - _group 'lib' - local msg="${2:-Command '$1' does not exist!}" - if type -t "$1" &> /dev/null - then - return 0 - else - _log_warning "$msg" - return 1 - fi + _about 'checks for existence of a command' + _param '1: command to check' + _param '2: (optional) log message to include when command not found' + _example '$ _command_exists ls && echo exists' + _group 'lib' + local msg="${2:-Command '$1' does not exist!}" + if type -t "$1" &> /dev/null; then + return 0 + else + _log_warning "$msg" + return 1 + fi } -function _binary_exists () +function _binary_exists() { - _about 'checks for existence of a binary' - _param '1: binary to check' - _param '2: (optional) log message to include when binary not found' - _example '$ _binary_exists ls && echo exists' - _group 'lib' - local msg="${2:-Binary '$1' does not exist!}" - if type -P "$1" &> /dev/null - then - return 0 - else - _log_warning "$msg" - return 1 - fi + _about 'checks for existence of a binary' + _param '1: binary to check' + _param '2: (optional) log message to include when binary not found' + _example '$ _binary_exists ls && echo exists' + _group 'lib' + local msg="${2:-Binary '$1' does not exist!}" + if type -P "$1" &> /dev/null; then + return 0 + else + _log_warning "$msg" + return 1 + fi } -function _completion_exists () +function _completion_exists() { - _about 'checks for existence of a completion' - _param '1: command to check' - _param '2: (optional) log message to include when completion is found' - _example '$ _completion_exists gh && echo exists' - _group 'lib' - local msg="${2:-Completion for '$1' already exists!}" - complete -p "$1" &> /dev/null && _log_warning "$msg" ; + _about 'checks for existence of a completion' + _param '1: command to check' + _param '2: (optional) log message to include when completion is found' + _example '$ _completion_exists gh && echo exists' + _group 'lib' + local msg="${2:-Completion for '$1' already exists!}" + complete -p "$1" &> /dev/null && _log_warning "$msg" } function _bash_it_homebrew_check() { - if _binary_exists 'brew' - then # Homebrew is installed - if [[ "${BASH_IT_HOMEBREW_PREFIX:-unset}" == 'unset' ]] - then # variable isn't set + if _binary_exists 'brew'; then # Homebrew is installed + if [[ "${BASH_IT_HOMEBREW_PREFIX:-unset}" == 'unset' ]]; then # variable isn't set BASH_IT_HOMEBREW_PREFIX="$(brew --prefix)" else true # Variable is set already, don't invoke `brew`. fi - else # Homebrew is not installed. + else # Homebrew is not installed. BASH_IT_HOMEBREW_PREFIX= # clear variable, if set to anything. - false # return failure if brew not installed. + false # return failure if brew not installed. fi } -function _make_reload_alias() { - echo "source \${BASH_IT}/scripts/reloader.bash ${1} ${2}" +function _make_reload_alias() +{ + echo "source \${BASH_IT}/scripts/reloader.bash ${1} ${2}" } # Alias for reloading aliases @@ -92,768 +89,785 @@ alias reload_completion="$(_make_reload_alias completion completion)" # shellcheck disable=SC2139 alias reload_plugins="$(_make_reload_alias plugin plugins)" -bash-it () +bash-it() { - about 'Bash-it help and maintenance' - param '1: verb [one of: help | show | enable | disable | migrate | update | search | version | reload | restart | doctor ] ' - param '2: component type [one of: alias(es) | completion(s) | plugin(s) ] or search term(s)' - param '3: specific component [optional]' - example '$ bash-it show plugins' - example '$ bash-it help aliases' - example '$ bash-it enable plugin git [tmux]...' - example '$ bash-it disable alias hg [tmux]...' - example '$ bash-it migrate' - example '$ bash-it update' - example '$ bash-it search [-|@]term1 [-|@]term2 ... [ -e/--enable ] [ -d/--disable ] [ -r/--refresh ] [ -c/--no-color ]' - example '$ bash-it version' - example '$ bash-it reload' - example '$ bash-it restart' - example '$ bash-it doctor errors|warnings|all' - typeset verb=${1:-} - shift - typeset component=${1:-} - shift - typeset func + about 'Bash-it help and maintenance' + param '1: verb [one of: help | show | enable | disable | migrate | update | search | version | reload | restart | doctor ] ' + param '2: component type [one of: alias(es) | completion(s) | plugin(s) ] or search term(s)' + param '3: specific component [optional]' + example '$ bash-it show plugins' + example '$ bash-it help aliases' + example '$ bash-it enable plugin git [tmux]...' + example '$ bash-it disable alias hg [tmux]...' + example '$ bash-it migrate' + example '$ bash-it update' + example '$ bash-it search [-|@]term1 [-|@]term2 ... [ -e/--enable ] [ -d/--disable ] [ -r/--refresh ] [ -c/--no-color ]' + example '$ bash-it version' + example '$ bash-it reload' + example '$ bash-it restart' + example '$ bash-it doctor errors|warnings|all' + typeset verb=${1:-} + shift + typeset component=${1:-} + shift + typeset func - case $verb in - show) - func=_bash-it-$component;; - enable) - func=_enable-$component;; - disable) - func=_disable-$component;; - help) - func=_help-$component;; - doctor) - func=_bash-it-doctor-$component;; - search) - _bash-it-search $component "$@" - return;; - update) - func=_bash-it-update-$component;; - migrate) - func=_bash-it-migrate;; - version) - func=_bash-it-version;; - restart) - func=_bash-it-restart;; - reload) - func=_bash-it-reload;; - *) - reference bash-it - return;; - esac + case $verb in + show) + func=_bash-it-$component + ;; + enable) + func=_enable-$component + ;; + disable) + func=_disable-$component + ;; + help) + func=_help-$component + ;; + doctor) + func=_bash-it-doctor-$component + ;; + search) + _bash-it-search $component "$@" + return + ;; + update) + func=_bash-it-update-$component + ;; + migrate) + func=_bash-it-migrate + ;; + version) + func=_bash-it-version + ;; + restart) + func=_bash-it-restart + ;; + reload) + func=_bash-it-reload + ;; + *) + reference bash-it + return + ;; + esac - # pluralize component if necessary - if ! _is_function $func; then - if _is_function ${func}s; then - func=${func}s - else - if _is_function ${func}es; then - func=${func}es - else - echo "oops! $component is not a valid option!" - reference bash-it - return - fi - fi - fi + # pluralize component if necessary + if ! _is_function $func; then + if _is_function ${func}s; then + func=${func}s + else + if _is_function ${func}es; then + func=${func}es + else + echo "oops! $component is not a valid option!" + reference bash-it + return + fi + fi + fi - if [ x"$verb" == x"enable" ] || [ x"$verb" == x"disable" ]; then - # Automatically run a migration if required - _bash-it-migrate + if [ x"$verb" == x"enable" ] || [ x"$verb" == x"disable" ]; then + # Automatically run a migration if required + _bash-it-migrate - for arg in "$@" - do - $func $arg - done + for arg in "$@"; do + $func $arg + done - if [ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]; then - _bash-it-reload - fi - else - $func "$@" - fi + if [ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]; then + _bash-it-reload + fi + else + $func "$@" + fi } -_is_function () +_is_function() { - _about 'sets $? to true if parameter is the name of a function' - _param '1: name of alleged function' - _group 'lib' - [ -n "$(LANG=C type -t $1 2>/dev/null | grep 'function')" ] + _about 'sets $? to true if parameter is the name of a function' + _param '1: name of alleged function' + _group 'lib' + [ -n "$(LANG=C type -t $1 2> /dev/null | grep 'function')" ] } -_bash-it-aliases () +_bash-it-aliases() { - _about 'summarizes available bash_it aliases' - _group 'lib' + _about 'summarizes available bash_it aliases' + _group 'lib' - _bash-it-describe "aliases" "an" "alias" "Alias" + _bash-it-describe "aliases" "an" "alias" "Alias" } -_bash-it-completions () +_bash-it-completions() { - _about 'summarizes available bash_it completions' - _group 'lib' + _about 'summarizes available bash_it completions' + _group 'lib' - _bash-it-describe "completion" "a" "completion" "Completion" + _bash-it-describe "completion" "a" "completion" "Completion" } -_bash-it-plugins () +_bash-it-plugins() { - _about 'summarizes available bash_it plugins' - _group 'lib' + _about 'summarizes available bash_it plugins' + _group 'lib' - _bash-it-describe "plugins" "a" "plugin" "Plugin" + _bash-it-describe "plugins" "a" "plugin" "Plugin" } -_bash-it-update-dev() { - _about 'updates Bash-it to the latest master' - _group 'lib' +_bash-it-update-dev() +{ + _about 'updates Bash-it to the latest master' + _group 'lib' - _bash-it-update- dev "$@" + _bash-it-update- dev "$@" } -_bash-it-update-stable() { - _about 'updates Bash-it to the latest tag' - _group 'lib' +_bash-it-update-stable() +{ + _about 'updates Bash-it to the latest tag' + _group 'lib' - _bash-it-update- stable "$@" + _bash-it-update- stable "$@" } -_bash-it_update_migrate_and_restart() { +_bash-it_update_migrate_and_restart() +{ _about 'Checks out the wanted version, pops directory and restart. Does not return (because of the restart!)' - _param '1: Which branch to checkout to' - _param '2: Which type of version we are using' - git checkout "$1" &> /dev/null - if [[ $? -eq 0 ]]; then - echo "Bash-it successfully updated." - echo "" - echo "Migrating your installation to the latest $2 version now..." - _bash-it-migrate - echo "" - echo "All done, enjoy!" - # Don't forget to restore the original pwd! - popd &> /dev/null - _bash-it-restart - else - echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean." - fi + _param '1: Which branch to checkout to' + _param '2: Which type of version we are using' + git checkout "$1" &> /dev/null + if [[ $? -eq 0 ]]; then + echo "Bash-it successfully updated." + echo "" + echo "Migrating your installation to the latest $2 version now..." + _bash-it-migrate + echo "" + echo "All done, enjoy!" + # Don't forget to restore the original pwd! + popd &> /dev/null + _bash-it-restart + else + echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean." + fi } -_bash-it-update-() { - _about 'updates Bash-it' - _param '1: What kind of update to do (stable|dev)' - _group 'lib' - - declare silent - for word in $@; do - if [[ ${word} == "--silent" || ${word} == "-s" ]]; then - silent=true - fi - done - - pushd "${BASH_IT}" &> /dev/null || return - - DIFF=$(git diff --name-status) - [ -n "$DIFF" ] && echo -e "Local changes detected in bash-it directory. Clean '$BASH_IT' directory to proceed.\n$DIFF" && return 1 - - if [ -z "$BASH_IT_REMOTE" ]; then - BASH_IT_REMOTE="origin" - fi - - git fetch $BASH_IT_REMOTE --tags &> /dev/null - - if [ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]; then - BASH_IT_DEVELOPMENT_BRANCH="master" - fi - # Defaults to stable update - if [ -z "$1" ] || [ "$1" == "stable" ]; then - version="stable" - TARGET=$(git describe --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null) - - if [[ -z "$TARGET" ]]; then - echo "Can not find tags, so can not update to latest stable version..." - popd &> /dev/null - return - fi - else - version="dev" - TARGET=${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH} - fi - - declare revision - revision="HEAD..${TARGET}" - declare status - status="$(git rev-list ${revision} 2> /dev/null)" - declare revert - - if [[ -z "${status}" && ${version} == "stable" ]]; then - revision="${TARGET}..HEAD" - status="$(git rev-list ${revision} 2> /dev/null)" - revert=true - fi - - if [[ -n "${status}" ]]; then - if [[ $revert ]]; then - echo "Your version is a more recent development version ($(git log -1 --format=%h HEAD))" - echo "You can continue in order to revert and update to the latest stable version" - echo "" - log_color="%Cred" - fi - - for i in $(git rev-list --merges --first-parent ${revision}); do - num_of_lines=$(git log -1 --format=%B $i | awk 'NF' | wc -l) - if [ $num_of_lines -eq 1 ]; then - description="%s" - else - description="%b" - fi - git log --format="${log_color}%h: $description (%an)" -1 $i - done - echo "" - - if [[ $silent ]]; then - echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..." - _bash-it_update_migrate_and_restart $TARGET $version - else - read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP - case $RESP in - [yY]|"") - _bash-it_update_migrate_and_restart $TARGET $version - ;; - [nN]) - echo "Not updating…" - ;; - *) - echo -e "\033[91mPlease choose y or n.\033[m" - ;; - esac - fi - else - if [[ ${version} == "stable" ]]; then - echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\"" - else - echo "Bash-it is up to date, nothing to do!" - fi - fi - popd &> /dev/null -} - -_bash-it-migrate() { - _about 'migrates Bash-it configuration from a previous format to the current one' - _group 'lib' - - declare migrated_something - migrated_something=false - - for file_type in "aliases" "plugins" "completion" - do - for f in `sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")` - do - typeset ff="${f##*/}" - - # Get the type of component from the extension - typeset single_type=$(echo $ff | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g') - # Cut off the optional "250---" prefix and the suffix - typeset component_name=$(echo $ff | sed -e 's/[0-9]*[-]*\(.*\)\..*\.bash/\1/g') - - migrated_something=true - - echo "Migrating $single_type $component_name." - - disable_func="_disable-$single_type" - enable_func="_enable-$single_type" - - $disable_func "$component_name" - $enable_func "$component_name" - done - done - - if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then - _bash-it-reload - fi - - if [ "$migrated_something" = "true" ]; then - echo "" - echo "If any migration errors were reported, please try the following: reload && bash-it migrate" - fi -} - -_bash-it-version() { - _about 'shows current Bash-it version' - _group 'lib' - - cd "${BASH_IT}" || return - - if [ -z "${BASH_IT_REMOTE:-}" ]; then - BASH_IT_REMOTE="origin" - fi - - BASH_IT_GIT_REMOTE=$(git remote get-url $BASH_IT_REMOTE) - BASH_IT_GIT_URL=${BASH_IT_GIT_REMOTE%.git} - if [[ "$BASH_IT_GIT_URL" == *"git@"* ]]; then - # Fix URL in case it is ssh based URL - BASH_IT_GIT_URL=${BASH_IT_GIT_URL/://} - BASH_IT_GIT_URL=${BASH_IT_GIT_URL/git@/https://} - fi - - current_tag=$(git describe --exact-match --tags 2> /dev/null) - - if [[ -z $current_tag ]]; then - BASH_IT_GIT_VERSION_INFO="$(git log --pretty=format:'%h on %aI' -n 1)" - TARGET=${BASH_IT_GIT_VERSION_INFO%% *} - echo "Version type: dev" - echo "Current git SHA: $BASH_IT_GIT_VERSION_INFO" - echo "Commit info: $BASH_IT_GIT_URL/commit/$TARGET" - else - TARGET=$current_tag - echo "Version type: stable" - echo "Current tag: $current_tag" - echo "Tag information: $BASH_IT_GIT_URL/releases/tag/$current_tag" - fi - - echo "Compare to latest: $BASH_IT_GIT_URL/compare/$TARGET...master" - - cd - &> /dev/null || return -} - -_bash-it-doctor() { - _about 'reloads a profile file with a BASH_IT_LOG_LEVEL set' - _param '1: BASH_IT_LOG_LEVEL argument: "errors" "warnings" "all"' - _group 'lib' - - BASH_IT_LOG_LEVEL=$1 - _bash-it-reload - unset BASH_IT_LOG_LEVEL -} - -_bash-it-doctor-all() { - _about 'reloads a profile file with error, warning and debug logs' - _group 'lib' - - _bash-it-doctor $BASH_IT_LOG_LEVEL_ALL -} - -_bash-it-doctor-warnings() { - _about 'reloads a profile file with error and warning logs' - _group 'lib' - - _bash-it-doctor $BASH_IT_LOG_LEVEL_WARNING -} - -_bash-it-doctor-errors() { - _about 'reloads a profile file with error logs' - _group 'lib' - - _bash-it-doctor $BASH_IT_LOG_LEVEL_ERROR -} - -_bash-it-doctor-() { - _about 'default bash-it doctor behavior, behaves like bash-it doctor all' - _group 'lib' - - _bash-it-doctor-all -} - -_bash-it-restart() { - _about 'restarts the shell in order to fully reload it' - _group 'lib' - - saved_pwd="${PWD}" - - case $OSTYPE in - darwin*) - init_file=.bash_profile - ;; - *) - init_file=.bashrc - ;; - esac - exec "${0/-/}" --rcfile <(echo "source \"$HOME/$init_file\"; cd \"$saved_pwd\"") -} - -_bash-it-reload() { - _about 'reloads a profile file' - _group 'lib' - - pushd "${BASH_IT}" &> /dev/null || return - - case $OSTYPE in - darwin*) - source ~/.bash_profile - ;; - *) - source ~/.bashrc - ;; - esac - - popd &> /dev/null || return -} - -_bash-it-describe () +_bash-it-update-() { - _about 'summarizes available bash_it components' - _param '1: subdirectory' - _param '2: preposition' - _param '3: file_type' - _param '4: column_header' - _example '$ _bash-it-describe "plugins" "a" "plugin" "Plugin"' + _about 'updates Bash-it' + _param '1: What kind of update to do (stable|dev)' + _group 'lib' - subdirectory="$1" - preposition="$2" - file_type="$3" - column_header="$4" + declare silent + for word in $@; do + if [[ ${word} == "--silent" || ${word} == "-s" ]]; then + silent=true + fi + done - typeset f - typeset enabled - printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description' - for f in "${BASH_IT}/$subdirectory/available/"*.bash - do - # Check for both the old format without the load priority, and the extended format with the priority - declare enabled_files enabled_file + pushd "${BASH_IT}" &> /dev/null || return + + DIFF=$(git diff --name-status) + [ -n "$DIFF" ] && echo -e "Local changes detected in bash-it directory. Clean '$BASH_IT' directory to proceed.\n$DIFF" && return 1 + + if [ -z "$BASH_IT_REMOTE" ]; then + BASH_IT_REMOTE="origin" + fi + + git fetch $BASH_IT_REMOTE --tags &> /dev/null + + if [ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]; then + BASH_IT_DEVELOPMENT_BRANCH="master" + fi + # Defaults to stable update + if [ -z "$1" ] || [ "$1" == "stable" ]; then + version="stable" + TARGET=$(git describe --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null) + + if [[ -z "$TARGET" ]]; then + echo "Can not find tags, so can not update to latest stable version..." + popd &> /dev/null + return + fi + else + version="dev" + TARGET=${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH} + fi + + declare revision + revision="HEAD..${TARGET}" + declare status + status="$(git rev-list ${revision} 2> /dev/null)" + declare revert + + if [[ -z "${status}" && ${version} == "stable" ]]; then + revision="${TARGET}..HEAD" + status="$(git rev-list ${revision} 2> /dev/null)" + revert=true + fi + + if [[ -n "${status}" ]]; then + if [[ $revert ]]; then + echo "Your version is a more recent development version ($(git log -1 --format=%h HEAD))" + echo "You can continue in order to revert and update to the latest stable version" + echo "" + log_color="%Cred" + fi + + for i in $(git rev-list --merges --first-parent ${revision}); do + num_of_lines=$(git log -1 --format=%B $i | awk 'NF' | wc -l) + if [ $num_of_lines -eq 1 ]; then + description="%s" + else + description="%b" + fi + git log --format="${log_color}%h: $description (%an)" -1 $i + done + echo "" + + if [[ $silent ]]; then + echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..." + _bash-it_update_migrate_and_restart $TARGET $version + else + read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP + case $RESP in + [yY] | "") + _bash-it_update_migrate_and_restart $TARGET $version + ;; + [nN]) + echo "Not updating…" + ;; + *) + echo -e "\033[91mPlease choose y or n.\033[m" + ;; + esac + fi + else + if [[ ${version} == "stable" ]]; then + echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\"" + else + echo "Bash-it is up to date, nothing to do!" + fi + fi + popd &> /dev/null +} + +_bash-it-migrate() +{ + _about 'migrates Bash-it configuration from a previous format to the current one' + _group 'lib' + + declare migrated_something + migrated_something=false + + for file_type in "aliases" "plugins" "completion"; do + for f in $(sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")); do + typeset ff="${f##*/}" + + # Get the type of component from the extension + typeset single_type=$(echo $ff | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g') + # Cut off the optional "250---" prefix and the suffix + typeset component_name=$(echo $ff | sed -e 's/[0-9]*[-]*\(.*\)\..*\.bash/\1/g') + + migrated_something=true + + echo "Migrating $single_type $component_name." + + disable_func="_disable-$single_type" + enable_func="_enable-$single_type" + + $disable_func "$component_name" + $enable_func "$component_name" + done + done + + if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then + _bash-it-reload + fi + + if [ "$migrated_something" = "true" ]; then + echo "" + echo "If any migration errors were reported, please try the following: reload && bash-it migrate" + fi +} + +_bash-it-version() +{ + _about 'shows current Bash-it version' + _group 'lib' + + cd "${BASH_IT}" || return + + if [ -z "${BASH_IT_REMOTE:-}" ]; then + BASH_IT_REMOTE="origin" + fi + + BASH_IT_GIT_REMOTE=$(git remote get-url $BASH_IT_REMOTE) + BASH_IT_GIT_URL=${BASH_IT_GIT_REMOTE%.git} + if [[ "$BASH_IT_GIT_URL" == *"git@"* ]]; then + # Fix URL in case it is ssh based URL + BASH_IT_GIT_URL=${BASH_IT_GIT_URL/://} + BASH_IT_GIT_URL=${BASH_IT_GIT_URL/git@/https://} + fi + + current_tag=$(git describe --exact-match --tags 2> /dev/null) + + if [[ -z $current_tag ]]; then + BASH_IT_GIT_VERSION_INFO="$(git log --pretty=format:'%h on %aI' -n 1)" + TARGET=${BASH_IT_GIT_VERSION_INFO%% *} + echo "Version type: dev" + echo "Current git SHA: $BASH_IT_GIT_VERSION_INFO" + echo "Commit info: $BASH_IT_GIT_URL/commit/$TARGET" + else + TARGET=$current_tag + echo "Version type: stable" + echo "Current tag: $current_tag" + echo "Tag information: $BASH_IT_GIT_URL/releases/tag/$current_tag" + fi + + echo "Compare to latest: $BASH_IT_GIT_URL/compare/$TARGET...master" + + cd - &> /dev/null || return +} + +_bash-it-doctor() +{ + _about 'reloads a profile file with a BASH_IT_LOG_LEVEL set' + _param '1: BASH_IT_LOG_LEVEL argument: "errors" "warnings" "all"' + _group 'lib' + + BASH_IT_LOG_LEVEL=$1 + _bash-it-reload + unset BASH_IT_LOG_LEVEL +} + +_bash-it-doctor-all() +{ + _about 'reloads a profile file with error, warning and debug logs' + _group 'lib' + + _bash-it-doctor $BASH_IT_LOG_LEVEL_ALL +} + +_bash-it-doctor-warnings() +{ + _about 'reloads a profile file with error and warning logs' + _group 'lib' + + _bash-it-doctor $BASH_IT_LOG_LEVEL_WARNING +} + +_bash-it-doctor-errors() +{ + _about 'reloads a profile file with error logs' + _group 'lib' + + _bash-it-doctor $BASH_IT_LOG_LEVEL_ERROR +} + +_bash-it-doctor-() +{ + _about 'default bash-it doctor behavior, behaves like bash-it doctor all' + _group 'lib' + + _bash-it-doctor-all +} + +_bash-it-restart() +{ + _about 'restarts the shell in order to fully reload it' + _group 'lib' + + saved_pwd="${PWD}" + + case $OSTYPE in + darwin*) + init_file=.bash_profile + ;; + *) + init_file=.bashrc + ;; + esac + exec "${0/-/}" --rcfile <(echo "source \"$HOME/$init_file\"; cd \"$saved_pwd\"") +} + +_bash-it-reload() +{ + _about 'reloads a profile file' + _group 'lib' + + pushd "${BASH_IT}" &> /dev/null || return + + case $OSTYPE in + darwin*) + source ~/.bash_profile + ;; + *) + source ~/.bashrc + ;; + esac + + popd &> /dev/null || return +} + +_bash-it-describe() +{ + _about 'summarizes available bash_it components' + _param '1: subdirectory' + _param '2: preposition' + _param '3: file_type' + _param '4: column_header' + _example '$ _bash-it-describe "plugins" "a" "plugin" "Plugin"' + + subdirectory="$1" + preposition="$2" + file_type="$3" + column_header="$4" + + typeset f + typeset enabled + printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description' + for f in "${BASH_IT}/$subdirectory/available/"*.bash; do + # Check for both the old format without the load priority, and the extended format with the priority + declare enabled_files enabled_file enabled_file="${f##*/}" - enabled_files=$(sort <(compgen -G "${BASH_IT}/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") | wc -l) + enabled_files=$(sort <(compgen -G "${BASH_IT}/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") | wc -l) - if [ $enabled_files -gt 0 ]; then - enabled='x' - else - enabled=' ' - fi - printf "%-20s%-10s%s\n" "$(basename $f | sed -e 's/\(.*\)\..*\.bash/\1/g')" " [$enabled]" "$(cat $f | metafor about-$file_type)" - done - printf '\n%s\n' "to enable $preposition $file_type, do:" - printf '%s\n' "$ bash-it enable $file_type <$file_type name> [$file_type name]... -or- $ bash-it enable $file_type all" - printf '\n%s\n' "to disable $preposition $file_type, do:" - printf '%s\n' "$ bash-it disable $file_type <$file_type name> [$file_type name]... -or- $ bash-it disable $file_type all" + if [ $enabled_files -gt 0 ]; then + enabled='x' + else + enabled=' ' + fi + printf "%-20s%-10s%s\n" "$(basename $f | sed -e 's/\(.*\)\..*\.bash/\1/g')" " [$enabled]" "$(cat $f | metafor about-$file_type)" + done + printf '\n%s\n' "to enable $preposition $file_type, do:" + printf '%s\n' "$ bash-it enable $file_type <$file_type name> [$file_type name]... -or- $ bash-it enable $file_type all" + printf '\n%s\n' "to disable $preposition $file_type, do:" + printf '%s\n' "$ bash-it disable $file_type <$file_type name> [$file_type name]... -or- $ bash-it disable $file_type all" } _on-disable-callback() { - _about 'Calls the disabled plugin destructor, if present' - _param '1: plugin name' - _example '$ _on-disable-callback gitstatus' - _group 'lib' + _about 'Calls the disabled plugin destructor, if present' + _param '1: plugin name' + _example '$ _on-disable-callback gitstatus' + _group 'lib' - callback=$1_on_disable - _command_exists $callback && $callback + callback=$1_on_disable + _command_exists $callback && $callback } -_disable-plugin () +_disable-plugin() { - _about 'disables bash_it plugin' - _param '1: plugin name' - _example '$ disable-plugin rvm' - _group 'lib' + _about 'disables bash_it plugin' + _param '1: plugin name' + _example '$ disable-plugin rvm' + _group 'lib' - _disable-thing "plugins" "plugin" $1 - _on-disable-callback $1 + _disable-thing "plugins" "plugin" $1 + _on-disable-callback $1 } -_disable-alias () +_disable-alias() { - _about 'disables bash_it alias' - _param '1: alias name' - _example '$ disable-alias git' - _group 'lib' + _about 'disables bash_it alias' + _param '1: alias name' + _example '$ disable-alias git' + _group 'lib' - _disable-thing "aliases" "alias" $1 + _disable-thing "aliases" "alias" $1 } -_disable-completion () +_disable-completion() { - _about 'disables bash_it completion' - _param '1: completion name' - _example '$ disable-completion git' - _group 'lib' + _about 'disables bash_it completion' + _param '1: completion name' + _example '$ disable-completion git' + _group 'lib' - _disable-thing "completion" "completion" $1 + _disable-thing "completion" "completion" $1 } -_disable-thing () +_disable-thing() { - _about 'disables a bash_it component' - _param '1: subdirectory' - _param '2: file_type' - _param '3: file_entity' - _example '$ _disable-thing "plugins" "plugin" "ssh"' + _about 'disables a bash_it component' + _param '1: subdirectory' + _param '2: file_type' + _param '3: file_entity' + _example '$ _disable-thing "plugins" "plugin" "ssh"' - subdirectory="$1" - file_type="$2" - file_entity="$3" + subdirectory="$1" + file_type="$2" + file_entity="$3" - if [ -z "$file_entity" ]; then - reference "disable-$file_type" - return - fi + if [ -z "$file_entity" ]; then + reference "disable-$file_type" + return + fi - typeset f suffix - suffix=$(echo "$subdirectory" | sed -e 's/plugins/plugin/g') + typeset f suffix + suffix=$(echo "$subdirectory" | sed -e 's/plugins/plugin/g') - if [ "$file_entity" = "all" ]; then - # Disable everything that's using the old structure - for f in `compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash"` - do - rm "$f" - done + if [ "$file_entity" = "all" ]; then + # Disable everything that's using the old structure + for f in $(compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash"); do + rm "$f" + done - # Disable everything in the global "enabled" directory - for f in `compgen -G "${BASH_IT}/enabled/*.${suffix}.bash"` - do - rm "$f" - done - else - typeset plugin_global=$(command ls $ "${BASH_IT}/enabled/"[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash 2>/dev/null | head -1) - if [ -z "$plugin_global" ]; then - # Use a glob to search for both possible patterns - # 250---node.plugin.bash - # node.plugin.bash - # Either one will be matched by this glob - typeset plugin=$(command ls $ "${BASH_IT}/$subdirectory/enabled/"{[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash,$file_entity.$suffix.bash} 2>/dev/null | head -1) - if [ -z "$plugin" ]; then - printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type." - return - fi - rm "${BASH_IT}/$subdirectory/enabled/${plugin##*/}" - else - rm "${BASH_IT}/enabled/${plugin_global##*/}" - fi - fi + # Disable everything in the global "enabled" directory + for f in $(compgen -G "${BASH_IT}/enabled/*.${suffix}.bash"); do + rm "$f" + done + else + typeset plugin_global=$(command ls $ "${BASH_IT}/enabled/"[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash 2> /dev/null | head -1) + if [ -z "$plugin_global" ]; then + # Use a glob to search for both possible patterns + # 250---node.plugin.bash + # node.plugin.bash + # Either one will be matched by this glob + typeset plugin=$(command ls $ "${BASH_IT}/$subdirectory/enabled/"{[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash,$file_entity.$suffix.bash} 2> /dev/null | head -1) + if [ -z "$plugin" ]; then + printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type." + return + fi + rm "${BASH_IT}/$subdirectory/enabled/${plugin##*/}" + else + rm "${BASH_IT}/enabled/${plugin_global##*/}" + fi + fi - _bash-it-clean-component-cache "${file_type}" + _bash-it-clean-component-cache "${file_type}" - printf '%s\n' "$file_entity disabled." + printf '%s\n' "$file_entity disabled." } -_enable-plugin () +_enable-plugin() { - _about 'enables bash_it plugin' - _param '1: plugin name' - _example '$ enable-plugin rvm' - _group 'lib' + _about 'enables bash_it plugin' + _param '1: plugin name' + _example '$ enable-plugin rvm' + _group 'lib' - _enable-thing "plugins" "plugin" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN + _enable-thing "plugins" "plugin" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN } -_enable-alias () +_enable-alias() { - _about 'enables bash_it alias' - _param '1: alias name' - _example '$ enable-alias git' - _group 'lib' + _about 'enables bash_it alias' + _param '1: alias name' + _example '$ enable-alias git' + _group 'lib' - _enable-thing "aliases" "alias" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS + _enable-thing "aliases" "alias" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS } -_enable-completion () +_enable-completion() { - _about 'enables bash_it completion' - _param '1: completion name' - _example '$ enable-completion git' - _group 'lib' + _about 'enables bash_it completion' + _param '1: completion name' + _example '$ enable-completion git' + _group 'lib' - _enable-thing "completion" "completion" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION + _enable-thing "completion" "completion" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION } -_enable-thing () +_enable-thing() { - cite _about _param _example - _about 'enables a bash_it component' - _param '1: subdirectory' - _param '2: file_type' - _param '3: file_entity' - _param '4: load priority' - _example '$ _enable-thing "plugins" "plugin" "ssh" "150"' + cite _about _param _example + _about 'enables a bash_it component' + _param '1: subdirectory' + _param '2: file_type' + _param '3: file_entity' + _param '4: load priority' + _example '$ _enable-thing "plugins" "plugin" "ssh" "150"' - subdirectory="$1" - file_type="$2" - file_entity="$3" - load_priority="$4" + subdirectory="$1" + file_type="$2" + file_entity="$3" + load_priority="$4" - if [ -z "$file_entity" ]; then - reference "enable-$file_type" - return - fi + if [ -z "$file_entity" ]; then + reference "enable-$file_type" + return + fi - if [ "$file_entity" = "all" ]; then - typeset f $file_type - for f in "${BASH_IT}/$subdirectory/available/"*.bash - do - to_enable=$(basename $f .$file_type.bash) - if [ "$file_type" = "alias" ]; then - to_enable=$(basename $f ".aliases.bash") - fi - _enable-thing $subdirectory $file_type $to_enable $load_priority - done - else - typeset to_enable=$(command ls "${BASH_IT}/$subdirectory/available/"$file_entity.*bash 2>/dev/null | head -1) - if [ -z "$to_enable" ]; then - printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type." - return - fi + if [ "$file_entity" = "all" ]; then + typeset f $file_type + for f in "${BASH_IT}/$subdirectory/available/"*.bash; do + to_enable=$(basename $f .$file_type.bash) + if [ "$file_type" = "alias" ]; then + to_enable=$(basename $f ".aliases.bash") + fi + _enable-thing $subdirectory $file_type $to_enable $load_priority + done + else + typeset to_enable=$(command ls "${BASH_IT}/$subdirectory/available/"$file_entity.*bash 2> /dev/null | head -1) + if [ -z "$to_enable" ]; then + printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type." + return + fi to_enable="${to_enable##*/}" - # Check for existence of the file using a wildcard, since we don't know which priority might have been used when enabling it. - typeset enabled_plugin=$(command ls "${BASH_IT}/$subdirectory/enabled/"{[0-9][0-9][0-9]$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable,$to_enable} 2>/dev/null | head -1) - if [ ! -z "$enabled_plugin" ] ; then - printf '%s\n' "$file_entity is already enabled." - return - fi + # Check for existence of the file using a wildcard, since we don't know which priority might have been used when enabling it. + typeset enabled_plugin=$(command ls "${BASH_IT}/$subdirectory/enabled/"{[0-9][0-9][0-9]$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable,$to_enable} 2> /dev/null | head -1) + if [ ! -z "$enabled_plugin" ]; then + printf '%s\n' "$file_entity is already enabled." + return + fi - typeset enabled_plugin_global=$(command compgen -G "${BASH_IT}/enabled/[0-9][0-9][0-9]$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable" 2>/dev/null | head -1) - if [ ! -z "$enabled_plugin_global" ] ; then - printf '%s\n' "$file_entity is already enabled." - return - fi + typeset enabled_plugin_global=$(command compgen -G "${BASH_IT}/enabled/[0-9][0-9][0-9]$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable" 2> /dev/null | head -1) + if [ ! -z "$enabled_plugin_global" ]; then + printf '%s\n' "$file_entity is already enabled." + return + fi - mkdir -p "${BASH_IT}/enabled" + mkdir -p "${BASH_IT}/enabled" - # Load the priority from the file if it present there - declare local_file_priority use_load_priority - local_file_priority=$(grep -E "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }') - use_load_priority=${local_file_priority:-$load_priority} + # Load the priority from the file if it present there + declare local_file_priority use_load_priority + local_file_priority=$(grep -E "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }') + use_load_priority=${local_file_priority:-$load_priority} - ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" - fi + ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" + fi - _bash-it-clean-component-cache "${file_type}" + _bash-it-clean-component-cache "${file_type}" - printf '%s\n' "$file_entity enabled with priority $use_load_priority." + printf '%s\n' "$file_entity enabled with priority $use_load_priority." } _help-completions() { - _about 'summarize all completions available in bash-it' - _group 'lib' + _about 'summarize all completions available in bash-it' + _group 'lib' - _bash-it-completions + _bash-it-completions } _help-aliases() { - _about 'shows help for all aliases, or a specific alias group' - _param '1: optional alias group' - _example '$ alias-help' - _example '$ alias-help git' + _about 'shows help for all aliases, or a specific alias group' + _param '1: optional alias group' + _example '$ alias-help' + _example '$ alias-help git' - if [ -n "$1" ]; then - case $1 in - custom) - alias_path='custom.aliases.bash' - ;; - *) - alias_path="available/$1.aliases.bash" - ;; - esac - cat "${BASH_IT}/aliases/$alias_path" | metafor alias | sed "s/$/'/" - else - typeset f + if [ -n "$1" ]; then + case $1 in + custom) + alias_path='custom.aliases.bash' + ;; + *) + alias_path="available/$1.aliases.bash" + ;; + esac + cat "${BASH_IT}/aliases/$alias_path" | metafor alias | sed "s/$/'/" + else + typeset f - for f in `sort <(compgen -G "${BASH_IT}/aliases/enabled/*") <(compgen -G "${BASH_IT}/enabled/*.aliases.bash")` - do - _help-list-aliases $f - done + for f in $(sort <(compgen -G "${BASH_IT}/aliases/enabled/*") <(compgen -G "${BASH_IT}/enabled/*.aliases.bash")); do + _help-list-aliases $f + done - if [ -e "${BASH_IT}/aliases/custom.aliases.bash" ]; then - _help-list-aliases "${BASH_IT}/aliases/custom.aliases.bash" - fi - fi + if [ -e "${BASH_IT}/aliases/custom.aliases.bash" ]; then + _help-list-aliases "${BASH_IT}/aliases/custom.aliases.bash" + fi + fi } -_help-list-aliases () +_help-list-aliases() { - typeset file=$(basename $1 | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g') - printf '\n\n%s:\n' "${file}" - # metafor() strips trailing quotes, restore them with sed.. - cat $1 | metafor alias | sed "s/$/'/" + typeset file=$(basename $1 | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g') + printf '\n\n%s:\n' "${file}" + # metafor() strips trailing quotes, restore them with sed.. + cat $1 | metafor alias | sed "s/$/'/" } _help-plugins() { - _about 'summarize all functions defined by enabled bash-it plugins' - _group 'lib' + _about 'summarize all functions defined by enabled bash-it plugins' + _group 'lib' - # display a brief progress message... - printf '%s' 'please wait, building help...' - typeset grouplist=$(mktemp -t grouplist.XXXXXX) - typeset func - for func in $(_typeset_functions) - do - typeset group="$(typeset -f $func | metafor group)" - if [ -z "$group" ]; then - group='misc' - fi - typeset about="$(typeset -f $func | metafor about)" - _letterpress "$about" $func >> $grouplist.$group - echo $grouplist.$group >> $grouplist - done - # clear progress message - printf '\r%s\n' ' ' - typeset group - typeset gfile - for gfile in $(cat $grouplist | sort | uniq) - do - printf '%s\n' "${gfile##*.}:" - cat $gfile - printf '\n' - rm $gfile 2> /dev/null - done | less - rm $grouplist 2> /dev/null + # display a brief progress message... + printf '%s' 'please wait, building help...' + typeset grouplist=$(mktemp -t grouplist.XXXXXX) + typeset func + for func in $(_typeset_functions); do + typeset group="$(typeset -f $func | metafor group)" + if [ -z "$group" ]; then + group='misc' + fi + typeset about="$(typeset -f $func | metafor about)" + _letterpress "$about" $func >> $grouplist.$group + echo $grouplist.$group >> $grouplist + done + # clear progress message + printf '\r%s\n' ' ' + typeset group + typeset gfile + for gfile in $(cat $grouplist | sort | uniq); do + printf '%s\n' "${gfile##*.}:" + cat $gfile + printf '\n' + rm $gfile 2> /dev/null + done | less + rm $grouplist 2> /dev/null } -_help-update () { - _about 'help message for update command' - _group 'lib' - - echo "Check for a new version of Bash-it and update it." -} - -_help-migrate () { - _about 'help message for migrate command' - _group 'lib' - - echo "Migrates internal Bash-it structure to the latest version in case of changes." - echo "The 'migrate' command is run automatically when calling 'update', 'enable' or 'disable'." -} - -all_groups () +_help-update() { - about 'displays all unique metadata groups' - group 'lib' + _about 'help message for update command' + _group 'lib' - typeset func - typeset file=$(mktemp -t composure.XXXX) - for func in $(_typeset_functions) - do - typeset -f $func | metafor group >> $file - done - cat $file | sort | uniq - rm $file + echo "Check for a new version of Bash-it and update it." } -if ! type pathmunge > /dev/null 2>&1 -then - function pathmunge () { - about 'prevent duplicate directories in you PATH variable' - group 'helpers' - example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH' - example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir' +_help-migrate() +{ + _about 'help message for migrate command' + _group 'lib' - if ! [[ $PATH =~ (^|:)$1($|:) ]] ; then - if [ "$2" = "after" ] ; then - export PATH=$PATH:$1 - else - export PATH=$1:$PATH - fi - fi - } + echo "Migrates internal Bash-it structure to the latest version in case of changes." + echo "The 'migrate' command is run automatically when calling 'update', 'enable' or 'disable'." +} + +all_groups() +{ + about 'displays all unique metadata groups' + group 'lib' + + typeset func + typeset file=$(mktemp -t composure.XXXX) + for func in $(_typeset_functions); do + typeset -f $func | metafor group >> $file + done + cat $file | sort | uniq + rm $file +} + +if ! type pathmunge > /dev/null 2>&1; then + function pathmunge() + { + about 'prevent duplicate directories in you PATH variable' + group 'helpers' + example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH' + example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir' + + if ! [[ $PATH =~ (^|:)$1($|:) ]]; then + if [ "$2" = "after" ]; then + export PATH=$PATH:$1 + else + export PATH=$1:$PATH + fi + fi + } fi # `_bash-it-find-in-ancestor` uses the shell's ability to run a function in # a subshell to simplify our search to a simple `cd ..` and `[[ -r $1 ]]` # without any external dependencies. Let the shell do what it's good at. -function _bash-it-find-in-ancestor() ( +function _bash-it-find-in-ancestor() +( about 'searches parents of the current directory for any of the specified file names' group 'helpers' param '*: names of files or folders to search for' diff --git a/lib/log.bash b/lib/log.bash index 6bc53a12..705c93e2 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -6,55 +6,55 @@ export BASH_IT_LOG_LEVEL_ALL=3 function _has_colors() { - # Check that stdout is a terminal - test -t 1 || return 1 + # Check that stdout is a terminal + test -t 1 || return 1 - ncolors=$(tput colors) - test -n "$ncolors" && test "$ncolors" -ge 8 || return 1 - return 0 + ncolors=$(tput colors) + test -n "$ncolors" && test "$ncolors" -ge 8 || return 1 + return 0 } function _log_general() { - about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' - param '1: color of the message' - param '2: log level to print before the prefix' - param '3: message to log' - group 'log' + about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' + param '1: color of the message' + param '2: log level to print before the prefix' + param '3: message to log' + group 'log' - message=$2${BASH_IT_LOG_PREFIX:-default: }$3 - _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" + message=$2${BASH_IT_LOG_PREFIX:-default: }$3 + _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" } function _log_debug() { - about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL' - param '1: message to log' - example '$ _log_debug "Loading plugin git..."' - group 'log' + about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL' + param '1: message to log' + example '$ _log_debug "Loading plugin git..."' + group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 - _log_general "${echo_green:-}" "DEBUG: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 + _log_general "${echo_green:-}" "DEBUG: " "$1" } function _log_warning() { - about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING' - param '1: message to log' - example '$ _log_warning "git binary not found, disabling git plugin..."' - group 'log' + about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING' + param '1: message to log' + example '$ _log_warning "git binary not found, disabling git plugin..."' + group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 - _log_general "${echo_yellow:-}" " WARN: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 + _log_general "${echo_yellow:-}" " WARN: " "$1" } function _log_error() { - about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR' - param '1: message to log' - example '$ _log_error "Failed to load git plugin..."' - group 'log' + about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR' + param '1: message to log' + example '$ _log_error "Failed to load git plugin..."' + group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 - _log_general "${echo_red:-}" "ERROR: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 + _log_general "${echo_red:-}" "ERROR: " "$1" } diff --git a/lib/preview.bash b/lib/preview.bash index 418839cd..9ed98190 100644 --- a/lib/preview.bash +++ b/lib/preview.bash @@ -1,19 +1,17 @@ -if [[ "${BASH_PREVIEW:-}" ]]; -then - unset BASH_PREVIEW #Prevent infinite looping - echo " +if [[ "${BASH_PREVIEW:-}" ]]; then + unset BASH_PREVIEW #Prevent infinite looping + echo " Previewing Bash-it Themes " - THEMES="$BASH_IT/themes/*/*.theme.bash" - for theme in $THEMES - do - BASH_IT_THEME=${theme%.theme.bash} - BASH_IT_THEME=${BASH_IT_THEME##*/} - echo " + THEMES="$BASH_IT/themes/*/*.theme.bash" + for theme in $THEMES; do + BASH_IT_THEME=${theme%.theme.bash} + BASH_IT_THEME=${BASH_IT_THEME##*/} + echo " $BASH_IT_THEME" - echo "" | bash --init-file "${BASH_IT}/bash_it.sh" -i - done + echo "" | bash --init-file "${BASH_IT}/bash_it.sh" -i + done fi diff --git a/lib/search.bash b/lib/search.bash index 58f98904..11794e8b 100644 --- a/lib/search.bash +++ b/lib/search.bash @@ -47,49 +47,51 @@ # completions: git # -_bash-it-search() { - _about 'searches for given terms amongst bash-it plugins, aliases and completions' - _param '1: term1' - _param '2: [ term2 ]...' - _example '$ _bash-it-search @git ruby -rvm rake bundler' +_bash-it-search() +{ + _about 'searches for given terms amongst bash-it plugins, aliases and completions' + _param '1: term1' + _param '2: [ term2 ]...' + _example '$ _bash-it-search @git ruby -rvm rake bundler' - [[ -z "$(type _bash-it-array-contains-element 2>/dev/null)" ]] && source "${BASH_IT}/lib/utilities.bash" + [[ -z "$(type _bash-it-array-contains-element 2> /dev/null)" ]] && source "${BASH_IT}/lib/utilities.bash" - local component - export BASH_IT_SEARCH_USE_COLOR=true - export BASH_IT_GREP=${BASH_IT_GREP:-$(which egrep)} - declare -a BASH_IT_COMPONENTS=(aliases plugins completions) + local component + export BASH_IT_SEARCH_USE_COLOR=true + export BASH_IT_GREP=${BASH_IT_GREP:-$(which egrep)} + declare -a BASH_IT_COMPONENTS=(aliases plugins completions) - if [[ -z "$*" ]] ; then - _bash-it-search-help - return 0 - fi + if [[ -z "$*" ]]; then + _bash-it-search-help + return 0 + fi - local -a args=() - for word in $@; do - if [[ ${word} == "--help" || ${word} == "-h" ]]; then - _bash-it-search-help - return 0 - elif [[ ${word} == "--refresh" || ${word} == "-r" ]]; then - _bash-it-clean-component-cache - elif [[ ${word} == "--no-color" || ${word} == '-c' ]]; then - export BASH_IT_SEARCH_USE_COLOR=false - else - args=(${args[@]} ${word}) - fi - done + local -a args=() + for word in $@; do + if [[ ${word} == "--help" || ${word} == "-h" ]]; then + _bash-it-search-help + return 0 + elif [[ ${word} == "--refresh" || ${word} == "-r" ]]; then + _bash-it-clean-component-cache + elif [[ ${word} == "--no-color" || ${word} == '-c' ]]; then + export BASH_IT_SEARCH_USE_COLOR=false + else + args=(${args[@]} ${word}) + fi + done - if [[ ${#args} -gt 0 ]]; then - for component in "${BASH_IT_COMPONENTS[@]}" ; do - _bash-it-search-component "${component}" "${args[@]}" - done - fi + if [[ ${#args} -gt 0 ]]; then + for component in "${BASH_IT_COMPONENTS[@]}"; do + _bash-it-search-component "${component}" "${args[@]}" + done + fi - return 0 + return 0 } -_bash-it-search-help() { - printf "${echo_normal} +_bash-it-search-help() +{ + printf "${echo_normal} ${echo_underline_yellow}USAGE${echo_normal} bash-it search [-|@]term1 [-|@]term2 ... \\ @@ -165,197 +167,208 @@ ${echo_underline_yellow}SUMMARY${echo_normal} " } -_bash-it-is-partial-match() { - local component="$1" - local term="$2" - _bash-it-component-help "${component}" | $(_bash-it-grep) -E -i -q -- "${term}" +_bash-it-is-partial-match() +{ + local component="$1" + local term="$2" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E -i -q -- "${term}" } -_bash-it-component-term-matches-negation() { - local match="$1"; shift - local negative - for negative in "$@"; do - [[ "${match}" =~ "${negative}" ]] && return 0 - done +_bash-it-component-term-matches-negation() +{ + local match="$1" + shift + local negative + for negative in "$@"; do + [[ "${match}" =~ "${negative}" ]] && return 0 + done - return 1 + return 1 } -_bash-it-search-component() { - local component="$1" - shift +_bash-it-search-component() +{ + local component="$1" + shift - _about 'searches for given terms amongst a given component' - _param '1: component type, one of: [ aliases | plugins | completions ]' - _param '2: term1 term2 @term3' - _param '3: [-]term4 [-]term5 ...' - _example '$ _bash-it-search-component aliases @git rake bundler -chruby' + _about 'searches for given terms amongst a given component' + _param '1: component type, one of: [ aliases | plugins | completions ]' + _param '2: term1 term2 @term3' + _param '3: [-]term4 [-]term5 ...' + _example '$ _bash-it-search-component aliases @git rake bundler -chruby' - # if one of the search terms is --enable or --disable, we will apply - # this action to the matches further ` down. - local component_singular action action_func - local -a search_commands=(enable disable) - for search_command in "${search_commands[@]}"; do - if $(_bash-it-array-contains-element "--${search_command}" "$@"); then - component_singular=${component} - component_singular=${component_singular/es/} # aliases -> alias - component_singular=${component_singular/ns/n} # plugins -> plugin + # if one of the search terms is --enable or --disable, we will apply + # this action to the matches further ` down. + local component_singular action action_func + local -a search_commands=(enable disable) + for search_command in "${search_commands[@]}"; do + if $(_bash-it-array-contains-element "--${search_command}" "$@"); then + component_singular=${component} + component_singular=${component_singular/es/} # aliases -> alias + component_singular=${component_singular/ns/n} # plugins -> plugin - action="${search_command}" - action_func="_${action}-${component_singular}" - break - fi - done + action="${search_command}" + action_func="_${action}-${component_singular}" + break + fi + done - local -a terms=($@) # passed on the command line + local -a terms=($@) # passed on the command line - unset exact_terms - unset partial_terms - unset negative_terms + unset exact_terms + unset partial_terms + unset negative_terms - local -a exact_terms=() # terms that should be included only if they match exactly - local -a partial_terms=() # terms that should be included if they match partially - local -a negative_terms=() # negated partial terms that should be excluded + local -a exact_terms=() # terms that should be included only if they match exactly + local -a partial_terms=() # terms that should be included if they match partially + local -a negative_terms=() # negated partial terms that should be excluded - unset component_list - local -a component_list=( $(_bash-it-component-list "${component}") ) - local term + unset component_list + local -a component_list=($(_bash-it-component-list "${component}")) + local term - for term in "${terms[@]}"; do - local search_term="${term:1}" - if [[ "${term:0:2}" == "--" ]] ; then - continue - elif [[ "${term:0:1}" == "-" ]] ; then - negative_terms=(${negative_terms[@]} "${search_term}") - elif [[ "${term:0:1}" == "@" ]] ; then - if $(_bash-it-array-contains-element "${search_term}" "${component_list[@]}"); then - exact_terms=(${exact_terms[@]} "${search_term}") - fi - else - partial_terms=(${partial_terms[@]} $(_bash-it-component-list-matching "${component}" "${term}") ) - fi - done + for term in "${terms[@]}"; do + local search_term="${term:1}" + if [[ "${term:0:2}" == "--" ]]; then + continue + elif [[ "${term:0:1}" == "-" ]]; then + negative_terms=(${negative_terms[@]} "${search_term}") + elif [[ "${term:0:1}" == "@" ]]; then + if $(_bash-it-array-contains-element "${search_term}" "${component_list[@]}"); then + exact_terms=(${exact_terms[@]} "${search_term}") + fi + else + partial_terms=(${partial_terms[@]} $(_bash-it-component-list-matching "${component}" "${term}")) + fi + done - local -a total_matches=( $(_bash-it-array-dedup ${exact_terms[@]} ${partial_terms[@]}) ) + local -a total_matches=($(_bash-it-array-dedup ${exact_terms[@]} ${partial_terms[@]})) - unset matches - declare -a matches=() - for match in ${total_matches[@]}; do - local include_match=true - if [[ ${#negative_terms[@]} -gt 0 ]]; then - ( _bash-it-component-term-matches-negation "${match}" "${negative_terms[@]}" ) && include_match=false - fi - ( ${include_match} ) && matches=(${matches[@]} "${match}") - done - _bash-it-search-result "${component}" "${action}" "${action_func}" "${matches[@]}" - unset matches final_matches terms + unset matches + declare -a matches=() + for match in ${total_matches[@]}; do + local include_match=true + if [[ ${#negative_terms[@]} -gt 0 ]]; then + (_bash-it-component-term-matches-negation "${match}" "${negative_terms[@]}") && include_match=false + fi + (${include_match}) && matches=(${matches[@]} "${match}") + done + _bash-it-search-result "${component}" "${action}" "${action_func}" "${matches[@]}" + unset matches final_matches terms } -_bash-it-search-result() { - local component="$1"; shift - local action="$1"; shift - local action_func="$1"; shift - local -a matches=($@) +_bash-it-search-result() +{ + local component="$1" + shift + local action="$1" + shift + local action_func="$1" + shift + local -a matches=($@) - local color_component color_enable color_disable color_off + local color_component color_enable color_disable color_off - color_sep=':' + color_sep=':' - ( ${BASH_IT_SEARCH_USE_COLOR} ) && { - color_component='\e[1;34m' - color_enable='\e[1;32m' - suffix_enable='' - suffix_disable='' - color_disable='\e[0;0m' - color_off='\e[0;0m' - } + (${BASH_IT_SEARCH_USE_COLOR}) && { + color_component='\e[1;34m' + color_enable='\e[1;32m' + suffix_enable='' + suffix_disable='' + color_disable='\e[0;0m' + color_off='\e[0;0m' + } - ( ${BASH_IT_SEARCH_USE_COLOR} ) || { - color_component='' - suffix_enable=' ✓ ︎' - suffix_disable=' ' - color_enable='' - color_disable='' - color_off='' - } + (${BASH_IT_SEARCH_USE_COLOR}) || { + color_component='' + suffix_enable=' ✓ ︎' + suffix_disable=' ' + color_enable='' + color_disable='' + color_off='' + } - local match - local modified=0 + local match + local modified=0 - if [[ "${#matches[@]}" -gt 0 ]] ; then - printf "${color_component}%13s${color_sep} ${color_off}" "${component}" + if [[ "${#matches[@]}" -gt 0 ]]; then + printf "${color_component}%13s${color_sep} ${color_off}" "${component}" - for match in "${matches[@]}"; do - local enabled=0 - ( _bash-it-component-item-is-enabled "${component}" "${match}" ) && enabled=1 + for match in "${matches[@]}"; do + local enabled=0 + (_bash-it-component-item-is-enabled "${component}" "${match}") && enabled=1 - local match_color compatible_action suffix opposite_suffix + local match_color compatible_action suffix opposite_suffix - (( ${enabled} )) && { - match_color=${color_enable} - suffix=${suffix_enable} - opposite_suffix=${suffix_disable} - compatible_action="disable" - } + ((${enabled})) && { + match_color=${color_enable} + suffix=${suffix_enable} + opposite_suffix=${suffix_disable} + compatible_action="disable" + } - (( ${enabled} )) || { - match_color=${color_disable} - suffix=${suffix_disable} - opposite_suffix=${suffix_enable} - compatible_action="enable" - } + ((${enabled})) || { + match_color=${color_disable} + suffix=${suffix_disable} + opposite_suffix=${suffix_enable} + compatible_action="enable" + } - local m="${match}${suffix}" - local len - len=${#m} + local m="${match}${suffix}" + local len + len=${#m} - printf " ${match_color}${match}${suffix}" # print current state - if [[ "${action}" == "${compatible_action}" ]]; then - if [[ ${action} == "enable" && ${BASH_IT_SEARCH_USE_COLOR} == false ]]; then - _bash-it-flash-term ${len} "${match}${suffix}" - else - _bash-it-erase-term ${len} - fi - modified=1 - result=$(${action_func} ${match}) - local temp="color_${compatible_action}" - match_color=${!temp} - _bash-it-rewind ${len} - printf "${match_color}${match}${opposite_suffix}" - fi + printf " ${match_color}${match}${suffix}" # print current state + if [[ "${action}" == "${compatible_action}" ]]; then + if [[ ${action} == "enable" && ${BASH_IT_SEARCH_USE_COLOR} == false ]]; then + _bash-it-flash-term ${len} "${match}${suffix}" + else + _bash-it-erase-term ${len} + fi + modified=1 + result=$(${action_func} ${match}) + local temp="color_${compatible_action}" + match_color=${!temp} + _bash-it-rewind ${len} + printf "${match_color}${match}${opposite_suffix}" + fi - printf "${color_off}" - done + printf "${color_off}" + done - [[ ${modified} -gt 0 ]] && _bash-it-clean-component-cache ${component} - printf "\n" - fi + [[ ${modified} -gt 0 ]] && _bash-it-clean-component-cache ${component} + printf "\n" + fi } -_bash-it-rewind() { - local len="$1" - printf "\033[${len}D" +_bash-it-rewind() +{ + local len="$1" + printf "\033[${len}D" } -_bash-it-flash-term() { - local len="$1" - local match="$2" - local delay=0.1 - local color +_bash-it-flash-term() +{ + local len="$1" + local match="$2" + local delay=0.1 + local color - for color in ${text_black} ${echo_bold_blue} ${bold_yellow} ${bold_red} ${echo_bold_green} ; do - sleep ${delay} - _bash-it-rewind "${len}" - printf "${color}${match}" - done + for color in ${text_black} ${echo_bold_blue} ${bold_yellow} ${bold_red} ${echo_bold_green}; do + sleep ${delay} + _bash-it-rewind "${len}" + printf "${color}${match}" + done } -_bash-it-erase-term() { - local len="$1" - _bash-it-rewind ${len} - for a in {0..30}; do - [[ ${a} -gt ${len} ]] && break - printf "%.*s" $a " " - sleep 0.05 - done +_bash-it-erase-term() +{ + local len="$1" + _bash-it-rewind ${len} + for a in {0..30}; do + [[ ${a} -gt ${len} ]] && break + printf "%.*s" $a " " + sleep 0.05 + done } diff --git a/lib/utilities.bash b/lib/utilities.bash index 1f249b98..ab7d1caa 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -6,7 +6,8 @@ # Generic utilies ########################################################################### -function _bash-it-get-component-name-from-path() { +function _bash-it-get-component-name-from-path() +{ local filename # filename without path filename="${1##*/}" @@ -16,7 +17,8 @@ function _bash-it-get-component-name-from-path() { echo "${filename%.*.bash}" } -function _bash-it-get-component-type-from-path() { +function _bash-it-get-component-type-from-path() +{ local filename # filename without path filename="${1##*/}" @@ -46,7 +48,8 @@ function _bash-it-get-component-type-from-path() { # contains pear! # # -function _bash-it-array-contains-element() { +function _bash-it-array-contains-element() +{ local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0 @@ -55,18 +58,21 @@ function _bash-it-array-contains-element() { } # Dedupe an array (without embedded newlines). -function _bash-it-array-dedup() { +function _bash-it-array-dedup() +{ printf '%s\n' "$@" | sort -u } # Outputs a full path of the grep found on the filesystem -function _bash-it-grep() { +function _bash-it-grep() +{ : "${BASH_IT_GREP:=$(type -p egrep || type -p grep)}" printf "%s" "${BASH_IT_GREP:-'/usr/bin/grep'}" } # Runs `grep` with extended regular expressions -function _bash-it-egrep() { +function _bash-it-egrep() +{ : "${BASH_IT_GREP:=$(type -p egrep || type -p grep)}" "${BASH_IT_GREP:-/usr/bin/grep}" -E "$@" } @@ -76,7 +82,8 @@ function _bash-it-egrep() { # completion). ########################################################################### -function _bash-it-component-help() { +function _bash-it-component-help() +{ local component file func component="$(_bash-it-pluralize-component "${1}")" file="$(_bash-it-component-cache-file "${component}")" @@ -87,7 +94,8 @@ function _bash-it-component-help() { cat "${file}" } -function _bash-it-component-cache-file() { +function _bash-it-component-cache-file() +{ local component file component="$(_bash-it-pluralize-component "${1?${FUNCNAME[0]}: component name required}")" file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}" @@ -95,7 +103,8 @@ function _bash-it-component-cache-file() { printf '%s' "${file}" } -function _bash-it-pluralize-component() { +function _bash-it-pluralize-component() +{ local component="${1}" local -i len=$((${#component} - 1)) # pluralize component name for consistency @@ -104,7 +113,8 @@ function _bash-it-pluralize-component() { printf '%s' "${component}" } -function _bash-it-clean-component-cache() { +function _bash-it-clean-component-cache() +{ local component="$1" local cache local -a BASH_IT_COMPONENTS=(aliases plugins completions) @@ -121,24 +131,28 @@ function _bash-it-clean-component-cache() { } # Returns an array of items within each compoenent. -function _bash-it-component-list() { +function _bash-it-component-list() +{ local IFS=$'\n' component="$1" _bash-it-component-help "${component}" | awk '{print $1}' | sort -u } -function _bash-it-component-list-matching() { +function _bash-it-component-list-matching() +{ local component="$1" shift local term="$1" _bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -- "${term}" | awk '{print $1}' | sort -u } -function _bash-it-component-list-enabled() { +function _bash-it-component-list-enabled() +{ local IFS=$'\n' component="$1" _bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E '\[x\]' | awk '{print $1}' | sort -u } -function _bash-it-component-list-disabled() { +function _bash-it-component-list-disabled() +{ local IFS=$'\n' component="$1" _bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -v '\[x\]' | awk '{print $1}' | sort -u } @@ -151,7 +165,8 @@ function _bash-it-component-list-disabled() { # # Examples: # _bash-it-component-item-is-enabled alias git && echo "git alias is enabled" -function _bash-it-component-item-is-enabled() { +function _bash-it-component-item-is-enabled() +{ local component="$1" local item="$2" _bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E '\[x\]' | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -q -- "^${item}\s" @@ -165,7 +180,8 @@ function _bash-it-component-item-is-enabled() { # # Examples: # _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled" -function _bash-it-component-item-is-disabled() { +function _bash-it-component-item-is-disabled() +{ local component="$1" local item="$2" _bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -v '\[x\]' | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -q -- "^${item}\s" diff --git a/lint_clean_files.sh b/lint_clean_files.sh index 26650b16..642b8415 100755 --- a/lint_clean_files.sh +++ b/lint_clean_files.sh @@ -7,10 +7,10 @@ # # shellcheck disable=SC2002 # Prefer 'cat' for cleaner script mapfile -t FILES < <( - cat clean_files.txt \ - | grep -v -E '^\s*$' \ - | grep -v -E '^\s*#' \ - | xargs -n1 -I{} find "{}" -type f + cat clean_files.txt | + grep -v -E '^\s*$' | + grep -v -E '^\s*#' | + xargs -n1 -I{} find "{}" -type f ) # We clear the BASH_IT variable to help the shellcheck checker diff --git a/plugins/available/alias-completion.plugin.bash b/plugins/available/alias-completion.plugin.bash index eb368d93..58bd8f3c 100644 --- a/plugins/available/alias-completion.plugin.bash +++ b/plugins/available/alias-completion.plugin.bash @@ -17,7 +17,8 @@ about-plugin 'Automatic completion of aliases' # 4) Custom scripts # Automatically add completion for all aliases to commands having completion functions -function alias_completion { +function alias_completion +{ local namespace="alias_completion" local tmp_file completion_loader alias_name alias_tokens line completions local alias_arg_words new_completion compl_func compl_wrapper diff --git a/plugins/available/base.plugin.bash b/plugins/available/base.plugin.bash index 6490ab88..858d7d94 100644 --- a/plugins/available/base.plugin.bash +++ b/plugins/available/base.plugin.bash @@ -2,7 +2,8 @@ cite about-plugin about-plugin 'miscellaneous tools' -function ips() { +function ips() +{ about 'display all ip addresses for this host' group 'base' if _command_exists ifconfig; then @@ -14,7 +15,8 @@ function ips() { fi } -function down4me() { +function down4me() +{ about 'checks whether a website is down for you, or everybody' param '1: website url' example '$ down4me http://www.google.com' @@ -22,7 +24,8 @@ function down4me() { curl -Ls "http://downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g' } -function myip() { +function myip() +{ about 'displays your ip address, as seen by the Internet' group 'base' list=("http://myip.dnsomatic.com/" "http://checkip.dyndns.com/" "http://checkip.dyndns.org/") @@ -35,7 +38,8 @@ function myip() { echo -e "Your public IP is: ${echo_bold_green-} $res ${echo_normal-}" } -function pickfrom() { +function pickfrom() +{ about 'picks random line from file' param '1: filename' example '$ pickfrom /usr/share/dict/words' @@ -50,7 +54,8 @@ function pickfrom() { head -n "$n" "$file" | tail -1 } -function passgen() { +function passgen() +{ about 'generates random password from dictionary words' param 'optional integer length' param 'if unset, defaults to 4' @@ -72,7 +77,8 @@ if ! _command_exists pass || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]]; then fi if _command_exists markdown && _command_exists browser; then - function pmdown() { + function pmdown() + { about 'preview markdown file in a browser' param '1: markdown file' example '$ pmdown README.md' @@ -82,7 +88,8 @@ if _command_exists markdown && _command_exists browser; then } fi -function mkcd() { +function mkcd() +{ about 'make one or more directories and cd into the last one' param 'one or more directories to create' example '$ mkcd foo' @@ -94,19 +101,22 @@ function mkcd() { } # shellcheck disable=SC2010 -function lsgrep() { +function lsgrep() +{ about 'search through directory contents with grep' group 'base' ls | grep "$@" } -function quiet() { +function quiet() +{ about 'what *does* this do?' group 'base' nohup "$@" &> /dev/null < /dev/null & } -function usage() { +function usage() +{ about 'disk usage per directory, in Mac OS X and Linux' param '1: directory name' group 'base' @@ -124,7 +134,8 @@ function usage() { if [[ ! -e "${BASH_IT?}/plugins/enabled/todo.plugin.bash" && ! -e "${BASH_IT?}/plugins/enabled"/*"${BASH_IT_LOAD_PRIORITY_SEPARATOR-}todo.plugin.bash" ]]; then # if user has installed todo plugin, skip this... - function t() { + function t() + { about 'one thing todo' param 'if not set, display todo item' param '1: todo text' @@ -137,7 +148,8 @@ if [[ ! -e "${BASH_IT?}/plugins/enabled/todo.plugin.bash" && fi if _command_exists mkisofs; then - function mkiso() { + function mkiso() + { about 'creates iso from current dir in the parent dir (unless defined)' param '1: ISO name' param '2: dest/path' @@ -160,7 +172,8 @@ if _command_exists mkisofs; then fi # useful for administrators and configs -function buf() { +function buf() +{ about 'back up file with timestamp' param 'filename' group 'base' @@ -170,7 +183,8 @@ function buf() { } if ! _command_exists del; then - function del() { + function del() + { about 'move files to hidden folder in tmp, that gets cleared on each reboot' param 'file or folder to be deleted' example 'del ./file.txt' diff --git a/plugins/available/cmd-returned-notify.plugin.bash b/plugins/available/cmd-returned-notify.plugin.bash index d9be5e4e..6f91bca3 100644 --- a/plugins/available/cmd-returned-notify.plugin.bash +++ b/plugins/available/cmd-returned-notify.plugin.bash @@ -2,13 +2,15 @@ cite about-plugin about-plugin 'Alert (BEL) when process ends after a threshold of seconds' -precmd_return_notification() { +precmd_return_notification() +{ export LAST_COMMAND_DURATION=$(($(date +%s) - ${LAST_COMMAND_TIME:=$(date +%s)})) [[ ${LAST_COMMAND_DURATION} -gt ${NOTIFY_IF_COMMAND_RETURNS_AFTER:-5} ]] && echo -e "\a" export LAST_COMMAND_TIME= } -preexec_return_notification() { +preexec_return_notification() +{ [[ -z "${LAST_COMMAND_TIME}" ]] && LAST_COMMAND_TIME=$(date +%s) } diff --git a/plugins/available/dirs.plugin.bash b/plugins/available/dirs.plugin.bash index 2c1adf7a..c95c931a 100644 --- a/plugins/available/dirs.plugin.bash +++ b/plugins/available/dirs.plugin.bash @@ -31,77 +31,87 @@ alias pu="pushd" # Pop current location alias po="popd" -function dirs-help() { - about 'directory navigation alias usage' - group 'dirs' +function dirs-help() +{ + about 'directory navigation alias usage' + group 'dirs' - echo "Directory Navigation Alias Usage" - echo - echo "Use the power of directory stacking to move" - echo "between several locations with ease." - echo - echo "d : Show directory stack." - echo "po : Remove current location from stack." - echo "pc : Adds current location to stack." - echo "pu