aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/prompt_adam3_setup
blob: 10fd35ea248ebef425143ca652e20fabd61d1ed7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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 () {
  prompt_adam3_color1=${1:-'multi'}
  prompt_adam3_color2=${2:-'blue'}
  prompt_adam3_color3=${3:-'cyan'}
  prompt_adam3_color4=${4:-'green'}

  if [[ $prompt_adam3_color1 == 'multi' ]]; then
    #prompt_adam3_hostnamecolor=$(hostname | od | tr ' ' '\n' | awk '{total = total + $1}END{print 1 + (total % 24)}')
    #prompt_adam3_hostnamebold=$(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
    prompt_adam3_hostnamecolors=(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.
    base_hostname_prompt="%K{${${prompt_adam3_hostnamecolors[$prompt_adam3_hostnamecolor]}%,*}}%F{${${prompt_adam3_hostnamecolors[$prompt_adam3_hostnamecolor]}##*,}}%m%f%k"


    # this will set the hostname in bold in 50% of the time
    if [[ $prompt_adam3_hostnamebold -eq 1 ]]; then base_hostname_prompt="%B$base_hostname_prompt%b"; fi

  else

    # If one wants a specific color, just set ut, plain and simple
    base_hostname_prompt="%K{$prompt_adam3_color1}%m%k"

  fi

  base_prompt="%K{$prompt_adam3_color2}%n@%k$base_hostname_prompt "
  post_prompt="%b%f%k"

  setopt localoptions extendedglob
  base_prompt_no_color="${base_prompt//(%K{[^\\\}]#\}|%k)/}"
  post_prompt_no_color="${post_prompt//(%K{[^\\\}]#\}|%k)/}"

  add-zsh-hook precmd prompt_adam3_precmd
}

prompt_adam3_precmd () {
#  setopt noxtrace localoptions
  local base_prompt_expanded_no_color base_prompt_etc
  local prompt_length space_left

  base_prompt_expanded_no_color=$(print -P "$base_prompt_no_color")
  base_prompt_etc=$(print -P "$base_prompt%(4~|...|)%3~")
  prompt_length=${#base_prompt_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_length -lt 69 ]]; then
    path_prompt="%B%F{$prompt_adam3_color3}%(4~|...|)%3~%F{white}"
  else
    space_left=$(( $COLUMNS - $#base_prompt_expanded_no_color - 2 ))
    path_prompt="%B%F{$prompt_adam3_color4}%${space_left}<...<%~$prompt_newline%F{white}"
  fi
  PS1="$base_prompt$path_prompt %# $post_prompt"
  PS2="$base_prompt$path_prompt %_> $post_prompt"
  PS3="$base_prompt$path_prompt ?# $post_prompt"
}

prompt_adam3_setup "$@"