Last active
April 7, 2020 12:59
-
-
Save emeric-martineau/8c7b3763c1ace42fc2a6ac69d2c960de to your computer and use it in GitHub Desktop.
Funny bash prompt with simple git support (display branch name)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function __show_git_repo_branch { | |
AFTER_HOOK="$1" | |
local CURRENT_BRANCH=$(git branch 2>/dev/null| grep -e '^*' | cut -b 3-) | |
if [ -n "${CURRENT_BRANCH}" ]; then | |
local IS_MODIFIED=$(git status --short | wc -l) | |
PS1+="${__GIT_COLOR}${__GIT_START_CHAR}${__GIT_BRANCH_COLOR}${CURRENT_BRANCH}" | |
if [ "${IS_MODIFIED}" -gt 0 ]; then | |
PS1+="${__GIT_MODIFIED_COLOR}${__GIT_MODIFIED_CHAR}$IS_MODIFIED" | |
fi | |
PS1+="${__GIT_COLOR}${__GIT_END_CHAR}" | |
if [ -n "${AFTER_HOOK}" ]; then | |
${AFTER_HOOK} | |
fi | |
fi | |
} | |
function __show_username { | |
PS1+="${__USERNAME_COLOR}\u" | |
} | |
function __show_exit_code { | |
if [ $__LAST_EXIT_CODE != 0 ]; then | |
PS1+="${__EXIT_CODE_FAIL_COLOR}${__EXIT_CODE_FAIL_CHAR}${__LAST_EXIT_CODE}" # red x with error status | |
else | |
PS1+="${__EXIT_CODE_SUCCESSFULY_COLOR}${__EXIT_CODE_SUCCESSFULY_CHAR}" # green tick | |
fi | |
} | |
function __show_hour { | |
PS1+="${__HOUR_COLOR}\A" # date, e.g. 17:00 | |
} | |
function __show_hostname { | |
PS1+="${__HOSTNAME_COLOR}\h" | |
} | |
function __show_prompt { | |
if [ $(id -u) -eq 0 ]; then | |
PS1+="${__PROMPT_ROOT_USER_COLOR}${__PROMPT_ROOT_USER_CHAR}${__COMMAND_LINE_COLOR}" | |
else | |
PS1+="${__PROMPT_NORMAL_USER_COLOR}${__PROMPT_NORMAL_USER_CHAR}${__COMMAND_LINE_COLOR}" | |
fi | |
} | |
function __show_workdir { | |
PS1+="${__WORKDING_DIR_COLOR}\w" # working directory | |
} | |
function __show_separator { | |
PS1+="${__SEPARATOR_DEFAULT_COLOR}${__SEPARATOR_DEFAULT_CHAR}" | |
} | |
__vte_urlencode() ( | |
# This is important to make sure string manipulation is handled | |
# byte-by-byte. | |
LC_ALL=C | |
str="$1" | |
while [ -n "$str" ]; do | |
safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}" | |
printf "%s" "$safe" | |
str="${str#"$safe"}" | |
if [ -n "$str" ]; then | |
printf "%%%02X" "'$str" | |
str="${str#?}" | |
fi | |
done | |
) | |
__vte_osc7 () { | |
printf "\033]7;file://%s%s\007" "${HOSTNAME:-}" "$(__vte_urlencode "${PWD}")" | |
} | |
function __set_prompt_cool1 { | |
__LAST_EXIT_CODE="$?" # MUST come first | |
# Not bash or zsh? | |
[ -n "$BASH_VERSION" ] || return 0 | |
# Not an interactive shell? | |
[[ $- == *i* ]] || return 0 | |
# Not running under vte? | |
[ "${VTE_VERSION:-0}" -ge 3405 ] || return 0 | |
__GIT_BRANCH_COLOR="\[\e[100;97m\]" | |
__GIT_COLOR="\[\e[94m\]" | |
__GIT_MODIFIED_COLOR="\[\e[31m\]" | |
__USERNAME_COLOR="\[\e[97m\]" | |
__EXIT_CODE_SUCCESSFULY_COLOR="\[\e[42m\]" | |
__EXIT_CODE_FAIL_COLOR="\[\e[41m\]" | |
__HOUR_COLOR="\[\e[100;93m\]" | |
__HOSTNAME_COLOR="\[\e[32m\]" | |
__PROMPT_NORMAL_USER_COLOR="\[\e[m\]" | |
__PROMPT_ROOT_USER_COLOR="\[\e[m\]\[\e[31m\]" | |
__WORKDING_DIR_COLOR="\[\e[1;94m\]" | |
__COMMAND_LINE_COLOR="\[\e[m\]" | |
__SEPARATOR_DEFAULT_COLOR="\[\e[100;93m\]" | |
__EXIT_CODE_SUCCESSFULY_CHAR=" ✔ " | |
__EXIT_CODE_FAIL_CHAR=" ✘ " | |
__PROMPT_NORMAL_USER_CHAR="▶ " | |
__PROMPT_ROOT_USER_CHAR="▷ " | |
__GIT_START_CHAR="[ " | |
__GIT_END_CHAR=" ]" | |
__GIT_MODIFIED_CHAR="*" | |
__SEPARATOR_DEFAULT_CHAR=" " | |
PS1="" # "\[\e[32m\]test\[\e[m\] " | |
__show_exit_code | |
__show_separator | |
__show_hour | |
__show_separator | |
__show_username | |
__show_separator | |
__show_workdir | |
__show_separator | |
__show_git_repo_branch __show_separator | |
__show_prompt | |
local pwd='~'; | |
[ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}; | |
printf "$(__vte_osc7)" | |
} | |
PROMPT_COMMAND=__set_prompt_cool1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment