diff options
author | Dennis Eriksen <d@ennis.no> | 2023-01-04 22:10:24 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-01-04 22:10:24 +0100 |
commit | 6de0f40aceb2a8cc51c75333855d05af833caece (patch) | |
tree | 029f6d1f5b99d7e0caa063c946426827cd0a4502 /prompt_adam3_setup | |
parent | Added new way to calculate the value of the hostname (diff) | |
download | adam3-6de0f40aceb2a8cc51c75333855d05af833caece.tar.gz |
made it possible to specify both fore- and background color, AND boldness. Also, added SHLVL when it gets higher than 10
Diffstat (limited to 'prompt_adam3_setup')
-rw-r--r-- | prompt_adam3_setup | 98 |
1 files changed, 63 insertions, 35 deletions
diff --git a/prompt_adam3_setup b/prompt_adam3_setup index 55e4005..50a1e54 100644 --- a/prompt_adam3_setup +++ b/prompt_adam3_setup @@ -25,21 +25,39 @@ prompt_adam3_setup () { # Colors # Prefer positional arguments. Use environment if posititionals are not # provided. - local hostcolor=${1:-${PROMPT_ADAM3_COLOR1:-'multi'}} - local usercolor=${2:-${PROMPT_ADAM3_COLOR2:-'blue'}} - local dircolor=${3:-${PROMPT_ADAM3_COLOR3:-'cyan'}} - local timecolor=${4:-${PROMPT_ADAM3_COLOR4:-'green'}} - # Prepare prompt. Start with username - typeset -g PROMPT_ADAM3_PS1="%K{$usercolor}%F{white}%n@%f%k" + # Colors are given in a comma-separated way. "foreground,bacground,bold". + # Both fore- and background-colors may be given by name or number. Bold + # should be given with 1 for bold, 0 for no bold. Arguments can be dropped + # from the right. I.e. "blue,white,1" for bold blue text on white background. + # "blue,white" for blue (non-bold) on white background. Or just "blue" for + # just blue text and no given background. 'none' can be given as a color, and + # indicates no color. So, bold blue text with no background is "blue,none,1". + + local -A prom color + color[host]=${1:-${PROMPT_ADAM3_COLOR1:-multi}} # host color + color[user]=${2:-${PROMPT_ADAM3_COLOR2:-white,blue}} # user color + color[dirS]=${3:-${PROMPT_ADAM3_COLOR3:-39,none,1}} # dir (single-mode) color + color[dirD]=${3:-${PROMPT_ADAM3_COLOR3:-39,none,1}} # dir (double mode) color + color[time]=${4:-${PROMPT_ADAM3_TIME:-${PROMPT_ADAM3_COLOR4:-66}}} # time color + + + # Username + prom[user]='%n@' + + # Hostname + prom[host]='%m' # DIR in single-line-mode - typeset -g PROMPT_ADAM3_SLM_DIR="%B%F{$dircolor}%-40<..<%(5~|%-1~/../%3~|%4~)%f%b%<< " + prom[dirS]='%-40<..<%(5~|%-1~/../%3~|%4~)%<< ' # DIR in double-line-mode - typeset -g PROMPT_ADAM3_DLM_DIR="%B%F{$dircolor}%-10<..<%~%f%b%<<" + prom[dirD]='%-10<..<%~%<<' - # time - typeset -g PROMPT_ADAM3_RPS1="%F{$timecolor}%*%f" + # Time + [[ $timecolor != false ]] && prom[time]='%*' + + # SHLVL + prom[shlvl]='%(10L.%F{58}%L%f .)' # Promptchar (including exitcode) typeset -g PROMPT_ADAM3_PROMPTCHAR='%(?..[%F{red}%?%f] )%B%F{%(!.red.white)}%#%f%b ' @@ -52,13 +70,14 @@ prompt_adam3_setup () { zstyle ':vcs_info:*' check-for-staged-changes true zstyle ':vcs_info:*' stagedstr ' S' zstyle ':vcs_info:*' unstagedstr ' U' - zstyle ':vcs_info:*' formats "%s %F{green}%b%F{red}%u%c%f " - zstyle ':vcs_info:git*' actionformats "%s %r/%S %F{green}%b%F{red}%u%c%a%f " + zstyle ':vcs_info:*' formats '%s %F{green}%b%F{red}%u%c%f ' + zstyle ':vcs_info:git*' actionformats '%s %r/%S %F{green}%b%F{red}%u%c%a%f ' # Change color of host, based on hostname - if [[ $hostcolor == 'multi' ]]; then + if [[ $color[host] == 'multi' ]]; then - # Here we take the charset-number of each character and add them together. + # Here we take the charset-number of each character in the hostname, and + # add them together. local -i hostnum i local c for i in {1..$#HOST}; do @@ -66,32 +85,39 @@ prompt_adam3_setup () { (( hostnum += #c )) done - # define available color-bombos - local -a colors=( black,red black,green black,yellow black,blue \ - black,magenta black,cyan red,white red,yellow \ - red,cyan green,black green,blue yellow,black \ - yellow,blue blue,white blue,red blue,yellow \ - magenta,white magenta,yellow cyan,white cyan,blue \ - white,black white,red white,blue white,magenta ) + local -a colors=( red,black green,black yellow,black blue,black \ + magenta,black cyan,black white,red yellow,red \ + cyan,red black,green blue,green black,yellow \ + blue,yellow white,blue red,blue yellow,blue \ + white,magenta yellow,magenta white,cyan blue,cyan \ + black,white red,white blue,white magenta,white ) # Select color - hostcolor=${colors[$hostnum % $#colors + 1]} + color[host]=${colors[$hostnum % $#colors + 1]} # Go bold? - local B b - ((hostnum % 2)) && B=%B b=%b + ((hostnum % 2)) && color[host]+=',1' # for testing purposes - #hostcolor=${colors[$RANDOM % $#colors + 1]} - #(( $RANDOM % 2 )) && B=%B b=%b || B= b= - - # This is where we set up the actual prompt. - PROMPT_ADAM3_PS1+="$B%K{${hostcolor%,*}}%F{${hostcolor##*,}}%m%f%k$b " - else - # If one wants a specific color, just set ut, plain and simple - PROMPT_ADAM3_PS1+="%K{$hostcolor}%m%k " + #color[host]=${colors[$RANDOM % $#colors + 1]} + #((RANDOM % 2)) && color[host]+=',1' fi + # colorize the prompt-parts. See footnotes for extra comments. + for item in user host dirS dirD time; do + c=(${(s.,.)color[$item]}) # cheaper to do this expansion just once + [[ ${c[1]:-none} != none ]] && prom[$item]="%F{${c[1]}}$prom[${item}]%f" + [[ ${c[2]:-none} != none ]] && prom[$item]="%K{${c[2]}}$prom[${item}]%k" + (( ${c[3]} )) && prom[$item]="%B$prom[${item}]%b" + done + + # Make each prompt-part global + typeset -g PROMPT_ADAM3_PS1="$prom[user]$prom[host] " + typeset -g PROMPT_ADAM3_SLM_DIR=$prom[dirS] + typeset -g PROMPT_ADAM3_DLM_DIR=$prom[dirD] + typeset -g PROMPT_ADAM3_RPS1=$prom[shlvl]$prom[time] + + # Set up hook add-zsh-hook precmd prompt_adam3_precmd } @@ -112,15 +138,17 @@ prompt_adam3_precmd () { local prompt_length=${#${(S%%)prom//\%([BSUbfksu]|[FK]\{*\})/}} # If the prompt is over half the terminal, we go into multiline-mode. - if (( (COLUMNS / 2) < prompt_length )); then + if (( prompt_length > ( COLUMNS / 2 ) )); then prom=$PROMPT_ADAM3_PS1 # reset prompt - prom+=$PROMPT_ADAM3_DLM_DIR # dir + prom+=$PROMPT_ADAM3_DLM_DIR # double-line-mode dir # We need to recalculate prompt_length to know where to put time prompt_length=${#${(S%%)prom//\%([BSUbfksu]|[FK]\{*\})/}} + local rprompt_length=${#${(S%%)PROMPT_ADAM3_RPS1//\%([BSUbfksu]|[FK]\{*\})/}} + local timepadding=$((COLUMNS - prompt_length + $#PROMPT_ADAM3_RPS1 - rprompt_length - 1)) # Add time. Left-pad with spaces. - prom+=${(l:$((COLUMNS - prompt_length + 1 )):)PROMPT_ADAM3_RPS1} + prom+=${(l:$timepadding:)PROMPT_ADAM3_RPS1} prom+=$'\n' # newline prom+=$vcs_info_msg_0_ # vcs prom+=$PROMPT_ADAM3_PROMPTCHAR # promptchar |