]>
Commit | Line | Data |
---|---|---|
6a32e673 | 1 | #!/bin/zsh |
2 | ||
d3712462 | 3 | prompt_arrows_help() { |
1f53b630 | 4 | cat << EOF |
d3712462 | 5 | prompt arrows <right|2line> <normal> <prompt> <ssh> <urgent> <comment> <dir> <unwritable> |
1f53b630 | 6 | |
7 | The first parameter determines whether or not to use right-aligned status | |
8 | text, or to put it above the command line. | |
9 | ||
87c38112 | 10 | All other parameters are Zsh PROMPT_SUBST color values wrapped in '%F{}'. |
11 | ||
1f53b630 | 12 | If 'prompt' or 'ssh' are 'random', their respective value will be |
13 | procedurally generated using the machine hostname as a seed; if the | |
14 | machine is connected to SSH, the 'prompt' value will be replaced with | |
15 | the 'ssh' value. | |
16 | ||
1f53b630 | 17 | |
18 | Defaults are white, green, random, red, blue, yellow, and darkyellow | |
19 | respectively. | |
20 | ||
21 | EOF | |
22 | } | |
23 | ||
d3712462 | 24 | local prompt_arrows_vimode= |
1f53b630 | 25 | |
d3712462 | 26 | function prompt_arrows_setup() { |
7ddca96d | 27 | prompt_opts=( cr sp percent subst ) |
28 | ||
29 | ||
c7e3e126 | 30 | # XXX: no checks for color terminal or if escapes work, except for echotc |
7ddca96d | 31 | local m n p s r c d u |
b341b38a | 32 | #TODO: remove n? |
7ddca96d | 33 | m=${1:-2line} # Mode |
34 | n=${2:-white} # Normal | |
35 | p=${3:-green} # Prompt | |
36 | s=${4:-random} # Ssh prompt | |
37 | r=${5:-red} # uRgent | |
38 | c=${6:-blue} # Comment | |
39 | d=${7:-yellow} # Dir | |
40 | u=${8:-darkyellow} # Unwritable | |
1f53b630 | 41 | |
42 | # [AC]CURSED COMMAND | |
43 | # concatenate the numerical values of each letter of the hostname, | |
44 | # moduloing by the number of possible colors. | |
c7e3e126 | 45 | x=$(echotc Co) |
46 | [[ -z $x ]] && x=8 | |
7ddca96d | 47 | local generated=$(uname -n | od -An -td1 | awk -F' ' \ |
48 | "{for (i=1; i<=NF; i++) {sum+=1; sum*=\$i; sum%=$(echotc Co)} print sum}") | |
49 | [[ $p = random ]] && p=$generated | |
50 | [[ $s = random ]] && s=$generated | |
1f53b630 | 51 | |
52 | # use ssh color if connected | |
7ddca96d | 53 | [[ -v SSH_CLIENT ]] && p=$s |
1f53b630 | 54 | |
55 | local clock="%F{$n}%T%f" dirvcs="%F{$d}"'${vcs_info_msg_0_:-%~}'"%f" | |
73492710 | 56 | # XXX: linux only |
1f53b630 | 57 | local battery=/sys/class/power_supply/BAT0/capacity |
57948260 | 58 | [[ -e "$battery" ]] && clock+=' $(cat "'"$battery"'")%%' |
d3712462 | 59 | local vicol='%F{${${prompt_arrows_vimode:+'"$r"'}:-'"$c"'}}' |
73492710 | 60 | local hist="%F{$n}%h" |
1f53b630 | 61 | case "$m" { |
62 | 2line) | |
73492710 | 63 | PROMPT="%f $hist $clock $dirvcs"$'\n'"%F{$c}>>> " |
1f53b630 | 64 | unset RPROMPT |
65 | ;; | |
66 | right) | |
67 | RPROMPT="$dirvcs $clock" | |
73492710 | 68 | PROMPT="%F{$c}>>> $hist" |
1f53b630 | 69 | ;; |
70 | *) | |
71 | echo "Unknown prompt mode $m" | |
72 | return | |
73 | ;; | |
74 | } | |
75 | PROMPT="$PROMPT%(1j,%F{$c}%j,)$vicol%#%F{$p}%n%F{$n}@%F{$p}%2m%(?..%F{$n}/%F{$r}\$?)%F{$n}: " | |
7ddca96d | 76 | POSTEDIT="$(print -P "%F{$n}%f")" |
81c3957e | 77 | autoload -Uz vcs_info |
1f53b630 | 78 | zstyle ':vcs_info:git*' formats "%c%u%%F{$p}%r/%b%%F{$n}/%%F{$d}%S%%f" |
79 | zstyle ':vcs_info:git*' actionformats "%%F{$r}(%a)%f %c%u%%F{$p}%r/%b%f/%%F{$d}%S%f" | |
80 | zstyle ':vcs_info:git*' stagedstr "%F{$c}+" | |
81 | zstyle ':vcs_info:git*' unstagedstr "%F{$r}*" | |
82 | ||
b133dc92 | 83 | # add-zsh-hook should have already been autoloaded by promptinit |
d3712462 | 84 | add-zsh-hook precmd prompt_arrows_precmd |
b133dc92 | 85 | autoload -Uz add-zle-hook-widget |
86 | add-zle-hook-widget line-init prompt_arrows_keymap_select | |
87 | add-zle-hook-widget keymap-select prompt_arrows_keymap_select | |
1f53b630 | 88 | } |
89 | ||
90 | # change color based on zle vi mode | |
b133dc92 | 91 | function prompt_arrows_keymap_select { |
d3712462 | 92 | prompt_arrows_vimode="${KEYMAP:/(main|viins)/}" |
1f53b630 | 93 | zle reset-prompt |
94 | } | |
95 | ||
d3712462 | 96 | function prompt_arrows_precmd { |
1f53b630 | 97 | vcs_info |
98 | } | |
99 | ||
7ddca96d | 100 | function prompt_arrows_preview { |
101 | if (( ! $#* )); then | |
102 | prompt_preview_theme arrows 2line | |
103 | print '\n' | |
104 | prompt_preview_theme arrows right | |
105 | ||
106 | else | |
107 | prompt_preview_theme arrows "$@" | |
108 | ||
109 | fi | |
110 | } | |
111 | ||
d3712462 | 112 | prompt_arrows_setup "$@" |