Merge pull request #195 from cauerego/master

themes: Add ab+simple theme
This commit is contained in:
Koichi Murase
2022-10-07 19:38:06 +09:00
committed by GitHub
9 changed files with 835 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
# bash preexec
This plugin loads `bash-preexec.sh` bundled with OMB if necessary.

View File

@@ -0,0 +1,4 @@
#! bash oh-my-bash.module
if [[ ! ${bash_preexec_imported:-${__bp_imported:-}} ]]; then
source "$OSH/tools/bash-preexec.sh"
fi

3
plugins/xterm/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Xterm plugin
This plugin sets the xterm title for the current directory and the currently running command.

View File

@@ -0,0 +1,52 @@
#! bash oh-my-bash.module
# Description: automatically set your xterm title with host and location info'
# Source: https://github.com/Bash-it/bash-it/blob/bf2034d13d/plugins/available/xterm.plugin.bash
#
# @var[opt] PROMPT_CHAR ... This variable is shared with powerline
# @var[opt] OMB_PLUGIN_XTERM_SHORT_TERM_LINE
# @var[opt] OMB_PLUGIN_XTERM_SHORT_USER
# @var[opt] OMB_PLUGIN_XTERM_SHORT_HOSTNAME
#
_omb_module_require plugin:bash-preexec
function _omb_plugin_xterm_short_dirname {
local dir_name=${PWD/~/\~}
if [[ ${OMB_PLUGIN_XTERM_SHORT_TERM_LINE-} == true ]] && ((${#dir_name} > 8)); then
dir_name=${dir_name##*/}
fi
echo "$dir_name"
}
function _omb_plugin_xterm_short_command {
local input_command="$*"
if [[ ${OMB_PLUGIN_XTERM_SHORT_TERM_LINE-} == true ]] ((${#input_command} > 8)); then
input_command=${input_command%% *}
fi
echo "$input_command"
}
function _omb_plugin_xterm_set_title {
local title=${1-}
printf '\e]0;%s\e\\' "$title"
}
function _omb_plugin_xterm_precmd_title {
local user=${OMB_PLUGIN_XTERM_SHORT_USER:-$USER}
local host=${OMB_PLUGIN_XTERM_SHORT_HOSTNAME:-$HOSTNAME}
_omb_plugin_xterm_set_title "$user@$host $(_omb_plugin_xterm_short_dirname) ${PROMPT_CHAR:-\$}"
}
function _omb_plugin_xterm_preexec_title {
local command_line=${BASH_COMMAND:-${1:-}}
local directory_name=$(_omb_plugin_xterm_short_dirname)
local short_command=$(_omb_plugin_xterm_short_command "$command_line")
local user=${OMB_PLUGIN_XTERM_SHORT_USER:-$USER}
local host=${OMB_PLUGIN_XTERM_SHORT_HOSTNAME:-$HOSTNAME}
_omb_plugin_xterm_set_title "$short_command {$directory_name} ($user@$host)"
}
function set_xterm_title { _omb_plugin_xterm_set_title "$@"; }
precmd_functions+=(_omb_plugin_xterm_precmd_title)
preexec_functions+=(_omb_plugin_xterm_preexec_title)

View File

@@ -2,6 +2,10 @@
[![](90210/90210-dark.png)](90210/90210-dark.png)
## `ab+simple`
[![](absimple/absimple-bash.png)](absimple/absimple-bash.png)
## `agnoster`
[![](agnoster/agnoster-bash-sshot.png)](agnoster/agnoster-bash-sshot.png)

14
themes/absimple/README.md Normal file
View File

@@ -0,0 +1,14 @@
# ab+simple theme
This theme needs a Powerline-patched font to be rendered correctly. The original author of the theme recommends https://github.com/adi1090x/termux-style
This is a colorful theme based on the agnoster theme, made shorter and simpler.
The aim of this theme is to only show you relevant information:
The git information will only be shown in a git working directory.
Similarly, everything will be displayed automatically when appropriate,
including the current user and the hostname,
whether the last call exited with an error,
and whether background jobs are running in this shell.
![bash screenshot](absimple-bash.png)

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

View File

@@ -0,0 +1,385 @@
#!/usr/bin/env bash
# vim: ft=bash ts=2 sw=2 sts=2
#
# absimple theme: a fork of the agnoster theme
#
# Tested on Termux in Android 2022-02-15
#
# The aim of this theme is to only show you relevant information: The
# git information will only be shown in a git working directory.
# Similarly, everything will be displayed automatically when
# appropriate, including the current user and the hostname, whether
# the last call exited with an error, and whether background jobs are
# running in this shell.
PROMPT_DIRTRIM=2 # bash4 and above
######################################################################
DEBUG=0
debug() {
if [[ ${DEBUG} -ne 0 ]]; then
>&2 echo -e $*
fi
}
######################################################################
### Segment drawing
# A few utility functions to make it easy and re-usable to draw segmented prompts
CURRENT_BG='NONE'
CURRENT_RBG='NONE'
SEGMENT_SEPARATOR=''
RIGHT_SEPARATOR=''
LEFT_SUBSEG=''
RIGHT_SUBSEG=''
text_effect() {
case "$1" in
reset) echo 0;;
bold) echo 1;;
underline) echo 4;;
esac
}
# to add colors, see
# http://bitmote.com/index.php?post/2012/11/19/Using-ANSI-Color-Codes-to-Colorize-Your-Bash-Prompt-on-Linux
# under the "256 (8-bit) Colors" section, and follow the example for orange below
fg_color() {
case "$1" in
black) echo 30;;
red) echo 31;;
green) echo 32;;
yellow) echo 33;;
blue) echo 34;;
magenta) echo 35;;
cyan) echo 36;;
white) echo 37;;
orange) echo 38\;5\;166;;
esac
}
bg_color() {
case "$1" in
black) echo 40;;
red) echo 41;;
green) echo 42;;
yellow) echo 43;;
blue) echo 44;;
magenta) echo 45;;
cyan) echo 46;;
white) echo 47;;
orange) echo 48\;5\;166;;
esac;
}
ansi() {
local seq
local -a mycodes=("${!1}")
debug "ansi: ${!1} all: $* aka ${mycodes[@]}"
seq=""
local i
for ((i = 0; i < ${#mycodes[@]}; i++)); do
if [[ -n $seq ]]; then
seq="${seq};"
fi
seq="${seq}${mycodes[$i]}"
done
debug "ansi debug:" '\\[\\033['${seq}'m\\]'
echo -ne '\[\033['${seq}'m\]'
# PR="$PR\[\033[${seq}m\]"
}
ansi_single() {
echo -ne '\[\033['$1'm\]'
}
# Begin a segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_segment() {
local bg fg
local -a codes
debug "Prompting $1 $2 $3"
# if commented out from kruton's original... I'm not clear
# if it did anything, but it messed up things like
# prompt_status - Erik 1/14/17
# if [[ -z $1 || ( -z $2 && $2 != default ) ]]; then
codes=("${codes[@]}" $(text_effect reset))
# fi
if [[ -n $1 ]]; then
bg=$(bg_color $1)
codes=("${codes[@]}" $bg)
debug "Added $bg as background to codes"
fi
if [[ -n $2 ]]; then
fg=$(fg_color $2)
codes=("${codes[@]}" $fg)
debug "Added $fg as foreground to codes"
fi
debug "Codes: "
# local -p codes
if [[ $CURRENT_BG != NONE && $1 != $CURRENT_BG ]]; then
local -a intermediate=($(fg_color $CURRENT_BG) $(bg_color $1))
debug "pre prompt " $(ansi intermediate[@])
PR="$PR $(ansi intermediate[@])$SEGMENT_SEPARATOR"
debug "post prompt " $(ansi codes[@])
PR="$PR$(ansi codes[@]) "
else
debug "no current BG, codes is $codes[@]"
PR="$PR$(ansi codes[@]) "
fi
CURRENT_BG=$1
[[ -n $3 ]] && PR="$PR$3"
}
# End the prompt, closing any open segments
prompt_end() {
if [[ -n $CURRENT_BG ]]; then
local -a codes=($(text_effect reset) $(fg_color $CURRENT_BG))
PR="$PR $(ansi codes[@])$SEGMENT_SEPARATOR"
fi
local -a reset=($(text_effect reset))
PR="$PR $(ansi reset[@])"
CURRENT_BG=''
}
### virtualenv prompt
prompt_virtualenv() {
if [[ -n $VIRTUAL_ENV ]]; then
# Python could output the version information in both stdout and
# stderr (e.g. if using pyenv, the output goes to stderr).
VERSION_OUTPUT=$($VIRTUAL_ENV/bin/python --version 2>&1)
# The last word of the output of `python --version`
# corresponds to the version number.
VENV_VERSION=$(echo $VERSION_OUTPUT | awk '{print $NF}')
color=cyan
prompt_segment $color $PRIMARY_FG
prompt_segment $color white "$(basename $VENV_VERSION)"
fi
}
### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown
# Context: user@hostname (who am I and where am I)
prompt_context() {
local user=`whoami`
if [[ $user != $DEFAULT_USER || -n $SSH_CLIENT ]]; then
prompt_segment black default "$user@\h"
fi
}
# prints history followed by HH:MM, useful for remembering what
# we did previously
prompt_histdt() {
prompt_segment black default "\!" # \A"
}
git_status_dirty() {
dirty=$(git status -s 2> /dev/null | tail -n 1)
[[ -n $dirty ]] && echo " ●"
}
# Git: branch/detached head, dirty status
prompt_git() {
local ref dirty
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
ZSH_THEME_GIT_PROMPT_DIRTY='±'
dirty=$(git_status_dirty)
ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="$(git show-ref --head -s --abbrev |head -n1 2> /dev/null)"
if [[ -n $dirty ]]; then
prompt_segment yellow black
else
prompt_segment green black
fi
PR="$PR${ref/refs\/heads\// }$dirty"
fi
}
# Dir: current working directory
prompt_dir() {
prompt_segment blue black '\W'
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="$(ansi_single $(fg_color red))"
[[ $UID -eq 0 ]] && symbols+="$(ansi_single $(fg_color yellow))"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="$(ansi_single $(fg_color cyan))"
[[ -n "$symbols" ]] && prompt_segment black default "$symbols"
}
######################################################################
#
# experimental right prompt stuff
# requires setting prompt_foo to use PRIGHT vs PR
# doesn't quite work per above
rightprompt() {
printf "%*s" $COLUMNS "$PRIGHT"
}
# quick right prompt I grabbed to test things.
__command_rprompt() {
local times= n=$COLUMNS tz
for tz in ZRH:Europe/Zurich PIT:US/Eastern \
MTV:US/Pacific TOK:Asia/Tokyo; do
[ $n -gt 40 ] || break
times="$times ${tz%%:*}\e[30;1m:\e[0;36;1m"
times="$times$(TZ=${tz#*:} date +%H:%M:%S)\e[0m"
n=$(( $n - 10 ))
done
[ -z "$times" ] || printf "%${n}s$times\\r" ''
}
# PROMPT_COMMAND=__command_rprompt
# this doens't wrap code in \[ \]
ansi_r() {
local seq
local -a mycodes2=("${!1}")
debug "ansi: ${!1} all: $* aka ${mycodes2[@]}"
seq=""
local i
for ((i = 0; i < ${#mycodes2[@]}; i++)); do
if [[ -n $seq ]]; then
seq="${seq};"
fi
seq="${seq}${mycodes2[$i]}"
done
debug "ansi debug:" '\\[\\033['${seq}'m\\]'
echo -ne '\033['${seq}'m'
# PR="$PR\[\033[${seq}m\]"
}
# Begin a segment on the right
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_right_segment() {
local bg fg
local -a codes
debug "Prompt right"
debug "Prompting $1 $2 $3"
# if commented out from kruton's original... I'm not clear
# if it did anything, but it messed up things like
# prompt_status - Erik 1/14/17
# if [[ -z $1 || ( -z $2 && $2 != default ) ]]; then
codes=("${codes[@]}" $(text_effect reset))
# fi
if [[ -n $1 ]]; then
bg=$(bg_color $1)
codes=("${codes[@]}" $bg)
debug "Added $bg as background to codes"
fi
if [[ -n $2 ]]; then
fg=$(fg_color $2)
codes=("${codes[@]}" $fg)
debug "Added $fg as foreground to codes"
fi
debug "Right Codes: "
# local -p codes
# right always has a separator
# if [[ $CURRENT_RBG != NONE && $1 != $CURRENT_RBG ]]; then
# $CURRENT_RBG=
# fi
local -a intermediate2=($(fg_color $1) $(bg_color $CURRENT_RBG) )
# PRIGHT="$PRIGHT---"
debug "pre prompt " $(ansi_r intermediate2[@])
PRIGHT="$PRIGHT$(ansi_r intermediate2[@])$RIGHT_SEPARATOR"
debug "post prompt " $(ansi_r codes[@])
PRIGHT="$PRIGHT$(ansi_r codes[@]) "
# else
# debug "no current BG, codes is $codes[@]"
# PRIGHT="$PRIGHT$(ansi codes[@]) "
# fi
CURRENT_RBG=$1
[[ -n $3 ]] && PRIGHT="$PRIGHT$3"
}
######################################################################
## Emacs prompt --- for dir tracking
# stick the following in your .emacs if you use this:
# (setq dirtrack-list '(".*DIR *\\([^ ]*\\) DIR" 1 nil))
# (defun dirtrack-filter-out-pwd-prompt (string)
# "dirtrack-mode doesn't remove the PWD match from the prompt. This does."
# ;; TODO: support dirtrack-mode's multiline regexp.
# (if (and (stringp string) (string-match (first dirtrack-list) string))
# (replace-match "" t t string 0)
# string))
# (add-hook 'shell-mode-hook
# #'(lambda ()
# (dirtrack-mode 1)
# (add-hook 'comint-preoutput-filter-functions
# 'dirtrack-filter-out-pwd-prompt t t)))
prompt_emacsdir() {
# no color or other setting... this will be deleted per above
PR="DIR \w DIR$PR"
}
######################################################################
## Main prompt
build_prompt() {
[[ ! -z ${AG_EMACS_DIR+x} ]] && prompt_emacsdir
prompt_status
[[ -z ${AG_NO_HIST+x} ]] && prompt_histdt
#[[ -z ${AG_NO_CONTEXT+x} ]] && prompt_context
prompt_virtualenv
prompt_dir
prompt_git
prompt_end
}
# from orig...
# export PS1='$(ansi_single $(text_effect reset)) $(build_prompt) '
# this doesn't work... new model: create a prompt via a PR variable and
# use that.
_omb_theme_PROMPT_COMMAND() {
local RETVAL=$?
local PR=""
local PRIGHT=""
local CURRENT_BG=NONE
PR="$(ansi_single $(text_effect reset))"
build_prompt
PS1=""
# date randomly or once per hour
if (( $(shuf -i 1-20 -n 1 --random-source=/dev/urandom) == 1 )) ; then #TK || (($PSDATE != $(date +%H))) ; then
PS1+="\$(date +%a) $(date +%Y-%m-%d) "
fi
PSDATE=$(date +%H)
# ... and time
PS1+="$(date +%H:%M:%S) "
# uncomment below to use right prompt
# PS1='\[$(tput sc; printf "%*s" $COLUMNS "$PRIGHT"; tput rc)\]'$PR
PS1+=$PR
}
_omb_util_add_prompt_command _omb_theme_PROMPT_COMMAND

370
tools/bash-preexec.sh Normal file
View File

@@ -0,0 +1,370 @@
# 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.5.0
#
# 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.
# Tell shellcheck what kind of file this is.
# shellcheck shell=bash
# Make sure this is bash that's running and return otherwise.
# Use POSIX syntax for this line:
if [ -z "${BASH_VERSION-}" ]; then
return 1;
fi
# We only support Bash 3.1+.
# Note: BASH_VERSINFO is first available in Bash-2.0.
if [[ -z "${BASH_VERSINFO-}" || BASH_VERSINFO[0] -lt 3 || BASH_VERSINFO[0] -eq 3 && BASH_VERSINFO[1] -lt 1 ]]; then
return 1
fi
# Avoid duplicate inclusion
if [[ -n "${bash_preexec_imported:-}" ]]; then
return 0
fi
bash_preexec_imported="defined"
# WARNING: This variable is no longer used and should not be relied upon.
# Use ${bash_preexec_imported} instead.
# shellcheck disable=SC2034
__bp_imported="${bash_preexec_imported}"
# 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:-}"
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=""
# These arrays are used to add functions to be run before, or after, prompts.
declare -a precmd_functions
declare -a preexec_functions
# 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_PIPESTATUS may be unused, ignore
# shellcheck disable=SC2034
__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
__bp_set_ret_value "$__bp_last_ret_value"
}
# 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:+"$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
# we can't easily do this with variable expansion. Leaving as sed command.
# shellcheck disable=SC2001
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:-}"
existing_prompt_command="${existing_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() {
# 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;