Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fancy, re-configurable prompt.
# The code below provides an elaborate system for composing custom prompts.
# Of these the `myprompt`, `devprompt` and `longprompt` are the most useful.
# Fancy, reconfigurable prompt.
multiprompt () {
parse_git_branch () {
if [[ -n $promptgit ]] && which git 2>&1 > /dev/null; then
ref=$(git symbolic-ref HEAD 2> /dev/null) || return 0
ref="${ref#refs/heads/}"
if [[ $ref != 'master' ]]; then
echo -n "^${ref#refs/heads/}"
fi
fi
}
exit_value_indicator () {
for stat in ${PIPESTATUS[*]}; do stat_sum=$(($stat_sum + $stat)); done
[ $stat_sum == 0 ] && echo ':)' || echo ':('
}
rvm_info_for_prompt () {
if [[ -n $promptrvm ]] && type -t rvm-prompt > /dev/null; then
rvm_result="$(rvm-prompt)"
if [[ "${rvm_result}" != 'system' ]] && [[ "${rvm_result}" != '' ]]; then echo "${rvm_result}"; fi
fi
}
path_info_for_prompt () {
if [[ -n $promptlong ]]; then
echo "\w"
else
echo "\W"
fi
}
name_info_for_prompt () {
if [[ -n $promptless ]]; then
echo ''
else
echo '${debian_chroot:+($debian_chroot)}\u@\h:'
fi
}
misc_info_for_prompt () {
if [[ -n $promptless ]]; then
echo ''
else
echo ' #\! \A'
fi
}
export PROMPT_COMMAND='export PS1="$(name_info_for_prompt)$(path_info_for_prompt)\$(parse_git_branch)\$(rvm_info_for_prompt)$(misc_info_for_prompt) \$(exit_value_indicator) "'
eval "$PROMPT_COMMAND"
}
multiprompt # Activate
# Reset prompt to defaults, e.g.
# myusername@mycomputer:myproject #28 23:52 :)
myprompt () {
unset promptgit
unset promptrvm
unset promptlong
unset promptless
multiprompt
}
# Toggle visibility of RVM information, e.g.
# myusername@mycomputer:myproject ∴ ree-1.8.7-2011.03@everything #29 23:52 :)
rvmprompt () {
if [[ -z $promptrvm ]]; then
export promptrvm=1
else
unset promptrvm
fi
multiprompt
}
# Toggle visibility of Git branch information, e.g.
# myusername@mycomputer:myproject^mybranch #30 23:52 :)
gitprompt () {
if [[ -z $promptgit ]]; then
export promptgit=1
else
unset promptgit
fi
multiprompt
}
# Toggle display of full path name, e.g.
# myusername@mycomputer:~/workspace/myproject #32 23:53 :)
longprompt () {
if [[ -z $promptlong ]]; then
export promptlong=1
else
unset promptlong
fi
multiprompt
}
# Toggle display of extra information, e.g.
# myproject :)
lessprompt () {
if [[ -z $promptless ]]; then
export promptless=1
else
unset promptless
fi
multiprompt
}
# Show prompt with dev information, e.g.
# myproject^mybranch ∴ ree-1.8.7-2011.03@everything :)
devprompt () {
export promptless=1
unset promptlong
export promptgit=1
export promptrvm=1
multiprompt
}
# Simple, with truncated path, e.g
# myusername@mycomputer:mypath 12 >>
simpleprompt () {
export PS1="\u@\h:\W/ \! >> "
}
# The code below provides an elaborate system for composing custom prompts.
# Of these the `myprompt`, `devprompt` and `longprompt` are the most useful.
# Fancy, reconfigurable prompt.
multiprompt () {
parse_git_branch () {
if [[ -n $promptgit ]] && which git 2>&1 > /dev/null; then
ref=$(git symbolic-ref HEAD 2> /dev/null) || return 0
ref="${ref#refs/heads/}"
if [[ $ref != 'master' ]]; then
echo -n "^${ref#refs/heads/}"
fi
fi
}
exit_value_indicator () {
for stat in ${PIPESTATUS[*]}; do stat_sum=$(($stat_sum + $stat)); done
[ $stat_sum == 0 ] && echo ':)' || echo ':('
}
rvm_info_for_prompt () {
if [[ -n $promptrvm ]] && type -t rvm-prompt > /dev/null; then
rvm_result="$(rvm-prompt)"
if [[ "${rvm_result}" != 'system' ]] && [[ "${rvm_result}" != '' ]]; then echo " ∴ ${rvm_result}"; fi
fi
}
path_info_for_prompt () {
if [[ -n $promptlong ]]; then
echo "\w"
else
echo "\W"
fi
}
name_info_for_prompt () {
if [[ -n $promptless ]]; then
echo ''
else
echo '${debian_chroot:+($debian_chroot)}\u@\h:'
fi
}
misc_info_for_prompt () {
if [[ -n $promptless ]]; then
echo ''
else
echo ' #\! \A'
fi
}
export PROMPT_COMMAND='export PS1="$(name_info_for_prompt)$(path_info_for_prompt)\$(parse_git_branch)\$(rvm_info_for_prompt)$(misc_info_for_prompt) \$(exit_value_indicator) "'
eval "$PROMPT_COMMAND"
}
multiprompt # Activate
# Reset prompt to defaults, e.g.
# myusername@mycomputer:myproject #28 23:52 :)
myprompt () {
unset promptgit
unset promptrvm
unset promptlong
unset promptless
multiprompt
}
# Toggle visibility of RVM information, e.g.
# myusername@mycomputer:myproject ∴ ree-1.8.7-2011.03@everything #29 23:52 :)
rvmprompt () {
if [[ -z $promptrvm ]]; then
export promptrvm=1
else
unset promptrvm
fi
multiprompt
}
# Toggle visibility of Git branch information, e.g.
# myusername@mycomputer:myproject^mybranch #30 23:52 :)
gitprompt () {
if [[ -z $promptgit ]]; then
export promptgit=1
else
unset promptgit
fi
multiprompt
}
# Toggle display of full path name, e.g.
# myusername@mycomputer:~/workspace/myproject #32 23:53 :)
longprompt () {
if [[ -z $promptlong ]]; then
export promptlong=1
else
unset promptlong
fi
multiprompt
}
# Toggle display of extra information, e.g.
# myproject :)
lessprompt () {
if [[ -z $promptless ]]; then
export promptless=1
else
unset promptless
fi
multiprompt
}
# Show prompt with dev information, e.g.
# myproject^mybranch ∴ ree-1.8.7-2011.03@everything :)
devprompt () {
export promptless=1
unset promptlong
export promptgit=1
export promptrvm=1
multiprompt
}
# Simple, with truncated path, e.g
# myusername@mycomputer:mypath 12 >>
simpleprompt () {
export PS1="\u@\h:\W/ \! >> "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment