diff --git a/.editorconfig b/.editorconfig old mode 100755 new mode 100644 diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/lib/helpers.bash b/lib/helpers.bash index abcbc675..c0ce2eb3 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -8,16 +8,19 @@ : "${BASH_IT_LOAD_PRIORITY_COMPLETION:=350}" BASH_IT_LOAD_PRIORITY_SEPARATOR="---" -# Handle the different ways of running `sed` without generating a backup file based on OS -# - GNU sed (Linux) uses `-i` -# - BSD sed (macOS) uses `-i ''` +# Handle the different ways of running `sed` without generating a backup file based on provenance: +# - GNU sed (Linux) uses `-i''` +# - BSD sed (FreeBSD/macOS/Solaris/PlayStation) uses `-i ''` # To use this in Bash-it for inline replacements with `sed`, use the following syntax: # sed "${BASH_IT_SED_I_PARAMETERS[@]}" -e "..." file -BASH_IT_SED_I_PARAMETERS=('-i') # shellcheck disable=SC2034 # expected for this case -case "$OSTYPE" in - 'darwin'*) BASH_IT_SED_I_PARAMETERS=('-i' '') ;; -esac +if sed --version > /dev/null 2>&1; then + # GNU sed accepts "long" options + BASH_IT_SED_I_PARAMETERS=('-i') +else + # BSD sed errors on invalid option `-` + BASH_IT_SED_I_PARAMETERS=('-i' '') +fi function _command_exists() { _about 'checks for existence of a command' @@ -255,10 +258,10 @@ function _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' - if git checkout "$1" &> /dev/null; then + if git checkout "${1?}" &> /dev/null; then echo "Bash-it successfully updated." echo "" - echo "Migrating your installation to the latest $2 version now..." + echo "Migrating your installation to the latest ${2:-} version now..." _bash-it-migrate echo "" echo "All done, enjoy!" @@ -275,7 +278,7 @@ function _bash-it-update-() { _param '1: What kind of update to do (stable|dev)' _group 'lib' - local silent word DIFF version TARGET revision status revert log_color num_of_lines description i RESP + local silent word DIFF version TARGET revision status revert log_color RESP for word in "$@"; do if [[ "${word}" == "--silent" || "${word}" == "-s" ]]; then silent=true @@ -300,7 +303,7 @@ function _bash-it-update-() { BASH_IT_DEVELOPMENT_BRANCH="master" fi # Defaults to stable update - if [[ -z "$1" || "$1" == "stable" ]]; then + if [[ -z "${1:-}" || "$1" == "stable" ]]; then version="stable" TARGET=$(git describe --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null) @@ -331,15 +334,7 @@ function _bash-it-update-() { 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 '!/^[[:space:]]*$/ {++i} END{print i}') - if [[ "$num_of_lines" -eq 1 ]]; then - description="%s" - else - description="%b" - fi - git log --format="${log_color}%h: $description (%an)" -1 "$i" - done + git log --format="${log_color}%h: %s (%an)" "${revision}" echo "" if [[ -n "${silent}" ]]; then @@ -542,7 +537,7 @@ function _bash-it-profile-save() { fi done done - if [[ -z "$something_exists" ]]; then + if [[ -z "${something_exists:-}" ]]; then echo "It seems like no configuration was enabled.." echo "Make sure to double check that this is the wanted behavior." fi @@ -560,30 +555,30 @@ _bash-it-profile-load-parse-profile() { _example '$ _bash-it-profile-load-parse-profile "profile.bash_it" "dry"' local -i num=0 - local line + local line enable_func subdirectory component to_enable bad while read -r -a line; do ((++num)) # Ignore comments and empty lines [[ -z "${line[*]}" || "${line[*]}" =~ ^#.* ]] && continue - local enable_func="_enable-${line[0]}" - local subdirectory=${line[0]} - local component=${line[1]} + enable_func="_enable-${line[0]}" + subdirectory=${line[0]} + component=${line[1]} - local to_enable=("${BASH_IT}/$subdirectory/available/$component.${subdirectory%s}"*.bash) + to_enable=("${BASH_IT}/$subdirectory/available/$component.${subdirectory%s}"*.bash) # Ignore botched lines if [[ ! -e "${to_enable[0]}" ]]; then echo -e "${echo_orange?}Bad line(#$num) in profile, aborting load...${line[*]}${echo_reset_color?}" - local bad="bad line" + bad="bad line" break fi # Do not actually modify config on dry run - [[ -z $2 ]] || continue + [[ -z "${2:-}" ]] || continue # Actually enable the component $enable_func "$component" - done < "$1" + done < "${1?}" # Make sure to propagate the error - [[ -z $bad ]] + [[ -z ${bad:-} ]] } _bash-it-profile-list() { @@ -602,7 +597,7 @@ _bash-it-profile-rm() { about 'Removes a profile from the "profiles" directory' _group 'lib' - local name="$1" + local name="${1:-}" if [[ -z $name ]]; then echo -e "${echo_orange?}Please specify profile name to remove...${echo_reset_color?}" return 1 @@ -628,7 +623,7 @@ _bash-it-profile-load() { _about 'loads a configuration from the "profiles" directory' _group 'lib' - local name="$1" + local name="${1:-}" if [[ -z $name ]]; then echo -e "${echo_orange?}Please specify profile name to load, not changing configuration...${echo_reset_color?}" return 1 @@ -659,19 +654,15 @@ function _bash-it-restart() { _about 'restarts the shell in order to fully reload it' _group 'lib' - local saved_pwd="${PWD}" init_file="${BASH_IT_BASHRC:-${HOME?}/.bashrc}" - - exec "${0/-/}" --rcfile <(echo "source \"${init_file}\"; cd \"$saved_pwd\"") + exec "${0#-}" --rcfile "${BASH_IT_BASHRC:-${HOME?}/.bashrc}" } function _bash-it-reload() { - _about 'reloads a profile file' + _about 'reloads the shell initialization file' _group 'lib' - pushd "${BASH_IT?}" > /dev/null || return # shellcheck disable=SC1090 source "${BASH_IT_BASHRC:-${HOME?}/.bashrc}" - popd > /dev/null || return } function _bash-it-describe() { @@ -709,7 +700,9 @@ function _on-disable-callback() { _group 'lib' local callback="${1}_on_disable" - _command_exists "$callback" && "$callback" + if _command_exists "$callback"; then + "$callback" + fi } function _disable-all() { @@ -728,8 +721,8 @@ function _disable-plugin() { _example '$ disable-plugin rvm' _group 'lib' - _disable-thing "plugins" "plugin" "$1" - _on-disable-callback "$1" + _disable-thing "plugins" "plugin" "${1?}" + _on-disable-callback "${1?}" } function _disable-alias() { @@ -738,7 +731,7 @@ function _disable-alias() { _example '$ disable-alias git' _group 'lib' - _disable-thing "aliases" "alias" "$1" + _disable-thing "aliases" "alias" "${1?}" } function _disable-completion() { @@ -747,7 +740,7 @@ function _disable-completion() { _example '$ disable-completion git' _group 'lib' - _disable-thing "completion" "completion" "$1" + _disable-thing "completion" "completion" "${1?}" } function _disable-thing() { @@ -781,7 +774,7 @@ function _disable-thing() { # Either one will be matched by this glob for plugin in "${BASH_IT}/enabled"/[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash" "${BASH_IT}/$subdirectory/enabled/"{[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash","${file_entity}.${suffix}.bash"}; do if [[ -e "${plugin}" ]]; then - rm "${plugin}" + rm -f "${plugin}" plugin= break fi @@ -792,10 +785,11 @@ function _disable-thing() { fi fi - _bash-it-clean-component-cache "${file_type}" + _bash-it-component-cache-clean "${file_type}" - if [[ "$file_entity" = "all" ]]; then - printf '%s\n' "$file_entity $(_bash-it-pluralize-component "$file_type") disabled." + if [[ "$file_entity" == "all" ]]; then + _bash-it-component-pluralize "$file_type" file_type + printf '%s\n' "$file_entity ${file_type} disabled." else printf '%s\n' "$file_entity disabled." fi @@ -807,7 +801,7 @@ function _enable-plugin() { _example '$ enable-plugin rvm' _group 'lib' - _enable-thing "plugins" "plugin" "$1" "$BASH_IT_LOAD_PRIORITY_PLUGIN" + _enable-thing "plugins" "plugin" "${1?}" "$BASH_IT_LOAD_PRIORITY_PLUGIN" } function _enable-plugins() { @@ -821,7 +815,7 @@ function _enable-alias() { _example '$ enable-alias git' _group 'lib' - _enable-thing "aliases" "alias" "$1" "$BASH_IT_LOAD_PRIORITY_ALIAS" + _enable-thing "aliases" "alias" "${1?}" "$BASH_IT_LOAD_PRIORITY_ALIAS" } function _enable-aliases() { @@ -835,7 +829,7 @@ function _enable-completion() { _example '$ enable-completion git' _group 'lib' - _enable-thing "completion" "completion" "$1" "$BASH_IT_LOAD_PRIORITY_COMPLETION" + _enable-thing "completion" "completion" "${1?}" "$BASH_IT_LOAD_PRIORITY_COMPLETION" } function _enable-thing() { @@ -890,7 +884,7 @@ function _enable-thing() { 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-component-cache-clean "${file_type}" printf '%s\n' "$file_entity enabled with priority $use_load_priority." } @@ -908,7 +902,7 @@ function _help-aliases() { _example '$ alias-help' _example '$ alias-help git' - if [[ -n "$1" ]]; then + if [[ -n "${1:-}" ]]; then case "$1" in custom) alias_path='custom.aliases.bash' @@ -1021,14 +1015,14 @@ function pathmunge() { # 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() ( - 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' - returns '0: prints path of closest matching ancestor directory to stdout' - returns '1: no match found' - returns '2: improper usage of shell builtin' # uncommon - example '_bash-it-find-in-ancestor .git .hg' - example '_bash-it-find-in-ancestor GNUmakefile Makefile makefile' + : _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' + : _returns '0: prints path of closest matching ancestor directory to stdout' + : _returns '1: no match found' + : _returns '2: improper usage of shell builtin' # uncommon + : _example '_bash-it-find-in-ancestor .git .hg' + : _example '_bash-it-find-in-ancestor GNUmakefile Makefile makefile' local kin # To keep things simple, we do not search the root dir. diff --git a/lib/search.bash b/lib/search.bash index 2da8f005..7073f879 100644 --- a/lib/search.bash +++ b/lib/search.bash @@ -70,7 +70,7 @@ function _bash-it-search() { return 0 ;; '-r' | '--refresh') - _bash-it-clean-component-cache + _bash-it-component-cache-clean ;; '-c' | '--no-color') BASH_IT_SEARCH_USE_COLOR=false @@ -266,9 +266,11 @@ function _bash-it-search-result() { shift local color_component color_enable color_disable color_off - local color_sep=':' line - + local match_color compatible_action suffix opposite_suffix + local color_sep=':' line match matched temp + local -i modified=0 enabled=0 len local -a matches=() + # Discard any empty arguments while IFS='' read -r line; do [[ -n "${line}" ]] && matches+=("$line") @@ -290,18 +292,13 @@ function _bash-it-search-result() { color_off='' fi - local match - local -i modified=0 - if [[ "${#matches[@]}" -gt 0 ]]; then printf "${color_component}%13s${color_sep}${color_off} " "${component}" for match in "${matches[@]}"; do - local -i enabled=0 + enabled=0 _bash-it-component-item-is-enabled "${component}" "${match}" && enabled=1 - local match_color compatible_action suffix opposite_suffix - if ((enabled)); then match_color="${color_enable}" suffix="${suffix_enable}" @@ -314,8 +311,8 @@ function _bash-it-search-result() { compatible_action="enable" fi - local matched="${match}${suffix}" - local -i len="${#matched}" + matched="${match}${suffix}" + len="${#matched}" printf '%b' "${match_color}${matched}" # print current state if [[ "${action}" == "${compatible_action}" ]]; then @@ -327,7 +324,7 @@ function _bash-it-search-result() { modified=1 # shellcheck disable=SC2034 # no idea if `$result` is ever used result=$("${action_func}" "${match}") - local temp="color_${compatible_action}" + temp="color_${compatible_action}" match_color="${!temp}" _bash-it-rewind "${len}" printf '%b' "${match_color}${match}${opposite_suffix}" @@ -336,7 +333,7 @@ function _bash-it-search-result() { printf '%b' "${color_off} " done - ((modified)) && _bash-it-clean-component-cache "${component}" + ((modified)) && _bash-it-component-cache-clean "${component}" printf "\n" fi } diff --git a/lib/utilities.bash b/lib/utilities.bash index 63734a0f..8ea6b98c 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -63,7 +63,7 @@ function _bash-it-array-dedup() { # Outputs a full path of the grep found on the filesystem function _bash-it-grep() { : "${BASH_IT_GREP:=$(type -p egrep || type -p grep)}" - printf "%s" "${BASH_IT_GREP:-'/usr/bin/grep'}" + printf "%s" "${BASH_IT_GREP:-/usr/bin/grep}" } # Runs `grep` with extended regular expressions @@ -91,7 +91,7 @@ function _bash-it-component-help() { function _bash-it-component-cache-file() { local _component_to_cache _file_path _result="${2:-${FUNCNAME[0]//-/_}}" _bash-it-component-pluralize "${1?${FUNCNAME[0]}: component name required}" _component_to_cache - _file_path="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${_component_to_cache?}" + _file_path="${XDG_CACHE_HOME:-${HOME?}/.cache}/bash/${_component_to_cache?}" [[ -f "${_file_path}" ]] || mkdir -p "${_file_path%/*}" printf -v "${_result?}" '%s' "${_file_path}" } @@ -121,19 +121,17 @@ function _bash-it-component-pluralize() { printf -v "${_result?}" '%s' "${_component_to_plural}" } -function _bash-it-clean-component-cache() { - local component="$1" +function _bash-it-component-cache-clean() { + local component="${1:-}" local cache - local -a BASH_IT_COMPONENTS=(aliases plugins completions) + local -a components=('aliases' 'plugins' 'completions') if [[ -z "${component}" ]]; then - for component in "${BASH_IT_COMPONENTS[@]}"; do - _bash-it-clean-component-cache "${component}" + for component in "${components[@]}"; do + _bash-it-component-cache-clean "${component}" done else _bash-it-component-cache-file "${component}" cache - if [[ -f "${cache}" ]]; then - rm -f "${cache}" - fi + : >| "${cache:?}" fi } @@ -170,18 +168,22 @@ function _bash-it-component-list-disabled() { function _bash-it-component-item-is-enabled() { local component_type item_name each_file - if [[ -f "${1}" ]]; then + if [[ -f "${1?}" ]]; then item_name="$(_bash-it-get-component-name-from-path "${1}")" component_type="$(_bash-it-get-component-type-from-path "${1}")" else - component_type="${1}" item_name="${2}" + component_type="${1}" item_name="${2?}" fi for each_file in "${BASH_IT}/enabled"/*"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}${item_name}.${component_type}"*."bash" \ "${BASH_IT}/${component_type}"*/"enabled/${item_name}.${component_type}"*."bash" \ "${BASH_IT}/${component_type}"*/"enabled"/*"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}${item_name}.${component_type}"*."bash"; do - [[ -f "${each_file}" ]] && return + if [[ -f "${each_file}" ]]; then + return 0 + fi done + + return 1 } # Checks if a given item is disabled for a particular component/file-type. diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index f06fa008..51ef93e9 100755 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -196,12 +196,11 @@ function setup_upower { percent="$1" BAT0="/org/freedesktop/UPower/devices/battery_BAT$RANDOM" - function upower { case $1 in '-e'|'--enumerate') - echo "$BAT0" - echo "/org/freedesktop/UPower/devices/mouse_hid_${RANDOM}_battery" + # don't just `echo` twice because `grep` will close the pipe after matching the first line... + echo "$BAT0"$'\n'"/org/freedesktop/UPower/devices/mouse_hid_${RANDOM}_battery" ;; '-i'|'--show-info') if [[ $2 == "$BAT0" ]] diff --git a/themes/githelpers.theme.bash b/themes/githelpers.theme.bash index ba089392..5ef18e9a 100644 --- a/themes/githelpers.theme.bash +++ b/themes/githelpers.theme.bash @@ -134,7 +134,7 @@ function _git-remote-info { elif [[ ${same_branch_name} != "true" ]]; then remote_info="${VCS_STATUS_REMOTE_BRANCH}" fi - if [[ -n "${remote_info}" ]];then + if [[ -n "${remote_info:-}" ]];then # no support for gone remote branches in gitstatusd local branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX}" echo "${branch_prefix}${remote_info}" @@ -155,7 +155,7 @@ function _git-remote-info { elif [[ ${same_branch_name} != "true" ]]; then remote_info="\$(_git-upstream-branch)" fi - if [[ -n "${remote_info}" ]];then + if [[ -n "${remote_info:-}" ]];then local branch_prefix if _git-upstream-branch-gone; then branch_prefix="${SCM_THEME_BRANCH_GONE_PREFIX}"