New awesome minimalistic Bash prompt.
This commit is contained in:
parent
42f12300ed
commit
b255959cb8
@ -1,89 +1,64 @@
|
||||
# Inspired by: https://github.com/dreadatour/dotfiles/blob/master/.bash_profile
|
||||
# Inspired by: https://github.com/dreadatour/dotfiles/blob/master/.bash_profile & https://github.com/sindresorhus/pure
|
||||
|
||||
# Add to ~/.bashlocal user name you don’t want to see in the prompt: `local_username="admin"`
|
||||
|
||||
# User color
|
||||
case $(id -u) in
|
||||
0) USER_COLOR=$RED ;; # root
|
||||
*) USER_COLOR=$GREEN ;;
|
||||
0) user_color="$RED" ;; # root
|
||||
*) user_color="$GREEN" ;;
|
||||
esac
|
||||
|
||||
# Some kind of optimization - check if Git installed only on config load
|
||||
PS1_GIT_BIN=$(which git 2>/dev/null)
|
||||
# Prompt symbol
|
||||
prompt_symbol="❯"
|
||||
|
||||
function prompt_command() {
|
||||
local PS1_GIT=
|
||||
local GIT_BRANCH=
|
||||
local GIT_DIRTY=
|
||||
local PWDNAME="$PWD"
|
||||
|
||||
# Local or SSH session?
|
||||
local remote=
|
||||
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && remote=1
|
||||
|
||||
# Beautify working directory name
|
||||
# Working directory name
|
||||
local dir_name="$PWD"
|
||||
if [ "$HOME" == "$PWD" ]; then
|
||||
PWDNAME="~"
|
||||
dir_name="~"
|
||||
elif [ "$HOME" == "${PWD:0:${#HOME}}" ]; then
|
||||
PWDNAME="~${PWD:${#HOME}}"
|
||||
dir_name="~${PWD:${#HOME}}"
|
||||
fi
|
||||
|
||||
# Parse Git status and get Git variables
|
||||
if [[ ! -z "$PS1_GIT_BIN" ]]; then
|
||||
# Check if we are in Git repo
|
||||
local CUR_DIR="$PWD"
|
||||
while [[ ! -d "$CUR_DIR/.git" ]] && [[ ! "$CUR_DIR" == "/" ]] && [[ ! "$CUR_DIR" == "~" ]] && [[ ! "$CUR_DIR" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done
|
||||
if [[ -d "$CUR_DIR/.git" ]]; then
|
||||
# Get Git branch
|
||||
GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)"
|
||||
if [[ ! -z "$GIT_BRANCH" ]]; then
|
||||
GIT_BRANCH="${GIT_BRANCH#refs/heads/}"
|
||||
# Git branch name and work tree status (only when we are inside Git working tree)
|
||||
local git_prompt=
|
||||
if [[ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]]; then
|
||||
# Branch name
|
||||
local branch="$(git symbolic-ref HEAD 2>/dev/null)"
|
||||
branch="${branch##refs/heads/}"
|
||||
|
||||
# Get Git status
|
||||
local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)"
|
||||
[[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1
|
||||
fi
|
||||
fi
|
||||
# Working tree status (red when dirty)
|
||||
local branch_color="$GREEN"
|
||||
git diff --no-ext-diff --quiet --exit-code --ignore-submodules || branch_color="$RED"
|
||||
|
||||
# Format Git info
|
||||
git_prompt=" #$branch_color$branch$NOCOLOR"
|
||||
fi
|
||||
|
||||
# Build B&W prompt for Git
|
||||
[[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #$GIT_BRANCH"
|
||||
# Only show username if not default
|
||||
local user_prompt=
|
||||
[ "$USER" != "$local_username" ] && user_prompt="$user_color$USER$NOCOLOR"
|
||||
|
||||
# Calculate prompt length
|
||||
local host_length=0
|
||||
[ -n "$remote" ] && host_length=$((${#HOSTNAME}+1))
|
||||
local PS1_length=$((${#USER}+${#PWDNAME}+${#PS1_GIT}+$host_length+2))
|
||||
local FILL=
|
||||
|
||||
# If length is greater, than terminal width
|
||||
if [[ $PS1_length -gt $COLUMNS ]]; then
|
||||
# Strip working directory name
|
||||
PWDNAME="...${PWDNAME:$(($PS1_length-$COLUMNS+3))}"
|
||||
else
|
||||
# Calculate fillsize
|
||||
local fillsize=$(($COLUMNS-$PS1_length))
|
||||
FILL=$GRAY
|
||||
while [[ $fillsize -gt 0 ]]; do FILL=$FILL"─"; fillsize=$(($fillsize-1)); done
|
||||
FILL=$FILL$NOCOLOR
|
||||
fi
|
||||
|
||||
# Git status for prompt
|
||||
if [ ! -z "$GIT_BRANCH" ]; then
|
||||
local BRANCH_COLOR=$GREEN
|
||||
[ ! -z $GIT_DIRTY ] && BRANCH_COLOR=$RED
|
||||
PS1_GIT=" #$BRANCH_COLOR$GIT_BRANCH$NOCOLOR"
|
||||
fi
|
||||
|
||||
# Set new color prompt
|
||||
# Show hostname inside SSH session
|
||||
local host_prompt=
|
||||
[ -n "$remote" ] && host_prompt="@$YELLOW$HOSTNAME$NOCOLOR"
|
||||
PS1="$USER_COLOR$USER$NOCOLOR$host_prompt:$WHITE$PWDNAME$NOCOLOR$PS1_GIT $FILL\n→ "
|
||||
|
||||
# Show delimiter if user or host visible
|
||||
local login_delimiter=
|
||||
[ -n "$user_prompt" ] || [ -n "$host_prompt" ] && login_delimiter=":"
|
||||
|
||||
# Format prompt
|
||||
PS1="\n$user_prompt$host_prompt$login_delimiter$WHITE$dir_name$NOCOLOR$git_prompt\n$CYAN$prompt_symbol$NOCOLOR "
|
||||
|
||||
# Terminal title
|
||||
local TITLE=$(basename $PWDNAME)
|
||||
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && TITLE="$TITLE \xE2\x80\x94 $HOSTNAME"
|
||||
echo -ne "\033]0;$TITLE"; echo -ne "\007"
|
||||
local title="$(basename $dir_name)"
|
||||
[ -n "$remote" ] && title="$title \xE2\x80\x94 $HOSTNAME"
|
||||
echo -ne "\033]0;$title"; echo -ne "\007"
|
||||
}
|
||||
|
||||
# Set prompt command (title update and color prompt)
|
||||
PROMPT_COMMAND=prompt_command
|
||||
# Set new B&W prompt (will be overwritten in `prompt_command` later with color prompt)
|
||||
PS1='\u@\h:\w\$ '
|
||||
# Show awesome prompt only if Git is istalled
|
||||
command -v git >/dev/null 2>&1 && PROMPT_COMMAND=prompt_command
|
||||
|
Loading…
Reference in New Issue
Block a user