Skip to content

Consolidate preexec-related functions; Eliminate vendor/init.d #2003

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

Merged
merged 4 commits into from
Jan 26, 2022
Merged
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
9 changes: 0 additions & 9 deletions bash_it.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ for _bash_it_config_file in $LIB; do
fi
done

# Load vendors
BASH_IT_LOG_PREFIX="vendor: "
for _bash_it_vendor_init in "${BASH_IT}"/vendor/init.d/*.bash; do
_log_debug "Loading \"$(basename "${_bash_it_vendor_init}" .bash)\"..."
# shellcheck disable=SC1090
source "${_bash_it_vendor_init}"
done
unset _bash_it_vendor_init

BASH_IT_LOG_PREFIX="core: main: "
# Load the global "enabled" directory
# "family" param is empty so that files get sources in glob order
Expand Down
2 changes: 1 addition & 1 deletion clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ completion/available/wpscan.completion.bash
# libraries
lib/helpers.bash
lib/log.bash
lib/preexec.bash
lib/utilities.bash

# plugins
Expand Down Expand Up @@ -165,4 +166,3 @@ themes/purity
# vendor init files
#
vendor/.gitattributes
vendor/init.d
82 changes: 82 additions & 0 deletions lib/preexec.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# shellcheck shell=bash
# shellcheck disable=SC2034
#
# Load the `bash-preexec.sh` library, and define helper functions

## Prepare, load, fix, and install `bash-preexec.sh`
: "${PROMPT_COMMAND:=}"

# Disable immediate `$PROMPT_COMMAND` modification
__bp_delay_install="delayed"

# shellcheck source-path=SCRIPTDIR/../vendor/github.com/rcaloras/bash-preexec
source "${BASH_IT?}/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh"

# Block damanaging user's `$HISTCONTROL`
function __bp_adjust_histcontrol() { :; }

# Don't fail on readonly variables
function __bp_require_not_readonly() { :; }

# Disable trap DEBUG on subshells - https://github.com/Bash-it/bash-it/pull/1040
__bp_enable_subshells= # blank
set +T

# Modify `$PROMPT_COMMAND` now
__bp_install_after_session_init

## Helper functions
function __check_precmd_conflict() {
local f
__bp_trim_whitespace f "${1?}"
! _bash-it-array-contains-element "${f}" "${precmd_functions[@]}"
}

function __check_preexec_conflict() {
local f
__bp_trim_whitespace f "${1?}"
! _bash-it-array-contains-element "${f}" "${preexec_functions[@]}"
}

function safe_append_prompt_command {
local prompt_re f
__bp_trim_whitespace f "${1?}"

if [ "${__bp_imported:-missing}" == "defined" ]; then
# We are using bash-preexec
if ! __check_precmd_conflict "${f}"; then
precmd_functions+=("${f}")
fi
else
# Set OS dependent exact match regular expression
if [[ ${OSTYPE} == darwin* ]]; then
# macOS
prompt_re="[[:<:]]${1}[[:>:]]"
else
# Linux, FreeBSD, etc.
prompt_re="\<${1}\>"
fi

if [[ ${PROMPT_COMMAND} =~ ${prompt_re} ]]; then
return
elif [[ -z ${PROMPT_COMMAND} ]]; then
PROMPT_COMMAND="${1}"
else
PROMPT_COMMAND="${1};${PROMPT_COMMAND}"
fi
fi
}

function safe_append_preexec {
local prompt_re f
__bp_trim_whitespace f "${1?}"

if [ "${__bp_imported:-missing}" == "defined" ]; then
# We are using bash-preexec
if ! __check_preexec_conflict "${f}"; then
preexec_functions+=("${f}")
fi
else
_log_error "${FUNCNAME[0]}: can't append to preexec hook because _bash-preexec.sh_ hasn't been loaded"
fi
}
1 change: 0 additions & 1 deletion test/plugins/cmd-returned-notify.plugin.bats
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

load ../test_helper
load ../test_helper_libs
load ../../vendor/init.d/preexec

load ../../plugins/available/cmd-returned-notify.plugin

Expand Down
1 change: 1 addition & 0 deletions test/test_helper_libs.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ load "${BASH_IT}/lib/log.bash"
load "${BASH_IT}/lib/utilities.bash"
load "${BASH_IT}/lib/helpers.bash"
load "${BASH_IT}/lib/search.bash"
load "${BASH_IT}/lib/preexec.bash"
38 changes: 0 additions & 38 deletions themes/base.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -583,44 +583,6 @@ function aws_profile {
fi
}

function __check_precmd_conflict() {
local f
for f in "${precmd_functions[@]}"; do
if [[ "${f}" == "${1}" ]]; then
return 0
fi
done
return 1
}

function safe_append_prompt_command {
local prompt_re

if [ "${__bp_imported:-missing}" == "defined" ]; then
# We are using bash-preexec
if ! __check_precmd_conflict "${1}"; then
precmd_functions+=("${1}")
fi
else
# Set OS dependent exact match regular expression
if [[ ${OSTYPE} == darwin* ]]; then
# macOS
prompt_re="[[:<:]]${1}[[:>:]]"
else
# Linux, FreeBSD, etc.
prompt_re="\<${1}\>"
fi

if [[ ${PROMPT_COMMAND[*]:-} =~ ${prompt_re} ]]; then
return
elif [[ -z ${PROMPT_COMMAND} ]]; then
PROMPT_COMMAND="${1}"
else
PROMPT_COMMAND="${1};${PROMPT_COMMAND}"
fi
fi
}

function _save-and-reload-history() {
local autosave=${1:-0}
[[ $autosave -eq 1 ]] && history -a && history -c && history -r
Expand Down
25 changes: 0 additions & 25 deletions vendor/init.d/preexec.bash

This file was deleted.