Browse Source

Bash rc for generating a PS1 string... technically working, but not quite

tags/original
Bryan Miller 6 years ago
parent
commit
a7f8c16550
1 changed files with 74 additions and 0 deletions
  1. +74
    -0
      ps1.rc

+ 74
- 0
ps1.rc View File

@@ -0,0 +1,74 @@

_show_last_exit_smilie() {
# If the exit status of the last run command was a 0 (success), show a smile, otherwise a frown
exit_status=$?
if [[ "$exit_status" -ne 0 ]]; then
echo "${NORMAL}[${RED}:(${NORMAL}]"
else
echo "${NORMAL}[${GREEN}:)${NORMAL}]"
fi
}


# This function was modified from one found @ https://www.reddit.com/r/linux/comments/2uf5uu/this_is_my_bash_prompt_which_is_your_favorite/?st=jikgswh4&sh=371ba8bf
_git_status() {
local unknown untracked stash clean ahead behind staged dirty diverged
unknown=$BLUE
untracked=$GREEN
stash=$GREEN
clean=$GREEN
ahead=$YELLOW
behind=$YELLOW
staged=$CYAN
dirty=$RED
diverged=$RED


branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [[ -n "$branch" ]]; then
git_status=$(git status 2> /dev/null)
# If nothing changes the color, we can spot unhandled cases.
color=$unknown
if [[ $git_status =~ 'Untracked files' ]]; then
color=$untracked
branch="${branch}?"
fi
if git stash show &>/dev/null; then
color=$stash
branch="${branch}+"
fi
if [[ $git_status =~ 'working directory clean' ]]; then
color=$clean
fi
if [[ $git_status =~ 'Your branch is ahead' ]]; then
color=$ahead
branch="${branch}>"
fi
if [[ $git_status =~ 'Your branch is behind' ]]; then
color=$behind
branch="${branch}<"
fi
if [[ $git_status =~ 'Changes to be committed' ]]; then
color=$staged
fi
if [[ $git_status =~ 'Changed but not updated' ||
$git_status =~ 'Changes not staged' ||
$git_status =~ 'Unmerged paths' ]]; then
color=$dirty
fi
if [[ $git_status =~ 'Your branch'.+diverged ]]; then
color=$diverged
branch="${branch}!"
fi
echo -n "${NORMAL}|${color}${branch}${NORMAL}|"
fi
}


set_prompt() {
if [ $1 = yes ]; then
echo -n "${debian_chroot:+($debian_chroot)}${GREEN}\u${NORMAL}[${BLUE}\w${NORMAL}]$(_show_last_exit_smilie)$(_git_status)\$ "
else
echo -n "${debian_chroot:+($debian_chroot)}\u@\h:\w\$ "
fi
}

Loading…
Cancel
Save