# # System Name and type detectedn # mySystem=$(uname -n) myFQDN="$(uname -n).$(dnsdomainname)" 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 } # 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" } 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 "$PROMPT_COLOR")\]\$(parse_git_branch) " } function datePrompt() { echo "\[$(colf 2)\]\D{%Y-%m-%d %H:%M:%S}" #echo "\[$(colf 2) \]" } 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)\]" } # 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 setPrompt "${myCategory}" "${myPrettyName}" ${1:-6} # color_my_prompt # TODO: background colors... # TODO: private history file for shared accounts # TODO: vim