Skip to content
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

lib/bourne-shell.sh: Don't force-load bash-completion #572

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 33 additions & 16 deletions completions/system.completion.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
#! bash oh-my-bash.module

# Loads the system's Bash completion modules.
# If Homebrew is installed (OS X), its Bash completion modules are loaded.
# Loads one of the system's Bash-completion modules.

if [ -f /etc/bash_completion ]; then
. /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
fi
# 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 [ $(uname) = "Darwin" ] && _omb_util_command_exists brew; then
# 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)

if [ -f "$BREW_PREFIX"/etc/bash_completion ]; then
. "$BREW_PREFIX"/etc/bash_completion
# homebrew/versions/bash-completion2 (required for projects.completion.bash)
# is installed to this path
if [[ -f "$BREW_PREFIX"/share/bash-completion/bash_completion ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One request I would make here is the ability to set a BASH_COMPLETION_DIR, and check it as a potential location.

Copy link
Contributor

@akinomyoga akinomyoga Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if that is useful. If you know the location of bash_completion and needs to specify it explicitly, how is it different from source '<path to bash_completion>/bash_completion'? A plugin that allows users to write

BASH_COMPLETION_DIR='<path to bash_completion>'

plugins+=(system)

instead of

source '<path to bash_completion>/bash_completion'

doesn't seem useful. It actually complicates the configuration.

Also, the plugin name system implies loading bash-completion in a system package. Allowing the configuration for an arbitrary location of bash-completion doesn't match the plugin's name. If the configuration would be added, the plugin name should also be changed.

In addition, BASH_COMPLETION_* is a kind of variable namespace used by bash-completion [1,2], so we should avoid defining a variable of the form BASH_COMPLETION_* outside bash-completion. It might conflict in the future. We should use OMB_COMPLETION_SYSTEM_BASH_COMPLETION_DIR or something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't think of just sourcing the file directly. Good call

source "$BREW_PREFIX"/share/bash-completion/bash_completion
return "$?"
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"/etc/bash_completion ]]; then
source "$BREW_PREFIX"/etc/bash_completion
return "$?"
fi
fi

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

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
6 changes: 3 additions & 3 deletions lib/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
11 changes: 0 additions & 11 deletions lib/bourne-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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* ]]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion oh-my-bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions templates/bashrc.osh-template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion themes/rjorgenson/rjorgenson.theme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading