Skip to content

Commit

Permalink
Merge pull request #239 from mchaker/patch-1
Browse files Browse the repository at this point in the history
Replace `which` with `command -v`
  • Loading branch information
akinomyoga authored Aug 30, 2022
2 parents 2cdaead + 49c66cf commit b9295c6
Show file tree
Hide file tree
Showing 28 changed files with 58 additions and 67 deletions.
2 changes: 1 addition & 1 deletion completions/awscli.completion.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#! bash oh-my-bash.module
[[ -x "$(which aws_completer)" ]] &>/dev/null && complete -C "$(which aws_completer)" aws
_omb_util_binary_exists aws_completer && complete -C "$(type -P aws_completer)" aws
4 changes: 2 additions & 2 deletions completions/conda.completion.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! bash oh-my-bash.module
which register-python-argcomplete &> /dev/null \
_omb_util_binary_exists register-python-argcomplete \
&& eval "$(register-python-argcomplete conda)" \
|| echo "Please install argcomplete to use conda completion" > /dev/null
|| echo "Please install argcomplete to use conda completion"
2 changes: 1 addition & 1 deletion completions/django.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ _python_django_completion()

# Support for multiple interpreters.
unset pythons
if command -v whereis &>/dev/null; then
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)"
Expand Down
4 changes: 2 additions & 2 deletions completions/docker-compose.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ __docker_compose_to_extglob() {
# suppress trailing whitespace
__docker_compose_nospace() {
# compopt is not available in ancient bash versions
type compopt &>/dev/null && compopt -o nospace
_omb_util_command_exists compopt && compopt -o nospace
}

# Extracts all service names from the compose file.
Expand Down Expand Up @@ -552,7 +552,7 @@ _docker_compose() {
done

local completions_func=_docker_compose_${command//-/_}
declare -F $completions_func >/dev/null && $completions_func
_omb_util_function_exists "$completions_func" && "$completions_func"

eval "$previous_extglob_setting"
return 0
Expand Down
4 changes: 2 additions & 2 deletions completions/docker-machine.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ _docker_machine() {
done

local completion_func=_docker_machine_"${command//-/_}"
if declare -F "${completion_func}" > /dev/null; then
${completion_func}
if _omb_util_function_exists "${completion_func}"; then
"${completion_func}"
fi

return 0
Expand Down
8 changes: 4 additions & 4 deletions completions/docker.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ __docker_subcommands() {
subcommand_pos=$counter
local subcommand=${words[$counter]}
local completions_func=_docker_${command}_${subcommand}
declare -F $completions_func >/dev/null && $completions_func
_omb_util_function_exists "$completions_func" && "$completions_func"
return 0
;;
esac
Expand All @@ -406,11 +406,11 @@ __docker_subcommands() {
# suppress trailing whitespace
__docker_nospace() {
# compopt is not available in ancient bash versions
type compopt &>/dev/null && compopt -o nospace
_omb_util_command_exists compopt && compopt -o nospace
}

__docker_complete_resolved_hostname() {
command -v host >/dev/null 2>&1 || return
_omb_util_command_exists host || return
COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') )
}

Expand Down Expand Up @@ -2953,7 +2953,7 @@ _docker() {
fi

local completions_func=_docker_${command}
declare -F $completions_func >/dev/null && $completions_func
_omb_util_function_exists "$completions_func" && "$completions_func"

eval "$previous_extglob_setting"
return 0
Expand Down
2 changes: 1 addition & 1 deletion completions/drush.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# http://github.com/drush-ops/drush/blob/master/drush.complete.sh

# Ensure drush is available.
which drush &> /dev/null || alias drush &> /dev/null || return
_omb_util_command_exists drush || return

__drush_ps1() {
f="${TMPDIR:-/tmp/}/drush-env/drush-drupal-site-$$"
Expand Down
2 changes: 1 addition & 1 deletion completions/fabric-completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function __fab_fabfile_mtime() {
#
function __fab_completion() {
# Return if "fab" command doesn't exists
[[ -e `which fab 2> /dev/null` ]] || return 0
_omb_util_binary_exists fab || return 0

# Variables to hold the current word and possible matches
local cur="${COMP_WORDS[COMP_CWORD]}"
Expand Down
2 changes: 1 addition & 1 deletion completions/gh.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This script complements the completion script that ships with git.

# Check that git tab completion is available
if declare -F _git > /dev/null; then
if _omb_util_function_exists _git; then
# Duplicate and rename the 'list_all_commands' function
eval "$(declare -f __git_list_all_commands | \
sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')"
Expand Down
4 changes: 2 additions & 2 deletions completions/hub.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# This script complements the completion script that ships with git.

# If there is no git tab completion, but we have the _completion loader try to load it
if ! declare -F _git > /dev/null && declare -F _completion_loader > /dev/null; then
if ! _omb_util_function_exists _git && _omb_util_function_exists _completion_loader; then
_completion_loader git
fi

# Check that git tab completion is available
if declare -F _git > /dev/null; then
if _omb_util_function_exists _git; then
# Duplicate and rename the 'list_all_commands' function
eval "$(declare -f __git_list_all_commands | \
sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')"
Expand Down
2 changes: 1 addition & 1 deletion completions/jungle.completion.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#! bash oh-my-bash.module
[[ -x "$(which jungle)" ]] &>/dev/null && eval "$(_JUNGLE_COMPLETE=source jungle)"
_omb_util_binary_exists jungle && eval "$(_JUNGLE_COMPLETE=source jungle)"
2 changes: 1 addition & 1 deletion completions/kontena.completion.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#! bash oh-my-bash.module
which kontena &> /dev/null && . "$( kontena whoami --bash-completion-path )"
_omb_util_binary_exists kontena && . "$( kontena whoami --bash-completion-path )"
2 changes: 1 addition & 1 deletion completions/kubectl.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# kubectl (Kubernetes CLI) completion

if command -v kubectl &>/dev/null
if _omb_util_command_exists kubectl
then
eval "$(kubectl completion bash)"
fi
6 changes: 3 additions & 3 deletions completions/maven.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

function_exists()
{
declare -F $1 > /dev/null
return $?
_omb_util_function_exists "$1"
return "$?"
}

function_exists _get_comp_words_by_ref ||
Expand Down Expand Up @@ -261,4 +261,4 @@ _mvn()
}

complete -o default -F _mvn -o nospace mvn
complete -o default -F _mvn -o nospace mvnDebug
complete -o default -F _mvn -o nospace mvnDebug
2 changes: 1 addition & 1 deletion completions/minikube.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# minikube (Kubernetes CLI) completion

if command -v minikube &>/dev/null
if _omb_util_command_exists minikube
then
eval "$(minikube completion bash)"
fi
2 changes: 1 addition & 1 deletion completions/npm.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# npm (Node Package Manager) completion
# https://docs.npmjs.com/cli/completion

if command -v npm &>/dev/null
if _omb_util_command_exists npm
then
eval "$(npm completion)"
fi
8 changes: 4 additions & 4 deletions completions/packer.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ _packer_completion ()
# Words containing an equal sign get split into tokens in bash > 4, which
# doesn't come in handy here.
# This is handled here. bash < 4 does not split.
declare -f _get_comp_words_by_ref >/dev/null && _get_comp_words_by_ref -n = cur
_omb_util_function_exists _get_comp_words_by_ref && _get_comp_words_by_ref -n = cur

COMPREPLY=()
local i c=1 command
local i c=1 command=

while [ $c -lt $COMP_CWORD ]; do
i="${COMP_WORDS[c]}"
Expand All @@ -144,7 +144,7 @@ _packer_completion ()
((c++))
done

if [ -z $command ]; then
if [ -z "$command" ]; then
case "$cur" in
'-'*)
__packercomp "-machine-readable --help --version"
Expand All @@ -157,7 +157,7 @@ _packer_completion ()
fi

local completion_func="__packer_${command}"
declare -f $completion_func >/dev/null && $completion_func
_omb_util_function_exists "$completion_func" && "$completion_func"
}

complete -o nospace -F _packer_completion packer
Expand Down
2 changes: 1 addition & 1 deletion completions/system.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if [ -f /etc/profile.d/bash_completion.sh ]; then
fi


if [ $(uname) = "Darwin" ] && command -v brew &>/dev/null ; then
if [ $(uname) = "Darwin" ] && _omb_util_command_exists brew; then
BREW_PREFIX=$(brew --prefix)

if [ -f "$BREW_PREFIX"/etc/bash_completion ]; then
Expand Down
12 changes: 3 additions & 9 deletions lib/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ zipf () { zip -r "$1".zip "$1" ; } # zipf: To create a ZIP arc
# mkiso: creates iso from current dir in the parent dir (unless defined)
# ---------------------------------------------------------
mkiso () {
if type "mkisofs" > /dev/null; then
if _omb_util_command_exists mkisofs; then
if [ -z ${1+x} ]; then
local isoname=${PWD##*/}
else
Expand Down Expand Up @@ -197,10 +197,10 @@ bigfind() {
# ips: display all ip addresses for this host
# -------------------------------------------------------------------
ips () {
if command -v ifconfig &>/dev/null
if _omb_util_command_exists ifconfig
then
ifconfig | awk '/inet /{ print $2 }'
elif command -v ip &>/dev/null
elif _omb_util_command_exists ip
then
ip addr | grep -oP 'inet \K[\d.]+'
else
Expand Down Expand Up @@ -272,12 +272,6 @@ bigfind() {
fi
}

# command_exists: checks for existence of a command (0 = true, 1 = false)
# -------------------------------------------------------------------
command_exists () {
type "$1" &> /dev/null ;
}

# pickfrom: picks random line from file
# -------------------------------------------------------------------
pickfrom () {
Expand Down
2 changes: 1 addition & 1 deletion lib/misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _omb_util_alias _='sudo'
_omb_util_alias please='sudo'

## more intelligent acking for ubuntu users
if which ack-grep &> /dev/null; then
if _omb_util_binary_exists ack-grep; then
_omb_util_alias afind='ack-grep -il'
else
_omb_util_alias afind='ack -il'
Expand Down
4 changes: 2 additions & 2 deletions lib/omb-prompt-base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ function _omb_prompt_format {
function scm {
if [[ "$SCM_CHECK" = false ]]; then SCM=$SCM_NONE
elif [[ -f .git/HEAD ]]; then SCM=$SCM_GIT
elif which git &> /dev/null && [[ -n "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]]; then SCM=$SCM_GIT
elif _omb_util_binary_exists git && [[ -n "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]]; then SCM=$SCM_GIT
elif [[ -d .hg ]]; then SCM=$SCM_HG
elif which hg &> /dev/null && [[ -n "$(hg root 2> /dev/null)" ]]; then SCM=$SCM_HG
elif _omb_util_binary_exists hg && [[ -n "$(hg root 2> /dev/null)" ]]; then SCM=$SCM_HG
elif [[ -d .svn ]]; then SCM=$SCM_SVN
else SCM=$SCM_NONE
fi
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ _omb_util_alias() {

function _omb_util_mktemp {
local template=tmp.oh-my-bash.XXXXXXXXXX
if type -t mktemp &>/dev/null; then
if _omb_util_command_exists mktemp; then
mktemp -t "$template"
else
m4 -D template="${TMPDIR:-/tmp}/$template" <<< 'mkstemp(template)'
Expand Down
33 changes: 15 additions & 18 deletions plugins/battery/battery.plugin.sh
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
#! bash oh-my-bash.module

# Bug: https://github.com/ohmybash/oh-my-bash/issues/68
if ! command -v "command_exists" >/dev/null; then command_exists() { command -v "$1" >/dev/null ; } fi

_omb_plugin_battery__upower_print_info() {
upower -i "$(upower -e | sed -n '/BAT/{p;q;}')"
}

ac_adapter_connected(){
if command_exists upower;
if _omb_util_command_exists upower;
then
_omb_plugin_battery__upower_print_info | grep -qE 'state[:[:blank:]]*(charging|fully-charged)'
return $?
elif command_exists acpi;
elif _omb_util_command_exists acpi;
then
acpi -a | grep -q "on-line"
return $?
elif command_exists pmset;
elif _omb_util_command_exists pmset;
then
pmset -g batt | grep -q 'AC Power'
return $?
elif command_exists ioreg;
elif _omb_util_command_exists ioreg;
then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
return $?
elif command_exists WMIC;
elif _omb_util_command_exists WMIC;
then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
return $?
fi
}

ac_adapter_disconnected(){
if command_exists upower;
if _omb_util_command_exists upower;
then
_omb_plugin_battery__upower_print_info | grep -qE 'state[:[:blank:]]*discharging'
return $?
elif command_exists acpi;
elif _omb_util_command_exists acpi;
then
acpi -a | grep -q "off-line"
return $?
elif command_exists pmset;
elif _omb_util_command_exists pmset;
then
pmset -g batt | grep -q 'Battery Power'
return $?
elif command_exists ioreg;
elif _omb_util_command_exists ioreg;
then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
return $?
elif command_exists WMIC;
elif _omb_util_command_exists WMIC;
then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
return $?
Expand All @@ -59,12 +56,12 @@ ac_adapter_disconnected(){
## @about 'displays battery charge as a percentage of full (100%)'
## @group 'battery'
battery_percentage(){
if command_exists upower;
if _omb_util_command_exists upower;
then
local UPOWER_OUTPUT=$(_omb_plugin_battery__upower_print_info | sed -n 's/.*percentage[:[:blank:]]*\([0-9%]\{1,\}\)$/\1/p')
[[ $UPOWER_OUTPUT ]] &&
echo "${UPOWER_OUTPUT::-1}"
elif command_exists acpi;
elif _omb_util_command_exists acpi;
then
local ACPI_OUTPUT=$(acpi -b)
case $ACPI_OUTPUT in
Expand All @@ -91,7 +88,7 @@ battery_percentage(){
echo '-1'
;;
esac
elif command_exists pmset;
elif _omb_util_command_exists pmset;
then
local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p')
case $PMSET_OUTPUT in
Expand All @@ -102,7 +99,7 @@ battery_percentage(){
echo $PMSET_OUTPUT | head -c 2
;;
esac
elif command_exists ioreg;
elif _omb_util_command_exists ioreg;
then
local IOREG_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}')
case $IOREG_OUTPUT in
Expand All @@ -113,7 +110,7 @@ battery_percentage(){
echo $IOREG_OUTPUT | head -c 2
;;
esac
elif command_exists WMIC;
elif _omb_util_command_exists WMIC;
then
local WINPC=$(echo porcent=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List) | grep -o '[0-9]*')
case $WINPC in
Expand Down
2 changes: 1 addition & 1 deletion plugins/nvm/nvm.plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


# Try to load nvm only if command not already available
if ! type "nvm" &> /dev/null; then
if ! _omb_util_command_exists nvm; then
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
# This is done as part of completions!!!
# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Expand Down
Loading

0 comments on commit b9295c6

Please sign in to comment.