2014-12-01 23:11:20 -09:00
|
|
|
#!/bin/bash
|
|
|
|
|
#
|
2014-12-07 16:05:04 -09:00
|
|
|
# bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions.
|
2015-01-03 11:50:27 -09:00
|
|
|
# https://github.com/rcaloras/bash-preexec
|
2014-12-01 23:11:20 -09:00
|
|
|
#
|
2015-02-23 16:36:46 -09:00
|
|
|
#
|
2014-12-01 23:11:20 -09:00
|
|
|
# '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)
|
2014-12-14 14:20:45 -09:00
|
|
|
# Forked from Original Author: Glyph Lefkowitz
|
2014-12-01 23:11:20 -09:00
|
|
|
#
|
2015-02-23 16:36:46 -09:00
|
|
|
# V0.2.0
|
|
|
|
|
#
|
|
|
|
|
|
2014-12-01 23:11:20 -09:00
|
|
|
# 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.
|
|
|
|
|
#
|
2015-01-03 00:03:33 -09:00
|
|
|
# 2. Add any precmd or preexec functions by appending them to their arrays:
|
2014-12-01 23:11:20 -09:00
|
|
|
# e.g.
|
|
|
|
|
# precmd_functions+=(my_precmd_function)
|
|
|
|
|
# precmd_functions+=(some_other_precmd_function)
|
|
|
|
|
#
|
|
|
|
|
# preexec_functions+=(my_preexec_function)
|
|
|
|
|
#
|
2015-01-03 00:03:33 -09:00
|
|
|
# 3. If you have anything that's using the Debug Trap, change it to use
|
2014-12-01 23:11:20 -09:00
|
|
|
# preexec. (Optional) change anything using PROMPT_COMMAND to now use
|
|
|
|
|
# precmd instead.
|
|
|
|
|
#
|
2015-02-23 16:36:46 -09:00
|
|
|
# Note: This module requires two bash features which you must not otherwise be
|
|
|
|
|
# using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. prexec_and_precmd_install
|
|
|
|
|
# will override these and if you override one or the other this will most likely break.
|
2014-12-01 23:11:20 -09:00
|
|
|
|
2015-01-03 00:03:33 -09:00
|
|
|
# Avoid duplicate inclusion
|
|
|
|
|
if [[ "$__bp_imported" == "defined" ]]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
__bp_imported="defined"
|
|
|
|
|
|
2014-12-01 23:11:20 -09:00
|
|
|
# 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.
|
2015-01-03 00:03:33 -09:00
|
|
|
__bp_preexec_interactive_mode=""
|
|
|
|
|
|
|
|
|
|
__bp_trim_whitespace() {
|
|
|
|
|
local var=$@
|
|
|
|
|
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
|
|
|
|
|
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
|
|
|
|
echo -n "$var"
|
|
|
|
|
}
|
2014-12-01 23:11:20 -09:00
|
|
|
|
|
|
|
|
# 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 to know that the next
|
|
|
|
|
# command is likely interactive.
|
|
|
|
|
#
|
|
|
|
|
# It will invoke any functions defined in the precmd_functions array.
|
2015-01-03 00:03:33 -09:00
|
|
|
__bp_precmd_invoke_cmd() {
|
2015-02-22 20:50:05 -09:00
|
|
|
|
|
|
|
|
# Should be available to each precmd function, should it want it.
|
|
|
|
|
local ret_value="$?"
|
|
|
|
|
|
2014-12-01 23:11:20 -09:00
|
|
|
# For every function defined in our function array. Invoke it.
|
|
|
|
|
local precmd_function
|
|
|
|
|
for precmd_function in ${precmd_functions[@]}; do
|
2014-12-07 20:09:02 -09:00
|
|
|
|
|
|
|
|
# Only execute this function if it actually exists.
|
|
|
|
|
if [[ -n $(type -t $precmd_function) ]]; then
|
2015-02-22 20:50:05 -09:00
|
|
|
__bp_set_ret_value $ret_value
|
2014-12-07 20:09:02 -09:00
|
|
|
$precmd_function
|
|
|
|
|
fi
|
2014-12-01 23:11:20 -09:00
|
|
|
done
|
2015-01-03 00:03:33 -09:00
|
|
|
__bp_preexec_interactive_mode="on";
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-22 20:50:05 -09:00
|
|
|
# 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
|
|
|
|
|
}
|
2015-01-03 00:03:33 -09:00
|
|
|
|
|
|
|
|
__bp_in_prompt_command() {
|
|
|
|
|
|
|
|
|
|
local prompt_command_array
|
|
|
|
|
IFS=';' read -ra prompt_command_array <<< "$PROMPT_COMMAND"
|
|
|
|
|
|
|
|
|
|
local trimmed_arg
|
|
|
|
|
trimmed_arg=$(__bp_trim_whitespace "$1")
|
|
|
|
|
|
|
|
|
|
local prompt_command_function
|
|
|
|
|
for command in "${prompt_command_array[@]}"; do
|
|
|
|
|
local trimmed_command
|
|
|
|
|
trimmed_command=$(__bp_trim_whitespace "$command")
|
|
|
|
|
# Only execute each function if it actually exists.
|
|
|
|
|
if [[ "$trimmed_command" == "$trimmed_arg" ]]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
return 1
|
2014-12-01 23:11:20 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 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.
|
2015-01-03 00:03:33 -09:00
|
|
|
__bp_preexec_invoke_exec() {
|
2014-12-01 23:11:20 -09:00
|
|
|
|
|
|
|
|
if [[ -n "$COMP_LINE" ]]
|
|
|
|
|
then
|
|
|
|
|
# We're in the middle of a completer. This obviously can't be
|
|
|
|
|
# an interactively issued command.
|
|
|
|
|
return
|
|
|
|
|
fi
|
2015-01-03 00:03:33 -09:00
|
|
|
if [[ -z "$__bp_preexec_interactive_mode" ]]
|
2014-12-01 23:11:20 -09:00
|
|
|
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
|
2015-01-03 00:03:33 -09:00
|
|
|
__bp_preexec_interactive_mode=""
|
2014-12-01 23:11:20 -09:00
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2015-01-03 00:03:33 -09:00
|
|
|
if __bp_in_prompt_command "$BASH_COMMAND"; then
|
2015-02-21 13:33:03 -09:00
|
|
|
# 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 :/
|
|
|
|
|
|
2015-01-03 00:03:33 -09:00
|
|
|
__bp_preexec_interactive_mode=""
|
2014-12-01 23:11:20 -09:00
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2015-02-21 13:33:03 -09:00
|
|
|
local this_command="$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g")";
|
2014-12-01 23:11:20 -09:00
|
|
|
|
|
|
|
|
# Sanity check to make sure we have something to invoke our function with.
|
|
|
|
|
if [ -z "$this_command" ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# If none of the previous checks have returned out of this function, then
|
|
|
|
|
# the command is in fact interactive and we should invoke the user's
|
|
|
|
|
# preexec functions.
|
|
|
|
|
|
|
|
|
|
# For every function defined in our function array. Invoke it.
|
|
|
|
|
local preexec_function
|
|
|
|
|
for preexec_function in "${preexec_functions[@]}"; do
|
2014-12-07 20:09:02 -09:00
|
|
|
|
|
|
|
|
# Only execute each function if it actually exists.
|
|
|
|
|
if [[ -n $(type -t $preexec_function) ]]; then
|
|
|
|
|
$preexec_function "$this_command"
|
|
|
|
|
fi
|
2014-12-01 23:11:20 -09:00
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Execute this to set up preexec and precmd execution.
|
2015-02-22 14:15:27 -09:00
|
|
|
__bp_preexec_and_precmd_install() {
|
2014-12-01 23:11:20 -09:00
|
|
|
|
2014-12-20 22:18:51 -09:00
|
|
|
# Make sure this is bash that's running this and return otherwise.
|
|
|
|
|
if [ -z "$BASH_VERSION" ]; then
|
|
|
|
|
return 1;
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Exit if we already have this installed.
|
2015-01-03 00:03:33 -09:00
|
|
|
if [[ "$PROMPT_COMMAND" == *"__bp_precmd_invoke_cmd"* ]]; then
|
2014-12-20 22:18:51 -09:00
|
|
|
return 1;
|
|
|
|
|
fi
|
|
|
|
|
|
2014-12-01 23:11:20 -09:00
|
|
|
# Take our existing prompt command and append a semicolon to it
|
|
|
|
|
# if it doesn't already have one.
|
2014-12-07 15:58:33 -09:00
|
|
|
local existing_prompt_command
|
|
|
|
|
|
|
|
|
|
if [ -n "$PROMPT_COMMAND" ]; then
|
|
|
|
|
existing_prompt_command=$(echo "$PROMPT_COMMAND" | sed '/; *$/!s/$/;/')
|
|
|
|
|
else
|
|
|
|
|
existing_prompt_command=""
|
|
|
|
|
fi
|
2014-12-01 23:11:20 -09:00
|
|
|
|
|
|
|
|
# Finally install our traps.
|
2015-02-22 20:50:05 -09:00
|
|
|
PROMPT_COMMAND="__bp_precmd_invoke_cmd; ${existing_prompt_command}"
|
2015-01-03 00:03:33 -09:00
|
|
|
trap '__bp_preexec_invoke_exec' DEBUG;
|
2015-01-03 00:43:40 -09:00
|
|
|
|
|
|
|
|
# Add two functions to our arrays for convenience
|
|
|
|
|
# of definition.
|
|
|
|
|
precmd_functions+=(precmd)
|
|
|
|
|
preexec_functions+=(preexec)
|
2014-12-01 23:11:20 -09:00
|
|
|
}
|
2015-01-03 00:03:33 -09:00
|
|
|
|
|
|
|
|
# Run our install so long as we're not delaying it.
|
|
|
|
|
if [[ -z "$__bp_delay_install" ]]; then
|
2015-02-22 14:15:27 -09:00
|
|
|
__bp_preexec_and_precmd_install
|
2015-01-03 00:03:33 -09:00
|
|
|
fi;
|