This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/shell/bash/prompt

46 lines
1.2 KiB
Plaintext
Raw Normal View History

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\]'
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
# [[ "$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')
name=$(echo "$name" | sed -r 's|/(...)[^/]*|/\1|g' | sed -r 's|(.*/)(.+)$|\1|g')
2017-02-07 16:16:45 -06: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()
{
# 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"