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
|
# User color
|
||||||
case $(id -u) in
|
case $(id -u) in
|
||||||
0) USER_COLOR=$RED ;; # root
|
0) user_color="$RED" ;; # root
|
||||||
*) USER_COLOR=$GREEN ;;
|
*) user_color="$GREEN" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Some kind of optimization - check if Git installed only on config load
|
# Prompt symbol
|
||||||
PS1_GIT_BIN=$(which git 2>/dev/null)
|
prompt_symbol="❯"
|
||||||
|
|
||||||
function prompt_command() {
|
function prompt_command() {
|
||||||
local PS1_GIT=
|
|
||||||
local GIT_BRANCH=
|
|
||||||
local GIT_DIRTY=
|
|
||||||
local PWDNAME="$PWD"
|
|
||||||
|
|
||||||
# Local or SSH session?
|
# Local or SSH session?
|
||||||
local remote=
|
local remote=
|
||||||
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && remote=1
|
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && remote=1
|
||||||
|
|
||||||
# Beautify working directory name
|
# Working directory name
|
||||||
|
local dir_name="$PWD"
|
||||||
if [ "$HOME" == "$PWD" ]; then
|
if [ "$HOME" == "$PWD" ]; then
|
||||||
PWDNAME="~"
|
dir_name="~"
|
||||||
elif [ "$HOME" == "${PWD:0:${#HOME}}" ]; then
|
elif [ "$HOME" == "${PWD:0:${#HOME}}" ]; then
|
||||||
PWDNAME="~${PWD:${#HOME}}"
|
dir_name="~${PWD:${#HOME}}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Parse Git status and get Git variables
|
# Git branch name and work tree status (only when we are inside Git working tree)
|
||||||
if [[ ! -z "$PS1_GIT_BIN" ]]; then
|
local git_prompt=
|
||||||
# Check if we are in Git repo
|
if [[ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]]; then
|
||||||
local CUR_DIR="$PWD"
|
# Branch name
|
||||||
while [[ ! -d "$CUR_DIR/.git" ]] && [[ ! "$CUR_DIR" == "/" ]] && [[ ! "$CUR_DIR" == "~" ]] && [[ ! "$CUR_DIR" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done
|
local branch="$(git symbolic-ref HEAD 2>/dev/null)"
|
||||||
if [[ -d "$CUR_DIR/.git" ]]; then
|
branch="${branch##refs/heads/}"
|
||||||
# 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/}"
|
|
||||||
|
|
||||||
# Get Git status
|
# Working tree status (red when dirty)
|
||||||
local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)"
|
local branch_color="$GREEN"
|
||||||
[[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1
|
git diff --no-ext-diff --quiet --exit-code --ignore-submodules || branch_color="$RED"
|
||||||
fi
|
|
||||||
fi
|
# Format Git info
|
||||||
|
git_prompt=" #$branch_color$branch$NOCOLOR"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Build B&W prompt for Git
|
# Only show username if not default
|
||||||
[[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #$GIT_BRANCH"
|
local user_prompt=
|
||||||
|
[ "$USER" != "$local_username" ] && user_prompt="$user_color$USER$NOCOLOR"
|
||||||
|
|
||||||
# Calculate prompt length
|
# Show hostname inside SSH session
|
||||||
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
|
|
||||||
local host_prompt=
|
local host_prompt=
|
||||||
[ -n "$remote" ] && host_prompt="@$YELLOW$HOSTNAME$NOCOLOR"
|
[ -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
|
# Terminal title
|
||||||
local TITLE=$(basename $PWDNAME)
|
local title="$(basename $dir_name)"
|
||||||
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && TITLE="$TITLE \xE2\x80\x94 $HOSTNAME"
|
[ -n "$remote" ] && title="$title \xE2\x80\x94 $HOSTNAME"
|
||||||
echo -ne "\033]0;$TITLE"; echo -ne "\007"
|
echo -ne "\033]0;$title"; echo -ne "\007"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set prompt command (title update and color prompt)
|
# Show awesome prompt only if Git is istalled
|
||||||
PROMPT_COMMAND=prompt_command
|
command -v git >/dev/null 2>&1 && PROMPT_COMMAND=prompt_command
|
||||||
# Set new B&W prompt (will be overwritten in `prompt_command` later with color prompt)
|
|
||||||
PS1='\u@\h:\w\$ '
|
|
||||||
|
Loading…
Reference in New Issue
Block a user