commit
2388c74d84
13
bash_it.sh
13
bash_it.sh
|
|
@ -49,6 +49,17 @@ do
|
||||||
fi
|
fi
|
||||||
done
|
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
|
# Load the global "enabled" directory
|
||||||
# "family" param is empty so that files get sources in glob order
|
# "family" param is empty so that files get sources in glob order
|
||||||
# shellcheck source=./scripts/reloader.bash
|
# shellcheck source=./scripts/reloader.bash
|
||||||
|
|
@ -62,7 +73,7 @@ do
|
||||||
done
|
done
|
||||||
|
|
||||||
# Load theme, if a theme was set
|
# Load theme, if a theme was set
|
||||||
if [[ ! -z "${BASH_IT_THEME}" ]]; then
|
if [[ -n "${BASH_IT_THEME}" ]]; then
|
||||||
_log_debug "Loading \"${BASH_IT_THEME}\" theme..."
|
_log_debug "Loading \"${BASH_IT_THEME}\" theme..."
|
||||||
# Load colors and helpers first so they can be used in base theme
|
# Load colors and helpers first so they can be used in base theme
|
||||||
BASH_IT_LOG_PREFIX="themes: colors: "
|
BASH_IT_LOG_PREFIX="themes: colors: "
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,13 @@ themes/bobby
|
||||||
themes/bobby-python
|
themes/bobby-python
|
||||||
themes/brainy
|
themes/brainy
|
||||||
themes/brunton
|
themes/brunton
|
||||||
|
themes/command_duration.theme.bash
|
||||||
|
|
||||||
# plugins
|
# plugins
|
||||||
#
|
#
|
||||||
plugins/available/basher.plugin.bash
|
plugins/available/basher.plugin.bash
|
||||||
|
plugins/available/cmd-returned-notify.plugin.bash
|
||||||
|
plugins/available/xterm.plugin.bash
|
||||||
|
|
||||||
# completions
|
# completions
|
||||||
#
|
#
|
||||||
|
|
@ -67,3 +70,6 @@ completion/available/vuejs.completion.bash
|
||||||
aliases/available/dnf.aliases.bash
|
aliases/available/dnf.aliases.bash
|
||||||
aliases/available/vim.aliases.bash
|
aliases/available/vim.aliases.bash
|
||||||
aliases/available/git.aliases.bash
|
aliases/available/git.aliases.bash
|
||||||
|
|
||||||
|
# vendor init files
|
||||||
|
vendor/init.d
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ The main ``bash_it.sh`` script loads the frameworks individual components in the
|
||||||
|
|
||||||
|
|
||||||
* ``lib/composure.bash``
|
* ``lib/composure.bash``
|
||||||
|
* ``vendor/init.d/*.bash``
|
||||||
* Files in ``lib`` with the exception of ``appearance.bash`` - this means that ``composure.bash`` is loaded again here (possible improvement?)
|
* Files in ``lib`` with the exception of ``appearance.bash`` - this means that ``composure.bash`` is loaded again here (possible improvement?)
|
||||||
* Enabled ``aliases``
|
* Enabled ``aliases``
|
||||||
* Enabled ``plugins``
|
* Enabled ``plugins``
|
||||||
|
|
@ -78,6 +79,65 @@ Having the order based on a numeric priority in a common directory allows for mo
|
||||||
|
|
||||||
These items are subject to change. When making changes to the internal functionality, this page needs to be updated as well.
|
These items are subject to change. When making changes to the internal functionality, this page needs to be updated as well.
|
||||||
|
|
||||||
|
Working with vendored libs
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
Vendored libs are external libraries, meaning source code not maintained by Bash-it
|
||||||
|
developers.
|
||||||
|
They are ``git subtrees`` curated in the ``vendor/`` folder. To ease the work with git
|
||||||
|
vendored libs as subtrees we use the `git-vendor <https://github.com/Tyrben/git-vendor>`_ tool.
|
||||||
|
The `original repo <https://github.com/brettlangdon/git-vendor>`_ for git vendor is
|
||||||
|
unmaintained so for now we are recommending Tyrben's fork.
|
||||||
|
|
||||||
|
For more information on ``git vendor`` there are a short `usage description <https://github.com/Tyrben/git-vendor#usage>`_
|
||||||
|
in the repositories ``README`` file and a website for the original repository has a `manual page <https://brettlangdon.github.io/git-vendor/>`_ which is also included in both
|
||||||
|
repositories.
|
||||||
|
|
||||||
|
To support a flexible loading of external libraries, a file unique to the vendored
|
||||||
|
library must be placed in ``vendor/init.d/`` with the ``.bash`` extension.
|
||||||
|
|
||||||
|
Rebasing a feature branch with an added/updated vendored library
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
If your feature branch with a newly added/updated vendored lib has fallen behind master
|
||||||
|
you might need to rebase it before creating a PR. However rebasing with dangling
|
||||||
|
subtree commits can cause problems.
|
||||||
|
The following rebase strategy will pause the rebase at the point where you added a
|
||||||
|
subtree and let you add it again before continuing the rebasing.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
[feature/branch] $ git rebase --rebase-merges --strategy subtree master
|
||||||
|
fatal: refusing to merge unrelated histories
|
||||||
|
Could not apply 0d6a56b... Add-preexec-from-https-github-com-rcaloras-bash-preexec-0-4-1- # Add "preexec" from "https://github.com/rcaloras/bash-preexec@0.4.1"
|
||||||
|
[feature/branch] $ git vendor add preexec https://github.com/rcaloras/bash-preexec 0.4.1
|
||||||
|
...
|
||||||
|
[feature/branch] $ git rebase --continue
|
||||||
|
|
||||||
|
If rebasing makes you a little uneasy (as it probably should). You can always test in
|
||||||
|
another branch.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
[feater/branch] $ git checkout -b feature/branch-test-rebase
|
||||||
|
[feater/branch-test-rebase] $ git rebase --rebase-merges --strategy subtree master
|
||||||
|
...
|
||||||
|
|
||||||
|
Afterwards you can make sure the rebase was successful by running ``git vendor list``
|
||||||
|
to see if your library is still recognized as a vendored lib
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
[feature/branch] $ git vendor list
|
||||||
|
preexec@0.4.1:
|
||||||
|
name: preexec
|
||||||
|
dir: vendor/github.com/rcaloras/bash-preexec
|
||||||
|
repo: https://github.com/rcaloras/bash-preexec
|
||||||
|
ref: 0.4.1
|
||||||
|
commit: 8fe585c5cf377a3830b895fe26e694b020d8db1a
|
||||||
|
[feature/branch] $
|
||||||
|
|
||||||
|
|
||||||
Plugin Disable Callbacks
|
Plugin Disable Callbacks
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
|
|
|
||||||
199
lib/preexec.bash
199
lib/preexec.bash
|
|
@ -1,199 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
# http://www.twistedmatrix.com/users/glyph/preexec.bash.txt
|
|
||||||
# preexec.bash -- Bash support for ZSH-like 'preexec' and 'precmd' functions.
|
|
||||||
|
|
||||||
# The 'preexec' function is executed before each interactive command is
|
|
||||||
# executed, with the interactive command as its argument. The 'precmd'
|
|
||||||
# function is executed before each prompt is displayed.
|
|
||||||
|
|
||||||
# To use, in order:
|
|
||||||
|
|
||||||
# 1. source this file
|
|
||||||
# 2. define 'preexec' and/or 'precmd' functions (AFTER sourcing this file),
|
|
||||||
# 3. as near as possible to the end of your shell setup, run 'preexec_install'
|
|
||||||
# to kick everything off.
|
|
||||||
|
|
||||||
# Note: this module requires 2 bash features which you must not otherwise be
|
|
||||||
# using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. preexec_install
|
|
||||||
# will override these and if you override one or the other this _will_ break.
|
|
||||||
|
|
||||||
# This is known to support bash3, as well as *mostly* support bash2.05b. It
|
|
||||||
# has been tested with the default shells on MacOS X 10.4 "Tiger", Ubuntu 5.10
|
|
||||||
# "Breezy Badger", Ubuntu 6.06 "Dapper Drake", and Ubuntu 6.10 "Edgy Eft".
|
|
||||||
|
|
||||||
|
|
||||||
# Copy screen-run variables from the remote host, if they're available.
|
|
||||||
|
|
||||||
if [[ "$SCREEN_RUN_HOST" == "" ]]
|
|
||||||
then
|
|
||||||
SCREEN_RUN_HOST="$LC_SCREEN_RUN_HOST"
|
|
||||||
SCREEN_RUN_USER="$LC_SCREEN_RUN_USER"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# This variable describes whether we are currently in "interactive mode";
|
|
||||||
# i.e. whether this shell has just executed a prompt and is waiting for user
|
|
||||||
# input. It documents whether the current command invoked by the trace hook is
|
|
||||||
# run interactively by the user; it's set immediately after the prompt hook,
|
|
||||||
# and unset as soon as the trace hook is run.
|
|
||||||
preexec_interactive_mode=""
|
|
||||||
|
|
||||||
# Default do-nothing implementation of preexec.
|
|
||||||
function preexec () {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
# Default do-nothing implementation of precmd.
|
|
||||||
function precmd () {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
# This function is installed as the PROMPT_COMMAND; it is invoked before each
|
|
||||||
# interactive prompt display. It sets a variable to indicate that the prompt
|
|
||||||
# was just displayed, to allow the DEBUG trap, below, to know that the next
|
|
||||||
# command is likely interactive.
|
|
||||||
function preexec_invoke_cmd () {
|
|
||||||
precmd
|
|
||||||
preexec_interactive_mode="yes"
|
|
||||||
}
|
|
||||||
|
|
||||||
# This function is installed as the DEBUG trap. It is invoked before each
|
|
||||||
# interactive prompt display. Its purpose is to inspect the current
|
|
||||||
# environment to attempt to detect if the current command is being invoked
|
|
||||||
# interactively, and invoke 'preexec' if so.
|
|
||||||
function preexec_invoke_exec () {
|
|
||||||
if [[ -n "$COMP_LINE" ]]
|
|
||||||
then
|
|
||||||
# We're in the middle of a completer. This obviously can't be
|
|
||||||
# an interactively issued command.
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
if [[ -z "$preexec_interactive_mode" ]]
|
|
||||||
then
|
|
||||||
# We're doing something related to displaying the prompt. Let the
|
|
||||||
# prompt set the title instead of me.
|
|
||||||
return
|
|
||||||
else
|
|
||||||
# If we're in a subshell, then the prompt won't be re-displayed to put
|
|
||||||
# us back into interactive mode, so let's not set the variable back.
|
|
||||||
# In other words, if you have a subshell like
|
|
||||||
# (sleep 1; sleep 2)
|
|
||||||
# You want to see the 'sleep 2' as a set_command_title as well.
|
|
||||||
if [[ 0 -eq "$BASH_SUBSHELL" ]]
|
|
||||||
then
|
|
||||||
preexec_interactive_mode=""
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
if [[ "preexec_invoke_cmd" == "$BASH_COMMAND" ]]
|
|
||||||
then
|
|
||||||
# Sadly, there's no cleaner way to detect two prompts being displayed
|
|
||||||
# one after another. This makes it important that PROMPT_COMMAND
|
|
||||||
# remain set _exactly_ as below in preexec_install. Let's switch back
|
|
||||||
# out of interactive mode and not trace any of the commands run in
|
|
||||||
# precmd.
|
|
||||||
|
|
||||||
# Given their buggy interaction between BASH_COMMAND and debug traps,
|
|
||||||
# versions of bash prior to 3.1 can't detect this at all.
|
|
||||||
preexec_interactive_mode=""
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
# In more recent versions of bash, this could be set via the "BASH_COMMAND"
|
|
||||||
# variable, but using history here is better in some ways: for example, "ps
|
|
||||||
# auxf | less" will show up with both sides of the pipe if we use history,
|
|
||||||
# but only as "ps auxf" if not.
|
|
||||||
local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`;
|
|
||||||
|
|
||||||
# If none of the previous checks have earlied out of this function, then
|
|
||||||
# the command is in fact interactive and we should invoke the user's
|
|
||||||
# preexec hook with the running command as an argument.
|
|
||||||
preexec "$this_command"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Execute this to set up preexec and precmd execution.
|
|
||||||
function preexec_install () {
|
|
||||||
|
|
||||||
# *BOTH* of these options need to be set for the DEBUG trap to be invoked
|
|
||||||
# in ( ) subshells. This smells like a bug in bash to me. The null stderr
|
|
||||||
# redirections are to quiet errors on bash2.05 (i.e. OSX's default shell)
|
|
||||||
# where the options can't be set, and it's impossible to inherit the trap
|
|
||||||
# into subshells.
|
|
||||||
|
|
||||||
set -o functrace > /dev/null 2>&1
|
|
||||||
shopt -s extdebug > /dev/null 2>&1
|
|
||||||
|
|
||||||
# Finally, install the actual traps.
|
|
||||||
if [[ ! -z "${PROMPT_COMMAND// }" ]]; then
|
|
||||||
PROMPT_COMMAND="${PROMPT_COMMAND}"$'\n'"preexec_invoke_cmd"
|
|
||||||
else
|
|
||||||
PROMPT_COMMAND="preexec_invoke_cmd"
|
|
||||||
fi
|
|
||||||
trap 'preexec_invoke_exec' DEBUG
|
|
||||||
}
|
|
||||||
|
|
||||||
# Since this is the reason that 99% of everybody is going to bother with a
|
|
||||||
# pre-exec hook anyway, we'll include it in this module.
|
|
||||||
|
|
||||||
# Change the title of the xterm.
|
|
||||||
function preexec_xterm_title () {
|
|
||||||
local title="$1"
|
|
||||||
echo -ne "\033]0;$title\007" > /dev/stderr
|
|
||||||
}
|
|
||||||
|
|
||||||
function preexec_screen_title () {
|
|
||||||
local title="$1"
|
|
||||||
echo -ne "\033k$1\033\\" > /dev/stderr
|
|
||||||
}
|
|
||||||
|
|
||||||
# Abbreviate the "user@host" string as much as possible to preserve space in
|
|
||||||
# screen titles. Elide the host if the host is the same, elide the user if the
|
|
||||||
# user is the same.
|
|
||||||
function preexec_screen_user_at_host () {
|
|
||||||
local RESULT=""
|
|
||||||
if [[ "$SCREEN_RUN_HOST" == "$SCREEN_HOST" ]]
|
|
||||||
then
|
|
||||||
return
|
|
||||||
else
|
|
||||||
if [[ "$SCREEN_RUN_USER" == "$USER" ]]
|
|
||||||
then
|
|
||||||
echo -n "@${SCREEN_HOST}"
|
|
||||||
else
|
|
||||||
echo -n "${USER}@${SCREEN_HOST}"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
function preexec_xterm_title_install () {
|
|
||||||
# These functions are defined here because they only make sense with the
|
|
||||||
# preexec_install below.
|
|
||||||
function precmd () {
|
|
||||||
preexec_xterm_title "${TERM} - ${USER}@${SCREEN_HOST} `dirs -0` $PROMPTCHAR"
|
|
||||||
if [[ "${TERM}" == screen ]]
|
|
||||||
then
|
|
||||||
preexec_screen_title "`preexec_screen_user_at_host`${PROMPTCHAR}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
function preexec () {
|
|
||||||
preexec_xterm_title "${TERM} - $1 {`dirs -0`} (${USER}@${SCREEN_HOST})"
|
|
||||||
if [[ "${TERM}" == screen ]]
|
|
||||||
then
|
|
||||||
local cutit="$1"
|
|
||||||
local cmdtitle=`echo "$cutit" | cut -d " " -f 1`
|
|
||||||
if [[ "$cmdtitle" == "exec" ]]
|
|
||||||
then
|
|
||||||
local cmdtitle=`echo "$cutit" | cut -d " " -f 2`
|
|
||||||
fi
|
|
||||||
if [[ "$cmdtitle" == "screen" ]]
|
|
||||||
then
|
|
||||||
# Since stacked screens are quite common, it would be nice to
|
|
||||||
# just display them as '$$'.
|
|
||||||
local cmdtitle="${PROMPTCHAR}"
|
|
||||||
else
|
|
||||||
local cmdtitle=":$cmdtitle"
|
|
||||||
fi
|
|
||||||
preexec_screen_title "`preexec_screen_user_at_host`${PROMPTCHAR}$cmdtitle"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
preexec_install
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
# shellcheck shell=bash
|
||||||
|
cite about-plugin
|
||||||
|
about-plugin 'Alert (BEL) when process ends after a threshold of seconds'
|
||||||
|
|
||||||
|
precmd_return_notification() {
|
||||||
|
export LAST_COMMAND_DURATION=$(($(date +%s) - ${LAST_COMMAND_TIME:=$(date +%s)}))
|
||||||
|
[[ ${LAST_COMMAND_DURATION} -gt ${NOTIFY_IF_COMMAND_RETURNS_AFTER:-5} ]] && echo -e "\a"
|
||||||
|
export LAST_COMMAND_TIME=
|
||||||
|
}
|
||||||
|
|
||||||
|
preexec_return_notification() {
|
||||||
|
[ -z "${LAST_COMMAND_TIME}" ] && export LAST_COMMAND_TIME=$(date +%s)
|
||||||
|
}
|
||||||
|
|
||||||
|
precmd_functions+=(precmd_return_notification)
|
||||||
|
preexec_functions+=(preexec_return_notification)
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
|
# shellcheck shell=bash
|
||||||
cite about-plugin
|
cite about-plugin
|
||||||
about-plugin 'automatically set your xterm title with host and location info'
|
about-plugin 'automatically set your xterm title with host and location info'
|
||||||
|
|
||||||
|
|
||||||
_short-dirname() {
|
_short-dirname() {
|
||||||
local dir_name=`dirs +0`
|
local dir_name=$(dirs +0)
|
||||||
[ "$SHORT_TERM_LINE" = true ] && [ ${#dir_name} -gt 8 ] && echo ${dir_name##*/} || echo $dir_name
|
[ "$SHORT_TERM_LINE" = true ] && [ "${#dir_name}" -gt 8 ] && echo "${dir_name##*/}" || echo "${dir_name}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_short-command() {
|
_short-command() {
|
||||||
local input_command="$@"
|
local input_command="$*"
|
||||||
[ "$SHORT_TERM_LINE" = true ] && [ ${#input_command} -gt 8 ] && echo ${input_command%% *} || echo $input_command
|
[ "$SHORT_TERM_LINE" = true ] && [ "${#input_command}" -gt 8 ] && echo "${input_command%% *}" || echo "${input_command}"
|
||||||
}
|
}
|
||||||
|
|
||||||
set_xterm_title() {
|
set_xterm_title() {
|
||||||
|
|
@ -17,14 +17,17 @@ set_xterm_title () {
|
||||||
echo -ne "\033]0;$title\007"
|
echo -ne "\033]0;$title\007"
|
||||||
}
|
}
|
||||||
|
|
||||||
precmd () {
|
precmd_xterm_title() {
|
||||||
set_xterm_title "${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}} `_short-dirname` $PROMPTCHAR"
|
set_xterm_title "${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}} $(_short-dirname) $PROMPT_CHAR"
|
||||||
}
|
}
|
||||||
|
|
||||||
preexec () {
|
preexec_xterm_title() {
|
||||||
set_xterm_title "`_short-command $1` {`_short-dirname`} (${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}})"
|
set_xterm_title "$(_short-command "${1}") {$(_short-dirname)} (${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}})"
|
||||||
}
|
}
|
||||||
|
|
||||||
case "$TERM" in
|
case "$TERM" in
|
||||||
xterm*|rxvt*) preexec_install;;
|
xterm* | rxvt*)
|
||||||
|
precmd_functions+=(precmd_xterm_title)
|
||||||
|
preexec_functions+=(preexec_xterm_title)
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
load ../test_helper
|
||||||
|
load ../../lib/helpers
|
||||||
|
load ../../lib/composure
|
||||||
|
|
||||||
|
load ../../plugins/available/cmd-returned-notify.plugin
|
||||||
|
|
||||||
|
@test "plugins cmd-returned-notify: notify after elapsed time" {
|
||||||
|
export NOTIFY_IF_COMMAND_RETURNS_AFTER=0
|
||||||
|
export LAST_COMMAND_TIME=$(date +%s)
|
||||||
|
sleep 1
|
||||||
|
run precmd_return_notification
|
||||||
|
assert_success
|
||||||
|
assert_output $'\a'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins cmd-returned-notify: do not notify before elapsed time" {
|
||||||
|
export NOTIFY_IF_COMMAND_RETURNS_AFTER=10
|
||||||
|
export LAST_COMMAND_TIME=$(date +%s)
|
||||||
|
sleep 1
|
||||||
|
run precmd_return_notification
|
||||||
|
assert_success
|
||||||
|
assert_output $''
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins cmd-returned-notify: preexec no output" {
|
||||||
|
export LAST_COMMAND_TIME=
|
||||||
|
run preexec_return_notification
|
||||||
|
assert_success
|
||||||
|
assert_output ""
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins cmd-returned-notify: preexec no output env set" {
|
||||||
|
export LAST_COMMAND_TIME=$(date +%s)
|
||||||
|
run preexec_return_notification
|
||||||
|
assert_failure
|
||||||
|
assert_output ""
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins cmd-returned-notify: preexec set LAST_COMMAND_TIME" {
|
||||||
|
export LAST_COMMAND_TIME=
|
||||||
|
assert_equal "${LAST_COMMAND_TIME}" ""
|
||||||
|
NOW=$(date +%s)
|
||||||
|
preexec_return_notification
|
||||||
|
assert_equal "${LAST_COMMAND_TIME}" "${NOW}"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
load ../test_helper
|
||||||
|
load ../../lib/helpers
|
||||||
|
load ../../lib/composure
|
||||||
|
|
||||||
|
load ../../plugins/available/xterm.plugin
|
||||||
|
|
||||||
|
function local_setup {
|
||||||
|
setup_test_fixture
|
||||||
|
|
||||||
|
# Copy the test fixture to the Bash-it folder
|
||||||
|
if _command_exists rsync; then
|
||||||
|
rsync -a "$BASH_IT/test/fixtures/plugin/xterm/" "$BASH_IT/"
|
||||||
|
else
|
||||||
|
find "$BASH_IT/test/fixtures/plugin/xterm" \
|
||||||
|
-mindepth 1 -maxdepth 1 \
|
||||||
|
-exec cp -r {} "$BASH_IT/" \;
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins xterm: shorten command output" {
|
||||||
|
export SHORT_TERM_LINE=true
|
||||||
|
run _short-command ${BASH_IT}/test/fixtures/plugin/xterm/files/*
|
||||||
|
assert_success
|
||||||
|
assert_output ${BASH_IT}/test/fixtures/plugin/xterm/files/arg0
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins xterm: full command output" {
|
||||||
|
export SHORT_TERM_LINE=false
|
||||||
|
run _short-command ${BASH_IT}/test/fixtures/plugin/xterm/files/*
|
||||||
|
assert_success
|
||||||
|
assert_output "$(echo ${BASH_IT}/test/fixtures/plugin/xterm/files/*)"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins xterm: shorten dirname output" {
|
||||||
|
export SHORT_TERM_LINE=true
|
||||||
|
run _short-dirname
|
||||||
|
assert_success
|
||||||
|
assert_output "$(basename $PWD)"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins xterm: full dirname output" {
|
||||||
|
export SHORT_TERM_LINE=false
|
||||||
|
run _short-dirname
|
||||||
|
assert_success
|
||||||
|
assert_output $PWD
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plugins xterm: set xterm title" {
|
||||||
|
run set_xterm_title title
|
||||||
|
assert_success
|
||||||
|
assert_output $'\033]0;title\007'
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env bash
|
# shellcheck shell=bash
|
||||||
|
|
||||||
if [ -z "$BASH_IT_COMMAND_DURATION" ] || [ "$BASH_IT_COMMAND_DURATION" != true ]; then
|
if [ -z "$BASH_IT_COMMAND_DURATION" ] || [ "$BASH_IT_COMMAND_DURATION" != true ]; then
|
||||||
_command_duration() {
|
_command_duration() {
|
||||||
|
|
@ -66,8 +66,4 @@ _command_duration() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
preexec() (
|
preexec_functions+=(_command_duration_pre_exec)
|
||||||
_command_duration_pre_exec
|
|
||||||
)
|
|
||||||
|
|
||||||
preexec_install
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
language: bash
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
# To install bats and test our shell/bash functions
|
||||||
|
- git clone -b "v1.1.0" "https://github.com/bats-core/bats-core.git"
|
||||||
|
- sudo ./bats-core/install.sh /usr/local
|
||||||
|
- rm -rf ./bats-core
|
||||||
|
- sudo apt-get install -qq zsh
|
||||||
|
|
||||||
|
# For bats functional tests
|
||||||
|
env:
|
||||||
|
- functional_test="true"
|
||||||
|
|
||||||
|
# command to run tests
|
||||||
|
script:
|
||||||
|
- /usr/local/bin/bats test
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
on_success: never
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2017 Ryan Caloras and contributors (see https://github.com/rcaloras/bash-preexec)
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
@ -0,0 +1,107 @@
|
||||||
|
[](https://travis-ci.org/rcaloras/bash-preexec)
|
||||||
|
[](https://badge.fury.io/gh/rcaloras%2Fbash-preexec)
|
||||||
|
|
||||||
|
Bash-Preexec
|
||||||
|
============
|
||||||
|
|
||||||
|
**preexec** and **precmd** hook functions for Bash in the style of Zsh. They aim to emulate the behavior [as described for Zsh](http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions).
|
||||||
|
|
||||||
|
<a href="https://bashhub.com" target="_blank"><img src="https://bashhub.com/assets/images/bashhub-logo.png" alt="Bashhub Logo" width="200"></a>
|
||||||
|
|
||||||
|
This project is currently being used in production by [Bashhub](https://github.com/rcaloras/bashhub-client) and [iTerm2](https://github.com/gnachman/iTerm2). Hype!
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
```bash
|
||||||
|
# Pull down our file from GitHub and write it to our home directory as a hidden file.
|
||||||
|
curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
|
||||||
|
# Source our file to bring it into our environment
|
||||||
|
source ~/.bash-preexec.sh
|
||||||
|
# Define a couple functions.
|
||||||
|
preexec() { echo "just typed $1"; }
|
||||||
|
precmd() { echo "printing the prompt"; }
|
||||||
|
```
|
||||||
|
|
||||||
|
## Install
|
||||||
|
You'll want to pull down the file and add it to your bash profile/configuration (i.e ~/.bashrc, ~/.profile, ~/.bash_profile, etc). **It must be the last thing imported in your bash profile.**
|
||||||
|
```bash
|
||||||
|
# Pull down our file from GitHub and write it to our home directory as a hidden file.
|
||||||
|
curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
|
||||||
|
# Source our file at the end of our bash profile (e.g. ~/.bashrc, ~/.profile, or ~/.bash_profile)
|
||||||
|
echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
Two functions **preexec** and **precmd** can now be defined and they'll be automatically invoked by bash-preexec if they exist.
|
||||||
|
|
||||||
|
* `preexec` Executed just after a command has been read and is about to be executed. The string that the user typed is passed as the first argument.
|
||||||
|
* `precmd` Executed just before each prompt. Equivalent to PROMPT_COMMAND, but more flexible and resilient.
|
||||||
|
```bash
|
||||||
|
source ~/.bash-preexec.sh
|
||||||
|
preexec() { echo "just typed $1"; }
|
||||||
|
precmd() { echo "printing the prompt"; }
|
||||||
|
```
|
||||||
|
Should output something like:
|
||||||
|
```
|
||||||
|
elementz@Kashmir:~/git/bash-preexec (master)$ ls
|
||||||
|
just typed ls
|
||||||
|
bash-preexec.sh README.md test
|
||||||
|
printing the prompt
|
||||||
|
```
|
||||||
|
#### Function Arrays
|
||||||
|
You can also define functions to be invoked by appending them to two different arrays. This is great if you want to have many functions invoked for either hook. Both preexec and precmd functions are added to these by default and don't need to be added manually.
|
||||||
|
* `$preexec_functions` Array of functions invoked by preexec.
|
||||||
|
* `$precmd_functions` Array of functions invoked by precmd.
|
||||||
|
|
||||||
|
#### preexec
|
||||||
|
```bash
|
||||||
|
# Define some function to use preexec
|
||||||
|
preexec_hello_world() { echo "You just entered $1"; }
|
||||||
|
# Add it to the array of functions to be invoked each time.
|
||||||
|
preexec_functions+=(preexec_hello_world)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### precmd
|
||||||
|
```bash
|
||||||
|
precmd_hello_world() { echo "This is invoked before the prompt is displayed"; }
|
||||||
|
precmd_functions+=(precmd_hello_world)
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also define multiple functions to be invoked like so.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
precmd_hello_one() { echo "This is invoked on precmd first"; }
|
||||||
|
precmd_hello_two() { echo "This is invoked on precmd second"; }
|
||||||
|
precmd_functions+=(precmd_hello_one)
|
||||||
|
precmd_functions+=(precmd_hello_two)
|
||||||
|
```
|
||||||
|
|
||||||
|
You can check the functions set for each by echoing its contents.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
echo ${preexec_functions[@]}
|
||||||
|
echo ${precmd_functions[@]}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Subshells
|
||||||
|
bash-preexec does not support invoking preexec() for subshells by default. It must be enabled by setting
|
||||||
|
`__bp_enable_subshells`.
|
||||||
|
```bash
|
||||||
|
# Enable experimental subshell support
|
||||||
|
export __bp_enable_subshells="true"
|
||||||
|
```
|
||||||
|
This is disabled by default due to buggy situations related to to `functrace` and Bash's `DEBUG trap`. See [Issue #25](https://github.com/rcaloras/bash-preexec/issues/25)
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
You can run tests using [Bats](https://github.com/bats-core/bats-core).
|
||||||
|
```bash
|
||||||
|
bats test
|
||||||
|
```
|
||||||
|
Should output something like:
|
||||||
|
```
|
||||||
|
elementz@Kashmir:~/git/bash-preexec(master)$ bats test
|
||||||
|
✓ No functions defined for preexec should simply return
|
||||||
|
✓ precmd should execute a function once
|
||||||
|
✓ preexec should execute a function with the last command in our history
|
||||||
|
✓ preexec should execute multiple functions in the order added to their arrays
|
||||||
|
✓ preecmd should execute multiple functions in the order added to their arrays
|
||||||
|
```
|
||||||
|
|
@ -0,0 +1,341 @@
|
||||||
|
# bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions.
|
||||||
|
# https://github.com/rcaloras/bash-preexec
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# 'preexec' functions are executed before each interactive command is
|
||||||
|
# executed, with the interactive command as its argument. The 'precmd'
|
||||||
|
# function is executed before each prompt is displayed.
|
||||||
|
#
|
||||||
|
# Author: Ryan Caloras (ryan@bashhub.com)
|
||||||
|
# Forked from Original Author: Glyph Lefkowitz
|
||||||
|
#
|
||||||
|
# V0.4.1
|
||||||
|
#
|
||||||
|
|
||||||
|
# General Usage:
|
||||||
|
#
|
||||||
|
# 1. Source this file at the end of your bash profile so as not to interfere
|
||||||
|
# with anything else that's using PROMPT_COMMAND.
|
||||||
|
#
|
||||||
|
# 2. Add any precmd or preexec functions by appending them to their arrays:
|
||||||
|
# e.g.
|
||||||
|
# precmd_functions+=(my_precmd_function)
|
||||||
|
# precmd_functions+=(some_other_precmd_function)
|
||||||
|
#
|
||||||
|
# preexec_functions+=(my_preexec_function)
|
||||||
|
#
|
||||||
|
# 3. Consider changing anything using the DEBUG trap or PROMPT_COMMAND
|
||||||
|
# to use preexec and precmd instead. Preexisting usages will be
|
||||||
|
# preserved, but doing so manually may be less surprising.
|
||||||
|
#
|
||||||
|
# Note: This module requires two Bash features which you must not otherwise be
|
||||||
|
# using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. If you override
|
||||||
|
# either of these after bash-preexec has been installed it will most likely break.
|
||||||
|
|
||||||
|
# Avoid duplicate inclusion
|
||||||
|
if [[ "${__bp_imported:-}" == "defined" ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
__bp_imported="defined"
|
||||||
|
|
||||||
|
# Should be available to each precmd and preexec
|
||||||
|
# functions, should they want it. $? and $_ are available as $? and $_, but
|
||||||
|
# $PIPESTATUS is available only in a copy, $BP_PIPESTATUS.
|
||||||
|
# TODO: Figure out how to restore PIPESTATUS before each precmd or preexec
|
||||||
|
# function.
|
||||||
|
__bp_last_ret_value="$?"
|
||||||
|
BP_PIPESTATUS=("${PIPESTATUS[@]}")
|
||||||
|
__bp_last_argument_prev_command="$_"
|
||||||
|
|
||||||
|
__bp_inside_precmd=0
|
||||||
|
__bp_inside_preexec=0
|
||||||
|
|
||||||
|
# Initial PROMPT_COMMAND string that is removed from PROMPT_COMMAND post __bp_install
|
||||||
|
__bp_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'
|
||||||
|
|
||||||
|
# Fails if any of the given variables are readonly
|
||||||
|
# Reference https://stackoverflow.com/a/4441178
|
||||||
|
__bp_require_not_readonly() {
|
||||||
|
local var
|
||||||
|
for var; do
|
||||||
|
if ! ( unset "$var" 2> /dev/null ); then
|
||||||
|
echo "bash-preexec requires write access to ${var}" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# Remove ignorespace and or replace ignoreboth from HISTCONTROL
|
||||||
|
# so we can accurately invoke preexec with a command from our
|
||||||
|
# history even if it starts with a space.
|
||||||
|
__bp_adjust_histcontrol() {
|
||||||
|
local histcontrol
|
||||||
|
histcontrol="${HISTCONTROL//ignorespace}"
|
||||||
|
# Replace ignoreboth with ignoredups
|
||||||
|
if [[ "$histcontrol" == *"ignoreboth"* ]]; then
|
||||||
|
histcontrol="ignoredups:${histcontrol//ignoreboth}"
|
||||||
|
fi;
|
||||||
|
export HISTCONTROL="$histcontrol"
|
||||||
|
}
|
||||||
|
|
||||||
|
# This variable describes whether we are currently in "interactive mode";
|
||||||
|
# i.e. whether this shell has just executed a prompt and is waiting for user
|
||||||
|
# input. It documents whether the current command invoked by the trace hook is
|
||||||
|
# run interactively by the user; it's set immediately after the prompt hook,
|
||||||
|
# and unset as soon as the trace hook is run.
|
||||||
|
__bp_preexec_interactive_mode=""
|
||||||
|
|
||||||
|
# Trims leading and trailing whitespace from $2 and writes it to the variable
|
||||||
|
# name passed as $1
|
||||||
|
__bp_trim_whitespace() {
|
||||||
|
local var=${1:?} text=${2:-}
|
||||||
|
text="${text#"${text%%[![:space:]]*}"}" # remove leading whitespace characters
|
||||||
|
text="${text%"${text##*[![:space:]]}"}" # remove trailing whitespace characters
|
||||||
|
printf -v "$var" '%s' "$text"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Trims whitespace and removes any leading or trailing semicolons from $2 and
|
||||||
|
# writes the resulting string to the variable name passed as $1. Used for
|
||||||
|
# manipulating substrings in PROMPT_COMMAND
|
||||||
|
__bp_sanitize_string() {
|
||||||
|
local var=${1:?} text=${2:-} sanitized
|
||||||
|
__bp_trim_whitespace sanitized "$text"
|
||||||
|
sanitized=${sanitized%;}
|
||||||
|
sanitized=${sanitized#;}
|
||||||
|
__bp_trim_whitespace sanitized "$sanitized"
|
||||||
|
printf -v "$var" '%s' "$sanitized"
|
||||||
|
}
|
||||||
|
|
||||||
|
# This function is installed as part of the PROMPT_COMMAND;
|
||||||
|
# It sets a variable to indicate that the prompt was just displayed,
|
||||||
|
# to allow the DEBUG trap to know that the next command is likely interactive.
|
||||||
|
__bp_interactive_mode() {
|
||||||
|
__bp_preexec_interactive_mode="on";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# This function is installed as part of the PROMPT_COMMAND.
|
||||||
|
# It will invoke any functions defined in the precmd_functions array.
|
||||||
|
__bp_precmd_invoke_cmd() {
|
||||||
|
# Save the returned value from our last command, and from each process in
|
||||||
|
# its pipeline. Note: this MUST be the first thing done in this function.
|
||||||
|
__bp_last_ret_value="$?" BP_PIPESTATUS=("${PIPESTATUS[@]}")
|
||||||
|
|
||||||
|
# Don't invoke precmds if we are inside an execution of an "original
|
||||||
|
# prompt command" by another precmd execution loop. This avoids infinite
|
||||||
|
# recursion.
|
||||||
|
if (( __bp_inside_precmd > 0 )); then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
local __bp_inside_precmd=1
|
||||||
|
|
||||||
|
# Invoke every function defined in our function array.
|
||||||
|
local precmd_function
|
||||||
|
for precmd_function in "${precmd_functions[@]}"; do
|
||||||
|
|
||||||
|
# Only execute this function if it actually exists.
|
||||||
|
# Test existence of functions with: declare -[Ff]
|
||||||
|
if type -t "$precmd_function" 1>/dev/null; then
|
||||||
|
__bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
|
||||||
|
# Quote our function invocation to prevent issues with IFS
|
||||||
|
"$precmd_function"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# Sets a return value in $?. We may want to get access to the $? variable in our
|
||||||
|
# precmd functions. This is available for instance in zsh. We can simulate it in bash
|
||||||
|
# by setting the value here.
|
||||||
|
__bp_set_ret_value() {
|
||||||
|
return ${1:-}
|
||||||
|
}
|
||||||
|
|
||||||
|
__bp_in_prompt_command() {
|
||||||
|
|
||||||
|
local prompt_command_array
|
||||||
|
IFS=$'\n;' read -rd '' -a prompt_command_array <<< "$PROMPT_COMMAND"
|
||||||
|
|
||||||
|
local trimmed_arg
|
||||||
|
__bp_trim_whitespace trimmed_arg "${1:-}"
|
||||||
|
|
||||||
|
local command trimmed_command
|
||||||
|
for command in "${prompt_command_array[@]:-}"; do
|
||||||
|
__bp_trim_whitespace trimmed_command "$command"
|
||||||
|
if [[ "$trimmed_command" == "$trimmed_arg" ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# This function is installed as the DEBUG trap. It is invoked before each
|
||||||
|
# interactive prompt display. Its purpose is to inspect the current
|
||||||
|
# environment to attempt to detect if the current command is being invoked
|
||||||
|
# interactively, and invoke 'preexec' if so.
|
||||||
|
__bp_preexec_invoke_exec() {
|
||||||
|
|
||||||
|
# Save the contents of $_ so that it can be restored later on.
|
||||||
|
# https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap#40944702
|
||||||
|
__bp_last_argument_prev_command="${1:-}"
|
||||||
|
# Don't invoke preexecs if we are inside of another preexec.
|
||||||
|
if (( __bp_inside_preexec > 0 )); then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
local __bp_inside_preexec=1
|
||||||
|
|
||||||
|
# Checks if the file descriptor is not standard out (i.e. '1')
|
||||||
|
# __bp_delay_install checks if we're in test. Needed for bats to run.
|
||||||
|
# Prevents preexec from being invoked for functions in PS1
|
||||||
|
if [[ ! -t 1 && -z "${__bp_delay_install:-}" ]]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -n "${COMP_LINE:-}" ]]; then
|
||||||
|
# We're in the middle of a completer. This obviously can't be
|
||||||
|
# an interactively issued command.
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
if [[ -z "${__bp_preexec_interactive_mode:-}" ]]; then
|
||||||
|
# We're doing something related to displaying the prompt. Let the
|
||||||
|
# prompt set the title instead of me.
|
||||||
|
return
|
||||||
|
else
|
||||||
|
# If we're in a subshell, then the prompt won't be re-displayed to put
|
||||||
|
# us back into interactive mode, so let's not set the variable back.
|
||||||
|
# In other words, if you have a subshell like
|
||||||
|
# (sleep 1; sleep 2)
|
||||||
|
# You want to see the 'sleep 2' as a set_command_title as well.
|
||||||
|
if [[ 0 -eq "${BASH_SUBSHELL:-}" ]]; then
|
||||||
|
__bp_preexec_interactive_mode=""
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if __bp_in_prompt_command "${BASH_COMMAND:-}"; then
|
||||||
|
# If we're executing something inside our prompt_command then we don't
|
||||||
|
# want to call preexec. Bash prior to 3.1 can't detect this at all :/
|
||||||
|
__bp_preexec_interactive_mode=""
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
local this_command
|
||||||
|
this_command=$(
|
||||||
|
export LC_ALL=C
|
||||||
|
HISTTIMEFORMAT= builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sanity check to make sure we have something to invoke our function with.
|
||||||
|
if [[ -z "$this_command" ]]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Invoke every function defined in our function array.
|
||||||
|
local preexec_function
|
||||||
|
local preexec_function_ret_value
|
||||||
|
local preexec_ret_value=0
|
||||||
|
for preexec_function in "${preexec_functions[@]:-}"; do
|
||||||
|
|
||||||
|
# Only execute each function if it actually exists.
|
||||||
|
# Test existence of function with: declare -[fF]
|
||||||
|
if type -t "$preexec_function" 1>/dev/null; then
|
||||||
|
__bp_set_ret_value ${__bp_last_ret_value:-}
|
||||||
|
# Quote our function invocation to prevent issues with IFS
|
||||||
|
"$preexec_function" "$this_command"
|
||||||
|
preexec_function_ret_value="$?"
|
||||||
|
if [[ "$preexec_function_ret_value" != 0 ]]; then
|
||||||
|
preexec_ret_value="$preexec_function_ret_value"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Restore the last argument of the last executed command, and set the return
|
||||||
|
# value of the DEBUG trap to be the return code of the last preexec function
|
||||||
|
# to return an error.
|
||||||
|
# If `extdebug` is enabled a non-zero return value from any preexec function
|
||||||
|
# will cause the user's command not to execute.
|
||||||
|
# Run `shopt -s extdebug` to enable
|
||||||
|
__bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
|
||||||
|
}
|
||||||
|
|
||||||
|
__bp_install() {
|
||||||
|
# Exit if we already have this installed.
|
||||||
|
if [[ "${PROMPT_COMMAND:-}" == *"__bp_precmd_invoke_cmd"* ]]; then
|
||||||
|
return 1;
|
||||||
|
fi
|
||||||
|
|
||||||
|
trap '__bp_preexec_invoke_exec "$_"' DEBUG
|
||||||
|
|
||||||
|
# Preserve any prior DEBUG trap as a preexec function
|
||||||
|
local prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
|
||||||
|
unset __bp_trap_string
|
||||||
|
if [[ -n "$prior_trap" ]]; then
|
||||||
|
eval '__bp_original_debug_trap() {
|
||||||
|
'"$prior_trap"'
|
||||||
|
}'
|
||||||
|
preexec_functions+=(__bp_original_debug_trap)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Adjust our HISTCONTROL Variable if needed.
|
||||||
|
__bp_adjust_histcontrol
|
||||||
|
|
||||||
|
# Issue #25. Setting debug trap for subshells causes sessions to exit for
|
||||||
|
# backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash.
|
||||||
|
#
|
||||||
|
# Disabling this by default. It can be enabled by setting this variable.
|
||||||
|
if [[ -n "${__bp_enable_subshells:-}" ]]; then
|
||||||
|
|
||||||
|
# Set so debug trap will work be invoked in subshells.
|
||||||
|
set -o functrace > /dev/null 2>&1
|
||||||
|
shopt -s extdebug > /dev/null 2>&1
|
||||||
|
fi;
|
||||||
|
|
||||||
|
local existing_prompt_command
|
||||||
|
# Remove setting our trap install string and sanitize the existing prompt command string
|
||||||
|
existing_prompt_command="${PROMPT_COMMAND//$__bp_install_string[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
|
||||||
|
existing_prompt_command="${existing_prompt_command//$__bp_install_string}"
|
||||||
|
__bp_sanitize_string existing_prompt_command "$existing_prompt_command"
|
||||||
|
|
||||||
|
# Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
|
||||||
|
# actually entered something.
|
||||||
|
PROMPT_COMMAND=$'__bp_precmd_invoke_cmd\n'
|
||||||
|
if [[ -n "$existing_prompt_command" ]]; then
|
||||||
|
PROMPT_COMMAND+=${existing_prompt_command}$'\n'
|
||||||
|
fi;
|
||||||
|
PROMPT_COMMAND+='__bp_interactive_mode'
|
||||||
|
|
||||||
|
# Add two functions to our arrays for convenience
|
||||||
|
# of definition.
|
||||||
|
precmd_functions+=(precmd)
|
||||||
|
preexec_functions+=(preexec)
|
||||||
|
|
||||||
|
# Invoke our two functions manually that were added to $PROMPT_COMMAND
|
||||||
|
__bp_precmd_invoke_cmd
|
||||||
|
__bp_interactive_mode
|
||||||
|
}
|
||||||
|
|
||||||
|
# Sets an installation string as part of our PROMPT_COMMAND to install
|
||||||
|
# after our session has started. This allows bash-preexec to be included
|
||||||
|
# at any point in our bash profile.
|
||||||
|
__bp_install_after_session_init() {
|
||||||
|
# Make sure this is bash that's running this and return otherwise.
|
||||||
|
if [[ -z "${BASH_VERSION:-}" ]]; then
|
||||||
|
return 1;
|
||||||
|
fi
|
||||||
|
|
||||||
|
# bash-preexec needs to modify these variables in order to work correctly
|
||||||
|
# if it can't, just stop the installation
|
||||||
|
__bp_require_not_readonly PROMPT_COMMAND HISTCONTROL HISTTIMEFORMAT || return
|
||||||
|
|
||||||
|
local sanitized_prompt_command
|
||||||
|
__bp_sanitize_string sanitized_prompt_command "$PROMPT_COMMAND"
|
||||||
|
if [[ -n "$sanitized_prompt_command" ]]; then
|
||||||
|
PROMPT_COMMAND=${sanitized_prompt_command}$'\n'
|
||||||
|
fi;
|
||||||
|
PROMPT_COMMAND+=${__bp_install_string}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Run our install so long as we're not delaying it.
|
||||||
|
if [[ -z "${__bp_delay_install:-}" ]]; then
|
||||||
|
__bp_install_after_session_init
|
||||||
|
fi;
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
Testing `bash-preexec`
|
||||||
|
======================
|
||||||
|
|
||||||
|
**Note on test conditions**
|
||||||
|
|
||||||
|
When writing test conditions, use `[ ... ]` instead of `[[ ... ]]` since the
|
||||||
|
former are supported by Bats on Bash versions before 4.1. In particular, macOS
|
||||||
|
uses Bash 3.2, and `[[ ... ]]` tests always pass on macOS.
|
||||||
|
|
||||||
|
In some cases, you may want to use a feature unique to `[[ ... ]]` such as
|
||||||
|
pattern matching (`[[ $name = a* ]]`) or regular expressions (`[[ $(date) =~
|
||||||
|
^Fri\ ...\ 13 ]]`). In those cases, use the following pattern to replace “bare”
|
||||||
|
`[[ ... ]]`.
|
||||||
|
|
||||||
|
```
|
||||||
|
[[ ... ]] || return 1
|
||||||
|
```
|
||||||
|
|
||||||
|
References:
|
||||||
|
* [Differences between `[` and `[[`](http://mywiki.wooledge.org/BashFAQ/031)
|
||||||
|
* [Problems with `[[` in Bats](https://github.com/sstephenson/bats/issues/49)
|
||||||
|
* [Using `|| return 1` instead of `|| false`](https://github.com/bats-core/bats-core/commit/e5695a673faad4d4d33446ed5c99d70dbfa6d8be)
|
||||||
|
|
@ -0,0 +1,364 @@
|
||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
PROMPT_COMMAND='' # in case the invoking shell has set this
|
||||||
|
history -s fake command # preexec requires there be some history
|
||||||
|
set -o nounset # in case the user has this set
|
||||||
|
__bp_delay_install="true"
|
||||||
|
source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
|
||||||
|
}
|
||||||
|
|
||||||
|
bp_install() {
|
||||||
|
__bp_install_after_session_init
|
||||||
|
eval "$PROMPT_COMMAND"
|
||||||
|
}
|
||||||
|
|
||||||
|
test_echo() {
|
||||||
|
echo "test echo"
|
||||||
|
}
|
||||||
|
|
||||||
|
test_preexec_echo() {
|
||||||
|
printf "%s\n" "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "__bp_install_after_session_init should exit with 1 if we're not using bash" {
|
||||||
|
unset BASH_VERSION
|
||||||
|
run '__bp_install_after_session_init'
|
||||||
|
[ $status -eq 1 ]
|
||||||
|
[ -z "$output" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "__bp_install should exit if it's already installed" {
|
||||||
|
bp_install
|
||||||
|
|
||||||
|
run '__bp_install'
|
||||||
|
[ $status -eq 1 ]
|
||||||
|
[ -z "$output" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "__bp_install should remove trap logic and itself from PROMPT_COMMAND" {
|
||||||
|
__bp_install_after_session_init
|
||||||
|
|
||||||
|
[[ "$PROMPT_COMMAND" == *"trap - DEBUG"* ]] || return 1
|
||||||
|
[[ "$PROMPT_COMMAND" == *"__bp_install"* ]] || return 1
|
||||||
|
|
||||||
|
eval "$PROMPT_COMMAND"
|
||||||
|
|
||||||
|
[[ "$PROMPT_COMMAND" != *"trap DEBUG"* ]] || return 1
|
||||||
|
[[ "$PROMPT_COMMAND" != *"__bp_install"* ]] || return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "__bp_install should preserve an existing DEBUG trap" {
|
||||||
|
trap_invoked_count=0
|
||||||
|
foo() { (( trap_invoked_count += 1 )); }
|
||||||
|
|
||||||
|
# note setting this causes BATS to mis-report the failure line when this test fails
|
||||||
|
trap foo DEBUG
|
||||||
|
[ "$(trap -p DEBUG | cut -d' ' -f3)" == "'foo'" ]
|
||||||
|
|
||||||
|
bp_install
|
||||||
|
trap_count_snapshot=$trap_invoked_count
|
||||||
|
|
||||||
|
[ "$(trap -p DEBUG | cut -d' ' -f3)" == "'__bp_preexec_invoke_exec" ]
|
||||||
|
[[ "${preexec_functions[*]}" == *"__bp_original_debug_trap"* ]] || return 1
|
||||||
|
|
||||||
|
__bp_interactive_mode # triggers the DEBUG trap
|
||||||
|
|
||||||
|
# ensure the trap count is still being incremented after the trap's been overwritten
|
||||||
|
(( trap_count_snapshot < trap_invoked_count ))
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "__bp_sanitize_string should remove semicolons and trim space" {
|
||||||
|
|
||||||
|
__bp_sanitize_string output " true1; "$'\n'
|
||||||
|
[ "$output" == "true1" ]
|
||||||
|
|
||||||
|
__bp_sanitize_string output " ; true2; "
|
||||||
|
[ "$output" == "true2" ]
|
||||||
|
|
||||||
|
__bp_sanitize_string output $'\n'" ; true3; "
|
||||||
|
[ "$output" == "true3" ]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "Appending to PROMPT_COMMAND should work after bp_install" {
|
||||||
|
bp_install
|
||||||
|
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND; true"
|
||||||
|
eval "$PROMPT_COMMAND"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "Appending or prepending to PROMPT_COMMAND should work after bp_install_after_session_init" {
|
||||||
|
__bp_install_after_session_init
|
||||||
|
nl=$'\n'
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND; true"
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND $nl true"
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND; true"
|
||||||
|
PROMPT_COMMAND="true; $PROMPT_COMMAND"
|
||||||
|
PROMPT_COMMAND="true; $PROMPT_COMMAND"
|
||||||
|
PROMPT_COMMAND="true; $PROMPT_COMMAND"
|
||||||
|
PROMPT_COMMAND="true $nl $PROMPT_COMMAND"
|
||||||
|
eval "$PROMPT_COMMAND"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Case where a user is appending or prepending to PROMPT_COMMAND.
|
||||||
|
# This can happen after 'source bash-preexec.sh' e.g.
|
||||||
|
# source bash-preexec.sh; PROMPT_COMMAND="$PROMPT_COMMAND; other_prompt_command_hook"
|
||||||
|
@test "Adding to PROMPT_COMMAND before and after initiating install" {
|
||||||
|
PROMPT_COMMAND="echo before"
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND; echo before2"
|
||||||
|
__bp_install_after_session_init
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND"$'\n echo after'
|
||||||
|
PROMPT_COMMAND="echo after2; $PROMPT_COMMAND;"
|
||||||
|
|
||||||
|
eval "$PROMPT_COMMAND"
|
||||||
|
|
||||||
|
expected_result=$'__bp_precmd_invoke_cmd\necho after2; echo before; echo before2\n echo after\n__bp_interactive_mode'
|
||||||
|
[ "$PROMPT_COMMAND" == "$expected_result" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "Adding to PROMPT_COMMAND after with semicolon" {
|
||||||
|
PROMPT_COMMAND="echo before"
|
||||||
|
__bp_install_after_session_init
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND; echo after"
|
||||||
|
|
||||||
|
eval "$PROMPT_COMMAND"
|
||||||
|
|
||||||
|
expected_result=$'__bp_precmd_invoke_cmd\necho before\n echo after\n__bp_interactive_mode'
|
||||||
|
[ "$PROMPT_COMMAND" == "$expected_result" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "during install PROMPT_COMMAND and precmd functions should be executed each once" {
|
||||||
|
PROMPT_COMMAND="echo before"
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND; echo before2"
|
||||||
|
__bp_install_after_session_init
|
||||||
|
PROMPT_COMMAND="$PROMPT_COMMAND; echo after"
|
||||||
|
PROMPT_COMMAND="echo after2; $PROMPT_COMMAND;"
|
||||||
|
|
||||||
|
precmd() { echo "inside precmd"; }
|
||||||
|
run eval "$PROMPT_COMMAND"
|
||||||
|
[ "${lines[0]}" == "after2" ]
|
||||||
|
[ "${lines[1]}" == "before" ]
|
||||||
|
[ "${lines[2]}" == "before2" ]
|
||||||
|
[ "${lines[3]}" == "inside precmd" ]
|
||||||
|
[ "${lines[4]}" == "after" ]
|
||||||
|
[ "${#lines[@]}" == '5' ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "No functions defined for preexec should simply return" {
|
||||||
|
__bp_interactive_mode
|
||||||
|
|
||||||
|
run '__bp_preexec_invoke_exec' 'true'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ -z "$output" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "precmd should execute a function once" {
|
||||||
|
precmd_functions+=(test_echo)
|
||||||
|
run '__bp_precmd_invoke_cmd'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "test echo" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "precmd should set \$? to be the previous exit code" {
|
||||||
|
echo_exit_code() {
|
||||||
|
echo "$?"
|
||||||
|
}
|
||||||
|
return_exit_code() {
|
||||||
|
return $1
|
||||||
|
}
|
||||||
|
# Helper function is necessary because Bats' run doesn't preserve $?
|
||||||
|
set_exit_code_and_run_precmd() {
|
||||||
|
return_exit_code 251
|
||||||
|
__bp_precmd_invoke_cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
precmd_functions+=(echo_exit_code)
|
||||||
|
run 'set_exit_code_and_run_precmd'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "251" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "precmd should set \$BP_PIPESTATUS to the previous \$PIPESTATUS" {
|
||||||
|
echo_pipestatus() {
|
||||||
|
echo "${BP_PIPESTATUS[*]}"
|
||||||
|
}
|
||||||
|
# Helper function is necessary because Bats' run doesn't preserve $PIPESTATUS
|
||||||
|
set_pipestatus_and_run_precmd() {
|
||||||
|
false | true
|
||||||
|
__bp_precmd_invoke_cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
precmd_functions+=(echo_pipestatus)
|
||||||
|
run 'set_pipestatus_and_run_precmd'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "1 0" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "precmd should set \$_ to be the previous last arg" {
|
||||||
|
echo_last_arg() {
|
||||||
|
echo "$_"
|
||||||
|
}
|
||||||
|
precmd_functions+=(echo_last_arg)
|
||||||
|
|
||||||
|
bats_trap=$(trap -p DEBUG)
|
||||||
|
trap DEBUG # remove the Bats stack-trace trap so $_ doesn't get overwritten
|
||||||
|
: "last-arg"
|
||||||
|
__bp_preexec_invoke_exec "$_"
|
||||||
|
eval "$bats_trap" # Restore trap
|
||||||
|
run '__bp_precmd_invoke_cmd'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "last-arg" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preexec should execute a function with the last command in our history" {
|
||||||
|
preexec_functions+=(test_preexec_echo)
|
||||||
|
__bp_interactive_mode
|
||||||
|
git_command="git commit -a -m 'committing some stuff'"
|
||||||
|
history -s $git_command
|
||||||
|
|
||||||
|
run '__bp_preexec_invoke_exec'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "$git_command" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preexec should execute multiple functions in the order added to their arrays" {
|
||||||
|
fun_1() { echo "$1 one"; }
|
||||||
|
fun_2() { echo "$1 two"; }
|
||||||
|
preexec_functions+=(fun_1)
|
||||||
|
preexec_functions+=(fun_2)
|
||||||
|
__bp_interactive_mode
|
||||||
|
|
||||||
|
run '__bp_preexec_invoke_exec'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "${#lines[@]}" == '2' ]
|
||||||
|
[ "${lines[0]}" == "fake command one" ]
|
||||||
|
[ "${lines[1]}" == "fake command two" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preecmd should execute multiple functions in the order added to their arrays" {
|
||||||
|
fun_1() { echo "one"; }
|
||||||
|
fun_2() { echo "two"; }
|
||||||
|
precmd_functions+=(fun_1)
|
||||||
|
precmd_functions+=(fun_2)
|
||||||
|
|
||||||
|
run '__bp_precmd_invoke_cmd'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "${#lines[@]}" == '2' ]
|
||||||
|
[ "${lines[0]}" == "one" ]
|
||||||
|
[ "${lines[1]}" == "two" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preexec should execute a function with IFS defined to local scope" {
|
||||||
|
IFS=_
|
||||||
|
name_with_underscores_1() { parts=(1_2); echo $parts; }
|
||||||
|
preexec_functions+=(name_with_underscores_1)
|
||||||
|
|
||||||
|
__bp_interactive_mode
|
||||||
|
run '__bp_preexec_invoke_exec'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "1 2" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "precmd should execute a function with IFS defined to local scope" {
|
||||||
|
IFS=_
|
||||||
|
name_with_underscores_2() { parts=(2_2); echo $parts; }
|
||||||
|
precmd_functions+=(name_with_underscores_2)
|
||||||
|
run '__bp_precmd_invoke_cmd'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "2 2" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preexec should set \$? to be the exit code of preexec_functions" {
|
||||||
|
return_nonzero() {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
preexec_functions+=(return_nonzero)
|
||||||
|
|
||||||
|
__bp_interactive_mode
|
||||||
|
|
||||||
|
run '__bp_preexec_invoke_exec'
|
||||||
|
[ $status -eq 1 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "in_prompt_command should detect if a command is part of PROMPT_COMMAND" {
|
||||||
|
|
||||||
|
PROMPT_COMMAND=$'precmd_invoke_cmd\n something; echo yo\n __bp_interactive_mode'
|
||||||
|
run '__bp_in_prompt_command' "something"
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
|
||||||
|
run '__bp_in_prompt_command' "something_else"
|
||||||
|
[ $status -eq 1 ]
|
||||||
|
|
||||||
|
# Should trim commands and arguments here.
|
||||||
|
PROMPT_COMMAND=" precmd_invoke_cmd ; something ; some_stuff_here;"
|
||||||
|
run '__bp_in_prompt_command' " precmd_invoke_cmd "
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
|
||||||
|
PROMPT_COMMAND=" precmd_invoke_cmd ; something ; some_stuff_here;"
|
||||||
|
run '__bp_in_prompt_command' " not_found"
|
||||||
|
[ $status -eq 1 ]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "__bp_adjust_histcontrol should remove ignorespace and ignoreboth" {
|
||||||
|
|
||||||
|
# Should remove ignorespace
|
||||||
|
HISTCONTROL="ignorespace:ignoredups:*"
|
||||||
|
__bp_adjust_histcontrol
|
||||||
|
[ "$HISTCONTROL" == ":ignoredups:*" ]
|
||||||
|
|
||||||
|
# Should remove ignoreboth and replace it with ignoredups
|
||||||
|
HISTCONTROL="ignoreboth"
|
||||||
|
__bp_adjust_histcontrol
|
||||||
|
[ "$HISTCONTROL" == "ignoredups:" ]
|
||||||
|
|
||||||
|
# Handle a few inputs
|
||||||
|
HISTCONTROL="ignoreboth:ignorespace:some_thing_else"
|
||||||
|
__bp_adjust_histcontrol
|
||||||
|
echo "$HISTCONTROL"
|
||||||
|
[ "$HISTCONTROL" == "ignoredups:::some_thing_else" ]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preexec should respect HISTTIMEFORMAT" {
|
||||||
|
preexec_functions+=(test_preexec_echo)
|
||||||
|
__bp_interactive_mode
|
||||||
|
git_command="git commit -a -m 'committing some stuff'"
|
||||||
|
HISTTIMEFORMAT='%F %T '
|
||||||
|
history -s $git_command
|
||||||
|
|
||||||
|
run '__bp_preexec_invoke_exec'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "$git_command" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preexec should not strip whitespace from commands" {
|
||||||
|
preexec_functions+=(test_preexec_echo)
|
||||||
|
__bp_interactive_mode
|
||||||
|
history -s " this command has whitespace "
|
||||||
|
|
||||||
|
run '__bp_preexec_invoke_exec'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == " this command has whitespace " ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preexec should preserve multi-line strings in commands" {
|
||||||
|
preexec_functions+=(test_preexec_echo)
|
||||||
|
__bp_interactive_mode
|
||||||
|
history -s "this 'command contains
|
||||||
|
a multiline string'"
|
||||||
|
run '__bp_preexec_invoke_exec'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == "this 'command contains
|
||||||
|
a multiline string'" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "preexec should work on options to 'echo' commands" {
|
||||||
|
preexec_functions+=(test_preexec_echo)
|
||||||
|
__bp_interactive_mode
|
||||||
|
history -s -- '-n'
|
||||||
|
run '__bp_preexec_invoke_exec'
|
||||||
|
[ $status -eq 0 ]
|
||||||
|
[ "$output" == '-n' ]
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
@test "should not import if it's already defined" {
|
||||||
|
__bp_imported="defined"
|
||||||
|
source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
|
||||||
|
[ -z $(type -t __bp_preexec_and_precmd_install) ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "should import if not defined" {
|
||||||
|
unset __bp_imported
|
||||||
|
source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
|
||||||
|
[ -n $(type -t __bp_install) ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "bp should stop installation if HISTTIMEFORMAT is readonly" {
|
||||||
|
readonly HISTTIMEFORMAT
|
||||||
|
run source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
|
||||||
|
[ $status -ne 0 ]
|
||||||
|
[[ "$output" =~ "HISTTIMEFORMAT" ]] || return 1
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
# shellcheck shell=bash
|
||||||
|
# shellcheck disable=1090
|
||||||
|
source "${BASH_IT}"/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh
|
||||||
Loading…
Reference in New Issue