2017-02-07 16:16:45 -06:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
COLOR_RESET='\[\e[0m\]'
|
|
|
|
PROMPT_SUCCESS_COLOR='\[\e[0;34m\]'
|
|
|
|
PROMPT_FAILURE_COLOR='\[\e[0;31m\]'
|
2018-10-23 14:37:57 -05:00
|
|
|
DIR_COLOR='\[\e[0;35m\]'
|
2017-02-07 16:16:45 -06:00
|
|
|
|
|
|
|
# prompt rendering functions
|
|
|
|
preprocess_pwd() {
|
|
|
|
name="$PWD"
|
|
|
|
# if we're in the home directory, replace it with tilde
|
2018-10-23 14:37:57 -05:00
|
|
|
# [[ "$name" =~ ^"$NICE_HOME"(/|$) ]] && name="~${name#$NICE_HOME}"
|
|
|
|
[[ "$name" == "${NICE_HOME}" ]] && name="~"
|
2017-02-07 16:16:45 -06:00
|
|
|
|
|
|
|
# replace all non-basename parts of the PWD with only the first two letters
|
|
|
|
curdir=$(echo "$PWD" | sed -r 's|.*/(.+)$|\1|g')
|
2018-10-23 14:37:57 -05:00
|
|
|
name=$(echo "$name" | sed -r 's|/(...)[^/]*|/\1|g' | sed -r 's|(.*/)(.+)$|\1|g')
|
2017-02-07 16:16:45 -06:00
|
|
|
|
2018-10-23 14:37:57 -05:00
|
|
|
# if we're just in the home or root directory, don't show any path stuff
|
|
|
|
[[ "$name" == "/" ]] && curdir=""
|
2017-02-07 16:16:45 -06:00
|
|
|
[[ "$name" == "~" ]] && curdir=""
|
|
|
|
|
|
|
|
# return our transformed PWD
|
|
|
|
echo "$name$curdir"
|
|
|
|
}
|
|
|
|
export -f "preprocess_pwd"
|
|
|
|
|
|
|
|
prompt_command_func()
|
|
|
|
{
|
2019-05-30 14:31:34 -05:00
|
|
|
# commit history to prevent data loss from edge cases
|
|
|
|
history -a
|
2017-02-07 16:16:45 -06:00
|
|
|
RET=$?
|
|
|
|
# set the color of the user and host based on the result of the previous
|
|
|
|
# command
|
|
|
|
if [ $RET -eq 0 ]; then
|
|
|
|
STATUS_COLOR=$PROMPT_SUCCESS_COLOR
|
|
|
|
else
|
|
|
|
STATUS_COLOR=$PROMPT_FAILURE_COLOR
|
|
|
|
fi;
|
|
|
|
PS1="$STATUS_COLOR\u@\h$COLOR_RESET $DIR_COLOR$(eval "preprocess_pwd")$COLOR_RESET "
|
|
|
|
}
|
|
|
|
export -f "prompt_command_func"
|
|
|
|
export PROMPT_COMMAND="prompt_command_func"
|
|
|
|
|
|
|
|
|