1
0
Fork 0
dotfiles/bash/prompt.sh

74 lines
1.9 KiB
Bash
Raw Normal View History

2020-09-12 07:58:04 +00:00
GIT="/sbin/git"
2017-03-10 10:42:12 +00:00
function __bash_prompt {
local baseDir=$(__bash_basedir)
2020-09-12 07:58:04 +00:00
__window_title $baseDir
2017-03-10 10:42:12 +00:00
# Define building blocks
2021-04-23 22:26:43 +00:00
local userhost="$_BOLD$_BLUE\u@\h$_RESET_COLOR$_RESET"
local basedir="$_BOLD$_GREEN\W$_RESET_COLOR$_RESET"
2017-03-10 10:42:12 +00:00
local git_y_u_no_commit="$_MAGENTA$(__bash_git_y_u_no_commit_warn)$_RESET_COLOR"
local git_branch="$(__bash_prompt_git)"
local cursor="\[${_DIM}\]\[$_RESET\]"
2021-04-23 22:26:43 +00:00
PS1="\n$userhost $basedir$git_branch$git_y_u_no_commit\n$cursor\[$_RESET\] "
2017-03-10 10:42:12 +00:00
}
PROMPT_COMMAND=__bash_prompt # <--- ta-da!
function __bash_prompt_escape {
echo -ne "\[$1\]"
}
#
# Git prompt
#
function __bash_prompt_git {
__bash_is_git_dir && {
__bash_git_is_clean && \
local branch="$_GREEN$(__bash_git_branch)$_RESET_COLOR" || \
local branch="$_RED$(__bash_git_branch)$_RESET_COLOR"
__bash_git_is_pushed || \
branch="$_UNDERLINED$branch$_RESET_UNDERLINED"
echo -ne " ${_DIM}git:$_RESET_DIM$branch"
}
}
function __bash_git_y_u_no_commit_warn {
__bash_is_git_dir && {
$GIT diff --stat 2>/dev/null | awk -F',' '/files? changed/ { lc += $2 + $3 } END {
if (lc > 100) printf " -- Y U NO COMMIT!? --"
}'
}
}
function __bash_git_branch {
__bash_is_git_dir && {
2019-08-30 10:40:25 +00:00
__bash_is_git_new_repo && {
echo -ne "[NEW REPO]"
} || {
$GIT rev-parse --symbolic-full-name --abbrev-ref HEAD
}
2017-03-10 10:42:12 +00:00
}
}
function __bash_git_is_clean {
__bash_is_git_dir && {
__bash_is_empty $($GIT status --porcelain)
}
}
function __bash_git_is_pushed {
__bash_is_git_dir && __bash_is_empty $($GIT cherry -v "@{upstream}" 2>/dev/null)
}
function __bash_is_git_dir {
if [ -d ".git" ]; then return 0; else return 1; fi
}
2019-08-30 10:40:25 +00:00
# Returns 1 if there are no existing commits in the repo
function __bash_is_git_new_repo {
__bash_is_empty $($GIT rev-list --all --parents --max-count=1)
}