about summary refs log tree commit diff
path: root/Functions/Prompts/prompt_adam2_setup
blob: 626ad431f458368fc84c6a2efedb7e9feea42f13 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# adam2 prompt theme

prompt_adam2_help () {
  cat <<'EOF'
This prompt is color-scheme-able.  You can invoke it thus:

  prompt adam2 [ plain ] [<color1> [<color2> [<color3>]]]

where the colors are for the hyphens, current directory, and user@host
bits respectively.  The default colors are cyan, green, and cyan.
This theme works best with a dark background.

Recommended fonts for this theme: nexus or vga or similar.  If you
don't have any of these, then specify the `plain' option to use 7-bit
replacements for the 8-bit characters.

And you probably thought adam1 was overkill.
EOF
}

prompt_adam2_setup () {
  # Some can't be local
  local prompt_gfx_tlc prompt_gfx_mlc prompt_gfx_blc prompt_gfx_bbox 

  if [[ $1 == 'plain' ]]; then
    shift
    prompt_gfx_tlc='.'
    prompt_gfx_mlc='|'
    prompt_gfx_blc='\`'
    prompt_gfx_hyphen='-'
  else
    prompt_gfx_tlc=$'\xda'
    prompt_gfx_mlc=$'\xc3'
    prompt_gfx_blc=$'\xc0'
    prompt_gfx_hyphen=$'\xc4'
  fi

  # Colour scheme
  prompt_adam2_color1=${1:-'cyan'}    # hyphens
  prompt_adam2_color2=${2:-'green'}   # current directory
  prompt_adam2_color3=${3:-'cyan'}    # user@host

  prompt_gfx_tbox="%{$fg_bold[$prompt_adam2_color1]%}${prompt_gfx_tlc}%{$fg_no_bold[$prompt_adam2_color1]%}${prompt_gfx_hyphen}"
  prompt_gfx_bbox="%{$fg_bold[$prompt_adam2_color1]%}${prompt_gfx_blc}${prompt_gfx_hyphen}%{$fg_no_bold[$prompt_adam2_color1]%}"

  # This is a cute hack.  Well I like it, anyway.
  prompt_gfx_bbox_to_mbox=$(print -n "%{\e[A\r$fg_bold[$prompt_adam2_color1]${prompt_gfx_mlc}$fg_no_bold[$prompt_adam2_color1]${prompt_gfx_hyphen}\e[B%}")

  prompt_l_paren="%{$fg_bold[grey]%}("
  prompt_r_paren="%{$fg_bold[grey]%})"

  prompt_machine="%{$fg_no_bold[$prompt_adam2_color3]%}%n%{$fg_bold[$prompt_adam2_color3]%}@%{$fg_no_bold[$prompt_adam2_color3]%}%m"

  prompt_padding_text=`perl -e "print qq{${prompt_gfx_hyphen}} x 200"`

  prompt_line_1a="$prompt_gfx_tbox$prompt_l_paren%{$fg_bold[$prompt_adam2_color2]%}%~$prompt_r_paren%{$fg_no_bold[$prompt_adam2_color1]%}"
  prompt_line_1a_no_color=$(echo "$prompt_line_1a" | perl -pe 's/%{.*?%}//g')
  prompt_line_1b="$prompt_l_paren$prompt_machine$prompt_r_paren%{$fg_no_bold[$prompt_adam2_color1]%}${prompt_gfx_hyphen}"
  prompt_line_1b_no_color=$(echo "$prompt_line_1b" | perl -pe 's/%{.*?%}//g')

  prompt_line_2="$prompt_gfx_bbox${prompt_gfx_hyphen}%{$fg_bold[white]%}"

  prompt_char="%(!.#.>)"

  precmd () { prompt_adam2_precmd; setopt promptsubst }
  preexec () { prompt_adam2_preexec }
}

prompt_adam2_precmd () {
  setopt noxtrace localoptions
  local prompt_line_1a_no_color_expanded prompt_line_2a_no_color_expanded
  local prompt_padding_size prompt_padding prompt_line_1 pre_prompt
  local prompt_pwd_size

  prompt_line_1a_no_color_expanded=$(print -P "$prompt_line_1a_no_color")
  prompt_line_1b_no_color_expanded=$(print -P "$prompt_line_1b_no_color")
  prompt_padding_size=$(( $COLUMNS
                            - $#prompt_line_1a_no_color_expanded 
                            - $#prompt_line_1b_no_color_expanded ))

  if [[ $prompt_padding_size -ge 0 ]]; then
    prompt_padding=$(printf "%$prompt_padding_size.${prompt_padding_size}s" "$prompt_padding_text")
    prompt_line_1="$prompt_line_1a$prompt_padding$prompt_line_1b"
  else
    prompt_padding_size=$(( $COLUMNS
                              - $#prompt_line_1a_no_color_expanded ))

    if [[ $prompt_padding_size -ge 0 ]]; then
      prompt_padding=$(printf "%$prompt_padding_size.${prompt_padding_size}s" "$prompt_padding_text")
      prompt_line_1="$prompt_line_1a$prompt_padding"
    else
      prompt_pwd_size=$(( $COLUMNS - 5 ))
      prompt_line_1="$prompt_gfx_tbox$prompt_l_paren%{$fg_bold[$prompt_adam2_color2]%}%$prompt_pwd_size<...<%~%<<$prompt_r_paren%{$fg_no_bold[$prompt_adam2_color1]$prompt_gfx_hyphen%}"
    fi
  fi

  pre_prompt="$prompt_line_1$prompt_newline$prompt_line_2"

  PS1="$pre_prompt%{$fg_bold[white]%}$prompt_char "
  PS2="$prompt_line_2%{$prompt_gfx_bbox_to_mbox$fg_bold[white]%}%_> "
  PS3="$prompt_line_2%{$prompt_gfx_bbox_to_mbox$fg_bold[white]%}?# "
}

prompt_adam2_preexec () {
  print -n "$fg_no_bold[white]"
}

prompt_adam2_setup "$@"