From 1f4657d7c79356ef1603e19e971214ffe09f8916 Mon Sep 17 00:00:00 2001 From: Stefan Gaiselmann Date: Sat, 13 Feb 2021 10:38:22 +0100 Subject: [PATCH] Changed PS1 creation to a more flexible solution. - added color parameter - used tput to set colors - fixed missing escape sequences --- .chezmoi.toml.tmpl | 2 + dot_bashrc.d/executable_10.prompt.tmpl | 150 ++++++++++++++++--------- 2 files changed, 99 insertions(+), 53 deletions(-) diff --git a/.chezmoi.toml.tmpl b/.chezmoi.toml.tmpl index e83ca56..20266cf 100644 --- a/.chezmoi.toml.tmpl +++ b/.chezmoi.toml.tmpl @@ -1,8 +1,10 @@ {{- $name := promptString "name" -}} {{- $email := promptString "email" -}} {{- $category := promptString "Server category" -}} +{{- $color := promptString "Prompt color (see https://unix.stackexchange.com/questions/269077/tput-setaf-color-table-how-to-determine-color-codes)" -}} [data] name = "{{ $name }}" email = "{{ $email }}" bash.prompt.category = "{{ $category }}" + bash.prompt.color = "{{ $color }}" diff --git a/dot_bashrc.d/executable_10.prompt.tmpl b/dot_bashrc.d/executable_10.prompt.tmpl index a798f14..5fd671f 100644 --- a/dot_bashrc.d/executable_10.prompt.tmpl +++ b/dot_bashrc.d/executable_10.prompt.tmpl @@ -4,6 +4,7 @@ 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}}" @@ -12,79 +13,122 @@ 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)" + # printf '\e[38;5;160m%-5s' "✕ ❗ ($exit)" + echo "$(colf 1)❗($exit)" else - printf '✔' + echo '✔' fi } -# sourcing Solarized Theme is already done in .bashrc.d -echo "mySystem: $mySystem" -echo "myFQDN: $myFQDN" +# 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) \]" +} -bg_type="\[\e[48;5;254m\]" -bg_reset="\[\e[49m\]" -fg_reset="\[\e[39m\]" -fg_system="${sol_base3}" +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}" -{{- if eq .bash.prompt.category "Materna" }} -# Materna cyan -bg_system="${sol_bg_cyan}" -fg_type="${sol_cyan}" -{{- else if eq .bash.prompt.category "AWS" }} -# AWS specific color -bg_system="${sol_bg_orange}" -fg_type="${sol_orange}" -{{- else }} -# Default color -bg_system="${sol_bg_magenta}" -fg_type="${sol_magenta}" -{{- end }} + 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 " +} -bg_type="${sol_bg_base3}" -fg_arrow1="${fg_type}" -fg_arrow2="${sol_base3}" +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 [ "$myNum" != "0" ]; then - TYPE="$TYPE ($myNum)" -fi -echo "System Prompt: $SYSTEM_PROMPT" - -SYSTEM_PROMPT="\n${fg_arrow1}░▒▓${bg_system} ${fg_system}${myCategory} ${bg_type}${fg_arrow1}▓▒░" -TYPE_PROMPT="$TYPE  \$(lastExitCode) ${fg_arrow1}  $myPrettyName ${sol_blue}\${STY#[0-9]*.} ${bg_reset}${fg_arrow2}▓▒░" - -PROMPT="${SYSTEM_PROMPT} ${TYPE_PROMPT}" -DATE="\[\e[0;32m\]\d \t" -if [ "$(whoami)" = "root" ]; then - DIR="\[\e[97;49m\]\[\e[0;91;107m\] \u@\h \[\e[97;49m\]\[\e[39;49m\] in \[\e[0;32m\]\w" -else - DIR="\[\e[0;32m\]\u@\h \[\e[31m\]☆ \[\e[0;32m\]\w" + TYPE=" $myType " fi -# PROMPT_COMMAND=promptcmd - -PS1="${PROMPT}\[\e[0m\] \n${DIR}\n${DATE} → \[\e[0m\]" - -if [ $(whoami | grep root | wc -l) == 1 ]; then - echo -e "\e[31;107m┌─────────────────────────────┐\e[0m" - echo -e "\e[31;107m│ YOU ARE ROOT, BE CAREFUL! │\e[0m" - echo -e "\e[31;107m└─────────────────────────────┘\e[0m" - - # Test mintty - echo -ne '\e]11;44,10,10\a' -fi +setPrompt "${myCategory}" "${myPrettyName}" ${1:-6} +# color_my_prompt # TODO: background colors... # TODO: private history file for shared accounts