From 173c5071af7c84de38f11915846ac5a0957ce41a Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Thu, 23 Mar 2023 01:13:40 -0700 Subject: update formatting --- rbpm.sh | 189 ++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 94 insertions(+), 95 deletions(-) diff --git a/rbpm.sh b/rbpm.sh index cd0696d..c0b49b1 100755 --- a/rbpm.sh +++ b/rbpm.sh @@ -16,168 +16,167 @@ # Placing the above function inside ~/.bashrc (or equivalent) will load # it upon starting your shell. -RBPM_VERSION='0.2' +RBPM_VERSION='0.3' : ${RUBIES_PATH:="${HOME}/.rubies"} _echo() { - echo "echo '${@//\'/\'\\\'\'}'" + echo "echo '${@//\'/\'\\\'\'}'" } _export() { - echo "export PATH='${PATH//\'/\'\\\'\'}'" - echo 'hash -r' + echo "export PATH='${PATH//\'/\'\\\'\'}'" + echo 'hash -r' } _warn() { - echo "echo 'rbpm: ${@//\'/\'\\\'\'}' 1>&2" + echo "echo 'rbpm: ${@//\'/\'\\\'\'}' 1>&2" } _die() { - [[ -n "${@}" ]] && _warn "${@}" + [[ -n "${@}" ]] && _warn "${@}" - echo 'false' - exit 1 + echo 'false' + exit 1 } _populate_dirs() { - [[ -d "${RUBIES_PATH}" ]] \ - || _die "directory ${RUBIES_PATH} does not exist." + [[ -d "${RUBIES_PATH}" ]] \ + || _die "directory ${RUBIES_PATH} does not exist" - mapfile -td $'\0' dirs < <(shopt -s nullglob; - printf '%s\0' "${RUBIES_PATH}"/* | sort -zV) + mapfile -td $'\0' dirs < <(shopt -s nullglob; + printf '%s\0' "${RUBIES_PATH}"/* | sort -zV) - [[ -n "${dirs}" ]] || _die "directory ${RUBIES_PATH} is empty." + [[ -n "${dirs}" ]] || _die "directory ${RUBIES_PATH} is empty" } _populate_selected() { - local dir dirs + local dir dirs - mapfile -td ':' dirs < <(printf %s "${PATH}") + mapfile -td : dirs < <(printf %s "${PATH}") - for dir in "${dirs[@]}"; do - if [[ "${dir}" == "${RUBIES_PATH}/"* ]]; then - dir="${dir%/bin}" - selected+=("${dir##*/}") - fi - done + for dir in "${dirs[@]}"; do + if [[ "${dir}" == "${RUBIES_PATH}/"* ]]; then + dir="${dir%/bin}" + selected+=("${dir##*/}") + fi + done - [[ "${#selected[@]}" -ne 0 ]] || return 1 - [[ "${#selected[@]}" -eq 1 ]] \ - || _warn 'warning: multiple rubies found in PATH.' + [[ "${#selected[@]}" -ne 0 ]] || return 1 + [[ "${#selected[@]}" -eq 1 ]] \ + || _warn 'warning: multiple managed paths found in PATH' } _clear() { - local dir dirs cdirs + local dir dirs cdirs - mapfile -td ':' dirs < <(printf %s "${PATH}") + mapfile -td : dirs < <(printf %s "${PATH}") - for dir in "${dirs[@]}"; do - if [[ "${dir}" == "${RUBIES_PATH}/"* ]]; then - _echo "Removing ${dir} from PATH." - else - cdirs+=("${dir}") - fi - done + for dir in "${dirs[@]}"; do + if [[ "${dir}" == "${RUBIES_PATH}/"* ]]; then + _echo "removing ${dir} from PATH" + else + cdirs+=("${dir}") + fi + done - [[ "${#cdirs[@]}" -ne "${#dirs[@]}" ]] || return 1 + [[ "${#cdirs[@]}" -ne "${#dirs[@]}" ]] || return 1 - PATH="$(IFS=':'; echo "${cdirs[*]}")" + PATH="$(IFS=':'; echo "${cdirs[*]}")" } _add() { - _echo "Adding ${1}/bin to PATH." + _echo "adding ${1}/bin to PATH" - PATH="${1}/bin:${PATH}" + PATH="${1}/bin:${PATH}" } rbpm_ls() { - local dir dirs selected str + local dir dirs selected str - _populate_dirs - _populate_selected + _populate_dirs + _populate_selected - for dir in "${dirs[@]}"; do - str=" ${dir##*/}" + for dir in "${dirs[@]}"; do + str=" ${dir##*/}" - [[ "${dir##*/}" == "${selected}" ]] && str="${str/ /*}" + [[ "${dir##*/}" == "${selected}" ]] && str="${str/ /*}" - _echo "${str}" - done + _echo "${str}" + done } rbpm_get() { - local dir selected + local dir selected - _populate_selected || _die 'no rubies found in PATH.' + _populate_selected || _die 'no managed path found in PATH' - for dir in "${selected[@]}"; do - _echo "${dir}" - done + for dir in "${selected[@]}"; do + _echo "${dir}" + done } rbpm_set() { - [[ -n "${1}" ]] || _die 'set command requires an argument.' + [[ -n "${1}" ]] || _die 'set command requires an argument' - local dir dirs match match_start + local dir dirs match match_start - _populate_dirs + _populate_dirs - for dir in "${dirs[@]}"; do - if [[ "${dir##*/}" == *"${1}"* ]]; then - match="${dir}" + for dir in "${dirs[@]}"; do + [[ "${dir##*/}" == *"${1}"* ]] || continue - # Match from start of version is preferred. We assume the version - # is at the beginning of the directory name or following a hyphen. - # This avoids surprising behaviour like `2.1' matching `2.2.1' over - # `2.1.0'. - if [[ "${dir##*/}" == "${1}"* || "${dir##*/}" == *-"${1}"* ]]; then - match_start="${dir}" - fi - fi - done + match="${dir}" - [[ -n "${match}" ]] || _die 'no matching ruby found.' - [[ -n "${match_start}" ]] && match="${match_start}" + # match from start of version is preferred. we assume the + # version is at the beginning of the directory name or + # immediately following a hyphen. this avoids unintuitive + # behaviour like `2.1' matching `2.2.1' over `2.1.0'. + if [[ "${dir##*/}" == "${1}"* || + "${dir##*/}" == *-"${1}"* ]]; then + match_start="${dir}" + fi + done - _clear - _add "${match}" - _export + [[ -n "${match}" ]] || _die 'no matching ruby found' + [[ -n "${match_start}" ]] && match="${match_start}" + + _clear + _add "${match}" + _export } rbpm_clear() { - _clear || _die 'no rubies found in PATH.' - _export + _clear || _die 'no managed path found in PATH' + _export } rbpm_version() { - _echo "rbpm ${RBPM_VERSION}" + _echo "rbpm ${RBPM_VERSION}" } rbpm_help() { - _echo 'Usage: rbpm [args]' - _echo - _echo 'Commands:' - _echo ' ls List all available rubies.' - _echo ' get Display currently selected ruby.' - _echo ' set Select specified ruby.' - _echo ' clear Unselect any selected rubies.' - _echo ' version Display rbpm version.' + _echo 'Usage: rbpm [args]' + _echo + _echo 'Commands:' + _echo ' ls List all available rubies' + _echo ' get Display currently selected ruby' + _echo ' set Select specified ruby' + _echo ' clear Unselect any selected rubies' + _echo ' version Display rbpm version' } case "${1}" in - 'ls') - rbpm_ls ;; - 'get') - rbpm_get ;; - 'set') - rbpm_set "${2}" ;; - 'clear') - rbpm_clear ;; - 'version') - rbpm_version ;; - *) - rbpm_help ;; + 'ls') + rbpm_ls ;; + 'get') + rbpm_get ;; + 'set') + rbpm_set "${2}" ;; + 'clear') + rbpm_clear ;; + 'version') + rbpm_version ;; + *) + rbpm_help ;; esac - -# vim: set sw=4 sts=4 ts=4 et : -- cgit v1.2.3-70-g09d2