From c9a7837b44c2c69b353bccd8e78cbdbcd66d7043 Mon Sep 17 00:00:00 2001
From: buhl <221842+buhl@users.noreply.github.com>
Date: Sat, 23 Jan 2021 18:28:43 +0100
Subject: [PATCH 1/5] Squashed 'vendor/github.com/rcaloras/bash-preexec/'
content from commit 7884535
git-subtree-dir: vendor/github.com/rcaloras/bash-preexec
git-subtree-split: 7884535ed423ac27d3b6b473c61b1fe41905aca1
---
.travis.yml | 20 +++
LICENSE.md | 21 +++
README.md | 107 ++++++++++++
bash-preexec.sh | 341 ++++++++++++++++++++++++++++++++++++++
test/README.md | 22 +++
test/bash-preexec.bats | 364 +++++++++++++++++++++++++++++++++++++++++
test/include-test.bats | 20 +++
7 files changed, 895 insertions(+)
create mode 100644 .travis.yml
create mode 100644 LICENSE.md
create mode 100644 README.md
create mode 100644 bash-preexec.sh
create mode 100644 test/README.md
create mode 100644 test/bash-preexec.bats
create mode 100644 test/include-test.bats
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 00000000..4f0c8610
--- /dev/null
+++ b/.travis.yml
@@ -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
diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 00000000..b4521b39
--- /dev/null
+++ b/LICENSE.md
@@ -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.
diff --git a/README.md b/README.md
new file mode 100644
index 00000000..3e88c844
--- /dev/null
+++ b/README.md
@@ -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).
+
+
+
+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
+```
diff --git a/bash-preexec.sh b/bash-preexec.sh
new file mode 100644
index 00000000..c23d0381
--- /dev/null
+++ b/bash-preexec.sh
@@ -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;
diff --git a/test/README.md b/test/README.md
new file mode 100644
index 00000000..fd6613c0
--- /dev/null
+++ b/test/README.md
@@ -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)
diff --git a/test/bash-preexec.bats b/test/bash-preexec.bats
new file mode 100644
index 00000000..84a30cae
--- /dev/null
+++ b/test/bash-preexec.bats
@@ -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' ]
+}
diff --git a/test/include-test.bats b/test/include-test.bats
new file mode 100644
index 00000000..bd1e3b5b
--- /dev/null
+++ b/test/include-test.bats
@@ -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
+}
From 29855ed1e6e8d0dee3281aa7289121fcc49883ab Mon Sep 17 00:00:00 2001
From: buhl <221842+buhl@users.noreply.github.com>
Date: Fri, 8 Jan 2021 23:51:37 +0100
Subject: [PATCH 2/5] Adding preexec as a vendored library
Added a vendored lib loading routine in bash-it.sh
Added documentation on how to vendor libs in bash-it
Added and fixed plugins using preexec
Added tests for two plugins
Removed the old preexec lib
---
bash_it.sh | 13 +-
clean_files.txt | 3 +
docs/development.rst | 60 ++++++
lib/preexec.bash | 199 ------------------
.../available/cmd-returned-notify.plugin.bash | 16 ++
plugins/available/xterm.plugin.bash | 33 +--
test/fixtures/plugin/xterm/files/arg0 | 0
test/fixtures/plugin/xterm/files/arg1 | 0
test/plugins/cmd-returned-notify.plugin.bats | 47 +++++
test/plugins/xterm.plugin.bats | 54 +++++
themes/command_duration.theme.bash | 86 ++++----
vendor/init.d/preexec.bash | 1 +
12 files changed, 252 insertions(+), 260 deletions(-)
delete mode 100644 lib/preexec.bash
create mode 100644 plugins/available/cmd-returned-notify.plugin.bash
create mode 100644 test/fixtures/plugin/xterm/files/arg0
create mode 100644 test/fixtures/plugin/xterm/files/arg1
create mode 100644 test/plugins/cmd-returned-notify.plugin.bats
create mode 100644 test/plugins/xterm.plugin.bats
create mode 100644 vendor/init.d/preexec.bash
diff --git a/bash_it.sh b/bash_it.sh
index a7960c75..dfe6f615 100755
--- a/bash_it.sh
+++ b/bash_it.sh
@@ -49,6 +49,17 @@ 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
# shellcheck source=./scripts/reloader.bash
@@ -62,7 +73,7 @@ do
done
# 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..."
# Load colors and helpers first so they can be used in base theme
BASH_IT_LOG_PREFIX="themes: colors: "
diff --git a/clean_files.txt b/clean_files.txt
index f94b2dca..d738d151 100644
--- a/clean_files.txt
+++ b/clean_files.txt
@@ -39,10 +39,13 @@ themes/barbuk
themes/atomic
themes/axin
themes/base.theme.bash
+themes/command_duration.theme.bash
# plugins
#
plugins/available/basher.plugin.bash
+plugins/available/cmd-returned-notify.plugin.bash
+plugins/available/xterm.plugin.bash
# completions
#
diff --git a/docs/development.rst b/docs/development.rst
index b49ff141..e700be3f 100644
--- a/docs/development.rst
+++ b/docs/development.rst
@@ -38,6 +38,7 @@ The main ``bash_it.sh`` script loads the frameworks individual components in the
* ``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?)
* Enabled ``aliases``
* 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.
+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 `_ tool.
+The `original repo `_ 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 `_
+in the repositories ``README`` file and a website for the original repository has a `manual page `_ 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
------------------------
diff --git a/lib/preexec.bash b/lib/preexec.bash
deleted file mode 100644
index 2386f46a..00000000
--- a/lib/preexec.bash
+++ /dev/null
@@ -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
-}
diff --git a/plugins/available/cmd-returned-notify.plugin.bash b/plugins/available/cmd-returned-notify.plugin.bash
new file mode 100644
index 00000000..a3050875
--- /dev/null
+++ b/plugins/available/cmd-returned-notify.plugin.bash
@@ -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)
diff --git a/plugins/available/xterm.plugin.bash b/plugins/available/xterm.plugin.bash
index c55dccd4..c5fe9009 100644
--- a/plugins/available/xterm.plugin.bash
+++ b/plugins/available/xterm.plugin.bash
@@ -1,30 +1,33 @@
+# shellcheck shell=bash
cite about-plugin
about-plugin 'automatically set your xterm title with host and location info'
-
-_short-dirname () {
- local dir_name=`dirs +0`
- [ "$SHORT_TERM_LINE" = true ] && [ ${#dir_name} -gt 8 ] && echo ${dir_name##*/} || echo $dir_name
+_short-dirname() {
+ local dir_name=$(dirs +0)
+ [ "$SHORT_TERM_LINE" = true ] && [ "${#dir_name}" -gt 8 ] && echo "${dir_name##*/}" || echo "${dir_name}"
}
-_short-command () {
- local input_command="$@"
- [ "$SHORT_TERM_LINE" = true ] && [ ${#input_command} -gt 8 ] && echo ${input_command%% *} || echo $input_command
+_short-command() {
+ local input_command="$*"
+ [ "$SHORT_TERM_LINE" = true ] && [ "${#input_command}" -gt 8 ] && echo "${input_command%% *}" || echo "${input_command}"
}
-set_xterm_title () {
- local title="$1"
- echo -ne "\033]0;$title\007"
+set_xterm_title() {
+ local title="$1"
+ echo -ne "\033]0;$title\007"
}
-precmd () {
- set_xterm_title "${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}} `_short-dirname` $PROMPTCHAR"
+precmd_xterm_title() {
+ set_xterm_title "${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}} $(_short-dirname) $PROMPT_CHAR"
}
-preexec () {
- set_xterm_title "`_short-command $1` {`_short-dirname`} (${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}})"
+preexec_xterm_title() {
+ set_xterm_title "$(_short-command "${1}") {$(_short-dirname)} (${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}})"
}
case "$TERM" in
- xterm*|rxvt*) preexec_install;;
+ xterm* | rxvt*)
+ precmd_functions+=(precmd_xterm_title)
+ preexec_functions+=(preexec_xterm_title)
+ ;;
esac
diff --git a/test/fixtures/plugin/xterm/files/arg0 b/test/fixtures/plugin/xterm/files/arg0
new file mode 100644
index 00000000..e69de29b
diff --git a/test/fixtures/plugin/xterm/files/arg1 b/test/fixtures/plugin/xterm/files/arg1
new file mode 100644
index 00000000..e69de29b
diff --git a/test/plugins/cmd-returned-notify.plugin.bats b/test/plugins/cmd-returned-notify.plugin.bats
new file mode 100644
index 00000000..69233601
--- /dev/null
+++ b/test/plugins/cmd-returned-notify.plugin.bats
@@ -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}"
+}
diff --git a/test/plugins/xterm.plugin.bats b/test/plugins/xterm.plugin.bats
new file mode 100644
index 00000000..6ded3cea
--- /dev/null
+++ b/test/plugins/xterm.plugin.bats
@@ -0,0 +1,54 @@
+#!/usr/bin/env bats
+
+load ../test_helper
+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 -v 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'
+}
diff --git a/themes/command_duration.theme.bash b/themes/command_duration.theme.bash
index c721003e..cf91785c 100644
--- a/themes/command_duration.theme.bash
+++ b/themes/command_duration.theme.bash
@@ -1,10 +1,10 @@
-#!/usr/bin/env bash
+# shellcheck shell=bash
if [ -z "$BASH_IT_COMMAND_DURATION" ] || [ "$BASH_IT_COMMAND_DURATION" != true ]; then
- _command_duration() {
- echo -n
- }
- return
+ _command_duration() {
+ echo -n
+ }
+ return
fi
# Define tmp dir and file
@@ -17,57 +17,53 @@ COMMAND_DURATION_MIN_SECONDS=${COMMAND_DURATION_MIN_SECONDS:-'1'}
trap _command_duration_delete_temp_file EXIT HUP INT TERM
_command_duration_delete_temp_file() {
- if [[ -f "$COMMAND_DURATION_FILE" ]]; then
- rm -f "$COMMAND_DURATION_FILE"
- fi
+ if [[ -f "$COMMAND_DURATION_FILE" ]]; then
+ rm -f "$COMMAND_DURATION_FILE"
+ fi
}
_command_duration_pre_exec() {
- date +%s.%1N > "$COMMAND_DURATION_FILE"
+ date +%s.%1N > "$COMMAND_DURATION_FILE"
}
_command_duration() {
- local command_duration command_start current_time
- local minutes seconds deciseconds
- local command_start_sseconds current_time_seconds command_start_deciseconds current_time_deciseconds
- current_time=$(date +%s.%1N)
+ local command_duration command_start current_time
+ local minutes seconds deciseconds
+ local command_start_sseconds current_time_seconds command_start_deciseconds current_time_deciseconds
+ current_time=$(date +%s.%1N)
- if [[ -f "$COMMAND_DURATION_FILE" ]]; then
- command_start=$(< "$COMMAND_DURATION_FILE")
- command_start_sseconds=${command_start%.*}
- current_time_seconds=${current_time%.*}
+ if [[ -f "$COMMAND_DURATION_FILE" ]]; then
+ command_start=$(< "$COMMAND_DURATION_FILE")
+ command_start_sseconds=${command_start%.*}
+ current_time_seconds=${current_time%.*}
- command_start_deciseconds=$((10#${command_start#*.}))
- current_time_deciseconds=$((10#${current_time#*.}))
+ command_start_deciseconds=$((10#${command_start#*.}))
+ current_time_deciseconds=$((10#${current_time#*.}))
- # seconds
- command_duration=$(( current_time_seconds - command_start_sseconds ))
+ # seconds
+ command_duration=$((current_time_seconds - command_start_sseconds))
- if (( current_time_deciseconds >= command_start_deciseconds )); then
- deciseconds=$(( (current_time_deciseconds - command_start_deciseconds) ))
- else
- ((command_duration-=1))
- deciseconds=$(( 10 - ( (command_start_deciseconds - current_time_deciseconds) ) ))
- fi
- command rm "$COMMAND_DURATION_FILE"
- else
- command_duration=0
- fi
+ if ((current_time_deciseconds >= command_start_deciseconds)); then
+ deciseconds=$(((current_time_deciseconds - command_start_deciseconds)))
+ else
+ ((command_duration -= 1))
+ deciseconds=$((10 - ((command_start_deciseconds - current_time_deciseconds))))
+ fi
+ command rm "$COMMAND_DURATION_FILE"
+ else
+ command_duration=0
+ fi
- if (( command_duration > 0 )); then
- minutes=$(( command_duration / 60 ))
- seconds=$(( command_duration % 60 ))
- fi
+ if ((command_duration > 0)); then
+ minutes=$((command_duration / 60))
+ seconds=$((command_duration % 60))
+ fi
- if (( minutes > 0 )); then
- printf "%s%s%dm %ds" "$COMMAND_DURATION_ICON" "$COMMAND_DURATION_COLOR" "$minutes" "$seconds"
- elif (( seconds >= COMMAND_DURATION_MIN_SECONDS )); then
- printf "%s%s%d.%01ds" "$COMMAND_DURATION_ICON" "$COMMAND_DURATION_COLOR" "$seconds" "$deciseconds"
- fi
+ if ((minutes > 0)); then
+ printf "%s%s%dm %ds" "$COMMAND_DURATION_ICON" "$COMMAND_DURATION_COLOR" "$minutes" "$seconds"
+ elif ((seconds >= COMMAND_DURATION_MIN_SECONDS)); then
+ printf "%s%s%d.%01ds" "$COMMAND_DURATION_ICON" "$COMMAND_DURATION_COLOR" "$seconds" "$deciseconds"
+ fi
}
-preexec() (
- _command_duration_pre_exec
-)
-
-preexec_install
+preexec_functions+=(_command_duration_pre_exec)
diff --git a/vendor/init.d/preexec.bash b/vendor/init.d/preexec.bash
new file mode 100644
index 00000000..f8140afc
--- /dev/null
+++ b/vendor/init.d/preexec.bash
@@ -0,0 +1 @@
+source ${BASH_IT}/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh
From 046e528cc28e0e61ea79ba40dbf061002f06af79 Mon Sep 17 00:00:00 2001
From: Noah Gorny
Date: Sat, 23 Jan 2021 22:47:15 +0200
Subject: [PATCH 3/5] plugins: xterm: Fix shfmt complaints
---
plugins/available/xterm.plugin.bash | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/plugins/available/xterm.plugin.bash b/plugins/available/xterm.plugin.bash
index c5fe9009..4f4fd50d 100644
--- a/plugins/available/xterm.plugin.bash
+++ b/plugins/available/xterm.plugin.bash
@@ -2,26 +2,26 @@
cite about-plugin
about-plugin 'automatically set your xterm title with host and location info'
-_short-dirname() {
+_short-dirname() {
local dir_name=$(dirs +0)
[ "$SHORT_TERM_LINE" = true ] && [ "${#dir_name}" -gt 8 ] && echo "${dir_name##*/}" || echo "${dir_name}"
}
-_short-command() {
+_short-command() {
local input_command="$*"
[ "$SHORT_TERM_LINE" = true ] && [ "${#input_command}" -gt 8 ] && echo "${input_command%% *}" || echo "${input_command}"
}
-set_xterm_title() {
+set_xterm_title() {
local title="$1"
echo -ne "\033]0;$title\007"
}
-precmd_xterm_title() {
+precmd_xterm_title() {
set_xterm_title "${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}} $(_short-dirname) $PROMPT_CHAR"
}
-preexec_xterm_title() {
+preexec_xterm_title() {
set_xterm_title "$(_short-command "${1}") {$(_short-dirname)} (${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}})"
}
From 6b3788051fd6f20d44eb0060dda4332efb28a36b Mon Sep 17 00:00:00 2001
From: Noah Gorny
Date: Sat, 23 Jan 2021 23:03:32 +0200
Subject: [PATCH 4/5] test: plugin: xterm: Use correct _command_exists
invocation
---
test/plugins/xterm.plugin.bats | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/test/plugins/xterm.plugin.bats b/test/plugins/xterm.plugin.bats
index 6ded3cea..6879b0fd 100644
--- a/test/plugins/xterm.plugin.bats
+++ b/test/plugins/xterm.plugin.bats
@@ -1,6 +1,7 @@
#!/usr/bin/env bats
load ../test_helper
+load ../../lib/helpers
load ../../lib/composure
load ../../plugins/available/xterm.plugin
@@ -9,8 +10,7 @@ function local_setup {
setup_test_fixture
# Copy the test fixture to the Bash-it folder
- if command_exists -v rsync
- then
+ if _command_exists rsync; then
rsync -a "$BASH_IT/test/fixtures/plugin/xterm/" "$BASH_IT/"
else
find "$BASH_IT/test/fixtures/plugin/xterm" \
From c79d6cd609296b377faa8e71836d2318c890fb8e Mon Sep 17 00:00:00 2001
From: Noah Gorny
Date: Wed, 27 Jan 2021 22:51:43 +0200
Subject: [PATCH 5/5] vendor: Add init.d directory to clean_files.txt
Also fix and lint the directory.
This is our code, and as such, should be linted.
---
clean_files.txt | 3 +++
vendor/init.d/preexec.bash | 4 +++-
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/clean_files.txt b/clean_files.txt
index e030ea4d..3b24f975 100644
--- a/clean_files.txt
+++ b/clean_files.txt
@@ -70,3 +70,6 @@ completion/available/vuejs.completion.bash
aliases/available/dnf.aliases.bash
aliases/available/vim.aliases.bash
aliases/available/git.aliases.bash
+
+# vendor init files
+vendor/init.d
diff --git a/vendor/init.d/preexec.bash b/vendor/init.d/preexec.bash
index f8140afc..296b478a 100644
--- a/vendor/init.d/preexec.bash
+++ b/vendor/init.d/preexec.bash
@@ -1 +1,3 @@
-source ${BASH_IT}/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh
+# shellcheck shell=bash
+# shellcheck disable=1090
+source "${BASH_IT}"/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh