From 475a563280333c1b5f59bab78e084367d8b2d148 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 16 Apr 2023 16:39:41 +0900 Subject: [PATCH 1/5] feat(lib/utils): Add "_omb_util_split" --- lib/utils.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/utils.sh b/lib/utils.sh index 0504758a5..6fcee3944 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -364,6 +364,15 @@ function _omb_util_add_prompt_command { fi } +## @fn _omb_util_split array str [sep] +function _omb_util_split { + local __set=$- IFS=${3:-$' \t\n'} + set -f + eval -- "$1=(\$2)" + [[ $__set == *f* ]] || set +f + return 0 +} + function _omb_util_glob_expand { local __set=$- __shopt __gignore=$GLOBIGNORE _omb_util_get_shopt failglob nullglob extglob From 0c07172c0232681f07f454eb1e9b210d2ee11122 Mon Sep 17 00:00:00 2001 From: Edwin Kofler Date: Thu, 6 Apr 2023 17:24:44 -0700 Subject: [PATCH 2/5] fix: Fix quoting Co-authored-by: Koichi Muarse --- completions/django.completion.sh | 20 ++++++++++---------- completions/gh.completion.sh | 2 +- completions/hub.completion.sh | 2 +- completions/vagrant.completion.sh | 4 ++-- completions/vault.completion.sh | 2 +- lib/omb-prompt-base.sh | 6 +++--- lib/readlink.sh | 1 + lib/utils.sh | 3 ++- themes/agnoster/agnoster.theme.sh | 2 +- themes/duru/duru.theme.sh | 4 ++-- themes/hawaii50/hawaii50.theme.sh | 2 +- 11 files changed, 25 insertions(+), 23 deletions(-) diff --git a/completions/django.completion.sh b/completions/django.completion.sh index 710160728..0170e5043 100644 --- a/completions/django.completion.sh +++ b/completions/django.completion.sh @@ -43,10 +43,10 @@ complete -F _django_completion -o default django-admin.py manage.py django-admin _python_django_completion() { if [[ ${COMP_CWORD} -ge 2 ]]; then - PYTHON_EXE=$( basename -- ${COMP_WORDS[0]} ) + PYTHON_EXE=$( basename -- "${COMP_WORDS[0]}" ) echo $PYTHON_EXE | command grep -E "python([2-9]\.[0-9])?" >/dev/null 2>&1 if [[ $? == 0 ]]; then - PYTHON_SCRIPT=$( basename -- ${COMP_WORDS[1]} ) + PYTHON_SCRIPT=$( basename -- "${COMP_WORDS[1]}" ) echo $PYTHON_SCRIPT | command grep -E "manage\.py|django-admin(\.py)?" >/dev/null 2>&1 if [[ $? == 0 ]]; then COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]:1}" \ @@ -58,16 +58,16 @@ _python_django_completion() } # Support for multiple interpreters. -unset pythons +unset -v pythons if _omb_util_command_exists whereis; then - python_interpreters=$(whereis python | cut -d " " -f 2-) - for python in $python_interpreters; do - pythons="${pythons} $(basename -- $python)" + _omb_util_split python_interpreters "$(whereis python | cut -d " " -f 2-)" + pythons=() + for python in "${python_interpreters[@]}"; do + pythons+=("$(basename -- "$python")") done - pythons=$(echo $pythons | tr " " "\n" | sort -u | tr "\n" " ") + pythons=($(printf '%s\n' "${pythons[@]}" | sort -u)) else - pythons=python + pythons=(python) fi -complete -F _python_django_completion -o default $pythons - +complete -F _python_django_completion -o default "${pythons[@]}" diff --git a/completions/gh.completion.sh b/completions/gh.completion.sh index 175fda4cb..463b922c9 100644 --- a/completions/gh.completion.sh +++ b/completions/gh.completion.sh @@ -200,7 +200,7 @@ EOF ((c++)) done if [ -z "$name" ]; then - repo=$(basename "$(pwd)") + repo=$(basename "$PWD") fi case "$prev" in -d|-h) diff --git a/completions/hub.completion.sh b/completions/hub.completion.sh index 7c76bde4e..034265e66 100644 --- a/completions/hub.completion.sh +++ b/completions/hub.completion.sh @@ -202,7 +202,7 @@ EOF ((c++)) done if [ -z "$name" ]; then - repo=$(basename "$(pwd)") + repo=$(basename "$PWD") fi case "$prev" in -d|-h) diff --git a/completions/vagrant.completion.sh b/completions/vagrant.completion.sh index 936873895..51652b983 100644 --- a/completions/vagrant.completion.sh +++ b/completions/vagrant.completion.sh @@ -83,9 +83,9 @@ function _vagrant { vagrant_state_file=$(__vagrantinvestigate) || return 1 if [[ -f $vagrant_state_file ]] then - running_vm_list=$(grep 'active' $vagrant_state_file | sed -e 's/"active"://' | tr ',' '\n' | cut -d '"' -f 2 | tr '\n' ' ') + running_vm_list=$(grep 'active' "$vagrant_state_file" | sed -e 's/"active"://' | tr ',' '\n' | cut -d '"' -f 2 | tr '\n' ' ') else - running_vm_list=$(find $vagrant_state_file -type f -name "id" | awk -F"/" '{print $(NF-2)}') + running_vm_list=$(find "$vagrant_state_file" -type f -name "id" | awk -F"/" '{print $(NF-2)}') fi COMPREPLY=($(compgen -W "${running_vm_list}" -- ${cur})) return 0 diff --git a/completions/vault.completion.sh b/completions/vault.completion.sh index e1a540a6e..02ec76fb4 100644 --- a/completions/vault.completion.sh +++ b/completions/vault.completion.sh @@ -32,7 +32,7 @@ function _vault() { if [[ $prev =~ ^(policies|policy-write|policy-delete) ]]; then local policies=$(vault policies 2> /dev/null) COMPREPLY=($(compgen -W "$policies" -- $cur)) - elif [ "$(echo $line | wc -w)" -le 2 ]; then + elif [ "$(echo "$line" | wc -w)" -le 2 ]; then if [[ "$line" =~ ^vault\ (read|write|delete|list)\ $ ]]; then COMPREPLY=($(compgen -W "$(_vault_mounts)" -- '')) else diff --git a/lib/omb-prompt-base.sh b/lib/omb-prompt-base.sh index f79d6824d..40e937f8b 100644 --- a/lib/omb-prompt-base.sh +++ b/lib/omb-prompt-base.sh @@ -244,7 +244,7 @@ function git_prompt_vars { command git status --porcelain ${git_status_flags} 2> /dev/null) | git_status_summary) local status=$(awk 'NR==1' <<< "$status_lines") local counts=$(awk 'NR==2' <<< "$status_lines") - IFS=$'\t' read untracked_count unstaged_count staged_count <<< "$counts" + IFS=$'\t' read -r untracked_count unstaged_count staged_count <<< "$counts" if [[ "${untracked_count}" -gt 0 || "${unstaged_count}" -gt 0 || "${staged_count}" -gt 0 ]]; then SCM_DIRTY=1 if [[ "${SCM_GIT_SHOW_DETAILS}" = "true" ]]; then @@ -341,7 +341,7 @@ function svn_prompt_vars { # - .hg is located in ~/Projects/Foo/.hg # - get_hg_root starts at ~/Projects/Foo/Bar and sees that there is no .hg directory, so then it goes into ~/Projects/Foo function get_hg_root { - local CURRENT_DIR=$(pwd) + local CURRENT_DIR=$PWD while [ "$CURRENT_DIR" != "/" ]; do if [ -d "$CURRENT_DIR/.hg" ]; then @@ -349,7 +349,7 @@ function get_hg_root { return fi - CURRENT_DIR=$(dirname $CURRENT_DIR) + CURRENT_DIR=$(dirname "$CURRENT_DIR") done } diff --git a/lib/readlink.sh b/lib/readlink.sh index 46f1f6515..98373a96e 100644 --- a/lib/readlink.sh +++ b/lib/readlink.sh @@ -87,6 +87,7 @@ function _omb_util_readlink__resolve { readlink=$(type -P readlink) case $readlink in (/bin/readlink | /usr/bin/readlink) + # shellcheck disable=SC2100 _omb_util_readlink_type=readlink-f function _omb_util_readlink__resolve { readlink -f -- "$1"; } ;; esac ;; diff --git a/lib/utils.sh b/lib/utils.sh index 6fcee3944..eca8c7efa 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -243,7 +243,7 @@ function _omb_log_note { printf "${_omb_term_underline}${_omb_term_bold}${_ # function seek_confirmation { printf "\\n${_omb_term_bold}%s${_omb_term_reset}" "$@" - read -p " (y/n) " -n 1 + read -rp " (y/n) " -n 1 printf "\\n" } @@ -309,6 +309,7 @@ function _omb_util_unload { } _omb_util_original_PS1=$PS1 +# shellcheck disable=SC2016 _omb_util_unload_hook+=('PS1=$_omb_util_original_PS1') _omb_util_prompt_command=() diff --git a/themes/agnoster/agnoster.theme.sh b/themes/agnoster/agnoster.theme.sh index 81fe114e6..cf5eaa87e 100644 --- a/themes/agnoster/agnoster.theme.sh +++ b/themes/agnoster/agnoster.theme.sh @@ -107,7 +107,7 @@ # export THEME=$HOME/.bash/themes/agnoster-bash/agnoster.bash # if [[ -f $THEME ]]; then # export DEFAULT_USER=$(whoami) -# source $THEME +# source "$THEME" # fi # diff --git a/themes/duru/duru.theme.sh b/themes/duru/duru.theme.sh index a968c2b0c..a8a485c48 100644 --- a/themes/duru/duru.theme.sh +++ b/themes/duru/duru.theme.sh @@ -6,7 +6,7 @@ SCM_THEME_PROMPT_DIRTY=" ${_omb_prompt_brown}with changes" SCM_THEME_PROMPT_CLEAN="" function venv { - if [ ! -z "$VIRTUAL_ENV" ] + if [ -n "$VIRTUAL_ENV" ] then local env=$VIRTUAL_ENV echo "${gray} in ${_omb_prompt_red}${env##*/} " @@ -14,7 +14,7 @@ function venv { } function last_two_dirs { - pwd|rev|awk -F / '{print $1,$2}'|rev|sed s_\ _/_|sed "s|$(sed 's,\/,,'<<<$HOME)|~|g" + pwd|rev|awk -F / '{print $1,$2}'|rev|sed s_\ _/_|sed "s|$(sed 's,\/,,'<<<"$HOME")|~|g" } function _omb_theme_PROMPT_COMMAND { diff --git a/themes/hawaii50/hawaii50.theme.sh b/themes/hawaii50/hawaii50.theme.sh index 7007d8b31..768e9da4e 100644 --- a/themes/hawaii50/hawaii50.theme.sh +++ b/themes/hawaii50/hawaii50.theme.sh @@ -169,7 +169,7 @@ function limited_pwd() { # Replace $HOME with ~ if possible local RELATIVE_PWD=${PWD/#$HOME/\~} - local offset=$((${#RELATIVE_PWD}-$MAX_PWD_LENGTH)) + local offset=$((${#RELATIVE_PWD}-MAX_PWD_LENGTH)) if ((offset > 0)); then local truncated_symbol="..." From 79655430185c96cb1ac67fd6a807222484a51d2a Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 16 Apr 2023 17:02:06 +0900 Subject: [PATCH 3/5] style(completions/django): Fix style --- completions/django.completion.sh | 59 ++++++++++++++++---------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/completions/django.completion.sh b/completions/django.completion.sh index 0170e5043..d96137354 100644 --- a/completions/django.completion.sh +++ b/completions/django.completion.sh @@ -32,42 +32,43 @@ # # To uninstall, just remove the line from your .bash_profile and .bashrc. -_django_completion() -{ - COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \ - COMP_CWORD=$COMP_CWORD \ - DJANGO_AUTO_COMPLETE=1 $1 ) ) +function _omb_completion_django { + COMPREPLY=($(COMP_WORDS="${COMP_WORDS[*]}" \ + COMP_CWORD=$COMP_CWORD \ + DJANGO_AUTO_COMPLETE=1 "$1")) } -complete -F _django_completion -o default django-admin.py manage.py django-admin +complete -F _omb_completion_django -o default django-admin.py manage.py django-admin -_python_django_completion() -{ - if [[ ${COMP_CWORD} -ge 2 ]]; then - PYTHON_EXE=$( basename -- "${COMP_WORDS[0]}" ) - echo $PYTHON_EXE | command grep -E "python([2-9]\.[0-9])?" >/dev/null 2>&1 - if [[ $? == 0 ]]; then - PYTHON_SCRIPT=$( basename -- "${COMP_WORDS[1]}" ) - echo $PYTHON_SCRIPT | command grep -E "manage\.py|django-admin(\.py)?" >/dev/null 2>&1 - if [[ $? == 0 ]]; then - COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]:1}" \ - COMP_CWORD=$(( COMP_CWORD-1 )) \ - DJANGO_AUTO_COMPLETE=1 ${COMP_WORDS[*]} ) ) - fi - fi +function _omb_completion_django_python { + if ((COMP_CWORD >= 2)); then + local PYTHON_EXE=$(basename -- "${COMP_WORDS[0]}") + if command grep -E "python([2-9]\.[0-9])?" >/dev/null 2>&1 <<< "$PYTHON_EXE"; then + local PYTHON_SCRIPT=$(basename -- "${COMP_WORDS[1]}") + if command grep -E "manage\.py|django-admin(\.py)?" >/dev/null 2>&1 <<< "$PYTHON_SCRIPT"; then + COMPREPLY=($(COMP_WORDS="${COMP_WORDS[*]:1}" \ + COMP_CWORD=$((COMP_CWORD - 1)) \ + DJANGO_AUTO_COMPLETE=1 "${COMP_WORDS[@]}")) + fi fi + fi } -# Support for multiple interpreters. -unset -v pythons -if _omb_util_command_exists whereis; then +function _omb_completion_django_init { + # Support for multiple interpreters. + local -a pythons=() + if _omb_util_command_exists whereis; then + local python_interpreters _omb_util_split python_interpreters "$(whereis python | cut -d " " -f 2-)" - pythons=() + local python for python in "${python_interpreters[@]}"; do - pythons+=("$(basename -- "$python")") + pythons+=("$(basename -- "$python")") done - pythons=($(printf '%s\n' "${pythons[@]}" | sort -u)) -else + _omb_util_split pythons "$(printf '%s\n' "${pythons[@]}" | sort -u)" $'\n' + else pythons=(python) -fi + fi -complete -F _python_django_completion -o default "${pythons[@]}" + complete -F _omb_completion_django_python -o default "${pythons[@]}" + unset -f "$FUNCNAME" +} +_omb_completion_django_init From 3a5b36d61499ea2ba24a3892f3068f86a08a23dc Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 16 Apr 2023 17:08:27 +0900 Subject: [PATCH 4/5] fix(completions/django): Check Existence of binary --- completions/django.completion.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/completions/django.completion.sh b/completions/django.completion.sh index d96137354..29d4f5521 100644 --- a/completions/django.completion.sh +++ b/completions/django.completion.sh @@ -55,17 +55,16 @@ function _omb_completion_django_python { function _omb_completion_django_init { # Support for multiple interpreters. - local -a pythons=() + local -a pythons=(python) if _omb_util_command_exists whereis; then local python_interpreters _omb_util_split python_interpreters "$(whereis python | cut -d " " -f 2-)" local python for python in "${python_interpreters[@]}"; do + [[ -x $python ]] || continue pythons+=("$(basename -- "$python")") done _omb_util_split pythons "$(printf '%s\n' "${pythons[@]}" | sort -u)" $'\n' - else - pythons=(python) fi complete -F _omb_completion_django_python -o default "${pythons[@]}" From 2960ba971fa6905e26afd4c1e66f6424b5ba001e Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 16 Apr 2023 17:13:22 +0900 Subject: [PATCH 5/5] fix(completions/django): Apply upstream changes https://github.com/django/django/commit/e535da6865f0e02f0b593b52ed2e040b24a886d6 https://github.com/django/django/commit/34057730a55efe31431070c529031ee5a6f7f47b https://github.com/django/django/commit/4af88ccbe68d6a2a6df428f569deb1f50d826cc7 https://github.com/django/django/commit/2ee1e1a1744c4d0679058124eb7ceba78a0a5a84 https://github.com/django/django/commit/5708327c3769262b845730996ca2784245ada4d1 https://github.com/django/django/commit/90c59b4e12e6ff41407694a460f5f30c4688dbfd --- completions/django.completion.sh | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/completions/django.completion.sh b/completions/django.completion.sh index 29d4f5521..3db111b3f 100644 --- a/completions/django.completion.sh +++ b/completions/django.completion.sh @@ -1,7 +1,8 @@ #! bash oh-my-bash.module +# Upstream: https://github.com/django/django/blob/90c59b4e12e6ff41407694a460f5f30c4688dbfd/extras/django_bash_completion +# # ######################################################################### -# This bash script adds tab-completion feature to django-admin.py and -# manage.py. +# This bash script adds tab-completion feature to django-admin and manage.py. # # Testing it out without installing # ================================= @@ -37,14 +38,13 @@ function _omb_completion_django { COMP_CWORD=$COMP_CWORD \ DJANGO_AUTO_COMPLETE=1 "$1")) } -complete -F _omb_completion_django -o default django-admin.py manage.py django-admin +# When the django-admin.py deprecation ends, remove django-admin.py. +complete -F _omb_completion_django -o default manage.py django-admin function _omb_completion_django_python { if ((COMP_CWORD >= 2)); then - local PYTHON_EXE=$(basename -- "${COMP_WORDS[0]}") - if command grep -E "python([2-9]\.[0-9])?" >/dev/null 2>&1 <<< "$PYTHON_EXE"; then - local PYTHON_SCRIPT=$(basename -- "${COMP_WORDS[1]}") - if command grep -E "manage\.py|django-admin(\.py)?" >/dev/null 2>&1 <<< "$PYTHON_SCRIPT"; then + if command grep -qE "python([3-9]\.[0-9])?" <<< "${COMP_WORDS[0]##*/}"; then + if command grep -qE "manage\.py|django-admin" <<< "${COMP_WORDS[1]##*/}"; then COMPREPLY=($(COMP_WORDS="${COMP_WORDS[*]:1}" \ COMP_CWORD=$((COMP_CWORD - 1)) \ DJANGO_AUTO_COMPLETE=1 "${COMP_WORDS[@]}")) @@ -62,7 +62,9 @@ function _omb_completion_django_init { local python for python in "${python_interpreters[@]}"; do [[ -x $python ]] || continue - pythons+=("$(basename -- "$python")") + [[ $python == *-config ]] || continue + python=${python##*/} + [[ $python ]] && pythons+=("$python") done _omb_util_split pythons "$(printf '%s\n' "${pythons[@]}" | sort -u)" $'\n' fi