From ebb8c36fc791c8d6005401d232771b79eb5a782f Mon Sep 17 00:00:00 2001 From: Barak Korren Date: Thu, 16 May 2024 11:03:24 +0300 Subject: [PATCH 1/6] lib/bourne-shell: Do not load bash-completion It's already done better by the "system" completion module, and doing it there too makes disabling it impossible and debgging issues harder. --- lib/bourne-shell.sh | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lib/bourne-shell.sh b/lib/bourne-shell.sh index 64499f125..5ec0875f8 100644 --- a/lib/bourne-shell.sh +++ b/lib/bourne-shell.sh @@ -98,14 +98,3 @@ fi if [ -f ~/.bashrc.local ]; then . ~/.bashrc.local fi - -# enable programmable completion features (you don't need to enable -# this, if it's already enabled in /etc/bash.bashrc and /etc/profile -# sources /etc/bash.bashrc). -if ! shopt -oq posix; then - if [ -f /usr/share/bash-completion/bash_completion ]; then - . /usr/share/bash-completion/bash_completion - elif [ -f /etc/bash_completion ]; then - . /etc/bash_completion - fi -fi From 4f4c84c89903b0dce910989de3f7df8433e9e40b Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 16 May 2024 17:52:04 +0900 Subject: [PATCH 2/6] feat(bashrc): enable "completions/system" by default --- templates/bashrc.osh-template | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/bashrc.osh-template b/templates/bashrc.osh-template index 4f1851d98..ad0d3aab3 100644 --- a/templates/bashrc.osh-template +++ b/templates/bashrc.osh-template @@ -84,6 +84,7 @@ OMB_USE_SUDO=true # Example format: completions=(ssh git bundler gem pip pip3) # Add wisely, as too many completions slow down shell startup. completions=( + system git composer ssh From 3a0381401be0701bdcfdbe0ed28a9abe14ec3565 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 16 May 2024 17:53:10 +0900 Subject: [PATCH 3/6] style(completions/system): adjust coding styles --- completions/system.completion.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/completions/system.completion.sh b/completions/system.completion.sh index 24743d1b2..076a50fac 100644 --- a/completions/system.completion.sh +++ b/completions/system.completion.sh @@ -3,25 +3,25 @@ # Loads the system's Bash completion modules. # If Homebrew is installed (OS X), its Bash completion modules are loaded. -if [ -f /etc/bash_completion ]; then - . /etc/bash_completion +if [[ -f /etc/bash_completion ]]; then + source /etc/bash_completion fi # Some distribution makes use of a profile.d script to import completion. -if [ -f /etc/profile.d/bash_completion.sh ]; then - . /etc/profile.d/bash_completion.sh +if [[ -f /etc/profile.d/bash_completion.sh ]]; then + source /etc/profile.d/bash_completion.sh fi -if [ $(uname) = "Darwin" ] && _omb_util_command_exists brew; then +if [[ $(uname) == "Darwin" ]] && _omb_util_command_exists brew; then BREW_PREFIX=$(brew --prefix) - if [ -f "$BREW_PREFIX"/etc/bash_completion ]; then - . "$BREW_PREFIX"/etc/bash_completion + if [[ -f "$BREW_PREFIX"/etc/bash_completion ]]; then + source "$BREW_PREFIX"/etc/bash_completion fi # homebrew/versions/bash-completion2 (required for projects.completion.bash) is installed to this path - if [ -f "$BREW_PREFIX"/share/bash-completion/bash_completion ]; then - . "$BREW_PREFIX"/share/bash-completion/bash_completion + if [[ -f "$BREW_PREFIX"/share/bash-completion/bash_completion ]]; then + source "$BREW_PREFIX"/share/bash-completion/bash_completion fi fi From 8ed02a816a436aa100c8f4fdcfab307a62a386ae Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 16 May 2024 18:01:02 +0900 Subject: [PATCH 4/6] refactor(rsync): normalize detection of macOS --- completions/system.completion.sh | 2 +- lib/base.sh | 6 +++--- lib/functions.sh | 2 +- lib/utils.sh | 6 +++--- oh-my-bash.sh | 2 +- themes/rjorgenson/rjorgenson.theme.sh | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/completions/system.completion.sh b/completions/system.completion.sh index 076a50fac..cde89fd8d 100644 --- a/completions/system.completion.sh +++ b/completions/system.completion.sh @@ -13,7 +13,7 @@ if [[ -f /etc/profile.d/bash_completion.sh ]]; then fi -if [[ $(uname) == "Darwin" ]] && _omb_util_command_exists brew; then +if is_os darwin && _omb_util_command_exists brew; then BREW_PREFIX=$(brew --prefix) if [[ -f "$BREW_PREFIX"/etc/bash_completion ]]; then diff --git a/lib/base.sh b/lib/base.sh index c9ed54a22..e9d6b4d93 100644 --- a/lib/base.sh +++ b/lib/base.sh @@ -229,9 +229,9 @@ function ii { echo -e "\\n${_omb_term_brown}Users logged on:$NC " ; w -h echo -e "\\n${_omb_term_brown}Current date :$NC " ; date echo -e "\\n${_omb_term_brown}Machine stats :$NC " ; uptime - [[ "$OSTYPE" == darwin* ]] && echo -e "\\n${_omb_term_brown}Current network location :$NC " ; scselect + is_os darwin && echo -e "\\n${_omb_term_brown}Current network location :$NC " ; scselect echo -e "\\n${_omb_term_brown}Public facing IP Address :$NC " ;myip - [[ "$OSTYPE" == darwin* ]] && echo -e "\\n${_omb_term_brown}DNS Configuration:$NC " ; scutil --dns + is_os darwin && echo -e "\\n${_omb_term_brown}DNS Configuration:$NC " ; scutil --dns echo } @@ -257,7 +257,7 @@ function batch_chmod { # usage: disk usage per directory, in Mac OS X and Linux # ------------------------------------------------------------------- function usage { - if [ "$(uname)" = "Darwin" ]; then + if is_os darwin; then if [ -n "$1" ]; then du -hd 1 "$1" else diff --git a/lib/functions.sh b/lib/functions.sh index 37202690e..7e87d6695 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -31,7 +31,7 @@ function open_command() { esac # don't use nohup on OSX - if [[ "$OSTYPE" == darwin* ]]; then + if is_os darwin; then $open_cmd "$@" &>/dev/null else nohup $open_cmd "$@" &>/dev/null diff --git a/lib/utils.sh b/lib/utils.sh index d2e01b546..c1e83c192 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -40,7 +40,7 @@ # exit 1 # fi -# if is_os "darwin"; then +# if is_os darwin; then # e_success "You are on a mac" # else # e_error "You are not on a mac" @@ -255,7 +255,7 @@ function is_confirmed { # # Test which OS the user runs # $1 = OS to test -# Usage: if is_os 'darwin'; then +# Usage: if is_os darwin; then # function is_os { [[ $OSTYPE == $1* ]] @@ -344,7 +344,7 @@ function _omb_util_add_prompt_command { # Set OS dependent exact match regular expression local prompt_re - if [[ $OSTYPE == darwin* ]]; then + if is_os darwin; then # macOS prompt_re='[[:<:]]'$hook'[[:>:]]' else diff --git a/oh-my-bash.sh b/oh-my-bash.sh index 689a49953..47de61579 100644 --- a/oh-my-bash.sh +++ b/oh-my-bash.sh @@ -117,7 +117,7 @@ _omb_module_require_lib "${_omb_init_files[@]}" unset -v _omb_init_files # Figure out the SHORT hostname -if [[ $OSTYPE = darwin* ]]; then +if is_os darwin; then # macOS's $HOST changes with dhcp, etc. Use ComputerName if possible. SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST=${HOST/.*} else diff --git a/themes/rjorgenson/rjorgenson.theme.sh b/themes/rjorgenson/rjorgenson.theme.sh index 57c37a727..798d86f3a 100644 --- a/themes/rjorgenson/rjorgenson.theme.sh +++ b/themes/rjorgenson/rjorgenson.theme.sh @@ -16,7 +16,7 @@ SCM_SVN_CHAR="${_omb_prompt_bold_teal}⑆${_omb_prompt_normal}" SCM_HG_CHAR="${_omb_prompt_bold_brown}☿${_omb_prompt_normal}" PROMPT_CHAR="${OMB_THEME_BRACKET_COLOR}➞ ${_omb_prompt_normal}" -if [[ $OSTYPE == *darwin* ]]; then +if is_os darwin; then PROMPT_CHAR="${OMB_THEME_BRACKET_COLOR}➞ ${_omb_prompt_normal}" fi From fcce66a85c4fa510bfb3c3feb0f4cbe76f3e1bd5 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 16 May 2024 18:01:49 +0900 Subject: [PATCH 5/6] fix(completions/system): load bash-completion only once We have been sourcing bash-completion as many times as the number of the installations and entry points that we can find. However, only one instance of bash-completion is enough. We also re-order the detection to adjust the precedence. --- completions/system.completion.sh | 36 ++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/completions/system.completion.sh b/completions/system.completion.sh index cde89fd8d..b301da8c0 100644 --- a/completions/system.completion.sh +++ b/completions/system.completion.sh @@ -1,27 +1,31 @@ #! bash oh-my-bash.module -# Loads the system's Bash completion modules. -# If Homebrew is installed (OS X), its Bash completion modules are loaded. - -if [[ -f /etc/bash_completion ]]; then - source /etc/bash_completion -fi - -# Some distribution makes use of a profile.d script to import completion. -if [[ -f /etc/profile.d/bash_completion.sh ]]; then - source /etc/profile.d/bash_completion.sh -fi - +# Loads one of the system's Bash-completion modules. +# If Homebrew is installed (OS X), its Bash completion module is loaded. if is_os darwin && _omb_util_command_exists brew; then BREW_PREFIX=$(brew --prefix) + # homebrew/versions/bash-completion2 (required for projects.completion.bash) + # is installed to this path + if [[ -f "$BREW_PREFIX"/share/bash-completion/bash_completion ]]; then + source "$BREW_PREFIX"/share/bash-completion/bash_completion + return "$?" + fi + if [[ -f "$BREW_PREFIX"/etc/bash_completion ]]; then source "$BREW_PREFIX"/etc/bash_completion + return "$?" fi +fi - # homebrew/versions/bash-completion2 (required for projects.completion.bash) is installed to this path - if [[ -f "$BREW_PREFIX"/share/bash-completion/bash_completion ]]; then - source "$BREW_PREFIX"/share/bash-completion/bash_completion - fi +if [[ -f /etc/bash_completion ]]; then + source /etc/bash_completion + return "$?" +fi + +if [[ -f /etc/profile.d/bash_completion.sh ]]; then + # Some distribution makes use of a profile.d script to import completion. + source /etc/profile.d/bash_completion.sh + return "$?" fi From d291140b2518e713ef8c2f78207188efcabd2120 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 16 May 2024 18:18:22 +0900 Subject: [PATCH 6/6] feat(completions/system): integrate bash-completion loader in lib/bourne-shell The search that has been in lib/bourne-shell.sh is slightly different from that in completions/system.completion.sh. We integrate the former into the latter. * We add a search location `/usr/share/bash-completion/bash_completion` for bash-completion. This is the standard location for bash-completion v2. We have been only checking /etc/bash_completion which is bash-completion v1. * We also add a guard for the POSIX mode. Older versions of bash-completion have an issue with the POSIX mode. In particular, bash-completion v1 can only be used with with the macOS Bash 3.2, but bash-completion v1 does not work well in the POSIX mode. * We also add a guard for already loaded bash-completion. Other system configuration might already load bash-completion. We skip loading bash-completion when we detect an existing bash-completion settings in tbe current shell environment. --- completions/system.completion.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/completions/system.completion.sh b/completions/system.completion.sh index b301da8c0..18200b6bb 100644 --- a/completions/system.completion.sh +++ b/completions/system.completion.sh @@ -2,6 +2,14 @@ # Loads one of the system's Bash-completion modules. +# If bash-completion is already enabled (by e.g. /etc/bash.bashrc sourced from +# /etc/profile or directly executed by Bash), we skip loading bash-completion. +[[ ${BASH_COMPLETION_VERSINFO-} ]] && return 0 + +# We skip loading bash-completion in the POSIX mode. Older versions of +# bash-completion do not work in the POSIX mode. +shopt -oq posix && return 0 + # If Homebrew is installed (OS X), its Bash completion module is loaded. if is_os darwin && _omb_util_command_exists brew; then BREW_PREFIX=$(brew --prefix) @@ -19,7 +27,12 @@ if is_os darwin && _omb_util_command_exists brew; then fi fi -if [[ -f /etc/bash_completion ]]; then +if [[ -f /usr/share/bash-completion/bash_completion ]]; then + # bash-completion v2 is installed at this location + source /usr/share/bash-completion/bash_completion + return "$?" +elif [[ -f /etc/bash_completion ]]; then + # bash-completion v1 is installed at this location source /etc/bash_completion return "$?" fi