if [[ -t 0 ]] && [[ "$__MCFLY_LOADED" != "loaded" ]]; then
__MCFLY_LOADED="loaded"
export MCFLY_HISTFILE="${HISTFILE:-$HOME/.bash_history}"
if [[ ! -r "${MCFLY_HISTFILE}" ]]; then
echo "McFly: ${MCFLY_HISTFILE} does not exist or is not readable. Please fix this or set HISTFILE to something else before using McFly."
return 1
fi
MCFLY_SESSION_ID="$(command dd if=/dev/urandom bs=256 count=1 2> /dev/null | LC_ALL=C command tr -dc 'a-zA-Z0-9' | command head -c 24)"
export MCFLY_SESSION_ID
MCFLY_PATH=${MCFLY_PATH:-$(command which mcfly)}
if [ -z "$MCFLY_PATH" ]; then
echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.bash."
return 1
fi
export HISTCONTROL="${HISTCONTROL:-ignorespace}"
shopt -s histappend
function mcfly_prompt_command {
local exit_code=$?
if [[ ! -f "${MCFLY_HISTORY}" ]]; then
MCFLY_HISTORY=$(mktemp ${TMPDIR:-/tmp}/mcfly.XXXXXXXX)
export MCFLY_HISTORY
command tail -n100 "${MCFLY_HISTFILE}" >| "${MCFLY_HISTORY}"
fi
history -a "${MCFLY_HISTORY}" $MCFLY_PATH add --exit ${exit_code} --append-to-histfile "${MCFLY_HISTFILE}"
history -cr "${MCFLY_HISTORY}"
return ${exit_code} }
if [ -z "$PROMPT_COMMAND" ]
then
PROMPT_COMMAND="mcfly_prompt_command"
elif [[ ! "$PROMPT_COMMAND" =~ "mcfly_prompt_command" ]]
then
PROMPT_COMMAND="mcfly_prompt_command;${PROMPT_COMMAND#;}"
fi
if [[ $- =~ .*i.* ]]; then
if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
bind -x '"\C-r": "echo \#mcfly: ${READLINE_LINE[@]} >> $MCFLY_HISTORY ; READLINE_LINE= ; mcfly search"'
else
if set -o | grep "vi " | grep -q on; then
bind "'\C-r': '\e0i#mcfly: \e\C-m mcfly search\C-m'"
else
bind "'\C-r': '\C-amcfly: \e# mcfly search\C-m'"
fi
fi
fi
fi