Skip to content

lib/helpers: fixes, improvements, consolations, constellations, and a partridge in a pear tree #2061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .editorconfig
100755 → 100644
Empty file.
Empty file modified .gitignore
100755 → 100644
Empty file.
114 changes: 54 additions & 60 deletions lib/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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!"
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand 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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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."
}
Expand All @@ -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'
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 10 additions & 13 deletions lib/search.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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}"
Expand All @@ -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
Expand All @@ -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}"
Expand All @@ -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
}
Expand Down
Loading