Last active
May 13, 2019 21:20
-
-
Save id-ilych/ee47791d3461e31e5df8deccd9183deb to your computer and use it in GitHub Desktop.
This file contains 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
build_prompt () { | |
local lhs=$1 | |
local rhs=$2 | |
local save='\e[s' # Save cursor position | |
local rest='\e[u' # Restore cursor to save point | |
# Strip ANSI commands before counting length | |
# From: https://www.commandlinefu.com/commands/view/12043/remove-color-special-escape-ansi-codes-from-text-with-sed | |
rhs_stripped=$(sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" <<<"$rhs") | |
echo "\[${save}\e[${COLUMNS:-$(tput cols)}C\e[${#rhs_stripped}D${rhs}${rest}\]${lhs}" | |
} | |
set_color_prompt () { | |
local err="$?" | |
local rhs=$(printf "\e[38;5;239m[%(%d.%m.%y %H:%M:%S)T]\e[0m" -1) # -1 is current time | |
local lhs="${debian_chroot:+($debian_chroot)}\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]\n" | |
if [[ $err != 0 ]]; then | |
lhs+="\[\e[01;91m\][$err] \[\e[00m\]" | |
fi | |
lhs+="\[\e[01;34m\]\$\[\e[00m\] " | |
PS1=$(build_prompt "$lhs" "$rhs") | |
} | |
set_gray_prompt () { | |
local err="$?" | |
local rhs=$(printf "[%(%d.%m.%y %H:%M:%S)T]" -1) # -1 is current time | |
local lhs="${debian_chroot:+($debian_chroot)}\u@\h:\w \D{%F %T}\n" | |
if [[ $err != 0 ]]; then | |
lhs+="[$err] " | |
fi | |
lhs+="\$ " | |
PS1=$(build_prompt "$lhs" "$rhs") | |
} | |
if [ "$color_prompt" = yes ]; then | |
PROMPT_COMMAND=set_color_prompt | |
else | |
PROMPT_COMMAND=set_gray_prompt | |
fi | |
unset color_prompt force_color_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment