aboutsummaryrefslogblamecommitdiffstatshomepage
path: root/prompt_adam3_setup
blob: adc444ddf35fdbf4c5d80c482c833d7d7be2396a (plain) (tree)
1
2
3
4
5
6
7
8
9
                    
 
                      
             
 


                                                                               
 


   
                       
                                        
 

                       
          




                                   

                                                    
                                           
                                                    

                                                                                                               
 
                          

                                                           
 
                            

                                                                                                                                                                                                                                                                                                  
 
                                                
                                                                                                               

                                                           
                                                                                    
 
      
                                                                  
                                                          

    
                                         
 
                                         

 
                        
                                                              

                                             


                                                    

                                                                                    
          

                           
 
                                                                                           
                                                                                               
                                                              


                                                                              

                                                                                                               
      

                                                                               
    

                                    
                                                                                                 

                                                                                                        
 

                                             


                                                
                             
                                                                          

                        

 
                         
# adam3 prompt theme

prompt_adam3_help () {
  cat <<'EOF'

This prompt changes the color of your prompt based on the
hostname of your current host. This can be quite nice if you log in to a lot of
different hosts (if you can manage to distribute the prompt, of course).

EOF
}

prompt_adam3_setup () {
  setopt localoptions nowarncreateglobal

  autoload -Uz vcs_info

  # colors
  prompt_adam3_color1=${1:-'multi'}
  prompt_adam3_color2=${2:-'blue'}
  prompt_adam3_color3=${3:-'cyan'}
  prompt_adam3_color4=${4:-'green'}

  prompt_adam3_PS1="%K{${prompt_adam3_color2}}%n@%k"

  # 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)}')

    # for testing purposes
    #prompt_adam3_hostnamecolor=$(( ( $RANDOM % 24 ) + 1 ))
    #prompt_adam3_hostnamebold=$(( $RANDOM % 2 ))

    # 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)

    # This is where we set up the actual prompt.
    local hostname_prompt="%K{${${colors[${hostname_color}]}%,*}}%F{${${colors[${hostname_color}]}##*,}}%m%f%k"

    # 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

  else
    # If one wants a specific color, just set ut, plain and simple
    local hostname_prompt="%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}"

  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}"

  # 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 "
  else
    local space_left=$(( $COLUMNS - ${prompt_adam3_PS1_length} - 2 ))
    PS1+="%B%F{${prompt_adam3_color4}}%${space_left}<...<%~${prompt_newline}%b"
  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}%}"

  # Display exit-code
  PS1+="%(?..[%{$fg[red]%}%?%{$reset_color%}] )"

  PS1+="${promptchar} %b%f%k"
  [ ${VIRTUAL_ENV} ] && PS1="%F{208}($(basename ${VIRTUAL_ENV}))%f ${PS1}"
  PS2="${PS1}%_> %b%f%k"
  PS3="${PS1}?# %b%f%k"
}

prompt_adam3_setup "${@}"