aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/prompt_adam3_setup
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2023-01-01 21:49:58 +0100
committerDennis Eriksen <d@ennis.no>2023-01-01 21:49:58 +0100
commitd92a1beebedcb1d102d58fff60df6bd0cd7154ab (patch)
treea443b4c83b8b6e2013fef5393bd9fbdb2f85b792 /prompt_adam3_setup
parentcheck if variable is set in a proper way (diff)
downloadadam3-d92a1beebedcb1d102d58fff60df6bd0cd7154ab.tar.gz
rewriting prompt. wip
Planning for better readability, better trunking of directories, and zero external dependencies.
Diffstat (limited to '')
-rw-r--r--prompt_adam3_setup158
1 files changed, 105 insertions, 53 deletions
diff --git a/prompt_adam3_setup b/prompt_adam3_setup
index f04ea3c..ad6fefb 100644
--- a/prompt_adam3_setup
+++ b/prompt_adam3_setup
@@ -1,3 +1,10 @@
+# Filename: prompt_adam3_setup
+# Purpose: a zsh-prompt that changes the color of your prompt based on
+# the hostname of your current host
+# Authors: Dennis Eriksen <https://dnns.no>
+# Bug-Reports: Email <idgatt@dnns.no>
+# License: This file is licensed under the GPL v2.
+################################################################################
# adam3 prompt theme
prompt_adam3_help () {
@@ -11,88 +18,133 @@ EOF
}
prompt_adam3_setup () {
- setopt localoptions nowarncreateglobal
+ setopt localoptions extendedglob
autoload -Uz vcs_info
- autoload -U colors && colors
# colors
- prompt_adam3_color1=${1:-'multi'}
- prompt_adam3_color2=${2:-'blue'}
- prompt_adam3_color3=${3:-'cyan'}
- prompt_adam3_color4=${4:-'green'}
+ typeset -g PROMPT_ADAM3_COLOR1=${1:-'multi'}
+ typeset -g PROMPT_ADAM3_COLOR2=${2:-'blue'}
+ typeset -g PROMPT_ADAM3_COLOR3=${3:-'cyan'}
+ typeset -g PROMPT_ADAM3_COLOR4=${4:-'green'}
- prompt_adam3_PS1="%K{${prompt_adam3_color2}}%n@%k"
+ # Prepare prompt
+ typeset -g PROMPT_ADAM3_PS1
- # Change color of host, based on hostname
- if [[ "${prompt_adam3_color1}" == 'multi' ]]; then
- local hostname_color=$(hostname | od | tr ' ' '\n' | awk '{total = total + $1}END{print 1 + (total % 24)}')
- local hostname_bold=$(hostname | od | tr ' ' '\n' | awk '{total = total + $1}END{print 1 + (total % 2)}')
+ # Promptchar
+ typeset -g PROMPT_ADAM3_PROMPTCHAR='%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'
- # for testing purposes
- #prompt_adam3_hostnamecolor=$(( ( $RANDOM % 24 ) + 1 ))
- #prompt_adam3_hostnamebold=$(( $RANDOM % 2 ))
+ # Prepare som local variables
+ local -i hm=15 # max chars in hostname. Recalculate if above.
- # Possible prompt colors
- local colors
- 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)
+ # Set some styles for vcs_info
+ zstyle ':vcs_info:*' enable git svn
+ zstyle ':vcs_info:*' check-for-changes true
+ 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 "
- # This is where we set up the actual prompt.
- local hostname_prompt="%K{${${colors[${hostname_color}]}%,*}}%F{${${colors[${hostname_color}]}##*,}}%m%f%k"
+ # 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
+
+ # 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 )
+
+ # Select color
+ local host_color=${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=
- # this will set the hostname in bold in 50% of the time
- if [[ $hostname_bold -eq 1 ]]; then hostname_prompt="%B${hostname_prompt}%b"; fi
+ # for testing purposes
+ #host_color=${colors[$RANDOM % $#colors + 1]}
+ #host_bold=$(( $RANDOM % 2 ))
+ # This is where we set up the actual prompt.
+ PROMPT_ADAM3_PS1="$userprom$B%K{${host_color%,*}}%F{${host_color##*,}}%m%f%k$b "
else
# If one wants a specific color, just set ut, plain and simple
- local hostname_prompt="%K{${prompt_adam3_color1}}%m%k"
+ PROMPT_ADAM3_PS1="$userprom%K{${PROMPT_ADAM3_COLOR1}}%m%k"
fi
- prompt_adam3_PS1+="${hostname_prompt} "
-
add-zsh-hook precmd prompt_adam3_precmd
}
prompt_adam3_precmd () {
- setopt localoptions extendedglob noxtrace nowarncreateglobal
- zstyle ':vcs_info:*' enable git svn
- zstyle ':vcs_info:*' check-for-changes true
- zstyle ':vcs_info:*' check-for-staged-changes true
- zstyle ':vcs_info:*' stagedstr ' S'
- zstyle ':vcs_info:*' unstagedstr ' U'
- zstyle ':vcs_info:*' formats "%F{grey}%s %F{green}%b%F{red}%u%c "
- zstyle ':vcs_info:git*' actionformats "%F{grey}%s %r/%S %F{green}%b%F{red}%u%c%a "
- vcs_info
- local promptchar
- PS1="${prompt_adam3_PS1}"
+ setopt localoptions extendedglob noxtrace
- local prompt_adam3_PS1_length="${#${(S%%)prompt_adam3_PS1//(\%([KF1]|)\{*\}|\%[Bbkf])}}"
- local prompt_adam3_PS1_etc=$(print -P "${(S%%)prompt_adam3_PS1//(\%([KF1]|)\{*\}|\%[Bbkf])}")
- local prompt_adam3_PS1_etc_length="${#prompt_adam3_PS1_etc}"
+ local nl=$'\n'
- # Changing the prompt based on the length of the prompt, should happen based
- # on how long it actually is. The method below will need to be changed.
- if [[ ${prompt_adam3_PS1_etc_length} -lt 60 && $((${prompt_adam3_PS1_etc_length} + 40)) -lt $COLUMNS ]]; then
- PS1+="%B%F{${prompt_adam3_color3}}%(4~|...|)%3~%b "
+ PS1="${PROMPT_ADAM3_PS1}"
+
+ # Change prompt layout at 70 cols. If more - single line. If less - double line.
+ if (( COLUMNS > 70 )); then
+ PS1+="%B%F{$PROMPT_ADAM3_COLOR3}%-40<..<%(4~,..,)%3~%f%b%<< "
+ RPS1="%F{$PROMPT_ADAM3_COLOR4}%*%f"
else
- local space_left=$(( $COLUMNS - ${prompt_adam3_PS1_length} - 2 ))
- PS1+="%B%F{${prompt_adam3_color4}}%${space_left}<...<%~${prompt_newline}%b"
+ PS1+="%B%F{$PROMPT_ADAM3_COLOR3}%-10<..<%~%f%b%<<"
+
+ # Do clock on same line. RPS1 does not support multiline.
+ RPS1=
+ # Calculate prompt-length by first removing all zero-length characters. From
+ # prompt_bart_setup in zsh.
+ local -i prompt_length=${#${(S%%)PS1//\%([BSUbfksu]|[FK]\{*\})/}}
+ #local spaces=$((COLUMNS - prompt_length + 2))
+ #local right="%F{$PROMPT_ADAM3_COLOR4}%*%f"
+ #PS1+="${(l:$spaces:)right}$nl"
+ PS1+="${(l:$((COLUMNS - prompt_length +2)):):-"%F{$PROMPT_ADAM3_COLOR4}%*%f"}$nl"
fi
- # make promptchar bold red if root
- if [[ $EUID == 0 ]]; then promptchar="%F{red}%B%#%b%f"; else promptchar="%F{white}%B%#%b%f"; fi
- # same as above, but with $ instead of % as promptchar for unprivileged users
- #if [[ $EUID == 0 ]]; then promptchar="%F{red}%B%#%b%f"; else promptchar="%F{white}%B%(!.#.$)%b%f"; fi
-
- PS1+="${vcs_info_msg_0_}%{$reset_color%}"
+ vcs_info
+ PS1+="${vcs_info_msg_0_}"
# Display exit-code
- PS1+="%(?..[%{$fg[red]%}%?%{$reset_color%}] )"
+ PS1+="%(?..[%F{red}%?%f] )"
- PS1+="${promptchar} %b%f%k"
- (( ${+VIRTUAL_ENV} )) && PS1="%F{208}($(basename ${VIRTUAL_ENV}))%f ${PS1}"
+ PS1+="$PROMPT_ADAM3_PROMPTCHAR "
+ (( ${+VIRTUAL_ENV} )) && PS1="%F{208}(${VIRTUAL_ENV:t})%f ${PS1}"
PS2="${PS1}%_> %b%f%k"
PS3="${PS1}?# %b%f%k"
+
}
prompt_adam3_setup "${@}"
+
+# END OF FILE #################################################################
+# vim: filetype=zsh