]>
Commit | Line | Data |
---|---|---|
e69caf64 | 1 | #!/bin/zsh |
2 | ||
4f471013 | 3 | prompt_arrows_help() { |
fab483dd | 4 | cat << EOF |
4f471013 | 5 | prompt arrows <right|2line> <normal> <prompt> <ssh> <urgent> <comment> <dir> <unwritable> |
fab483dd | 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 | ||
325a74cd | 10 | All other parameters are Zsh PROMPT_SUBST color values wrapped in '%F{}'. |
11 | ||
fab483dd | 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 | ||
fab483dd | 17 | |
18 | Defaults are white, green, random, red, blue, yellow, and darkyellow | |
19 | respectively. | |
20 | ||
21 | EOF | |
22 | } | |
23 | ||
4f471013 | 24 | local prompt_arrows_vimode= |
fab483dd | 25 | |
4f471013 | 26 | function prompt_arrows_setup() { |
79d4a356 | 27 | prompt_opts=( cr sp percent subst ) |
28 | ||
29 | ||
833b2af3 | 30 | # XXX: no checks for color terminal or if escapes work, except for echotc |
79d4a356 | 31 | local m n p s r c d u |
8d4a98e1 | 32 | #TODO: remove n? |
79d4a356 | 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 | |
fab483dd | 41 | |
42 | # [AC]CURSED COMMAND | |
43 | # concatenate the numerical values of each letter of the hostname, | |
44 | # moduloing by the number of possible colors. | |
833b2af3 | 45 | x=$(echotc Co) |
46 | [[ -z $x ]] && x=8 | |
79d4a356 | 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 | |
fab483dd | 51 | |
52 | # use ssh color if connected | |
79d4a356 | 53 | [[ -v SSH_CLIENT ]] && p=$s |
fab483dd | 54 | |
55 | local clock="%F{$n}%T%f" dirvcs="%F{$d}"'${vcs_info_msg_0_:-%~}'"%f" | |
973a2767 | 56 | # XXX: linux only |
fab483dd | 57 | local battery=/sys/class/power_supply/BAT0/capacity |
8b2d206d | 58 | [[ -e "$battery" ]] && clock+=' $(cat "'"$battery"'")%%' |
4f471013 | 59 | local vicol='%F{${${prompt_arrows_vimode:+'"$r"'}:-'"$c"'}}' |
973a2767 | 60 | local hist="%F{$n}%h" |
fab483dd | 61 | case "$m" { |
62 | 2line) | |
973a2767 | 63 | PROMPT="%f $hist $clock $dirvcs"$'\n'"%F{$c}>>> " |
fab483dd | 64 | unset RPROMPT |
65 | ;; | |
66 | right) | |
67 | RPROMPT="$dirvcs $clock" | |
973a2767 | 68 | PROMPT="%F{$c}>>> $hist" |
fab483dd | 69 | ;; |
70 | *) | |
71 | echo "Unknown prompt mode $m" | |
72 | return | |
73 | ;; | |
74 | } | |
196f1d13 | 75 | PROMPT="$PROMPT%(1j,%F{$c}%j,)$vicol%#%F{$p}%n%F{$n}@%F{$p}%2m%(?..%F{$n}/%F{$r}%?)%F{$n}: " |
79d4a356 | 76 | POSTEDIT="$(print -P "%F{$n}%f")" |
8eb81f95 | 77 | autoload -Uz vcs_info |
5326b23a | 78 | zstyle ':vcs_info:git*' get-revision true |
79 | local gitformat="%c%u%F{$p}%r%F{$n} %F{$c}%7.7i%F{$n}:%F{$p}%b%F{$n} %F{$d}%S%f" | |
80 | zstyle ':vcs_info:git*' formats $gitformat | |
81 | zstyle ':vcs_info:git*' get-unapplied true | |
82 | local patchformat=' %n/%a' | |
83 | zstyle ':vcs_info:git*' patch-format $patchformat | |
84 | zstyle ':vcs_info:git*' nopatch-format $patchformat | |
85 | zstyle ':vcs_info:git*+post-backend:*' hooks prompt_arrows_revertfix | |
86 | zstyle ':vcs_info:git*' actionformats "%F{$r}(%a%m)%f $gitformat" | |
fab483dd | 87 | zstyle ':vcs_info:git*' stagedstr "%F{$c}+" |
2120ed89 | 88 | zstyle ':vcs_info:git*' unstagedstr "%F{$r}!" |
fab483dd | 89 | |
d569f3f7 | 90 | # add-zsh-hook should have already been autoloaded by promptinit |
4f471013 | 91 | add-zsh-hook precmd prompt_arrows_precmd |
d569f3f7 | 92 | autoload -Uz add-zle-hook-widget |
93 | add-zle-hook-widget line-init prompt_arrows_keymap_select | |
94 | add-zle-hook-widget keymap-select prompt_arrows_keymap_select | |
fab483dd | 95 | } |
96 | ||
97 | # change color based on zle vi mode | |
d569f3f7 | 98 | function prompt_arrows_keymap_select { |
4f471013 | 99 | prompt_arrows_vimode="${KEYMAP:/(main|viins)/}" |
fab483dd | 100 | zle reset-prompt |
101 | } | |
102 | ||
4f471013 | 103 | function prompt_arrows_precmd { |
fab483dd | 104 | vcs_info |
105 | } | |
106 | ||
79d4a356 | 107 | function prompt_arrows_preview { |
108 | if (( ! $#* )); then | |
109 | prompt_preview_theme arrows 2line | |
110 | print '\n' | |
111 | prompt_preview_theme arrows right | |
112 | ||
113 | else | |
114 | prompt_preview_theme arrows "$@" | |
115 | ||
116 | fi | |
117 | } | |
118 | ||
5326b23a | 119 | function +vi-prompt_arrows_revertfix() { |
120 | [[ -f "$gitdir/REVERT_HEAD" ]] && hook_com[action]=revert | |
121 | } | |
122 | ||
4f471013 | 123 | prompt_arrows_setup "$@" |