dotfiles/dot_bashrc.d/executable_10.prompt.tmpl

167 lines
5.2 KiB
Cheetah

#
# System Name and type detectedn
#
mySystem=$(uname -n)
if dnsdomainname &> /dev/null ; then
myFQDN=$(uname -n).$(dnsdomainname)
else
myFQDN=$(uname -n)
fi
myCategory="{{.bash.prompt.category}}"
myColor="{{.bash.prompt.color}}"
myType="{{.chezmoi.hostname}}"
{{ if eq .chezmoi.os "windows" -}}
myPrettyName="{{.chezmoi.os}}"
{{ else -}}
myPrettyName="{{.chezmoi.osRelease.prettyName}}"
{{ end -}}
myNum=0
# Helper functions go set colors with tput
function colf() {
local color=$1
tput setaf "$color"
}
function colb() {
local color=$1
tput setab "$color"
}
function res() {
tput sgr0
}
# last exit code
function lastExitCode {
local exit="$?"
if [ $exit != 0 ];then
# printf '\e[38;5;160m%-5s' "✕ ❗ ($exit)"
echo "$(colf 1)(⚡$exit)"
else
echo '✓'
fi
}
# TODO: switch to use themes
# So far the colors used:
# - green (2) for directory
# - blue (4) for git prompt
# - red (1) for 'star'
# Color sequences must be escaped with '\[ ... \]
# See https://unix.stackexchange.com/questions/105958/terminal-prompt-not-wrapping-correctly
function hostPrompt() {
local system=$1
# echo "\[$(colf "$PROMPT_COLOR")\]░▒▓\[$(colb "$PROMPT_COLOR")\]\[$(colf 7)$(tput bold)\] $system \[$(res)$(colf "$PROMPT_COLOR")$(colb 7)\]▓▒░ \h"
echo "$system \[\033[0;34m\]──\[\033[1;34m\] $myPrettyName\[\033[0;34m\]"
}
function statusPrompt() {
local type=$1
echo "\[$(colf "$PROMPT_COLOR")\]  \$(lastExitCode)\[$(colf "$PROMPT_COLOR")\]  $type \[$(res)\]\[$(colf 7)\]▓▒░"
}
function userPrompt() {
echo "\[$(res)\]\[$(colf 2)\]\u@\h \[$(colf 1)\]☆ \[$(colf 2)\]\w \[$(colf 1)\]☆ \[$(colf 4)\]\$(parse_git_branch) "
}
function datePrompt() {
# echo "\[$(colf 2)\]\D{%Y-%m-%d %H:%M:%S}"
#echo "\[$(colf 2) \]"
echo "\[\033[1;34m\]\D{%Y-%m-%d %H:%M:%S}\[\033[0;34m\]"
}
function setPrompt() {
local category=$1
local type=$2
PROMPT_COLOR=$3
SYSTEM_PROMPT="$(hostPrompt "$category")"
TYPE_PROMPT="$(statusPrompt "$type")"
USER_PROMPT="$(userPrompt) "
DATE="$(datePrompt)"
PROMPT="${SYSTEM_PROMPT} ${TYPE_PROMPT}\n${USER_PROMPT}\n${DATE}"
# PS1="${PROMPT} → \[$(res)\]"
local LINECOL="\[\033[0;34m\]"
local COL1="\[\033[1;32m\]"
local COL2="\[\033[1;34m\]"
local COL3="\[\033[1;36m\]"
local CLEAR="\[\033[0;\]"
local DIV="${LINECOL}──"
local LINE1="${LINECOL}\n┌─ ${COL2}${category} ${DIV} ${COL2}${myPrettyName} ${DIV} ${DATE}"
local LINE2="${LINECOL}\n├─ ${COL1}\u@\h ${DIV} ${COL1}\w ${CLEAR}"
local LINE3="${LINECOL}\n└> \$ \[\033[0m\]"
PS1="$LINE1 $LINE2 $LINE3"
}
# example from http://stackoverflow.com/questions/4133904/ps1-line-with-git-current-branch-and-colors
function color_my_prompt {
local __user_and_host="\[\033[01;32m\]\u@\h"
local __cur_location="\[\033[01;34m\]\w"
local __git_branch_color="\[\033[31m\]"
#local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
local __prompt_tail="\[\033[35m\]$"
local __last_color="\[\033[00m\]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
function parse_git_branch {
# echo -n $(git branch --no-color 2>/dev/null | awk -v out=$1 '/^*/ { if(out=="") print $2; else print out}')
git_branch="$(git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\*\ \(.+\)$/\(\ \\\1\)\ /)"
if [ -z "$git_branch" ];
then
git_branch="(no git branch) "
else
local git_dir="$(git rev-parse --git-dir)"
if [ -f $git_dir/logs/refs/stash ];
then
local git_stash="$(wc -l $git_dir/logs/refs/stash | sed -E 's/^([0-9]+).*/\1/')"
else
local git_stash="0"
fi
if [ $git_stash -gt 0 ];
then
git_branch=$(echo $git_branch | sed -E "s/^(.*)\)$/\1\|stash:${git_stash}\)/")
fi
fi
echo -n -e "$git_branch"
}
# fast version form https://stackoverflow.com/questions/4485059/git-bash-is-extremely-slow-on-windows-7-x64
fast_git_ps1 ()
{
printf -- "$(git branch 2>/dev/null | sed -ne '/^\* / s/^\* \(.*\)/ [\1] / p')"
}
# Set backgroud color of prompt, default to magenta
# Color #define Value RGB
# black COLOR_BLACK 0 0, 0, 0
# red COLOR_RED 1 max,0,0
# green COLOR_GREEN 2 0,max,0
# yellow COLOR_YELLOW 3 max,max,0
# blue COLOR_BLUE 4 0,0,max
# magenta COLOR_MAGENTA 5 max,0,max
# cyan COLOR_CYAN 6 0,max,max
# white COLOR_WHITE 7 max,max,max
# setPrompt "${myCategory}" "${myPrettyName}" "${myColor:-5}"
if [[ -z "${myType// }" ]]; then
TYPE=$(uname -n)
else
TYPE=" $myType "
fi
if command -v 2>&1 >/dev/null; then
echo "Found starship, using it"
STARSHIP_EXEC="$(which starship)"
eval "$($STARSHIP_EXEC init bash)"
else
echo "No starship, using custom PS1"
setPrompt "${myCategory}" "${myPrettyName}" ${myColor:-6}
fi
# color_my_prompt
# TODO: background colors...
# TODO: private history file for shared accounts
# TODO: vim