rework the bash PS1 prompt
This commit is contained in:
55
.bashrc
55
.bashrc
@@ -9,20 +9,6 @@
|
|||||||
[[ $- != *i* ]] && return
|
[[ $- != *i* ]] && return
|
||||||
[[ -z "${PS1-}" ]] && return
|
[[ -z "${PS1-}" ]] && return
|
||||||
|
|
||||||
# wrapper for lazily loading __git_ps1 whenever it is actually needed
|
|
||||||
_lazy_git_ps1() {
|
|
||||||
if [[ -z $__GIT_PROMPT_SOURCED ]]; then
|
|
||||||
. /usr/share/bash-completion/completions/git &>/dev/null
|
|
||||||
. /usr/share/git/completion/git-prompt.sh &>/dev/null
|
|
||||||
if [ -d .git ] || git worktree list &>/dev/null; then
|
|
||||||
export __GIT_PROMPT_SOURCED=1
|
|
||||||
__git_ps1 "$@"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
__git_ps1 "$@"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# lazy loading of ssh agents
|
# lazy loading of ssh agents
|
||||||
_lazy_ssh() {
|
_lazy_ssh() {
|
||||||
if [[ -z $SSH_AUTH_SOCK ]]; then
|
if [[ -z $SSH_AUTH_SOCK ]]; then
|
||||||
@@ -43,16 +29,37 @@ _make() {
|
|||||||
-e "s/^.*error.*$/\x1b[31m&\x1b[0m/I" >&2)
|
-e "s/^.*error.*$/\x1b[31m&\x1b[0m/I" >&2)
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
# lazily loads the git utilities, to prevent slowdowns
|
||||||
# envs
|
__lazy_git_ps1() {
|
||||||
#
|
if [ -z "$__GIT_PROMPT_SOURCED__" ]; then
|
||||||
# set PS1
|
if [ -d .git ] || git worktree list &>/dev/null; then
|
||||||
PS1='\[\033[?25h\]' # show cursor
|
. /usr/share/bash-completion/completions/git &>/dev/null
|
||||||
PS1="$PS1"'\[\033[01;35m\]\u@\h' # user@host
|
. /usr/share/git/completion/git-prompt.sh &>/dev/null
|
||||||
PS1="$PS1"'\[\033[00m\]:' # separator
|
export __GIT_PROMPT_SOURCED__=1
|
||||||
PS1="$PS1"'\[\033[01;34m\]\w' # working directory
|
__git_ps1
|
||||||
PS1="$PS1"'\[\033[01;93m\]$(_lazy_git_ps1)' # github branch
|
fi
|
||||||
PS1="$PS1"'\[\033[00m\]\$ ' # shell sign
|
else __git_ps1; fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# regenerates the PS1 prompt
|
||||||
|
__regenprompt() {
|
||||||
|
local err=$? # acquire the error code of the last executed command
|
||||||
|
local git=$(__lazy_git_ps1)
|
||||||
|
|
||||||
|
if [ $err -ne 0 ]; then
|
||||||
|
err="\033[s\033[$((COLUMNS - 4))G${err}\033[u"
|
||||||
|
else unset err; fi
|
||||||
|
|
||||||
|
PS1="\[\033[?25h\]" # show cursor
|
||||||
|
PS1="$PS1\[\033[01;35m\]\u@\h" # user@host
|
||||||
|
PS1="$PS1\[\033[00m\]:" # separator
|
||||||
|
PS1="$PS1\[\033[01;34m\]\w" # working directory
|
||||||
|
PS1="$PS1\[\033[01;93m\]$git" # git branch
|
||||||
|
PS1="$PS1\[\033[01;31m\]\[$err\]" # error code
|
||||||
|
PS1="$PS1\[\033[00m\]\$ " # shell sign
|
||||||
|
}
|
||||||
|
PROMPT_COMMAND=__regenprompt
|
||||||
|
|
||||||
# history settings
|
# history settings
|
||||||
HISTSIZE=2048
|
HISTSIZE=2048
|
||||||
|
|||||||
Reference in New Issue
Block a user