diff options
author | Dennis Eriksen <d@ennis.no> | 2023-01-02 15:26:36 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-01-02 15:26:36 +0100 |
commit | 5797f7e9791e4825304650488b529f33d220af4b (patch) | |
tree | 025b1f486e5a4b93412beccbe3d6032e1526020c /prompt_adam3_setup | |
parent | change the way we assemble the prompt. Now we cas measure the length and slip... (diff) | |
download | adam3-5797f7e9791e4825304650488b529f33d220af4b.tar.gz |
Added new way to calculate the value of the hostname
Added a simpler and more elegant way to find a numeric value of the hostname. Also, moved a few things around.
Diffstat (limited to '')
-rw-r--r-- | prompt_adam3_setup | 101 |
1 files changed, 41 insertions, 60 deletions
diff --git a/prompt_adam3_setup b/prompt_adam3_setup index c3ef586..55e4005 100644 --- a/prompt_adam3_setup +++ b/prompt_adam3_setup @@ -22,27 +22,29 @@ prompt_adam3_setup () { autoload -Uz vcs_info - # colors - typeset PROMPT_ADAM3_COLOR1=${1:-'multi'} - typeset PROMPT_ADAM3_COLOR2=${2:-'blue'} - typeset PROMPT_ADAM3_COLOR3=${3:-'cyan'} - typeset -g PROMPT_ADAM3_COLOR4=${4:-'green'} + # 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 - typeset -g PROMPT_ADAM3_PS1 - - # Promptchar (including exitcode) - typeset -g PROMPT_ADAM3_PROMPTCHAR='%(?..[%F{red}%?%f] )%B%F{%(!.red.white)}%#%f%b ' - # same as above, but with $ instead of % as promptchar for unprivileged users - #typeset -g PROMPT_ADAM3_PROMPTCHAR='%B%(!.%F{red}#.F{white}$)%f%b' + # Prepare prompt. Start with username + typeset -g PROMPT_ADAM3_PS1="%K{$usercolor}%F{white}%n@%f%k" # DIR in single-line-mode - typeset -g PROMPT_ADAM3_SL_DIR="%B%F{$PROMPT_ADAM3_COLOR3}%-40<..<%(5~|%-1~/../%3~|%4~)%f%b%<< " + typeset -g PROMPT_ADAM3_SLM_DIR="%B%F{$dircolor}%-40<..<%(5~|%-1~/../%3~|%4~)%f%b%<< " # DIR in double-line-mode - typeset -g PROMPT_ADAM3_DL_DIR="%B%F{$PROMPT_ADAM3_COLOR3}%-10<..<%~%f%b%<<" + typeset -g PROMPT_ADAM3_DLM_DIR="%B%F{$dircolor}%-10<..<%~%f%b%<<" - # Prepare som local variables - local -i hm=15 # max chars in hostname. Recalculate if above. + # time + typeset -g PROMPT_ADAM3_RPS1="%F{$timecolor}%*%f" + + # Promptchar (including exitcode) + typeset -g PROMPT_ADAM3_PROMPTCHAR='%(?..[%F{red}%?%f] )%B%F{%(!.red.white)}%#%f%b ' + # same as above, but with $ instead of % as promptchar for unprivileged users + #typeset -g PROMPT_ADAM3_PROMPTCHAR='%B%(!.%F{red}#.F{white}$)%f%b ' # Set some styles for vcs_info zstyle ':vcs_info:*' enable git svn @@ -53,59 +55,41 @@ prompt_adam3_setup () { 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 " - # Set username prompt - local userprom="%K{${PROMPT_ADAM3_COLOR2}}%n@%k" - # Change color of host, based on hostname - if [[ "${PROMPT_ADAM3_COLOR1}" == 'multi' ]]; then - # Map up letters to numbers to convert hostname to a number - local -A map=( 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 \ - a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 \ - k 0 l 1 m 2 n 3 o 4 p 5 q 6 r 7 s 8 t 9 \ - u 0 v 1 w 2 x 3 y 4 z 5 , 6 . 7 - 8 _ 9 \ - / 0 ) - - # Translate hostname into a pseudo-random number that won't change with - # time. We do this "simply" by translating each letter into a corresponding - # number, using the map above. - local -i hostnum=${HOST//(#m)?/${map[$MATCH]}} - - # If we have a REALLY LONG hostname, the number we end up with will be too - # large for zsh to handle. Here's a trick to shorten the number, while - # still ending up with a predictable number for each host. - if (( $#hostnum > hm )); then - local -i s i - for i in {0..$(( $#hostnum / hm ))}; s=$(( s + 0${hostnum:$(( i * hm )):$hm} )) - hostnum=$s - fi + if [[ $hostcolor == 'multi' ]]; then + + # Here we take the charset-number of each character and add them together. + local -i hostnum i + local c + for i in {1..$#HOST}; do + c=$HOST[$i] + (( 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 \ + 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 ) # Select color - local host_color=${colors[$hostnum % $#colors + 1]} + hostcolor=${colors[$hostnum % $#colors + 1]} # Go bold? local B b ((hostnum % 2)) && B=%B b=%b - # Just in case hostnum is b0rked. - (( ! hostnum )) && host_color="white,black" B= b= - # for testing purposes - #host_color=${colors[$RANDOM % $#colors + 1]} - #host_bold=$(( $RANDOM % 2 )) + #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="$userprom$B%K{${host_color%,*}}%F{${host_color##*,}}%m%f%k$b " + 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="$userprom%K{${PROMPT_ADAM3_COLOR1}}%m%k" + PROMPT_ADAM3_PS1+="%K{$hostcolor}%m%k " fi add-zsh-hook precmd prompt_adam3_precmd @@ -114,15 +98,12 @@ prompt_adam3_setup () { prompt_adam3_precmd () { setopt localoptions extendedglob noxtrace - # time - local time="%F{$PROMPT_ADAM3_COLOR4}%*%f" - # vcs vcs_info #$vcs_info_msg_0_ local prom prom=$PROMPT_ADAM3_PS1 - prom+=$PROMPT_ADAM3_SL_DIR + prom+=$PROMPT_ADAM3_SLM_DIR prom+=$vcs_info_msg_0_ prom+=$PROMPT_ADAM3_PROMPTCHAR @@ -133,19 +114,19 @@ prompt_adam3_precmd () { # If the prompt is over half the terminal, we go into multiline-mode. if (( (COLUMNS / 2) < prompt_length )); then prom=$PROMPT_ADAM3_PS1 # reset prompt - prom+=$PROMPT_ADAM3_DL_DIR # dir + prom+=$PROMPT_ADAM3_DLM_DIR # dir # We need to recalculate prompt_length to know where to put time prompt_length=${#${(S%%)prom//\%([BSUbfksu]|[FK]\{*\})/}} # Add time. Left-pad with spaces. - prom+=${(l:$((COLUMNS - prompt_length + 1 )):)time} + prom+=${(l:$((COLUMNS - prompt_length + 1 )):)PROMPT_ADAM3_RPS1} prom+=$'\n' # newline prom+=$vcs_info_msg_0_ # vcs prom+=$PROMPT_ADAM3_PROMPTCHAR # promptchar RPS1= # RPS1 does not support multiline, so reset it when we're in multiline else - RPS1=$time + RPS1=$PROMPT_ADAM3_RPS1 fi PS1=$prom |