my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

9193 lines
326 KiB

3 years ago
  1. if [[ $__p9k_sourced != 13 ]]; then
  2. >&2 print -P ""
  3. >&2 print -P "[%F{1}ERROR%f]: Corrupted powerlevel10k installation."
  4. >&2 print -P ""
  5. if (( ${+functions[antigen]} )); then
  6. >&2 print -P "If using %Bantigen%b, run the following command to fix:"
  7. >&2 print -P ""
  8. >&2 print -P " %F{2}antigen%f reset"
  9. if [[ -d ~/.antigen ]]; then
  10. >&2 print -P ""
  11. >&2 print -P "If it doesn't help, try this:"
  12. >&2 print -P ""
  13. >&2 print -P " %F{2}rm%f -rf %U~/.antigen%u"
  14. fi
  15. else
  16. >&2 print -P "Try resetting cache in your plugin manager or"
  17. >&2 print -P "reinstalling powerlevel10k from scratch."
  18. fi
  19. >&2 print -P ""
  20. return 1
  21. fi
  22. if ! autoload -Uz is-at-least || ! is-at-least 5.1; then
  23. () {
  24. >&2 echo -E "You are using ZSH version $ZSH_VERSION. The minimum required version for Powerlevel10k is 5.1."
  25. >&2 echo -E "Type 'echo \$ZSH_VERSION' to see your current zsh version."
  26. local def=${SHELL:c:A}
  27. local cur=${${ZSH_ARGZERO#-}:c:A}
  28. local cur_v="$($cur -c 'echo -E $ZSH_VERSION' 2>/dev/null)"
  29. if [[ $cur_v == $ZSH_VERSION && $cur != $def ]]; then
  30. >&2 echo -E "The shell you are currently running is likely $cur."
  31. fi
  32. local other=${${:-zsh}:c}
  33. if [[ -n $other ]] && $other -c 'autoload -Uz is-at-least && is-at-least 5.1' &>/dev/null; then
  34. local other_v="$($other -c 'echo -E $ZSH_VERSION' 2>/dev/null)"
  35. if [[ -n $other_v && $other_v != $ZSH_VERSION ]]; then
  36. >&2 echo -E "You have $other with version $other_v but this is not what you are using."
  37. if [[ -n $def && $def != ${other:A} ]]; then
  38. >&2 echo -E "To change your user shell, type the following command:"
  39. >&2 echo -E ""
  40. if [[ "$(grep -F $other /etc/shells 2>/dev/null)" != $other ]]; then
  41. >&2 echo -E " echo ${(q-)other} | sudo tee -a /etc/shells"
  42. fi
  43. >&2 echo -E " chsh -s ${(q-)other}"
  44. fi
  45. fi
  46. fi
  47. }
  48. return 1
  49. fi
  50. builtin source "${__p9k_root_dir}/internal/configure.zsh"
  51. builtin source "${__p9k_root_dir}/internal/worker.zsh"
  52. builtin source "${__p9k_root_dir}/internal/parser.zsh"
  53. builtin source "${__p9k_root_dir}/internal/icons.zsh"
  54. # For compatibility with Powerlevel9k. It's not recommended to use mnemonic color
  55. # names in the configuration except for colors 0-7 as these are standard.
  56. typeset -grA __p9k_colors=(
  57. black 000 red 001 green 002 yellow 003
  58. blue 004 magenta 005 cyan 006 white 007
  59. grey 008 maroon 009 lime 010 olive 011
  60. navy 012 fuchsia 013 aqua 014 teal 014
  61. silver 015 grey0 016 navyblue 017 darkblue 018
  62. blue3 020 blue1 021 darkgreen 022 deepskyblue4 025
  63. dodgerblue3 026 dodgerblue2 027 green4 028 springgreen4 029
  64. turquoise4 030 deepskyblue3 032 dodgerblue1 033 darkcyan 036
  65. lightseagreen 037 deepskyblue2 038 deepskyblue1 039 green3 040
  66. springgreen3 041 cyan3 043 darkturquoise 044 turquoise2 045
  67. green1 046 springgreen2 047 springgreen1 048 mediumspringgreen 049
  68. cyan2 050 cyan1 051 purple4 055 purple3 056
  69. blueviolet 057 grey37 059 mediumpurple4 060 slateblue3 062
  70. royalblue1 063 chartreuse4 064 paleturquoise4 066 steelblue 067
  71. steelblue3 068 cornflowerblue 069 darkseagreen4 071 cadetblue 073
  72. skyblue3 074 chartreuse3 076 seagreen3 078 aquamarine3 079
  73. mediumturquoise 080 steelblue1 081 seagreen2 083 seagreen1 085
  74. darkslategray2 087 darkred 088 darkmagenta 091 orange4 094
  75. lightpink4 095 plum4 096 mediumpurple3 098 slateblue1 099
  76. wheat4 101 grey53 102 lightslategrey 103 mediumpurple 104
  77. lightslateblue 105 yellow4 106 darkseagreen 108 lightskyblue3 110
  78. skyblue2 111 chartreuse2 112 palegreen3 114 darkslategray3 116
  79. skyblue1 117 chartreuse1 118 lightgreen 120 aquamarine1 122
  80. darkslategray1 123 deeppink4 125 mediumvioletred 126 darkviolet 128
  81. purple 129 mediumorchid3 133 mediumorchid 134 darkgoldenrod 136
  82. rosybrown 138 grey63 139 mediumpurple2 140 mediumpurple1 141
  83. darkkhaki 143 navajowhite3 144 grey69 145 lightsteelblue3 146
  84. lightsteelblue 147 darkolivegreen3 149 darkseagreen3 150 lightcyan3 152
  85. lightskyblue1 153 greenyellow 154 darkolivegreen2 155 palegreen1 156
  86. darkseagreen2 157 paleturquoise1 159 red3 160 deeppink3 162
  87. magenta3 164 darkorange3 166 indianred 167 hotpink3 168
  88. hotpink2 169 orchid 170 orange3 172 lightsalmon3 173
  89. lightpink3 174 pink3 175 plum3 176 violet 177
  90. gold3 178 lightgoldenrod3 179 tan 180 mistyrose3 181
  91. thistle3 182 plum2 183 yellow3 184 khaki3 185
  92. lightyellow3 187 grey84 188 lightsteelblue1 189 yellow2 190
  93. darkolivegreen1 192 darkseagreen1 193 honeydew2 194 lightcyan1 195
  94. red1 196 deeppink2 197 deeppink1 199 magenta2 200
  95. magenta1 201 orangered1 202 indianred1 204 hotpink 206
  96. mediumorchid1 207 darkorange 208 salmon1 209 lightcoral 210
  97. palevioletred1 211 orchid2 212 orchid1 213 orange1 214
  98. sandybrown 215 lightsalmon1 216 lightpink1 217 pink1 218
  99. plum1 219 gold1 220 lightgoldenrod2 222 navajowhite1 223
  100. mistyrose1 224 thistle1 225 yellow1 226 lightgoldenrod1 227
  101. khaki1 228 wheat1 229 cornsilk1 230 grey100 231
  102. grey3 232 grey7 233 grey11 234 grey15 235
  103. grey19 236 grey23 237 grey27 238 grey30 239
  104. grey35 240 grey39 241 grey42 242 grey46 243
  105. grey50 244 grey54 245 grey58 246 grey62 247
  106. grey66 248 grey70 249 grey74 250 grey78 251
  107. grey82 252 grey85 253 grey89 254 grey93 255)
  108. # For compatibility with Powerlevel9k.
  109. #
  110. # Type `getColorCode background` or `getColorCode foreground` to see the list of predefined colors.
  111. function getColorCode() {
  112. eval "$__p9k_intro"
  113. if (( ARGC == 1 )); then
  114. case $1 in
  115. foreground)
  116. local k
  117. for k in "${(k@)__p9k_colors}"; do
  118. local v=${__p9k_colors[$k]}
  119. print -rP -- "%F{$v}$v - $k%f"
  120. done
  121. return 0
  122. ;;
  123. background)
  124. local k
  125. for k in "${(k@)__p9k_colors}"; do
  126. local v=${__p9k_colors[$k]}
  127. print -rP -- "%K{$v}$v - $k%k"
  128. done
  129. return 0
  130. ;;
  131. esac
  132. fi
  133. echo "Usage: getColorCode background|foreground" >&2
  134. return 1
  135. }
  136. # _p9k_declare <type> <uppercase-name> [default]...
  137. function _p9k_declare() {
  138. local -i set=$+parameters[$2]
  139. (( ARGC > 2 || set )) || return 0
  140. case $1 in
  141. -b)
  142. if (( set )); then
  143. [[ ${(P)2} == true ]] && typeset -gi _$2=1 || typeset -gi _$2=0
  144. else
  145. typeset -gi _$2=$3
  146. fi
  147. ;;
  148. -a)
  149. local -a v=("${(@P)2}")
  150. if (( set )); then
  151. eval "typeset -ga _${(q)2}=(${(@qq)v})";
  152. else
  153. if [[ $3 != '--' ]]; then
  154. echo "internal error in _p9k_declare " "${(qqq)@}" >&2
  155. fi
  156. eval "typeset -ga _${(q)2}=(${(@qq)*[4,-1]})"
  157. fi
  158. ;;
  159. -i)
  160. (( set )) && typeset -gi _$2=$2 || typeset -gi _$2=$3
  161. ;;
  162. -F)
  163. (( set )) && typeset -gF _$2=$2 || typeset -gF _$2=$3
  164. ;;
  165. -s)
  166. (( set )) && typeset -g _$2=${(P)2} || typeset -g _$2=$3
  167. ;;
  168. -e)
  169. if (( set )); then
  170. local v=${(P)2}
  171. typeset -g _$2=${(g::)v}
  172. else
  173. typeset -g _$2=${(g::)3}
  174. fi
  175. ;;
  176. *)
  177. echo "internal error in _p9k_declare " "${(qqq)@}" >&2
  178. esac
  179. }
  180. function _p9k_read_word() {
  181. local -a stat
  182. zstat -A stat +mtime -- $1 2>/dev/null || stat=(-1)
  183. local cached=$_p9k__read_word_cache[$1]
  184. if [[ $cached == $stat[1]:* ]]; then
  185. _p9k__ret=${cached#*:}
  186. else
  187. local rest
  188. _p9k__ret=
  189. { read _p9k__ret rest <$1 } 2>/dev/null
  190. _p9k__ret=${_p9k__ret%$'\r'}
  191. _p9k__read_word_cache[$1]=$stat[1]:$_p9k__ret
  192. fi
  193. [[ -n $_p9k__ret ]]
  194. }
  195. function _p9k_fetch_cwd() {
  196. if [[ $PWD == /* && $PWD -ef . ]]; then
  197. _p9k__cwd=$PWD
  198. else
  199. _p9k__cwd=${${${:-.}:a}:-.}
  200. fi
  201. _p9k__cwd_a=${${_p9k__cwd:A}:-.}
  202. case $_p9k__cwd in
  203. /|.)
  204. _p9k__parent_dirs=()
  205. _p9k__parent_mtimes=()
  206. _p9k__parent_mtimes_i=()
  207. _p9k__parent_mtimes_s=
  208. return
  209. ;;
  210. ~|~/*)
  211. local parent=${${${:-~/..}:a}%/}/
  212. local parts=(${(s./.)_p9k__cwd#$parent})
  213. ;;
  214. *)
  215. local parent=/
  216. local parts=(${(s./.)_p9k__cwd})
  217. ;;
  218. esac
  219. local MATCH
  220. _p9k__parent_dirs=(${(@)${:-{$#parts..1}}/(#m)*/$parent${(pj./.)parts[1,MATCH]}})
  221. if ! zstat -A _p9k__parent_mtimes +mtime -- $_p9k__parent_dirs 2>/dev/null; then
  222. _p9k__parent_mtimes=(${(@)parts/*/-1})
  223. fi
  224. _p9k__parent_mtimes_i=(${(@)${:-{1..$#parts}}/(#m)*/$MATCH:$_p9k__parent_mtimes[MATCH]})
  225. _p9k__parent_mtimes_s="$_p9k__parent_mtimes_i"
  226. }
  227. # Usage: _p9k_glob parent_dir_index pattern
  228. #
  229. # parent_dir_index indexes _p9k__parent_dirs.
  230. #
  231. # Returns the number of matches.
  232. #
  233. # Pattern cannot have slashes.
  234. #
  235. # Example: _p9k_glob 3 '*.csproj'
  236. function _p9k_glob() {
  237. local dir=$_p9k__parent_dirs[$1]
  238. local cached=$_p9k__glob_cache[$dir/$2]
  239. if [[ $cached == $_p9k__parent_mtimes[$1]:* ]]; then
  240. return ${cached##*:}
  241. fi
  242. local -a stat
  243. zstat -A stat +mtime -- $dir 2>/dev/null || stat=(-1)
  244. local files=($dir/$~2(N:t))
  245. _p9k__glob_cache[$dir/$2]="$stat[1]:$#files"
  246. return $#files
  247. }
  248. # Usage: _p9k_upglob pattern
  249. #
  250. # Returns index within _p9k__parent_dirs or 0 if there is no match.
  251. #
  252. # Search stops before reaching ~/../ or / and never matches in those directories.
  253. #
  254. # Example: _p9k_upglob '*.csproj'
  255. function _p9k_upglob() {
  256. local cached=$_p9k__upsearch_cache[$_p9k__cwd/$1]
  257. if [[ -n $cached ]]; then
  258. if [[ $_p9k__parent_mtimes_s == ${cached% *}(| *) ]]; then
  259. return ${cached##* }
  260. fi
  261. cached=(${(s: :)cached})
  262. local last_idx=$cached[-1]
  263. cached[-1]=()
  264. local -i i
  265. for i in ${(@)${cached:|_p9k__parent_mtimes_i}%:*}; do
  266. _p9k_glob $i $1 && continue
  267. _p9k__upsearch_cache[$_p9k__cwd/$1]="${_p9k__parent_mtimes_i[1,i]} $i"
  268. return i
  269. done
  270. if (( i != last_idx )); then
  271. _p9k__upsearch_cache[$_p9k__cwd/$1]="${_p9k__parent_mtimes_i[1,$#cached]} $last_idx"
  272. return last_idx
  273. fi
  274. i=$(($#cached + 1))
  275. else
  276. local -i i=1
  277. fi
  278. for ((; i <= $#_p9k__parent_mtimes; ++i)); do
  279. _p9k_glob $i $1 && continue
  280. _p9k__upsearch_cache[$_p9k__cwd/$1]="${_p9k__parent_mtimes_i[1,i]} $i"
  281. return i
  282. done
  283. _p9k__upsearch_cache[$_p9k__cwd/$1]="$_p9k__parent_mtimes_s 0"
  284. return 0
  285. }
  286. # If we execute `print -P $1`, how many characters will be printed on the last line?
  287. # Assumes that `%{%}` and `%G` don't lie.
  288. #
  289. # _p9k_prompt_length '' => 0
  290. # _p9k_prompt_length 'abc' => 3
  291. # _p9k_prompt_length $'abc\nxy' => 2
  292. # _p9k_prompt_length $'\t' => 8
  293. # _p9k_prompt_length '%F{red}abc' => 3
  294. # _p9k_prompt_length $'%{a\b%Gb%}' => 1
  295. function _p9k_prompt_length() {
  296. local -i COLUMNS=1024
  297. local -i x y=${#1} m
  298. if (( y )); then
  299. while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
  300. x=y
  301. (( y *= 2 ))
  302. done
  303. while (( y > x + 1 )); do
  304. (( m = x + (y - x) / 2 ))
  305. (( ${${(%):-$1%$m(l.x.y)}[-1]} = m ))
  306. done
  307. fi
  308. typeset -g _p9k__ret=$x
  309. }
  310. typeset -gr __p9k_byte_suffix=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
  311. # 512 => 512B
  312. # 1800 => 1.76K
  313. # 18000 => 17.6K
  314. function _p9k_human_readable_bytes() {
  315. typeset -F n=$1
  316. local suf
  317. for suf in $__p9k_byte_suffix; do
  318. (( n < 1024 )) && break
  319. (( n /= 1024 ))
  320. done
  321. if (( n >= 100 )); then
  322. printf -v _p9k__ret '%.0f.' $n
  323. elif (( n >= 10 )); then
  324. printf -v _p9k__ret '%.1f' $n
  325. else
  326. printf -v _p9k__ret '%.2f' $n
  327. fi
  328. _p9k__ret=${${_p9k__ret%%0#}%.}$suf
  329. }
  330. if is-at-least 5.4; then
  331. function _p9k_print_params() { typeset -p -- "$@" }
  332. else
  333. # Cannot use `typeset -p` unconditionally because of bugs in zsh.
  334. function _p9k_print_params() {
  335. local name
  336. for name; do
  337. case $parameters[$name] in
  338. array*)
  339. print -r -- "$name=(" "${(@q)${(@P)name}}" ")"
  340. ;;
  341. association*)
  342. # Cannot use "${(@q)${(@kvP)name}}" because of bugs in zsh.
  343. local kv=("${(@kvP)name}")
  344. print -r -- "$name=(" "${(@q)kv}" ")"
  345. ;;
  346. *)
  347. print -r -- "$name=${(q)${(P)name}}"
  348. ;;
  349. esac
  350. done
  351. }
  352. fi
  353. # Determine if the passed segment is used in the prompt
  354. #
  355. # Pass the name of the segment to this function to test for its presence in
  356. # either the LEFT or RIGHT prompt arrays.
  357. # * $1: The segment to be tested.
  358. _p9k_segment_in_use() {
  359. (( $_POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(I)$1(|_joined)] ||
  360. $_POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(I)$1(|_joined)] ))
  361. }
  362. # Caching allows storing array-to-array associations. It should be used like this:
  363. #
  364. # if ! _p9k_cache_get "$key1" "$key2"; then
  365. # # Compute val1 and val2 and then store them in the cache.
  366. # _p9k_cache_set "$val1" "$val2"
  367. # fi
  368. # # Here ${_p9k__cache_val[1]} and ${_p9k__cache_val[2]} are $val1 and $val2 respectively.
  369. #
  370. # Limitations:
  371. #
  372. # * Calling _p9k_cache_set without arguments clears the cache entry. Subsequent calls to
  373. # _p9k_cache_get for the same key will return an error.
  374. # * There must be no intervening _p9k_cache_get calls between the associated _p9k_cache_get
  375. # and _p9k_cache_set.
  376. _p9k_cache_set() {
  377. # Uncomment to see cache misses.
  378. # echo "caching: ${(@0q)_p9k__cache_key} => (${(q)@})" >&2
  379. _p9k_cache[$_p9k__cache_key]="${(pj:\0:)*}0"
  380. _p9k__cache_val=("$@")
  381. _p9k__state_dump_scheduled=1
  382. }
  383. _p9k_cache_get() {
  384. _p9k__cache_key="${(pj:\0:)*}"
  385. local v=$_p9k_cache[$_p9k__cache_key]
  386. [[ -n $v ]] && _p9k__cache_val=("${(@0)${v[1,-2]}}")
  387. }
  388. _p9k_cache_ephemeral_set() {
  389. # Uncomment to see cache misses.
  390. # echo "caching: ${(@0q)_p9k__cache_key} => (${(q)@})" >&2
  391. _p9k__cache_ephemeral[$_p9k__cache_key]="${(pj:\0:)*}0"
  392. _p9k__cache_val=("$@")
  393. }
  394. _p9k_cache_ephemeral_get() {
  395. _p9k__cache_key="${(pj:\0:)*}"
  396. local v=$_p9k__cache_ephemeral[$_p9k__cache_key]
  397. [[ -n $v ]] && _p9k__cache_val=("${(@0)${v[1,-2]}}")
  398. }
  399. _p9k_cache_stat_get() {
  400. local -H stat
  401. local label=$1 f
  402. shift
  403. _p9k__cache_stat_meta=
  404. _p9k__cache_stat_fprint=
  405. for f; do
  406. if zstat -H stat -- $f 2>/dev/null; then
  407. _p9k__cache_stat_meta+="${(q)f} $stat[inode] $stat[mtime] $stat[size] $stat[mode]; "
  408. fi
  409. done
  410. if _p9k_cache_get $0 $label meta "$@"; then
  411. if [[ $_p9k__cache_val[1] == $_p9k__cache_stat_meta ]]; then
  412. _p9k__cache_stat_fprint=$_p9k__cache_val[2]
  413. local -a key=($0 $label fprint "$@" "$_p9k__cache_stat_fprint")
  414. _p9k__cache_fprint_key="${(pj:\0:)key}"
  415. shift 2 _p9k__cache_val
  416. return 0
  417. else
  418. local -a key=($0 $label fprint "$@" "$_p9k__cache_val[2]")
  419. _p9k__cache_ephemeral[${(pj:\0:)key}]="${(pj:\0:)_p9k__cache_val[3,-1]}0"
  420. fi
  421. fi
  422. if (( $+commands[md5] )); then
  423. _p9k__cache_stat_fprint="$(md5 -- $* 2>&1)"
  424. elif (( $+commands[md5sum] )); then
  425. _p9k__cache_stat_fprint="$(md5sum -b -- $* 2>&1)"
  426. else
  427. return 1
  428. fi
  429. local meta_key=$_p9k__cache_key
  430. if _p9k_cache_ephemeral_get $0 $label fprint "$@" "$_p9k__cache_stat_fprint"; then
  431. _p9k__cache_fprint_key=$_p9k__cache_key
  432. _p9k__cache_key=$meta_key
  433. _p9k_cache_set "$_p9k__cache_stat_meta" "$_p9k__cache_stat_fprint" "$_p9k__cache_val[@]"
  434. shift 2 _p9k__cache_val
  435. return 0
  436. fi
  437. _p9k__cache_fprint_key=$_p9k__cache_key
  438. _p9k__cache_key=$meta_key
  439. return 1
  440. }
  441. _p9k_cache_stat_set() {
  442. _p9k_cache_set "$_p9k__cache_stat_meta" "$_p9k__cache_stat_fprint" "$@"
  443. _p9k__cache_key=$_p9k__cache_fprint_key
  444. _p9k_cache_ephemeral_set "$@"
  445. }
  446. # _p9k_param prompt_foo_BAR BACKGROUND red
  447. _p9k_param() {
  448. local key="_p9k_param ${(pj:\0:)*}"
  449. _p9k__ret=$_p9k_cache[$key]
  450. if [[ -n $_p9k__ret ]]; then
  451. _p9k__ret[-1,-1]=''
  452. else
  453. if [[ ${1//-/_} == (#b)prompt_([a-z0-9_]#)(*) ]]; then
  454. local var=_POWERLEVEL9K_${${(U)match[1]}//İ/I}$match[2]_$2
  455. if (( $+parameters[$var] )); then
  456. _p9k__ret=${(P)var}
  457. else
  458. var=_POWERLEVEL9K_${${(U)match[1]%_}//İ/I}_$2
  459. if (( $+parameters[$var] )); then
  460. _p9k__ret=${(P)var}
  461. else
  462. var=_POWERLEVEL9K_$2
  463. if (( $+parameters[$var] )); then
  464. _p9k__ret=${(P)var}
  465. else
  466. _p9k__ret=$3
  467. fi
  468. fi
  469. fi
  470. else
  471. local var=_POWERLEVEL9K_$2
  472. if (( $+parameters[$var] )); then
  473. _p9k__ret=${(P)var}
  474. else
  475. _p9k__ret=$3
  476. fi
  477. fi
  478. _p9k_cache[$key]=${_p9k__ret}.
  479. fi
  480. }
  481. # _p9k_get_icon prompt_foo_BAR BAZ_ICON quix
  482. _p9k_get_icon() {
  483. local key="_p9k_get_icon ${(pj:\0:)*}"
  484. _p9k__ret=$_p9k_cache[$key]
  485. if [[ -n $_p9k__ret ]]; then
  486. _p9k__ret[-1,-1]=''
  487. else
  488. if [[ $2 == $'\1'* ]]; then
  489. _p9k__ret=${2[2,-1]}
  490. else
  491. _p9k_param "$1" "$2" ${icons[$2]-$'\1'$3}
  492. if [[ $_p9k__ret == $'\1'* ]]; then
  493. _p9k__ret=${_p9k__ret[2,-1]}
  494. else
  495. _p9k__ret=${(g::)_p9k__ret}
  496. [[ $_p9k__ret != $'\b'? ]] || _p9k__ret="%{$_p9k__ret%}" # penance for past sins
  497. fi
  498. fi
  499. _p9k_cache[$key]=${_p9k__ret}.
  500. fi
  501. }
  502. _p9k_translate_color() {
  503. if [[ $1 == <-> ]]; then # decimal color code: 255
  504. _p9k__ret=${(l.3..0.)1}
  505. elif [[ $1 == '#'[[:xdigit:]]## ]]; then # hexademical color code: #ffffff
  506. _p9k__ret=${${(L)1}//ı/i}
  507. else # named color: red
  508. # Strip prifixes if there are any.
  509. _p9k__ret=$__p9k_colors[${${${1#bg-}#fg-}#br}]
  510. fi
  511. }
  512. # _p9k_color prompt_foo_BAR BACKGROUND red
  513. _p9k_color() {
  514. local key="_p9k_color ${(pj:\0:)*}"
  515. _p9k__ret=$_p9k_cache[$key]
  516. if [[ -n $_p9k__ret ]]; then
  517. _p9k__ret[-1,-1]=''
  518. else
  519. _p9k_param "$@"
  520. _p9k_translate_color $_p9k__ret
  521. _p9k_cache[$key]=${_p9k__ret}.
  522. fi
  523. }
  524. # _p9k_vcs_style CLEAN REMOTE_BRANCH
  525. _p9k_vcs_style() {
  526. local key="$0 ${(pj:\0:)*}"
  527. _p9k__ret=$_p9k_cache[$key]
  528. if [[ -n $_p9k__ret ]]; then
  529. _p9k__ret[-1,-1]=''
  530. else
  531. local style=%b # TODO: support bold
  532. _p9k_color prompt_vcs_$1 BACKGROUND "${__p9k_vcs_states[$1]}"
  533. _p9k_background $_p9k__ret
  534. style+=$_p9k__ret
  535. local var=_POWERLEVEL9K_VCS_${1}_${2}FORMAT_FOREGROUND
  536. if (( $+parameters[$var] )); then
  537. _p9k_translate_color "${(P)var}"
  538. else
  539. var=_POWERLEVEL9K_VCS_${2}FORMAT_FOREGROUND
  540. if (( $+parameters[$var] )); then
  541. _p9k_translate_color "${(P)var}"
  542. else
  543. _p9k_color prompt_vcs_$1 FOREGROUND "$_p9k_color1"
  544. fi
  545. fi
  546. _p9k_foreground $_p9k__ret
  547. _p9k__ret=$style$_p9k__ret
  548. _p9k_cache[$key]=${_p9k__ret}.
  549. fi
  550. }
  551. _p9k_background() {
  552. [[ -n $1 ]] && _p9k__ret="%K{$1}" || _p9k__ret="%k"
  553. }
  554. _p9k_foreground() {
  555. # Note: This code used to produce `%1F` instead of `%F{1}` because it's more efficient.
  556. # Unfortunately, this triggers a bug in zsh. Namely, `%1F{2}` gets percent-expanded as if
  557. # it was `%F{2}`.
  558. [[ -n $1 ]] && _p9k__ret="%F{$1}" || _p9k__ret="%f"
  559. }
  560. _p9k_escape_style() {
  561. [[ $1 == *'}'* ]] && _p9k__ret='${:-"'$1'"}' || _p9k__ret=$1
  562. }
  563. _p9k_escape() {
  564. [[ $1 == *["~!#\`\$^&*()\\\"'<>?{}[]"]* ]] && _p9k__ret="\${(Q)\${:-${(qqq)${(q)1}}}}" || _p9k__ret=$1
  565. }
  566. # * $1: Name of the function that was originally invoked.
  567. # Necessary, to make the dynamic color-overwrite mechanism work.
  568. # * $2: Background color.
  569. # * $3: Foreground color.
  570. # * $4: An identifying icon.
  571. # * $5: 1 to to perform parameter expansion and process substitution.
  572. # * $6: If not empty but becomes empty after parameter expansion and process substitution,
  573. # the segment isn't rendered.
  574. # * $7: Content.
  575. _p9k_left_prompt_segment() {
  576. if ! _p9k_cache_get "$0" "$1" "$2" "$3" "$4" "$_p9k__segment_index"; then
  577. _p9k_color $1 BACKGROUND $2
  578. local bg_color=$_p9k__ret
  579. _p9k_background $bg_color
  580. local bg=$_p9k__ret
  581. _p9k_color $1 FOREGROUND $3
  582. local fg_color=$_p9k__ret
  583. _p9k_foreground $fg_color
  584. local fg=$_p9k__ret
  585. local style=%b$bg$fg
  586. local style_=${style//\}/\\\}}
  587. _p9k_get_icon $1 LEFT_SEGMENT_SEPARATOR
  588. local sep=$_p9k__ret
  589. _p9k_escape $_p9k__ret
  590. local sep_=$_p9k__ret
  591. _p9k_get_icon $1 LEFT_SUBSEGMENT_SEPARATOR
  592. _p9k_escape $_p9k__ret
  593. local subsep_=$_p9k__ret
  594. local icon_
  595. if [[ -n $4 ]]; then
  596. _p9k_get_icon $1 $4
  597. _p9k_escape $_p9k__ret
  598. icon_=$_p9k__ret
  599. fi
  600. _p9k_get_icon $1 LEFT_PROMPT_FIRST_SEGMENT_START_SYMBOL
  601. local start_sep=$_p9k__ret
  602. [[ -n $start_sep ]] && start_sep="%b%k%F{$bg_color}$start_sep"
  603. _p9k_get_icon $1 LEFT_PROMPT_LAST_SEGMENT_END_SYMBOL $sep
  604. _p9k_escape $_p9k__ret
  605. local end_sep_=$_p9k__ret
  606. _p9k_get_icon $1 WHITESPACE_BETWEEN_LEFT_SEGMENTS ' '
  607. local space=$_p9k__ret
  608. _p9k_get_icon $1 LEFT_LEFT_WHITESPACE $space
  609. local left_space=$_p9k__ret
  610. [[ $left_space == *%* ]] && left_space+=$style
  611. _p9k_get_icon $1 LEFT_RIGHT_WHITESPACE $space
  612. _p9k_escape $_p9k__ret
  613. local right_space_=$_p9k__ret
  614. [[ $right_space_ == *%* ]] && right_space_+=$style_
  615. local s='<_p9k__s>' ss='<_p9k__ss>'
  616. local -i non_hermetic=0
  617. # Segment separator logic:
  618. #
  619. # if [[ $_p9k__bg == NONE ]]; then
  620. # 1
  621. # elif (( joined )); then
  622. # 2
  623. # elif [[ $bg_color == (${_p9k__bg}|${_p9k__bg:-0}) ]]; then
  624. # 3
  625. # else
  626. # 4
  627. # fi
  628. local t=$(($#_p9k_t - __p9k_ksh_arrays))
  629. _p9k_t+=$start_sep$style$left_space # 1
  630. _p9k_t+=$style # 2
  631. if [[ -n $fg_color && $fg_color == $bg_color ]]; then
  632. if [[ $fg_color == $_p9k_color1 ]]; then
  633. _p9k_foreground $_p9k_color2
  634. else
  635. _p9k_foreground $_p9k_color1
  636. fi
  637. _p9k_t+=%b$bg$_p9k__ret$ss$style$left_space # 3
  638. else
  639. _p9k_t+=%b$bg$ss$style$left_space # 3
  640. fi
  641. _p9k_t+=%b$bg$s$style$left_space # 4
  642. local join="_p9k__i>=$_p9k_left_join[$_p9k__segment_index]"
  643. _p9k_param $1 SELF_JOINED false
  644. if [[ $_p9k__ret == false ]]; then
  645. if (( _p9k__segment_index > $_p9k_left_join[$_p9k__segment_index] )); then
  646. join+="&&_p9k__i<$_p9k__segment_index"
  647. else
  648. join=
  649. fi
  650. fi
  651. local p=
  652. p+="\${_p9k__n::=}"
  653. p+="\${\${\${_p9k__bg:-0}:#NONE}:-\${_p9k__n::=$((t+1))}}" # 1
  654. if [[ -n $join ]]; then
  655. p+="\${_p9k__n:=\${\${\$(($join)):#0}:+$((t+2))}}" # 2
  656. fi
  657. if (( __p9k_sh_glob )); then
  658. p+="\${_p9k__n:=\${\${(M)\${:-x$bg_color}:#x\$_p9k__bg}:+$((t+3))}}" # 3
  659. p+="\${_p9k__n:=\${\${(M)\${:-x$bg_color}:#x\$${_p9k__bg:-0}}:+$((t+3))}}" # 3
  660. else
  661. p+="\${_p9k__n:=\${\${(M)\${:-x$bg_color}:#x(\$_p9k__bg|\${_p9k__bg:-0})}:+$((t+3))}}" # 3
  662. fi
  663. p+="\${_p9k__n:=$((t+4))}" # 4
  664. _p9k_param $1 VISUAL_IDENTIFIER_EXPANSION '${P9K_VISUAL_IDENTIFIER}'
  665. [[ $_p9k__ret == (|*[^\\])'$('* ]] && non_hermetic=1
  666. local icon_exp_=${_p9k__ret:+\"$_p9k__ret\"}
  667. _p9k_param $1 CONTENT_EXPANSION '${P9K_CONTENT}'
  668. [[ $_p9k__ret == (|*[^\\])'$('* ]] && non_hermetic=1
  669. local content_exp_=${_p9k__ret:+\"$_p9k__ret\"}
  670. if [[ ( $icon_exp_ != '"${P9K_VISUAL_IDENTIFIER}"' && $icon_exp_ == *'$'* ) ||
  671. ( $content_exp_ != '"${P9K_CONTENT}"' && $content_exp_ == *'$'* ) ]]; then
  672. p+="\${P9K_VISUAL_IDENTIFIER::=$icon_}"
  673. fi
  674. local -i has_icon=-1 # maybe
  675. if [[ $icon_exp_ != '"${P9K_VISUAL_IDENTIFIER}"' && $icon_exp_ == *'$'* ]]; then
  676. p+='${_p9k__v::='$icon_exp_$style_'}'
  677. else
  678. [[ $icon_exp_ == '"${P9K_VISUAL_IDENTIFIER}"' ]] && _p9k__ret=$icon_ || _p9k__ret=$icon_exp_
  679. if [[ -n $_p9k__ret ]]; then
  680. p+="\${_p9k__v::=$_p9k__ret"
  681. [[ $_p9k__ret == *%* ]] && p+=$style_
  682. p+="}"
  683. has_icon=1 # definitely yes
  684. else
  685. has_icon=0 # definitely no
  686. fi
  687. fi
  688. p+='${_p9k__c::='$content_exp_'}${_p9k__c::=${_p9k__c//'$'\r''}}'
  689. p+='${_p9k__e::=${${_p9k__'${_p9k__line_index}l${${1#prompt_}%%[A-Z0-9_]#}'+00}:-'
  690. if (( has_icon == -1 )); then
  691. p+='${${(%):-$_p9k__c%1(l.1.0)}[-1]}${${(%):-$_p9k__v%1(l.1.0)}[-1]}}'
  692. else
  693. p+='${${(%):-$_p9k__c%1(l.1.0)}[-1]}'$has_icon'}'
  694. fi
  695. p+='}}+}'
  696. p+='${${_p9k__e:#00}:+${${_p9k_t[$_p9k__n]/'$ss'/$_p9k__ss}/'$s'/$_p9k__s}'
  697. _p9k_param $1 ICON_BEFORE_CONTENT ''
  698. if [[ $_p9k__ret != false ]]; then
  699. _p9k_param $1 PREFIX ''
  700. _p9k__ret=${(g::)_p9k__ret}
  701. _p9k_escape $_p9k__ret
  702. p+=$_p9k__ret
  703. [[ $_p9k__ret == *%* ]] && local -i need_style=1 || local -i need_style=0
  704. if (( has_icon != 0 )); then
  705. _p9k_color $1 VISUAL_IDENTIFIER_COLOR $fg_color
  706. _p9k_foreground $_p9k__ret
  707. _p9k__ret=%b$bg$_p9k__ret
  708. _p9k__ret=${_p9k__ret//\}/\\\}}
  709. if [[ $_p9k__ret != $style_ ]]; then
  710. p+=$_p9k__ret'${_p9k__v}'$style_
  711. else
  712. (( need_style )) && p+=$style_
  713. p+='${_p9k__v}'
  714. fi
  715. _p9k_get_icon $1 LEFT_MIDDLE_WHITESPACE ' '
  716. if [[ -n $_p9k__ret ]]; then
  717. _p9k_escape $_p9k__ret
  718. [[ _p9k__ret == *%* ]] && _p9k__ret+=$style_
  719. p+='${${(M)_p9k__e:#11}:+'$_p9k__ret'}'
  720. fi
  721. elif (( need_style )); then
  722. p+=$style_
  723. fi
  724. p+='${_p9k__c}'$style_
  725. else
  726. _p9k_param $1 PREFIX ''
  727. _p9k__ret=${(g::)_p9k__ret}
  728. _p9k_escape $_p9k__ret
  729. p+=$_p9k__ret
  730. [[ $_p9k__ret == *%* ]] && p+=$style_
  731. p+='${_p9k__c}'$style_
  732. if (( has_icon != 0 )); then
  733. local -i need_style=0
  734. _p9k_get_icon $1 LEFT_MIDDLE_WHITESPACE ' '
  735. if [[ -n $_p9k__ret ]]; then
  736. _p9k_escape $_p9k__ret
  737. [[ $_p9k__ret == *%* ]] && need_style=1
  738. p+='${${(M)_p9k__e:#11}:+'$_p9k__ret'}'
  739. fi
  740. _p9k_color $1 VISUAL_IDENTIFIER_COLOR $fg_color
  741. _p9k_foreground $_p9k__ret
  742. _p9k__ret=%b$bg$_p9k__ret
  743. _p9k__ret=${_p9k__ret//\}/\\\}}
  744. [[ $_p9k__ret != $style_ || $need_style == 1 ]] && p+=$_p9k__ret
  745. p+='$_p9k__v'
  746. fi
  747. fi
  748. _p9k_param $1 SUFFIX ''
  749. _p9k__ret=${(g::)_p9k__ret}
  750. _p9k_escape $_p9k__ret
  751. p+=$_p9k__ret
  752. [[ $_p9k__ret == *%* && -n $right_space_ ]] && p+=$style_
  753. p+=$right_space_
  754. p+='${${:-'
  755. p+="\${_p9k__s::=%F{$bg_color\}$sep_}\${_p9k__ss::=$subsep_}\${_p9k__sss::=%F{$bg_color\}$end_sep_}"
  756. p+="\${_p9k__i::=$_p9k__segment_index}\${_p9k__bg::=$bg_color}"
  757. p+='}+}'
  758. p+='}'
  759. _p9k_param $1 SHOW_ON_UPGLOB ''
  760. _p9k_cache_set "$p" $non_hermetic $_p9k__ret
  761. fi
  762. if [[ -n $_p9k__cache_val[3] ]]; then
  763. _p9k__has_upglob=1
  764. _p9k_upglob $_p9k__cache_val[3] && return
  765. fi
  766. _p9k__non_hermetic_expansion=$_p9k__cache_val[2]
  767. (( $5 )) && _p9k__ret=\"$7\" || _p9k_escape $7
  768. if [[ -z $6 ]]; then
  769. _p9k__prompt+="\${\${:-\${P9K_CONTENT::=$_p9k__ret}$_p9k__cache_val[1]"
  770. else
  771. _p9k__prompt+="\${\${:-\"$6\"}:+\${\${:-\${P9K_CONTENT::=$_p9k__ret}$_p9k__cache_val[1]}"
  772. fi
  773. }
  774. # The same as _p9k_left_prompt_segment above but for the right prompt.
  775. _p9k_right_prompt_segment() {
  776. if ! _p9k_cache_get "$0" "$1" "$2" "$3" "$4" "$_p9k__segment_index"; then
  777. _p9k_color $1 BACKGROUND $2
  778. local bg_color=$_p9k__ret
  779. _p9k_background $bg_color
  780. local bg=$_p9k__ret
  781. local bg_=${_p9k__ret//\}/\\\}}
  782. _p9k_color $1 FOREGROUND $3
  783. local fg_color=$_p9k__ret
  784. _p9k_foreground $fg_color
  785. local fg=$_p9k__ret
  786. local style=%b$bg$fg
  787. local style_=${style//\}/\\\}}
  788. _p9k_get_icon $1 RIGHT_SEGMENT_SEPARATOR
  789. local sep=$_p9k__ret
  790. _p9k_escape $_p9k__ret
  791. local sep_=$_p9k__ret
  792. _p9k_get_icon $1 RIGHT_SUBSEGMENT_SEPARATOR
  793. local subsep=$_p9k__ret
  794. [[ $subsep == *%* ]] && subsep+=$style
  795. local icon_
  796. if [[ -n $4 ]]; then
  797. _p9k_get_icon $1 $4
  798. _p9k_escape $_p9k__ret
  799. icon_=$_p9k__ret
  800. fi
  801. _p9k_get_icon $1 RIGHT_PROMPT_FIRST_SEGMENT_START_SYMBOL $sep
  802. local start_sep=$_p9k__ret
  803. [[ -n $start_sep ]] && start_sep="%b%k%F{$bg_color}$start_sep"
  804. _p9k_get_icon $1 RIGHT_PROMPT_LAST_SEGMENT_END_SYMBOL
  805. _p9k_escape $_p9k__ret
  806. local end_sep_=$_p9k__ret
  807. _p9k_get_icon $1 WHITESPACE_BETWEEN_RIGHT_SEGMENTS ' '
  808. local space=$_p9k__ret
  809. _p9k_get_icon $1 RIGHT_LEFT_WHITESPACE $space
  810. local left_space=$_p9k__ret
  811. [[ $left_space == *%* ]] && left_space+=$style
  812. _p9k_get_icon $1 RIGHT_RIGHT_WHITESPACE $space
  813. _p9k_escape $_p9k__ret
  814. local right_space_=$_p9k__ret
  815. [[ $right_space_ == *%* ]] && right_space_+=$style_
  816. local w='<_p9k__w>' s='<_p9k__s>'
  817. local -i non_hermetic=0
  818. # Segment separator logic:
  819. #
  820. # if [[ $_p9k__bg == NONE ]]; then
  821. # 1
  822. # elif (( joined )); then
  823. # 2
  824. # elif [[ $_p9k__bg == (${bg_color}|${bg_color:-0}) ]]; then
  825. # 3
  826. # else
  827. # 4
  828. # fi
  829. local t=$(($#_p9k_t - __p9k_ksh_arrays))
  830. _p9k_t+=$start_sep$style$left_space # 1
  831. _p9k_t+=$w$style # 2
  832. _p9k_t+=$w$style$subsep$left_space # 3
  833. _p9k_t+=$w%F{$bg_color}$sep$style$left_space # 4
  834. local join="_p9k__i>=$_p9k_right_join[$_p9k__segment_index]"
  835. _p9k_param $1 SELF_JOINED false
  836. if [[ $_p9k__ret == false ]]; then
  837. if (( _p9k__segment_index > $_p9k_right_join[$_p9k__segment_index] )); then
  838. join+="&&_p9k__i<$_p9k__segment_index"
  839. else
  840. join=
  841. fi
  842. fi
  843. local p=
  844. p+="\${_p9k__n::=}"
  845. p+="\${\${\${_p9k__bg:-0}:#NONE}:-\${_p9k__n::=$((t+1))}}" # 1
  846. if [[ -n $join ]]; then
  847. p+="\${_p9k__n:=\${\${\$(($join)):#0}:+$((t+2))}}" # 2
  848. fi
  849. if (( __p9k_sh_glob )); then
  850. p+="\${_p9k__n:=\${\${(M)\${:-x\$_p9k__bg}:#x${(b)bg_color}}:+$((t+3))}}" # 3
  851. p+="\${_p9k__n:=\${\${(M)\${:-x\$_p9k__bg}:#x${(b)bg_color:-0}}:+$((t+3))}}" # 3
  852. else
  853. p+="\${_p9k__n:=\${\${(M)\${:-x\$_p9k__bg}:#x(${(b)bg_color}|${(b)bg_color:-0})}:+$((t+3))}}" # 3
  854. fi
  855. p+="\${_p9k__n:=$((t+4))}" # 4
  856. _p9k_param $1 VISUAL_IDENTIFIER_EXPANSION '${P9K_VISUAL_IDENTIFIER}'
  857. [[ $_p9k__ret == (|*[^\\])'$('* ]] && non_hermetic=1
  858. local icon_exp_=${_p9k__ret:+\"$_p9k__ret\"}
  859. _p9k_param $1 CONTENT_EXPANSION '${P9K_CONTENT}'
  860. [[ $_p9k__ret == (|*[^\\])'$('* ]] && non_hermetic=1
  861. local content_exp_=${_p9k__ret:+\"$_p9k__ret\"}
  862. if [[ ( $icon_exp_ != '"${P9K_VISUAL_IDENTIFIER}"' && $icon_exp_ == *'$'* ) ||
  863. ( $content_exp_ != '"${P9K_CONTENT}"' && $content_exp_ == *'$'* ) ]]; then
  864. p+="\${P9K_VISUAL_IDENTIFIER::=$icon_}"
  865. fi
  866. local -i has_icon=-1 # maybe
  867. if [[ $icon_exp_ != '"${P9K_VISUAL_IDENTIFIER}"' && $icon_exp_ == *'$'* ]]; then
  868. p+="\${_p9k__v::=$icon_exp_$style_}"
  869. else
  870. [[ $icon_exp_ == '"${P9K_VISUAL_IDENTIFIER}"' ]] && _p9k__ret=$icon_ || _p9k__ret=$icon_exp_
  871. if [[ -n $_p9k__ret ]]; then
  872. p+="\${_p9k__v::=$_p9k__ret"
  873. [[ $_p9k__ret == *%* ]] && p+=$style_
  874. p+="}"
  875. has_icon=1 # definitely yes
  876. else
  877. has_icon=0 # definitely no
  878. fi
  879. fi
  880. p+='${_p9k__c::='$content_exp_'}${_p9k__c::=${_p9k__c//'$'\r''}}'
  881. p+='${_p9k__e::=${${_p9k__'${_p9k__line_index}r${${1#prompt_}%%[A-Z0-9_]#}'+00}:-'
  882. if (( has_icon == -1 )); then
  883. p+='${${(%):-$_p9k__c%1(l.1.0)}[-1]}${${(%):-$_p9k__v%1(l.1.0)}[-1]}}'
  884. else
  885. p+='${${(%):-$_p9k__c%1(l.1.0)}[-1]}'$has_icon'}'
  886. fi
  887. p+='}}+}'
  888. p+='${${_p9k__e:#00}:+${_p9k_t[$_p9k__n]/'$w'/$_p9k__w}'
  889. _p9k_param $1 ICON_BEFORE_CONTENT ''
  890. if [[ $_p9k__ret != true ]]; then
  891. _p9k_param $1 PREFIX ''
  892. _p9k__ret=${(g::)_p9k__ret}
  893. _p9k_escape $_p9k__ret
  894. p+=$_p9k__ret
  895. [[ $_p9k__ret == *%* ]] && p+=$style_
  896. p+='${_p9k__c}'$style_
  897. if (( has_icon != 0 )); then
  898. local -i need_style=0
  899. _p9k_get_icon $1 RIGHT_MIDDLE_WHITESPACE ' '
  900. if [[ -n $_p9k__ret ]]; then
  901. _p9k_escape $_p9k__ret
  902. [[ $_p9k__ret == *%* ]] && need_style=1
  903. p+='${${(M)_p9k__e:#11}:+'$_p9k__ret'}'
  904. fi
  905. _p9k_color $1 VISUAL_IDENTIFIER_COLOR $fg_color
  906. _p9k_foreground $_p9k__ret
  907. _p9k__ret=%b$bg$_p9k__ret
  908. _p9k__ret=${_p9k__ret//\}/\\\}}
  909. [[ $_p9k__ret != $style_ || $need_style == 1 ]] && p+=$_p9k__ret
  910. p+='$_p9k__v'
  911. fi
  912. else
  913. _p9k_param $1 PREFIX ''
  914. _p9k__ret=${(g::)_p9k__ret}
  915. _p9k_escape $_p9k__ret
  916. p+=$_p9k__ret
  917. [[ $_p9k__ret == *%* ]] && local -i need_style=1 || local -i need_style=0
  918. if (( has_icon != 0 )); then
  919. _p9k_color $1 VISUAL_IDENTIFIER_COLOR $fg_color
  920. _p9k_foreground $_p9k__ret
  921. _p9k__ret=%b$bg$_p9k__ret
  922. _p9k__ret=${_p9k__ret//\}/\\\}}
  923. if [[ $_p9k__ret != $style_ ]]; then
  924. p+=$_p9k__ret'${_p9k__v}'$style_
  925. else
  926. (( need_style )) && p+=$style_
  927. p+='${_p9k__v}'
  928. fi
  929. _p9k_get_icon $1 RIGHT_MIDDLE_WHITESPACE ' '
  930. if [[ -n $_p9k__ret ]]; then
  931. _p9k_escape $_p9k__ret
  932. [[ _p9k__ret == *%* ]] && _p9k__ret+=$style_
  933. p+='${${(M)_p9k__e:#11}:+'$_p9k__ret'}'
  934. fi
  935. elif (( need_style )); then
  936. p+=$style_
  937. fi
  938. p+='${_p9k__c}'$style_
  939. fi
  940. _p9k_param $1 SUFFIX ''
  941. _p9k__ret=${(g::)_p9k__ret}
  942. _p9k_escape $_p9k__ret
  943. p+=$_p9k__ret
  944. p+='${${:-'
  945. if [[ -n $fg_color && $fg_color == $bg_color ]]; then
  946. if [[ $fg_color == $_p9k_color1 ]]; then
  947. _p9k_foreground $_p9k_color2
  948. else
  949. _p9k_foreground $_p9k_color1
  950. fi
  951. else
  952. _p9k__ret=$fg
  953. fi
  954. _p9k__ret=${_p9k__ret//\}/\\\}}
  955. p+="\${_p9k__w::=${right_space_:+$style_}$right_space_%b$bg_$_p9k__ret}"
  956. p+='${_p9k__sss::='
  957. p+=$style_$right_space_
  958. [[ $right_space_ == *%* ]] && p+=$style_
  959. if [[ -n $end_sep_ ]]; then
  960. p+="%k%F{$bg_color\}$end_sep_$style_"
  961. fi
  962. p+='}'
  963. p+="\${_p9k__i::=$_p9k__segment_index}\${_p9k__bg::=$bg_color}"
  964. p+='}+}'
  965. p+='}'
  966. _p9k_param $1 SHOW_ON_UPGLOB ''
  967. _p9k_cache_set "$p" $non_hermetic $_p9k__ret
  968. fi
  969. if [[ -n $_p9k__cache_val[3] ]]; then
  970. _p9k__has_upglob=1
  971. _p9k_upglob $_p9k__cache_val[3] && return
  972. fi
  973. _p9k__non_hermetic_expansion=$_p9k__cache_val[2]
  974. (( $5 )) && _p9k__ret=\"$7\" || _p9k_escape $7
  975. if [[ -z $6 ]]; then
  976. _p9k__prompt+="\${\${:-\${P9K_CONTENT::=$_p9k__ret}$_p9k__cache_val[1]"
  977. else
  978. _p9k__prompt+="\${\${:-\"$6\"}:+\${\${:-\${P9K_CONTENT::=$_p9k__ret}$_p9k__cache_val[1]}"
  979. fi
  980. }
  981. function _p9k_prompt_segment() { "_p9k_${_p9k__prompt_side}_prompt_segment" "$@" }
  982. function p9k_prompt_segment() { p10k segment "$@" }
  983. function _p9k_python_version() {
  984. case $commands[python] in
  985. "")
  986. return 1
  987. ;;
  988. ${PYENV_ROOT:-~/.pyenv}/shims/python)
  989. local P9K_PYENV_PYTHON_VERSION _p9k__pyenv_version
  990. local -i _POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW=1 _POWERLEVEL9K_PYENV_SHOW_SYSTEM=1
  991. local _POWERLEVEL9K_PYENV_SOURCES=(shell local global)
  992. if _p9k_pyenv_compute && [[ $P9K_PYENV_PYTHON_VERSION == ([[:digit:].]##)* ]]; then
  993. _p9k__ret=$P9K_PYENV_PYTHON_VERSION
  994. return 0
  995. fi
  996. ;& # fall through
  997. *)
  998. _p9k_cached_cmd 1 '' python --version || return
  999. [[ $_p9k__ret == (#b)Python\ ([[:digit:].]##)* ]] && _p9k__ret=$match[1]
  1000. ;;
  1001. esac
  1002. }
  1003. ################################################################
  1004. # Prompt Segment Definitions
  1005. ################################################################
  1006. ################################################################
  1007. # Anaconda Environment
  1008. prompt_anaconda() {
  1009. local msg
  1010. if _p9k_python_version; then
  1011. P9K_ANACONDA_PYTHON_VERSION=$_p9k__ret
  1012. if (( _POWERLEVEL9K_ANACONDA_SHOW_PYTHON_VERSION )); then
  1013. msg="${P9K_ANACONDA_PYTHON_VERSION//\%/%%} "
  1014. fi
  1015. else
  1016. unset P9K_ANACONDA_PYTHON_VERSION
  1017. fi
  1018. local p=${CONDA_PREFIX:-$CONDA_ENV_PATH}
  1019. msg+="$_POWERLEVEL9K_ANACONDA_LEFT_DELIMITER${${p:t}//\%/%%}$_POWERLEVEL9K_ANACONDA_RIGHT_DELIMITER"
  1020. _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '' "$msg"
  1021. }
  1022. _p9k_prompt_anaconda_init() {
  1023. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${CONDA_PREFIX:-$CONDA_ENV_PATH}'
  1024. }
  1025. # Populates array `reply` with "$#profile:$profile:$region" where $profile and $region
  1026. # come from the AWS config (~/.aws/config).
  1027. function _p9k_parse_aws_config() {
  1028. local cfg=$1
  1029. typeset -ga reply=()
  1030. [[ -f $cfg && -r $cfg ]] || return
  1031. local -a lines
  1032. lines=(${(f)"$(<$cfg)"}) || return
  1033. local line profile
  1034. local -a match mbegin mend
  1035. for line in $lines; do
  1036. if [[ $line == [[:space:]]#'[default]'[[:space:]]#(|'#'*) ]]; then
  1037. # example: [default]
  1038. profile=default
  1039. elif [[ $line == (#b)'[profile'[[:space:]]##([^[:space:]]|[^[:space:]]*[^[:space:]])[[:space:]]#']'[[:space:]]#(|'#'*) ]]; then
  1040. # example: [profile prod]
  1041. profile=${(Q)match[1]}
  1042. elif [[ $line == (#b)[[:space:]]#region[[:space:]]#=[[:space:]]#([^[:space:]]|[^[:space:]]*[^[:space:]])[[:space:]]# ]]; then
  1043. # example: region = eu-west-1
  1044. if [[ -n $profile ]]; then
  1045. reply+=$#profile:$profile:$match[1]
  1046. profile=
  1047. fi
  1048. fi
  1049. done
  1050. }
  1051. ################################################################
  1052. # AWS Profile
  1053. prompt_aws() {
  1054. typeset -g P9K_AWS_PROFILE="${AWS_VAULT:-${AWSUME_PROFILE:-${AWS_PROFILE:-$AWS_DEFAULT_PROFILE}}}"
  1055. local pat class state
  1056. for pat class in "${_POWERLEVEL9K_AWS_CLASSES[@]}"; do
  1057. if [[ $P9K_AWS_PROFILE == ${~pat} ]]; then
  1058. [[ -n $class ]] && state=_${${(U)class}//İ/I}
  1059. break
  1060. fi
  1061. done
  1062. if [[ -n ${AWS_REGION:-$AWS_DEFAULT_REGION} ]]; then
  1063. typeset -g P9K_AWS_REGION=${AWS_REGION:-$AWS_DEFAULT_REGION}
  1064. else
  1065. local cfg=${AWS_CONFIG_FILE:-~/.aws/config}
  1066. if ! _p9k_cache_stat_get $0 $cfg; then
  1067. local -a reply
  1068. _p9k_parse_aws_config $cfg
  1069. _p9k_cache_stat_set $reply
  1070. fi
  1071. local prefix=$#P9K_AWS_PROFILE:$P9K_AWS_PROFILE:
  1072. local kv=$_p9k__cache_val[(r)${(b)prefix}*]
  1073. typeset -g P9K_AWS_REGION=${kv#$prefix}
  1074. fi
  1075. _p9k_prompt_segment "$0$state" red white 'AWS_ICON' 0 '' "${P9K_AWS_PROFILE//\%/%%}"
  1076. }
  1077. _p9k_prompt_aws_init() {
  1078. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${AWS_VAULT:-${AWSUME_PROFILE:-${AWS_PROFILE:-$AWS_DEFAULT_PROFILE}}}'
  1079. }
  1080. ################################################################
  1081. # Current Elastic Beanstalk environment
  1082. prompt_aws_eb_env() {
  1083. _p9k_upglob .elasticbeanstalk && return
  1084. local dir=$_p9k__parent_dirs[$?]
  1085. if ! _p9k_cache_stat_get $0 $dir/.elasticbeanstalk/config.yml; then
  1086. local env
  1087. env="$(command eb list 2>/dev/null)" || env=
  1088. env="${${(@M)${(@f)env}:#\* *}#\* }"
  1089. _p9k_cache_stat_set "$env"
  1090. fi
  1091. [[ -n $_p9k__cache_val[1] ]] || return
  1092. _p9k_prompt_segment "$0" black green 'AWS_EB_ICON' 0 '' "${_p9k__cache_val[1]//\%/%%}"
  1093. }
  1094. _p9k_prompt_aws_eb_env_init() {
  1095. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[eb]'
  1096. }
  1097. ################################################################
  1098. # Segment to indicate background jobs with an icon.
  1099. prompt_background_jobs() {
  1100. local -i len=$#_p9k__prompt _p9k__has_upglob
  1101. local msg
  1102. if (( _POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE )); then
  1103. if (( _POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS )); then
  1104. msg='${(%):-%j}'
  1105. else
  1106. msg='${${(%):-%j}:#1}'
  1107. fi
  1108. fi
  1109. _p9k_prompt_segment $0 "$_p9k_color1" cyan BACKGROUND_JOBS_ICON 1 '${${(%):-%j}:#0}' "$msg"
  1110. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  1111. }
  1112. ################################################################
  1113. # Segment that indicates usage level of current partition.
  1114. prompt_disk_usage() {
  1115. local -i len=$#_p9k__prompt _p9k__has_upglob
  1116. _p9k_prompt_segment $0_CRITICAL red white DISK_ICON 1 '$_p9k__disk_usage_critical' '$_p9k__disk_usage_pct%%'
  1117. _p9k_prompt_segment $0_WARNING yellow $_p9k_color1 DISK_ICON 1 '$_p9k__disk_usage_warning' '$_p9k__disk_usage_pct%%'
  1118. if (( ! _POWERLEVEL9K_DISK_USAGE_ONLY_WARNING )); then
  1119. _p9k_prompt_segment $0_NORMAL $_p9k_color1 yellow DISK_ICON 1 '$_p9k__disk_usage_normal' '$_p9k__disk_usage_pct%%'
  1120. fi
  1121. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  1122. }
  1123. _p9k_prompt_disk_usage_init() {
  1124. typeset -g _p9k__disk_usage_pct=
  1125. typeset -g _p9k__disk_usage_normal=
  1126. typeset -g _p9k__disk_usage_warning=
  1127. typeset -g _p9k__disk_usage_critical=
  1128. _p9k__async_segments_compute+='_p9k_worker_invoke disk_usage "_p9k_prompt_disk_usage_compute ${(q)_p9k__cwd_a}"'
  1129. }
  1130. _p9k_prompt_disk_usage_compute() {
  1131. (( $+commands[df] )) || return
  1132. _p9k_worker_async "_p9k_prompt_disk_usage_async ${(q)1}" _p9k_prompt_disk_usage_sync
  1133. }
  1134. _p9k_prompt_disk_usage_async() {
  1135. local pct=${${=${(f)"$(df -P $1 2>/dev/null)"}[2]}[5]%%%}
  1136. [[ $pct == <0-100> && $pct != $_p9k__disk_usage_pct ]] || return
  1137. _p9k__disk_usage_pct=$pct
  1138. _p9k__disk_usage_normal=
  1139. _p9k__disk_usage_warning=
  1140. _p9k__disk_usage_critical=
  1141. if (( _p9k__disk_usage_pct >= _POWERLEVEL9K_DISK_USAGE_CRITICAL_LEVEL )); then
  1142. _p9k__disk_usage_critical=1
  1143. elif (( _p9k__disk_usage_pct >= _POWERLEVEL9K_DISK_USAGE_WARNING_LEVEL )); then
  1144. _p9k__disk_usage_warning=1
  1145. elif (( ! _POWERLEVEL9K_DISK_USAGE_ONLY_WARNING )); then
  1146. _p9k__disk_usage_normal=1
  1147. fi
  1148. _p9k_print_params \
  1149. _p9k__disk_usage_pct \
  1150. _p9k__disk_usage_normal \
  1151. _p9k__disk_usage_warning \
  1152. _p9k__disk_usage_critical
  1153. echo -E - 'reset=1'
  1154. }
  1155. _p9k_prompt_disk_usage_sync() {
  1156. eval $REPLY
  1157. _p9k_worker_reply $REPLY
  1158. }
  1159. function _p9k_read_file() {
  1160. _p9k__ret=''
  1161. [[ -n $1 ]] && IFS='' read -r _p9k__ret <$1
  1162. [[ -n $_p9k__ret ]]
  1163. }
  1164. function _p9k_fvm_old() {
  1165. _p9k_upglob fvm && return 1
  1166. local fvm=$_p9k__parent_dirs[$?]/fvm
  1167. if [[ -L $fvm ]]; then
  1168. if [[ ${fvm:A} == (#b)*/versions/([^/]##)/bin/flutter ]]; then
  1169. _p9k_prompt_segment prompt_fvm blue $_p9k_color1 FLUTTER_ICON 0 '' ${match[1]//\%/%%}
  1170. return 0
  1171. fi
  1172. fi
  1173. return 1
  1174. }
  1175. function _p9k_fvm_new() {
  1176. _p9k_upglob .fvm && return 1
  1177. local sdk=$_p9k__parent_dirs[$?]/.fvm/flutter_sdk
  1178. if [[ -L $sdk ]]; then
  1179. if [[ ${sdk:A} == (#b)*/versions/([^/]##) ]]; then
  1180. _p9k_prompt_segment prompt_fvm blue $_p9k_color1 FLUTTER_ICON 0 '' ${match[1]//\%/%%}
  1181. return 0
  1182. fi
  1183. fi
  1184. return 1
  1185. }
  1186. prompt_fvm() {
  1187. _p9k_fvm_new || _p9k_fvm_old
  1188. }
  1189. _p9k_prompt_fvm_init() {
  1190. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[fvm]'
  1191. }
  1192. ################################################################
  1193. # Segment that displays the battery status in levels and colors
  1194. prompt_battery() {
  1195. [[ $_p9k_os == (Linux|Android) ]] && _p9k_prompt_battery_set_args
  1196. (( $#_p9k__battery_args )) && _p9k_prompt_segment "${_p9k__battery_args[@]}"
  1197. }
  1198. _p9k_prompt_battery_init() {
  1199. typeset -ga _p9k__battery_args=()
  1200. if [[ $_p9k_os == OSX && $+commands[pmset] == 1 ]]; then
  1201. _p9k__async_segments_compute+='_p9k_worker_invoke battery _p9k_prompt_battery_compute'
  1202. return
  1203. fi
  1204. if [[ $_p9k_os != (Linux|Android) ||
  1205. -z /sys/class/power_supply/(CMB*|BAT*|battery)/(energy_full|charge_full|charge_counter)(#qN) ]]; then
  1206. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
  1207. fi
  1208. }
  1209. _p9k_prompt_battery_compute() {
  1210. _p9k_worker_async _p9k_prompt_battery_async _p9k_prompt_battery_sync
  1211. }
  1212. _p9k_prompt_battery_async() {
  1213. local prev="${(pj:\0:)_p9k__battery_args}"
  1214. _p9k_prompt_battery_set_args
  1215. [[ "${(pj:\0:)_p9k__battery_args}" == $prev ]] && return 1
  1216. _p9k_print_params _p9k__battery_args
  1217. echo -E - 'reset=2'
  1218. }
  1219. _p9k_prompt_battery_sync() {
  1220. eval $REPLY
  1221. _p9k_worker_reply $REPLY
  1222. }
  1223. _p9k_prompt_battery_set_args() {
  1224. _p9k__battery_args=()
  1225. local state remain
  1226. local -i bat_percent
  1227. case $_p9k_os in
  1228. OSX)
  1229. (( $+commands[pmset] )) || return
  1230. local raw_data=${${(Af)"$(pmset -g batt 2>/dev/null)"}[2]}
  1231. [[ $raw_data == *InternalBattery* ]] || return
  1232. remain=${${(s: :)${${(s:; :)raw_data}[3]}}[1]}
  1233. [[ $remain == *no* ]] && remain="..."
  1234. [[ $raw_data =~ '([0-9]+)%' ]] && bat_percent=$match[1]
  1235. case "${${(s:; :)raw_data}[2]}" in
  1236. 'charging'|'finishing charge'|'AC attached')
  1237. if (( bat_percent == 100 )); then
  1238. state=CHARGED
  1239. remain=''
  1240. else
  1241. state=CHARGING
  1242. fi
  1243. ;;
  1244. 'discharging')
  1245. (( bat_percent < _POWERLEVEL9K_BATTERY_LOW_THRESHOLD )) && state=LOW || state=DISCONNECTED
  1246. ;;
  1247. *)
  1248. state=CHARGED
  1249. remain=''
  1250. ;;
  1251. esac
  1252. ;;
  1253. Linux|Android)
  1254. # See https://sourceforge.net/projects/acpiclient.
  1255. local -a bats=( /sys/class/power_supply/(CMB*|BAT*|battery)/(FN) )
  1256. (( $#bats )) || return
  1257. local -i energy_now energy_full power_now
  1258. local -i is_full=1 is_calculating is_charching
  1259. local dir
  1260. for dir in $bats; do
  1261. local -i pow=0 full=0
  1262. if _p9k_read_file $dir/(energy_full|charge_full|charge_counter)(N); then
  1263. (( energy_full += ${full::=_p9k__ret} ))
  1264. fi
  1265. if _p9k_read_file $dir/(power|current)_now(N) && (( $#_p9k__ret < 9 )); then
  1266. (( power_now += ${pow::=$_p9k__ret} ))
  1267. fi
  1268. if _p9k_read_file $dir/capacity(N); then
  1269. (( energy_now += _p9k__ret * full / 100. + 0.5 ))
  1270. elif _p9k_read_file $dir/(energy|charge)_now(N); then
  1271. (( energy_now += _p9k__ret ))
  1272. fi
  1273. _p9k_read_file $dir/status(N) && local bat_status=$_p9k__ret || continue
  1274. [[ $bat_status != Full ]] && is_full=0
  1275. [[ $bat_status == Charging ]] && is_charching=1
  1276. [[ $bat_status == (Charging|Discharging) && $pow == 0 ]] && is_calculating=1
  1277. done
  1278. (( energy_full )) || return
  1279. bat_percent=$(( 100. * energy_now / energy_full + 0.5 ))
  1280. (( bat_percent > 100 )) && bat_percent=100
  1281. if (( is_full || (bat_percent == 100 && is_charching) )); then
  1282. state=CHARGED
  1283. else
  1284. if (( is_charching )); then
  1285. state=CHARGING
  1286. elif (( bat_percent < _POWERLEVEL9K_BATTERY_LOW_THRESHOLD )); then
  1287. state=LOW
  1288. else
  1289. state=DISCONNECTED
  1290. fi
  1291. if (( power_now > 0 )); then
  1292. (( is_charching )) && local -i e=$((energy_full - energy_now)) || local -i e=energy_now
  1293. local -i minutes=$(( 60 * e / power_now ))
  1294. (( minutes > 0 )) && remain=$((minutes/60)):${(l#2##0#)$((minutes%60))}
  1295. elif (( is_calculating )); then
  1296. remain="..."
  1297. fi
  1298. fi
  1299. ;;
  1300. *)
  1301. return 0
  1302. ;;
  1303. esac
  1304. (( bat_percent >= _POWERLEVEL9K_BATTERY_${state}_HIDE_ABOVE_THRESHOLD )) && return
  1305. local msg="$bat_percent%%"
  1306. [[ $_POWERLEVEL9K_BATTERY_VERBOSE == 1 && -n $remain ]] && msg+=" ($remain)"
  1307. local icon=BATTERY_ICON
  1308. local var=_POWERLEVEL9K_BATTERY_${state}_STAGES
  1309. local -i idx="${#${(@P)var}}"
  1310. if (( idx )); then
  1311. (( bat_percent < 100 )) && idx=$((bat_percent * idx / 100 + 1))
  1312. icon=$'\1'"${${(@P)var}[idx]}"
  1313. fi
  1314. local bg=$_p9k_color1
  1315. local var=_POWERLEVEL9K_BATTERY_${state}_LEVEL_BACKGROUND
  1316. local -i idx="${#${(@P)var}}"
  1317. if (( idx )); then
  1318. (( bat_percent < 100 )) && idx=$((bat_percent * idx / 100 + 1))
  1319. bg="${${(@P)var}[idx]}"
  1320. fi
  1321. local fg=$_p9k_battery_states[$state]
  1322. local var=_POWERLEVEL9K_BATTERY_${state}_LEVEL_FOREGROUND
  1323. local -i idx="${#${(@P)var}}"
  1324. if (( idx )); then
  1325. (( bat_percent < 100 )) && idx=$((bat_percent * idx / 100 + 1))
  1326. fg="${${(@P)var}[idx]}"
  1327. fi
  1328. _p9k__battery_args=(prompt_battery_$state "$bg" "$fg" $icon 0 '' $msg)
  1329. }
  1330. ################################################################
  1331. # Public IP segment
  1332. prompt_public_ip() {
  1333. local -i len=$#_p9k__prompt _p9k__has_upglob
  1334. local ip='${_p9k__public_ip:-$_POWERLEVEL9K_PUBLIC_IP_NONE}'
  1335. if [[ -n $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then
  1336. _p9k_prompt_segment "$0" "$_p9k_color1" "$_p9k_color2" PUBLIC_IP_ICON 1 '${_p9k__public_ip_not_vpn:+'$ip'}' $ip
  1337. _p9k_prompt_segment "$0" "$_p9k_color1" "$_p9k_color2" VPN_ICON 1 '${_p9k__public_ip_vpn:+'$ip'}' $ip
  1338. else
  1339. _p9k_prompt_segment "$0" "$_p9k_color1" "$_p9k_color2" PUBLIC_IP_ICON 1 $ip $ip
  1340. fi
  1341. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  1342. }
  1343. _p9k_prompt_public_ip_init() {
  1344. typeset -g _p9k__public_ip=
  1345. typeset -gF _p9k__public_ip_next_time=0
  1346. _p9k__async_segments_compute+='_p9k_worker_invoke public_ip _p9k_prompt_public_ip_compute'
  1347. }
  1348. _p9k_prompt_public_ip_compute() {
  1349. (( EPOCHREALTIME >= _p9k__public_ip_next_time )) || return
  1350. _p9k_worker_async _p9k_prompt_public_ip_async _p9k_prompt_public_ip_sync
  1351. }
  1352. _p9k_prompt_public_ip_async() {
  1353. local ip method
  1354. local -F start=EPOCHREALTIME
  1355. local -F next='start + 5'
  1356. for method in $_POWERLEVEL9K_PUBLIC_IP_METHODS $_POWERLEVEL9K_PUBLIC_IP_METHODS; do
  1357. case $method in
  1358. dig)
  1359. if (( $+commands[dig] )); then
  1360. ip="$(dig +tries=1 +short -4 A myip.opendns.com @resolver1.opendns.com 2>/dev/null)"
  1361. [[ $ip == ';'* ]] && ip=
  1362. if [[ -z $ip ]]; then
  1363. ip="$(dig +tries=1 +short -6 AAAA myip.opendns.com @resolver1.opendns.com 2>/dev/null)"
  1364. [[ $ip == ';'* ]] && ip=
  1365. fi
  1366. fi
  1367. ;;
  1368. curl)
  1369. if (( $+commands[curl] )); then
  1370. ip="$(curl --max-time 5 -w '\n' "$_POWERLEVEL9K_PUBLIC_IP_HOST" 2>/dev/null)"
  1371. fi
  1372. ;;
  1373. wget)
  1374. if (( $+commands[wget] )); then
  1375. ip="$(wget -T 5 -qO- "$_POWERLEVEL9K_PUBLIC_IP_HOST" 2>/dev/null)"
  1376. fi
  1377. ;;
  1378. esac
  1379. [[ $ip =~ '^[0-9a-f.:]+$' ]] || ip=''
  1380. if [[ -n $ip ]]; then
  1381. next=$((start + _POWERLEVEL9K_PUBLIC_IP_TIMEOUT))
  1382. break
  1383. fi
  1384. done
  1385. _p9k__public_ip_next_time=$next
  1386. _p9k_print_params _p9k__public_ip_next_time
  1387. [[ $_p9k__public_ip == $ip ]] && return
  1388. _p9k__public_ip=$ip
  1389. _p9k_print_params _p9k__public_ip
  1390. echo -E - 'reset=1'
  1391. }
  1392. _p9k_prompt_public_ip_sync() {
  1393. eval $REPLY
  1394. _p9k_worker_reply $REPLY
  1395. }
  1396. ################################################################
  1397. # Context: user@hostname (who am I and where am I)
  1398. prompt_context() {
  1399. local -i len=$#_p9k__prompt _p9k__has_upglob
  1400. local content
  1401. if [[ $_POWERLEVEL9K_ALWAYS_SHOW_CONTEXT == 0 && -n $DEFAULT_USER && $P9K_SSH == 0 ]]; then
  1402. local user="${(%):-%n}"
  1403. if [[ $user == $DEFAULT_USER ]]; then
  1404. content="${user//\%/%%}"
  1405. fi
  1406. fi
  1407. local state
  1408. if (( P9K_SSH )); then
  1409. if [[ -n "$SUDO_COMMAND" ]]; then
  1410. state="REMOTE_SUDO"
  1411. else
  1412. state="REMOTE"
  1413. fi
  1414. elif [[ -n "$SUDO_COMMAND" ]]; then
  1415. state="SUDO"
  1416. else
  1417. state="DEFAULT"
  1418. fi
  1419. local cond
  1420. for state cond in $state '${${(%):-%#}:#\#}' ROOT '${${(%):-%#}:#\%}'; do
  1421. local text=$content
  1422. if [[ -z $text ]]; then
  1423. local var=_POWERLEVEL9K_CONTEXT_${state}_TEMPLATE
  1424. if (( $+parameters[$var] )); then
  1425. text=${(P)var}
  1426. text=${(g::)text}
  1427. else
  1428. text=$_POWERLEVEL9K_CONTEXT_TEMPLATE
  1429. fi
  1430. fi
  1431. _p9k_prompt_segment "$0_$state" "$_p9k_color1" yellow '' 0 "$cond" "$text"
  1432. done
  1433. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  1434. }
  1435. instant_prompt_context() {
  1436. if [[ $_POWERLEVEL9K_ALWAYS_SHOW_CONTEXT == 0 && -n $DEFAULT_USER && $P9K_SSH == 0 ]]; then
  1437. if [[ ${(%):-%n} == $DEFAULT_USER ]]; then
  1438. if (( ! _POWERLEVEL9K_ALWAYS_SHOW_USER )); then
  1439. return
  1440. fi
  1441. fi
  1442. fi
  1443. prompt_context
  1444. }
  1445. _p9k_prompt_context_init() {
  1446. if [[ $_POWERLEVEL9K_ALWAYS_SHOW_CONTEXT == 0 && -n $DEFAULT_USER && $P9K_SSH == 0 ]]; then
  1447. if [[ ${(%):-%n} == $DEFAULT_USER ]]; then
  1448. if (( ! _POWERLEVEL9K_ALWAYS_SHOW_USER )); then
  1449. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
  1450. fi
  1451. fi
  1452. fi
  1453. }
  1454. ################################################################
  1455. # User: user (who am I)
  1456. prompt_user() {
  1457. local -i len=$#_p9k__prompt _p9k__has_upglob
  1458. _p9k_prompt_segment "${0}_ROOT" "${_p9k_color1}" yellow ROOT_ICON 0 '${${(%):-%#}:#\%}' "$_POWERLEVEL9K_USER_TEMPLATE"
  1459. if [[ -n "$SUDO_COMMAND" ]]; then
  1460. _p9k_prompt_segment "${0}_SUDO" "${_p9k_color1}" yellow SUDO_ICON 0 '${${(%):-%#}:#\#}' "$_POWERLEVEL9K_USER_TEMPLATE"
  1461. else
  1462. _p9k_prompt_segment "${0}_DEFAULT" "${_p9k_color1}" yellow USER_ICON 0 '${${(%):-%#}:#\#}' "%n"
  1463. fi
  1464. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  1465. }
  1466. instant_prompt_user() {
  1467. if [[ $_POWERLEVEL9K_ALWAYS_SHOW_USER == 0 && "${(%):-%n}" == $DEFAULT_USER ]]; then
  1468. return
  1469. fi
  1470. prompt_user
  1471. }
  1472. _p9k_prompt_user_init() {
  1473. if [[ $_POWERLEVEL9K_ALWAYS_SHOW_USER == 0 && "${(%):-%n}" == $DEFAULT_USER ]]; then
  1474. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
  1475. fi
  1476. }
  1477. ################################################################
  1478. # Host: machine (where am I)
  1479. prompt_host() {
  1480. local -i len=$#_p9k__prompt _p9k__has_upglob
  1481. if (( P9K_SSH )); then
  1482. _p9k_prompt_segment "$0_REMOTE" "${_p9k_color1}" yellow SSH_ICON 0 '' "$_POWERLEVEL9K_HOST_TEMPLATE"
  1483. else
  1484. _p9k_prompt_segment "$0_LOCAL" "${_p9k_color1}" yellow HOST_ICON 0 '' "$_POWERLEVEL9K_HOST_TEMPLATE"
  1485. fi
  1486. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  1487. }
  1488. instant_prompt_host() { prompt_host; }
  1489. ################################################################
  1490. # Toolbox: https://github.com/containers/toolbox
  1491. function prompt_toolbox() {
  1492. _p9k_prompt_segment $0 $_p9k_color1 yellow TOOLBOX_ICON 0 '' $P9K_TOOLBOX_NAME
  1493. }
  1494. _p9k_prompt_toolbox_init() {
  1495. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$P9K_TOOLBOX_NAME'
  1496. }
  1497. function instant_prompt_toolbox() {
  1498. _p9k_prompt_segment prompt_toolbox $_p9k_color1 yellow TOOLBOX_ICON 1 '$P9K_TOOLBOX_NAME' '$P9K_TOOLBOX_NAME'
  1499. }
  1500. ################################################################
  1501. # The 'custom` prompt provides a way for users to invoke commands and display
  1502. # the output in a segment.
  1503. _p9k_custom_prompt() {
  1504. local segment_name=${1:u}
  1505. local command=_POWERLEVEL9K_CUSTOM_${segment_name}
  1506. command=${(P)command}
  1507. local parts=("${(@z)command}")
  1508. local cmd="${(Q)parts[1]}"
  1509. (( $+functions[$cmd] || $+commands[$cmd] )) || return
  1510. local content="$(eval $command)"
  1511. [[ -n $content ]] || return
  1512. _p9k_prompt_segment "prompt_custom_$1" $_p9k_color2 $_p9k_color1 "CUSTOM_${segment_name}_ICON" 0 '' "$content"
  1513. }
  1514. ################################################################
  1515. # Display the duration the command needed to run.
  1516. prompt_command_execution_time() {
  1517. (( $+P9K_COMMAND_DURATION_SECONDS )) || return
  1518. (( P9K_COMMAND_DURATION_SECONDS >= _POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD )) || return
  1519. if (( P9K_COMMAND_DURATION_SECONDS < 60 )); then
  1520. if (( !_POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION )); then
  1521. local -i sec=$((P9K_COMMAND_DURATION_SECONDS + 0.5))
  1522. else
  1523. local -F $_POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION sec=P9K_COMMAND_DURATION_SECONDS
  1524. fi
  1525. local text=${sec}s
  1526. else
  1527. local -i d=$((P9K_COMMAND_DURATION_SECONDS + 0.5))
  1528. if [[ $_POWERLEVEL9K_COMMAND_EXECUTION_TIME_FORMAT == "H:M:S" ]]; then
  1529. local text=${(l.2..0.)$((d % 60))}
  1530. if (( d >= 60 )); then
  1531. text=${(l.2..0.)$((d / 60 % 60))}:$text
  1532. if (( d >= 36000 )); then
  1533. text=$((d / 3600)):$text
  1534. elif (( d >= 3600 )); then
  1535. text=0$((d / 3600)):$text
  1536. fi
  1537. fi
  1538. else
  1539. local text="$((d % 60))s"
  1540. if (( d >= 60 )); then
  1541. text="$((d / 60 % 60))m $text"
  1542. if (( d >= 3600 )); then
  1543. text="$((d / 3600 % 24))h $text"
  1544. if (( d >= 86400 )); then
  1545. text="$((d / 86400))d $text"
  1546. fi
  1547. fi
  1548. fi
  1549. fi
  1550. fi
  1551. _p9k_prompt_segment "$0" "red" "yellow1" 'EXECUTION_TIME_ICON' 0 '' $text
  1552. }
  1553. function _p9k_shorten_delim_len() {
  1554. local def=$1
  1555. _p9k__ret=${_POWERLEVEL9K_SHORTEN_DELIMITER_LENGTH:--1}
  1556. (( _p9k__ret >= 0 )) || _p9k_prompt_length $1
  1557. }
  1558. # Percents are duplicated because this function is currently used only
  1559. # where the result is going to be percent-expanded.
  1560. function _p9k_url_escape() {
  1561. if [[ $1 == [a-zA-Z0-9"/:_.-!'()~ "]# ]]; then
  1562. _p9k__ret=${1// /%%20}
  1563. else
  1564. local c
  1565. _p9k__ret=
  1566. for c in ${(s::)1}; do
  1567. [[ $c == [a-zA-Z0-9"/:_.-!'()~"] ]] || printf -v c '%%%%%02X' $(( #c ))
  1568. _p9k__ret+=$c
  1569. done
  1570. fi
  1571. }
  1572. ################################################################
  1573. # Dir: current working directory
  1574. prompt_dir() {
  1575. if (( _POWERLEVEL9K_DIR_PATH_ABSOLUTE )); then
  1576. local p=${(V)_p9k__cwd}
  1577. local -a parts=("${(s:/:)p}")
  1578. elif [[ -o auto_name_dirs ]]; then
  1579. local p=${(V)${_p9k__cwd/#(#b)$HOME(|\/*)/'~'$match[1]}}
  1580. local -a parts=("${(s:/:)p}")
  1581. else
  1582. local p=${(%):-%~}
  1583. if [[ $p == '~['* ]]; then
  1584. # If "${(%):-%~}" expands to "~[a]/]/b", is the first component "~[a]" or "~[a]/]"?
  1585. # One would expect "${(%):-%-1~}" to give the right answer but alas it always simply
  1586. # gives the segment before the first slash, which would be "~[a]" in this case. Worse,
  1587. # for "~[a/b]" it'll give the nonsensical "~[a". To solve this problem we have to
  1588. # repeat what "${(%):-%~}" does and hope that it produces the same result.
  1589. local func=''
  1590. local -a parts=()
  1591. for func in zsh_directory_name $zsh_directory_name_functions; do
  1592. local reply=()
  1593. if (( $+functions[$func] )) && $func d $_p9k__cwd && [[ $p == '~['${(V)reply[1]}']'* ]]; then
  1594. parts+='~['${(V)reply[1]}']'
  1595. break
  1596. fi
  1597. done
  1598. if (( $#parts )); then
  1599. parts+=(${(s:/:)${p#$parts[1]}})
  1600. else
  1601. p=${(V)_p9k__cwd}
  1602. parts=("${(s:/:)p}")
  1603. fi
  1604. else
  1605. local -a parts=("${(s:/:)p}")
  1606. fi
  1607. fi
  1608. local -i fake_first=0 expand=0 shortenlen=${_POWERLEVEL9K_SHORTEN_DIR_LENGTH:--1}
  1609. if (( $+_POWERLEVEL9K_SHORTEN_DELIMITER )); then
  1610. local delim=$_POWERLEVEL9K_SHORTEN_DELIMITER
  1611. else
  1612. if [[ $langinfo[CODESET] == (utf|UTF)(-|)8 ]]; then
  1613. local delim=$'\u2026'
  1614. else
  1615. local delim='..'
  1616. fi
  1617. fi
  1618. case $_POWERLEVEL9K_SHORTEN_STRATEGY in
  1619. truncate_absolute|truncate_absolute_chars)
  1620. if (( shortenlen > 0 && $#p > shortenlen )); then
  1621. _p9k_shorten_delim_len $delim
  1622. if (( $#p > shortenlen + $_p9k__ret )); then
  1623. local -i n=shortenlen
  1624. local -i i=$#parts
  1625. while true; do
  1626. local dir=$parts[i]
  1627. local -i len=$(( $#dir + (i > 1) ))
  1628. if (( len <= n )); then
  1629. (( n -= len ))
  1630. (( --i ))
  1631. else
  1632. parts[i]=$'\1'$dir[-n,-1]
  1633. parts[1,i-1]=()
  1634. break
  1635. fi
  1636. done
  1637. fi
  1638. fi
  1639. ;;
  1640. truncate_with_package_name|truncate_middle|truncate_from_right)
  1641. () {
  1642. [[ $_POWERLEVEL9K_SHORTEN_STRATEGY == truncate_with_package_name &&
  1643. $+commands[jq] == 1 && $#_POWERLEVEL9K_DIR_PACKAGE_FILES > 0 ]] || return
  1644. local pats="(${(j:|:)_POWERLEVEL9K_DIR_PACKAGE_FILES})"
  1645. local -i i=$#parts
  1646. local dir=$_p9k__cwd
  1647. for (( ; i > 0; --i )); do
  1648. local markers=($dir/${~pats}(N))
  1649. if (( $#markers )); then
  1650. local pat= pkg_file=
  1651. for pat in $_POWERLEVEL9K_DIR_PACKAGE_FILES; do
  1652. for pkg_file in $markers; do
  1653. [[ $pkg_file == $dir/${~pat} ]] || continue
  1654. if ! _p9k_cache_stat_get $0_pkg $pkg_file; then
  1655. local pkg_name=''
  1656. pkg_name="$(jq -j '.name | select(. != null)' <$pkg_file 2>/dev/null)" || pkg_name=''
  1657. _p9k_cache_stat_set "$pkg_name"
  1658. fi
  1659. [[ -n $_p9k__cache_val[1] ]] || continue
  1660. parts[1,i]=($_p9k__cache_val[1])
  1661. fake_first=1
  1662. return 0
  1663. done
  1664. done
  1665. fi
  1666. dir=${dir:h}
  1667. done
  1668. }
  1669. if (( shortenlen > 0 )); then
  1670. _p9k_shorten_delim_len $delim
  1671. local -i d=_p9k__ret pref=shortenlen suf=0 i=2
  1672. [[ $_POWERLEVEL9K_SHORTEN_STRATEGY == truncate_middle ]] && suf=pref
  1673. for (( ; i < $#parts; ++i )); do
  1674. local dir=$parts[i]
  1675. if (( $#dir > pref + suf + d )); then
  1676. dir[pref+1,-suf-1]=$'\1'
  1677. parts[i]=$dir
  1678. fi
  1679. done
  1680. fi
  1681. ;;
  1682. truncate_to_last)
  1683. shortenlen=${_POWERLEVEL9K_SHORTEN_DIR_LENGTH:-1}
  1684. (( shortenlen > 0 )) || shortenlen=1
  1685. local -i i='shortenlen+1'
  1686. if [[ $#parts -gt i || $p[1] != / && $#parts -gt shortenlen ]]; then
  1687. fake_first=1
  1688. parts[1,-i]=()
  1689. fi
  1690. ;;
  1691. truncate_to_first_and_last)
  1692. if (( shortenlen > 0 )); then
  1693. local -i i=$(( shortenlen + 1 ))
  1694. [[ $p == /* ]] && (( ++i ))
  1695. for (( ; i <= $#parts - shortenlen; ++i )); do
  1696. parts[i]=$'\1'
  1697. done
  1698. fi
  1699. ;;
  1700. truncate_to_unique)
  1701. expand=1
  1702. delim=${_POWERLEVEL9K_SHORTEN_DELIMITER-'*'}
  1703. shortenlen=${_POWERLEVEL9K_SHORTEN_DIR_LENGTH:-1}
  1704. (( shortenlen >= 0 )) || shortenlen=1
  1705. local rp=${(g:oce:)p}
  1706. local rparts=("${(@s:/:)rp}")
  1707. local -i i=2 e=$(($#parts - shortenlen))
  1708. if [[ -n $_POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER ]]; then
  1709. (( e += shortenlen ))
  1710. local orig=("$parts[2]" "${(@)parts[$((shortenlen > $#parts ? -$#parts : -shortenlen)),-1]}")
  1711. elif [[ $p[1] == / ]]; then
  1712. (( ++i ))
  1713. fi
  1714. if (( i <= e )); then
  1715. local mtimes=(${(Oa)_p9k__parent_mtimes:$(($#parts-e)):$((e-i+1))})
  1716. local key="${(pj.:.)mtimes}"
  1717. else
  1718. local key=
  1719. fi
  1720. if ! _p9k_cache_ephemeral_get $0 $e $i $_p9k__cwd || [[ $key != $_p9k__cache_val[1] ]]; then
  1721. local rtail=${(j./.)rparts[i,-1]}
  1722. local parent=$_p9k__cwd[1,-2-$#rtail]
  1723. _p9k_prompt_length $delim
  1724. local -i real_delim_len=_p9k__ret
  1725. [[ -n $parts[i-1] ]] && parts[i-1]="\${(Q)\${:-${(qqq)${(q)parts[i-1]}}}}"$'\2'
  1726. local -i d=${_POWERLEVEL9K_SHORTEN_DELIMITER_LENGTH:--1}
  1727. (( d >= 0 )) || d=real_delim_len
  1728. local -i m=1
  1729. for (( ; i <= e; ++i, ++m )); do
  1730. local sub=$parts[i]
  1731. local rsub=$rparts[i]
  1732. local dir=$parent/$rsub mtime=$mtimes[m]
  1733. local pair=$_p9k__dir_stat_cache[$dir]
  1734. if [[ $pair == ${mtime:-x}:* ]]; then
  1735. parts[i]=${pair#*:}
  1736. else
  1737. [[ $sub != *["~!#\`\$^&*()\\\"'<>?{}[]"]* ]]
  1738. local -i q=$?
  1739. if [[ -n $_POWERLEVEL9K_SHORTEN_FOLDER_MARKER &&
  1740. -n $dir/${~_POWERLEVEL9K_SHORTEN_FOLDER_MARKER}(#qN) ]]; then
  1741. (( q )) && parts[i]="\${(Q)\${:-${(qqq)${(q)sub}}}}"
  1742. parts[i]+=$'\2'
  1743. else
  1744. local -i j=$rsub[(i)[^.]]
  1745. for (( ; j + d < $#rsub; ++j )); do
  1746. local -a matching=($parent/$rsub[1,j]*/(N))
  1747. (( $#matching == 1 )) && break
  1748. done
  1749. local -i saved=$((${(m)#${(V)${rsub:$j}}} - d))
  1750. if (( saved > 0 )); then
  1751. if (( q )); then
  1752. parts[i]='${${${_p9k__d:#-*}:+${(Q)${:-'${(qqq)${(q)sub}}'}}}:-${(Q)${:-'
  1753. parts[i]+=$'\3'${(qqq)${(q)${(V)${rsub[1,j]}}}}$'}}\1\3''${$((_p9k__d+='$saved'))+}}'
  1754. else
  1755. parts[i]='${${${_p9k__d:#-*}:+'$sub$'}:-\3'${(V)${rsub[1,j]}}$'\1\3''${$((_p9k__d+='$saved'))+}}'
  1756. fi
  1757. else
  1758. (( q )) && parts[i]="\${(Q)\${:-${(qqq)${(q)sub}}}}"
  1759. fi
  1760. fi
  1761. [[ -n $mtime ]] && _p9k__dir_stat_cache[$dir]="$mtime:$parts[i]"
  1762. fi
  1763. parent+=/$rsub
  1764. done
  1765. if [[ -n $_POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER ]]; then
  1766. local _2=$'\2'
  1767. if [[ $_POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER == last* ]]; then
  1768. (( e = ${parts[(I)*$_2]} + ${_POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER#*:} ))
  1769. else
  1770. (( e = ${parts[(ib:2:)*$_2]} + ${_POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER#*:} ))
  1771. fi
  1772. if (( e > 1 && e <= $#parts )); then
  1773. parts[1,e-1]=()
  1774. fake_first=1
  1775. elif [[ $p == /?* ]]; then
  1776. parts[2]="\${(Q)\${:-${(qqq)${(q)orig[1]}}}}"$'\2'
  1777. fi
  1778. for ((i = $#parts < shortenlen ? $#parts : shortenlen; i > 0; --i)); do
  1779. [[ $#parts[-i] == *$'\2' ]] && continue
  1780. if [[ $orig[-i] == *["~!#\`\$^&*()\\\"'<>?{}[]"]* ]]; then
  1781. parts[-i]='${(Q)${:-'${(qqq)${(q)orig[-i]}}'}}'$'\2'
  1782. else
  1783. parts[-i]=${orig[-i]}$'\2'
  1784. fi
  1785. done
  1786. else
  1787. for ((; i <= $#parts; ++i)); do
  1788. [[ $parts[i] == *["~!#\`\$^&*()\\\"'<>?{}[]"]* ]] && parts[i]='${(Q)${:-'${(qqq)${(q)parts[i]}}'}}'
  1789. parts[i]+=$'\2'
  1790. done
  1791. fi
  1792. _p9k_cache_ephemeral_set "$key" "${parts[@]}"
  1793. fi
  1794. parts=("${(@)_p9k__cache_val[2,-1]}")
  1795. ;;
  1796. truncate_with_folder_marker)
  1797. if [[ -n $_POWERLEVEL9K_SHORTEN_FOLDER_MARKER ]]; then
  1798. local dir=$_p9k__cwd
  1799. local -a m=()
  1800. local -i i=$(($#parts - 1))
  1801. for (( ; i > 1; --i )); do
  1802. dir=${dir:h}
  1803. [[ -n $dir/${~_POWERLEVEL9K_SHORTEN_FOLDER_MARKER}(#qN) ]] && m+=$i
  1804. done
  1805. m+=1
  1806. for (( i=1; i < $#m; ++i )); do
  1807. (( m[i] - m[i+1] > 2 )) && parts[m[i+1]+1,m[i]-1]=($'\1')
  1808. done
  1809. fi
  1810. ;;
  1811. *)
  1812. if (( shortenlen > 0 )); then
  1813. local -i len=$#parts
  1814. [[ -z $parts[1] ]] && (( --len ))
  1815. if (( len > shortenlen )); then
  1816. parts[1,-shortenlen-1]=($'\1')
  1817. fi
  1818. fi
  1819. ;;
  1820. esac
  1821. # w=0: writable
  1822. # w=1: not writable
  1823. # w=2: does not exist
  1824. (( !_POWERLEVEL9K_DIR_SHOW_WRITABLE )) || [[ -w $_p9k__cwd ]]
  1825. local -i w=$?
  1826. (( w && _POWERLEVEL9K_DIR_SHOW_WRITABLE > 2 )) && [[ ! -e $_p9k__cwd ]] && w=2
  1827. if ! _p9k_cache_ephemeral_get $0 $_p9k__cwd $p $w $fake_first "${parts[@]}"; then
  1828. local state=$0
  1829. local icon=''
  1830. local a='' b='' c=''
  1831. for a b c in "${_POWERLEVEL9K_DIR_CLASSES[@]}"; do
  1832. if [[ $_p9k__cwd == ${~a} ]]; then
  1833. [[ -n $b ]] && state+=_${${(U)b}//İ/I}
  1834. icon=$'\1'$c
  1835. break
  1836. fi
  1837. done
  1838. if (( w )); then
  1839. if (( _POWERLEVEL9K_DIR_SHOW_WRITABLE == 1 )); then
  1840. state=${0}_NOT_WRITABLE
  1841. elif (( w == 2 )); then
  1842. state+=_NON_EXISTENT
  1843. else
  1844. state+=_NOT_WRITABLE
  1845. fi
  1846. icon=LOCK_ICON
  1847. fi
  1848. local state_u=${${(U)state}//İ/I}
  1849. local style=%b
  1850. _p9k_color $state BACKGROUND blue
  1851. _p9k_background $_p9k__ret
  1852. style+=$_p9k__ret
  1853. _p9k_color $state FOREGROUND "$_p9k_color1"
  1854. _p9k_foreground $_p9k__ret
  1855. style+=$_p9k__ret
  1856. if (( expand )); then
  1857. _p9k_escape_style $style
  1858. style=$_p9k__ret
  1859. fi
  1860. parts=("${(@)parts//\%/%%}")
  1861. if [[ $_POWERLEVEL9K_HOME_FOLDER_ABBREVIATION != '~' && $fake_first == 0 && $p == ('~'|'~/'*) ]]; then
  1862. (( expand )) && _p9k_escape $_POWERLEVEL9K_HOME_FOLDER_ABBREVIATION || _p9k__ret=$_POWERLEVEL9K_HOME_FOLDER_ABBREVIATION
  1863. parts[1]=$_p9k__ret
  1864. [[ $_p9k__ret == *%* ]] && parts[1]+=$style
  1865. elif [[ $_POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER == 1 && $fake_first == 0 && $#parts > 1 && -z $parts[1] && -n $parts[2] ]]; then
  1866. parts[1]=()
  1867. fi
  1868. local last_style=
  1869. _p9k_param $state PATH_HIGHLIGHT_BOLD ''
  1870. [[ $_p9k__ret == true ]] && last_style+=%B
  1871. if (( $+parameters[_POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND] ||
  1872. $+parameters[_POWERLEVEL9K_${state_u}_PATH_HIGHLIGHT_FOREGROUND] )); then
  1873. _p9k_color $state PATH_HIGHLIGHT_FOREGROUND ''
  1874. _p9k_foreground $_p9k__ret
  1875. last_style+=$_p9k__ret
  1876. fi
  1877. if [[ -n $last_style ]]; then
  1878. (( expand )) && _p9k_escape_style $last_style || _p9k__ret=$last_style
  1879. parts[-1]=$_p9k__ret${parts[-1]//$'\1'/$'\1'$_p9k__ret}$style
  1880. fi
  1881. local anchor_style=
  1882. _p9k_param $state ANCHOR_BOLD ''
  1883. [[ $_p9k__ret == true ]] && anchor_style+=%B
  1884. if (( $+parameters[_POWERLEVEL9K_DIR_ANCHOR_FOREGROUND] ||
  1885. $+parameters[_POWERLEVEL9K_${state_u}_ANCHOR_FOREGROUND] )); then
  1886. _p9k_color $state ANCHOR_FOREGROUND ''
  1887. _p9k_foreground $_p9k__ret
  1888. anchor_style+=$_p9k__ret
  1889. fi
  1890. if [[ -n $anchor_style ]]; then
  1891. (( expand )) && _p9k_escape_style $anchor_style || _p9k__ret=$anchor_style
  1892. if [[ -z $last_style ]]; then
  1893. parts=("${(@)parts/%(#b)(*)$'\2'/$_p9k__ret$match[1]$style}")
  1894. else
  1895. (( $#parts > 1 )) && parts[1,-2]=("${(@)parts[1,-2]/%(#b)(*)$'\2'/$_p9k__ret$match[1]$style}")
  1896. parts[-1]=${parts[-1]/$'\2'}
  1897. fi
  1898. else
  1899. parts=("${(@)parts/$'\2'}")
  1900. fi
  1901. if (( $+parameters[_POWERLEVEL9K_DIR_SHORTENED_FOREGROUND] ||
  1902. $+parameters[_POWERLEVEL9K_${state_u}_SHORTENED_FOREGROUND] )); then
  1903. _p9k_color $state SHORTENED_FOREGROUND ''
  1904. _p9k_foreground $_p9k__ret
  1905. (( expand )) && _p9k_escape_style $_p9k__ret
  1906. local shortened_fg=$_p9k__ret
  1907. (( expand )) && _p9k_escape $delim || _p9k__ret=$delim
  1908. [[ $_p9k__ret == *%* ]] && _p9k__ret+=$style$shortened_fg
  1909. parts=("${(@)parts/(#b)$'\3'(*)$'\1'(*)$'\3'/$shortened_fg$match[1]$_p9k__ret$match[2]$style}")
  1910. parts=("${(@)parts/(#b)(*)$'\1'(*)/$shortened_fg$match[1]$_p9k__ret$match[2]$style}")
  1911. else
  1912. (( expand )) && _p9k_escape $delim || _p9k__ret=$delim
  1913. [[ $_p9k__ret == *%* ]] && _p9k__ret+=$style
  1914. parts=("${(@)parts/$'\1'/$_p9k__ret}")
  1915. parts=("${(@)parts//$'\3'}")
  1916. fi
  1917. if [[ $_p9k__cwd == / && $_POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER == 1 ]]; then
  1918. local sep='/'
  1919. else
  1920. local sep=''
  1921. if (( $+parameters[_POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND] ||
  1922. $+parameters[_POWERLEVEL9K_${state_u}_PATH_SEPARATOR_FOREGROUND] )); then
  1923. _p9k_color $state PATH_SEPARATOR_FOREGROUND ''
  1924. _p9k_foreground $_p9k__ret
  1925. (( expand )) && _p9k_escape_style $_p9k__ret
  1926. sep=$_p9k__ret
  1927. fi
  1928. _p9k_param $state PATH_SEPARATOR /
  1929. _p9k__ret=${(g::)_p9k__ret}
  1930. (( expand )) && _p9k_escape $_p9k__ret
  1931. sep+=$_p9k__ret
  1932. [[ $sep == *%* ]] && sep+=$style
  1933. fi
  1934. local content="${(pj.$sep.)parts}"
  1935. if (( _POWERLEVEL9K_DIR_HYPERLINK && _p9k_term_has_href )) && [[ $_p9k__cwd == /* ]]; then
  1936. _p9k_url_escape $_p9k__cwd
  1937. local header=$'%{\e]8;;file://'$_p9k__ret$'\a%}'
  1938. local footer=$'%{\e]8;;\a%}'
  1939. if (( expand )); then
  1940. _p9k_escape $header
  1941. header=$_p9k__ret
  1942. _p9k_escape $footer
  1943. footer=$_p9k__ret
  1944. fi
  1945. content=$header$content$footer
  1946. fi
  1947. (( expand )) && _p9k_prompt_length "${(e):-"\${\${_p9k__d::=0}+}$content"}" || _p9k__ret=
  1948. _p9k_cache_ephemeral_set "$state" "$icon" "$expand" "$content" $_p9k__ret
  1949. fi
  1950. if (( _p9k__cache_val[3] )); then
  1951. if (( $+_p9k__dir )); then
  1952. _p9k__cache_val[4]='${${_p9k__d::=-1024}+}'$_p9k__cache_val[4]
  1953. else
  1954. _p9k__dir=$_p9k__cache_val[4]
  1955. _p9k__dir_len=$_p9k__cache_val[5]
  1956. _p9k__cache_val[4]='%{d%}'$_p9k__cache_val[4]'%{d%}'
  1957. fi
  1958. fi
  1959. _p9k_prompt_segment "$_p9k__cache_val[1]" "blue" "$_p9k_color1" "$_p9k__cache_val[2]" "$_p9k__cache_val[3]" "" "$_p9k__cache_val[4]"
  1960. }
  1961. instant_prompt_dir() { prompt_dir; }
  1962. ################################################################
  1963. # Docker machine
  1964. prompt_docker_machine() {
  1965. _p9k_prompt_segment "$0" "magenta" "$_p9k_color1" 'SERVER_ICON' 0 '' "${DOCKER_MACHINE_NAME//\%/%%}"
  1966. }
  1967. _p9k_prompt_docker_machine_init() {
  1968. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$DOCKER_MACHINE_NAME'
  1969. }
  1970. ################################################################
  1971. # GO prompt
  1972. prompt_go_version() {
  1973. _p9k_cached_cmd 0 '' go version || return
  1974. [[ $_p9k__ret == (#b)*go([[:digit:].]##)* ]] || return
  1975. local v=$match[1]
  1976. if (( _POWERLEVEL9K_GO_VERSION_PROJECT_ONLY )); then
  1977. local p=$GOPATH
  1978. if [[ -z $p ]]; then
  1979. if [[ -d $HOME/go ]]; then
  1980. p=$HOME/go
  1981. else
  1982. p="$(go env GOPATH 2>/dev/null)" && [[ -n $p ]] || return
  1983. fi
  1984. fi
  1985. if [[ $_p9k__cwd/ != $p/* && $_p9k__cwd_a/ != $p/* ]]; then
  1986. _p9k_upglob go.mod && return
  1987. fi
  1988. fi
  1989. _p9k_prompt_segment "$0" "green" "grey93" "GO_ICON" 0 '' "${v//\%/%%}"
  1990. }
  1991. _p9k_prompt_go_version_init() {
  1992. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[go]'
  1993. }
  1994. ################################################################
  1995. # Command number (in local history)
  1996. prompt_history() {
  1997. local -i len=$#_p9k__prompt _p9k__has_upglob
  1998. _p9k_prompt_segment "$0" "grey50" "$_p9k_color1" '' 0 '' '%h'
  1999. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  2000. }
  2001. prompt_package() {
  2002. unset P9K_PACKAGE_NAME P9K_PACKAGE_VERSION
  2003. _p9k_upglob package.json && return
  2004. local file=$_p9k__parent_dirs[$?]/package.json
  2005. if ! _p9k_cache_stat_get $0 $file; then
  2006. () {
  2007. local data field
  2008. local -A found
  2009. # Redneck json parsing. Yields correct results for any well-formed json document.
  2010. # Produces random garbage for invalid json.
  2011. { data="$(<$file)" || return } 2>/dev/null
  2012. data=${${data//$'\r'}##[[:space:]]#}
  2013. [[ $data == '{'* ]] || return
  2014. data[1]=
  2015. local -i depth=1
  2016. while true; do
  2017. data=${data##[[:space:]]#}
  2018. [[ -n $data ]] || return
  2019. case $data[1] in
  2020. '{'|'[') data[1]=; (( ++depth ));;
  2021. '}'|']') data[1]=; (( --depth > 0 )) || return;;
  2022. ':') data[1]=;;
  2023. ',') data[1]=; field=;;
  2024. [[:alnum:].]) data=${data##[[:alnum:].]#};;
  2025. '"')
  2026. local tail=${data##\"([^\"\\]|\\?)#}
  2027. [[ $tail == '"'* ]] || return
  2028. local s=${data:1:-$#tail}
  2029. data=${tail:1}
  2030. (( depth == 1 )) || continue
  2031. if [[ -z $field ]]; then
  2032. field=${s:-x}
  2033. elif [[ $field == (name|version) ]]; then
  2034. (( ! $+found[$field] )) || return
  2035. [[ -n $s ]] || return
  2036. [[ $s != *($'\n'|'\')* ]] || return
  2037. found[$field]=$s
  2038. (( $#found == 2 )) && break
  2039. fi
  2040. ;;
  2041. *) return 1;;
  2042. esac
  2043. done
  2044. _p9k_cache_stat_set 1 $found[name] $found[version]
  2045. return 0
  2046. } || _p9k_cache_stat_set 0
  2047. fi
  2048. (( _p9k__cache_val[1] )) || return
  2049. P9K_PACKAGE_NAME=$_p9k__cache_val[2]
  2050. P9K_PACKAGE_VERSION=$_p9k__cache_val[3]
  2051. _p9k_prompt_segment "$0" "cyan" "$_p9k_color1" PACKAGE_ICON 0 '' ${P9K_PACKAGE_VERSION//\%/%%}
  2052. }
  2053. ################################################################
  2054. # Detection for virtualization (systemd based systems only)
  2055. prompt_detect_virt() {
  2056. local virt="$(systemd-detect-virt 2>/dev/null)"
  2057. if [[ "$virt" == "none" ]]; then
  2058. local -a inode
  2059. if zstat -A inode +inode / 2>/dev/null && [[ $inode[1] != 2 ]]; then
  2060. virt="chroot"
  2061. fi
  2062. fi
  2063. if [[ -n "${virt}" ]]; then
  2064. _p9k_prompt_segment "$0" "$_p9k_color1" "yellow" '' 0 '' "${virt//\%/%%}"
  2065. fi
  2066. }
  2067. _p9k_prompt_detect_virt_init() {
  2068. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[systemd-detect-virt]'
  2069. }
  2070. ################################################################
  2071. # Segment to display the current IP address
  2072. prompt_ip() {
  2073. local -i len=$#_p9k__prompt _p9k__has_upglob
  2074. _p9k_prompt_segment "$0" "cyan" "$_p9k_color1" 'NETWORK_ICON' 1 '$P9K_IP_IP' '$P9K_IP_IP'
  2075. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  2076. }
  2077. ################################################################
  2078. # Segment to display if VPN is active
  2079. prompt_vpn_ip() {
  2080. typeset -ga _p9k__vpn_ip_segments
  2081. _p9k__vpn_ip_segments+=($_p9k__prompt_side $_p9k__line_index $_p9k__segment_index)
  2082. local p='${(e)_p9k__vpn_ip_'$_p9k__prompt_side$_p9k__segment_index'}'
  2083. _p9k__prompt+=$p
  2084. typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$p
  2085. }
  2086. _p9k_vpn_ip_render() {
  2087. local _p9k__segment_name=vpn_ip _p9k__prompt_side ip
  2088. local -i _p9k__has_upglob _p9k__segment_index
  2089. for _p9k__prompt_side _p9k__line_index _p9k__segment_index in $_p9k__vpn_ip_segments; do
  2090. local _p9k__prompt=
  2091. for ip in $_p9k__vpn_ip_ips; do
  2092. _p9k_prompt_segment prompt_vpn_ip "cyan" "$_p9k_color1" 'VPN_ICON' 0 '' $ip
  2093. done
  2094. typeset -g _p9k__vpn_ip_$_p9k__prompt_side$_p9k__segment_index=$_p9k__prompt
  2095. done
  2096. }
  2097. ################################################################
  2098. # Segment to display laravel version
  2099. prompt_laravel_version() {
  2100. _p9k_upglob artisan && return
  2101. local dir=$_p9k__parent_dirs[$?]
  2102. local app=$dir/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
  2103. [[ -r $app ]] || return
  2104. if ! _p9k_cache_stat_get $0 $dir/artisan $app; then
  2105. local v="$(php $dir/artisan --version 2> /dev/null)"
  2106. _p9k_cache_stat_set "${${(M)v:#Laravel Framework *}#Laravel Framework }"
  2107. fi
  2108. [[ -n $_p9k__cache_val[1] ]] || return
  2109. _p9k_prompt_segment "$0" "maroon" "white" 'LARAVEL_ICON' 0 '' "${_p9k__cache_val[1]//\%/%%}"
  2110. }
  2111. _p9k_prompt_laravel_version_init() {
  2112. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[php]'
  2113. }
  2114. ################################################################
  2115. # Segment to display load
  2116. prompt_load() {
  2117. if [[ $_p9k_os == (OSX|BSD) ]]; then
  2118. local -i len=$#_p9k__prompt _p9k__has_upglob
  2119. _p9k_prompt_segment $0_CRITICAL red "$_p9k_color1" LOAD_ICON 1 '$_p9k__load_critical' '$_p9k__load_value'
  2120. _p9k_prompt_segment $0_WARNING yellow "$_p9k_color1" LOAD_ICON 1 '$_p9k__load_warning' '$_p9k__load_value'
  2121. _p9k_prompt_segment $0_NORMAL green "$_p9k_color1" LOAD_ICON 1 '$_p9k__load_normal' '$_p9k__load_value'
  2122. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  2123. return
  2124. fi
  2125. [[ -r /proc/loadavg ]] || return
  2126. _p9k_read_file /proc/loadavg || return
  2127. local load=${${(A)=_p9k__ret}[_POWERLEVEL9K_LOAD_WHICH]//,/.}
  2128. local -F pct='100. * load / _p9k_num_cpus'
  2129. if (( pct > _POWERLEVEL9K_LOAD_CRITICAL_PCT )); then
  2130. _p9k_prompt_segment $0_CRITICAL red "$_p9k_color1" LOAD_ICON 0 '' $load
  2131. elif (( pct > _POWERLEVEL9K_LOAD_WARNING_PCT )); then
  2132. _p9k_prompt_segment $0_WARNING yellow "$_p9k_color1" LOAD_ICON 0 '' $load
  2133. else
  2134. _p9k_prompt_segment $0_NORMAL green "$_p9k_color1" LOAD_ICON 0 '' $load
  2135. fi
  2136. }
  2137. _p9k_prompt_load_init() {
  2138. if [[ $_p9k_os == (OSX|BSD) ]]; then
  2139. typeset -g _p9k__load_value=
  2140. typeset -g _p9k__load_normal=
  2141. typeset -g _p9k__load_warning=
  2142. typeset -g _p9k__load_critical=
  2143. _p9k__async_segments_compute+='_p9k_worker_invoke load _p9k_prompt_load_compute'
  2144. elif [[ ! -r /proc/loadavg ]]; then
  2145. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
  2146. fi
  2147. }
  2148. _p9k_prompt_load_compute() {
  2149. (( $+commands[sysctl] )) || return
  2150. _p9k_worker_async _p9k_prompt_load_async _p9k_prompt_load_sync
  2151. }
  2152. _p9k_prompt_load_async() {
  2153. local load="$(sysctl -n vm.loadavg 2>/dev/null)" || return
  2154. load=${${(A)=load}[_POWERLEVEL9K_LOAD_WHICH+1]//,/.}
  2155. [[ $load == <->(|.<->) && $load != $_p9k__load_value ]] || return
  2156. _p9k__load_value=$load
  2157. _p9k__load_normal=
  2158. _p9k__load_warning=
  2159. _p9k__load_critical=
  2160. local -F pct='100. * _p9k__load_value / _p9k_num_cpus'
  2161. if (( pct > _POWERLEVEL9K_LOAD_CRITICAL_PCT )); then
  2162. _p9k__load_critical=1
  2163. elif (( pct > _POWERLEVEL9K_LOAD_WARNING_PCT )); then
  2164. _p9k__load_warning=1
  2165. else
  2166. _p9k__load_normal=1
  2167. fi
  2168. _p9k_print_params \
  2169. _p9k__load_value \
  2170. _p9k__load_normal \
  2171. _p9k__load_warning \
  2172. _p9k__load_critical
  2173. echo -E - 'reset=1'
  2174. }
  2175. _p9k_prompt_load_sync() {
  2176. eval $REPLY
  2177. _p9k_worker_reply $REPLY
  2178. }
  2179. # Usage: _p9k_cached_cmd <0|1> <dep> <cmd> [args...]
  2180. #
  2181. # The first argument says whether to capture stderr (1) or ignore it (0).
  2182. # The second argument can be empty or a file. If it's a file, the
  2183. # output of the command is presumed to potentially depend on it.
  2184. function _p9k_cached_cmd() {
  2185. local cmd=$commands[$3]
  2186. [[ -n $cmd ]] || return
  2187. if ! _p9k_cache_stat_get $0" ${(q)*}" $2 $cmd; then
  2188. local out
  2189. if (( $1 )); then
  2190. out="$($cmd "${@:4}" 2>&1)"
  2191. else
  2192. out="$($cmd "${@:4}" 2>/dev/null)"
  2193. fi
  2194. _p9k_cache_stat_set $(( ! $? )) "$out"
  2195. fi
  2196. (( $_p9k__cache_val[1] )) || return
  2197. _p9k__ret=$_p9k__cache_val[2]
  2198. }
  2199. ################################################################
  2200. # Segment to diplay Node version
  2201. prompt_node_version() {
  2202. _p9k_upglob package.json
  2203. local -i idx=$?
  2204. if (( idx )); then
  2205. _p9k_cached_cmd 0 $_p9k__parent_dirs[idx]/package.json node --version || return
  2206. else
  2207. (( _POWERLEVEL9K_NODE_VERSION_PROJECT_ONLY )) && return
  2208. _p9k_cached_cmd 0 '' node --version || return
  2209. fi
  2210. [[ $_p9k__ret == v?* ]] || return
  2211. _p9k_prompt_segment "$0" "green" "white" 'NODE_ICON' 0 '' "${_p9k__ret#v}"
  2212. }
  2213. _p9k_prompt_node_version_init() {
  2214. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[node]'
  2215. }
  2216. # Almost the same as `nvm_version default` but faster. The differences shouldn't affect
  2217. # the observable behavior of Powerlevel10k.
  2218. function _p9k_nvm_ls_default() {
  2219. local v=default
  2220. local -a seen=($v)
  2221. while [[ -r $NVM_DIR/alias/$v ]]; do
  2222. local target=
  2223. IFS='' read -r target <$NVM_DIR/alias/$v
  2224. target=${target%$'\r'}
  2225. [[ -z $target ]] && break
  2226. (( $seen[(I)$target] )) && return
  2227. seen+=$target
  2228. v=$target
  2229. done
  2230. case $v in
  2231. default|N/A)
  2232. return 1
  2233. ;;
  2234. system|v)
  2235. _p9k__ret=system
  2236. return 0
  2237. ;;
  2238. iojs-[0-9]*)
  2239. v=iojs-v${v#iojs-}
  2240. ;;
  2241. [0-9]*)
  2242. v=v$v
  2243. ;;
  2244. esac
  2245. if [[ $v == v*.*.* ]]; then
  2246. if [[ -x $NVM_DIR/versions/node/$v/bin/node || -x $NVM_DIR/$v/bin/node ]]; then
  2247. _p9k__ret=$v
  2248. return 0
  2249. elif [[ -x $NVM_DIR/versions/io.js/$v/bin/node ]]; then
  2250. _p9k__ret=iojs-$v
  2251. return 0
  2252. else
  2253. return 1
  2254. fi
  2255. fi
  2256. local -a dirs=()
  2257. case $v in
  2258. node|node-|stable)
  2259. dirs=($NVM_DIR/versions/node $NVM_DIR)
  2260. v='(v[1-9]*|v0.*[02468].*)'
  2261. ;;
  2262. unstable)
  2263. dirs=($NVM_DIR/versions/node $NVM_DIR)
  2264. v='v0.*[13579].*'
  2265. ;;
  2266. iojs*)
  2267. dirs=($NVM_DIR/versions/io.js)
  2268. v=v${${${v#iojs}#-}#v}'*'
  2269. ;;
  2270. *)
  2271. dirs=($NVM_DIR/versions/node $NVM_DIR $NVM_DIR/versions/io.js)
  2272. v=v${v#v}'*'
  2273. ;;
  2274. esac
  2275. local -a matches=(${^dirs}/${~v}(/N))
  2276. (( $#matches )) || return
  2277. local max path
  2278. for path in ${(Oa)matches}; do
  2279. [[ ${path:t} == (#b)v(*).(*).(*) ]] || continue
  2280. v=${(j::)${(@l:6::0:)match}}
  2281. [[ $v > $max ]] || continue
  2282. max=$v
  2283. _p9k__ret=${path:t}
  2284. [[ ${path:h:t} != io.js ]] || _p9k__ret=iojs-$_p9k__ret
  2285. done
  2286. [[ -n $max ]]
  2287. }
  2288. # The same as `nvm_version current` but faster.
  2289. _p9k_nvm_ls_current() {
  2290. local node_path=${commands[node]:A}
  2291. [[ -n $node_path ]] || return
  2292. local nvm_dir=${NVM_DIR:A}
  2293. if [[ -n $nvm_dir && $node_path == $nvm_dir/versions/io.js/* ]]; then
  2294. _p9k_cached_cmd 0 '' iojs --version || return
  2295. _p9k__ret=iojs-v${_p9k__ret#v}
  2296. elif [[ -n $nvm_dir && $node_path == $nvm_dir/* ]]; then
  2297. _p9k_cached_cmd 0 '' node --version || return
  2298. _p9k__ret=v${_p9k__ret#v}
  2299. else
  2300. _p9k__ret=system
  2301. fi
  2302. }
  2303. ################################################################
  2304. # Segment to display Node version from NVM
  2305. # Only prints the segment if different than the default value
  2306. prompt_nvm() {
  2307. [[ -n $NVM_DIR ]] && _p9k_nvm_ls_current || return
  2308. local current=$_p9k__ret
  2309. ! _p9k_nvm_ls_default || [[ $_p9k__ret != $current ]] || return
  2310. _p9k_prompt_segment "$0" "magenta" "black" 'NODE_ICON' 0 '' "${${current#v}//\%/%%}"
  2311. }
  2312. _p9k_prompt_nvm_init() {
  2313. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[nvm]:-${${+functions[nvm]}:#0}}'
  2314. }
  2315. ################################################################
  2316. # Segment to display NodeEnv
  2317. prompt_nodeenv() {
  2318. local msg
  2319. if (( _POWERLEVEL9K_NODEENV_SHOW_NODE_VERSION )) && _p9k_cached_cmd 0 '' node --version; then
  2320. msg="${_p9k__ret//\%/%%} "
  2321. fi
  2322. msg+="$_POWERLEVEL9K_NODEENV_LEFT_DELIMITER${${NODE_VIRTUAL_ENV:t}//\%/%%}$_POWERLEVEL9K_NODEENV_RIGHT_DELIMITER"
  2323. _p9k_prompt_segment "$0" "black" "green" 'NODE_ICON' 0 '' "$msg"
  2324. }
  2325. _p9k_prompt_nodeenv_init() {
  2326. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$NODE_VIRTUAL_ENV'
  2327. }
  2328. function _p9k_nodeenv_version_transform() {
  2329. local dir=${NODENV_ROOT:-$HOME/.nodenv}/versions
  2330. [[ -z $1 || $1 == system ]] && _p9k__ret=$1 && return
  2331. [[ -d $dir/$1 ]] && _p9k__ret=$1 && return
  2332. [[ -d $dir/${1/v} ]] && _p9k__ret=${1/v} && return
  2333. [[ -d $dir/${1#node-} ]] && _p9k__ret=${1#node-} && return
  2334. [[ -d $dir/${1#node-v} ]] && _p9k__ret=${1#node-v} && return
  2335. return 1
  2336. }
  2337. function _p9k_nodenv_global_version() {
  2338. _p9k_read_word ${NODENV_ROOT:-$HOME/.nodenv}/version || _p9k__ret=system
  2339. }
  2340. ################################################################
  2341. # Segment to display nodenv information
  2342. # https://github.com/nodenv/nodenv
  2343. prompt_nodenv() {
  2344. if [[ -n $NODENV_VERSION ]]; then
  2345. (( ${_POWERLEVEL9K_NODENV_SOURCES[(I)shell]} )) || return
  2346. local v=$NODENV_VERSION
  2347. else
  2348. (( ${_POWERLEVEL9K_NODENV_SOURCES[(I)local|global]} )) || return
  2349. _p9k__ret=
  2350. if [[ $NODENV_DIR != (|.) ]]; then
  2351. [[ $NODENV_DIR == /* ]] && local dir=$NODENV_DIR || local dir="$_p9k__cwd_a/$NODENV_DIR"
  2352. dir=${dir:A}
  2353. if [[ $dir != $_p9k__cwd_a ]]; then
  2354. while true; do
  2355. if _p9k_read_word $dir/.node-version; then
  2356. (( ${_POWERLEVEL9K_NODENV_SOURCES[(I)local]} )) || return
  2357. break
  2358. fi
  2359. [[ $dir == (/|.) ]] && break
  2360. dir=${dir:h}
  2361. done
  2362. fi
  2363. fi
  2364. if [[ -z $_p9k__ret ]]; then
  2365. _p9k_upglob .node-version
  2366. local -i idx=$?
  2367. if (( idx )) && _p9k_read_word $_p9k__parent_dirs[idx]/.node-version; then
  2368. (( ${_POWERLEVEL9K_NODENV_SOURCES[(I)local]} )) || return
  2369. else
  2370. _p9k__ret=
  2371. fi
  2372. fi
  2373. if [[ -z $_p9k__ret ]]; then
  2374. (( _POWERLEVEL9K_NODENV_PROMPT_ALWAYS_SHOW )) || return
  2375. (( ${_POWERLEVEL9K_NODENV_SOURCES[(I)global]} )) || return
  2376. _p9k_nodenv_global_version
  2377. fi
  2378. _p9k_nodeenv_version_transform $_p9k__ret || return
  2379. local v=$_p9k__ret
  2380. fi
  2381. if (( !_POWERLEVEL9K_NODENV_PROMPT_ALWAYS_SHOW )); then
  2382. _p9k_nodenv_global_version
  2383. _p9k_nodeenv_version_transform $_p9k__ret && [[ $v == $_p9k__ret ]] && return
  2384. fi
  2385. if (( !_POWERLEVEL9K_NODENV_SHOW_SYSTEM )); then
  2386. [[ $v == system ]] && return
  2387. fi
  2388. _p9k_prompt_segment "$0" "black" "green" 'NODE_ICON' 0 '' "${v//\%/%%}"
  2389. }
  2390. _p9k_prompt_nodenv_init() {
  2391. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[nodenv]:-${${+functions[nodenv]}:#0}}'
  2392. }
  2393. prompt_dotnet_version() {
  2394. if (( _POWERLEVEL9K_DOTNET_VERSION_PROJECT_ONLY )); then
  2395. _p9k_upglob 'project.json|global.json|packet.dependencies|*.csproj|*.fsproj|*.xproj|*.sln' && return
  2396. fi
  2397. _p9k_cached_cmd 0 '' dotnet --version || return
  2398. _p9k_prompt_segment "$0" "magenta" "white" 'DOTNET_ICON' 0 '' "$_p9k__ret"
  2399. }
  2400. _p9k_prompt_dotnet_init() {
  2401. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[dotnet]'
  2402. }
  2403. ################################################################
  2404. # Segment to print a little OS icon
  2405. prompt_os_icon() {
  2406. local -i len=$#_p9k__prompt _p9k__has_upglob
  2407. _p9k_prompt_segment "$0" "black" "white" '' 0 '' "$_p9k_os_icon"
  2408. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  2409. }
  2410. instant_prompt_os_icon() { prompt_os_icon; }
  2411. ################################################################
  2412. # Segment to display PHP version number
  2413. prompt_php_version() {
  2414. if (( _POWERLEVEL9K_PHP_VERSION_PROJECT_ONLY )); then
  2415. _p9k_upglob 'composer.json|*.php' && return
  2416. fi
  2417. _p9k_cached_cmd 0 '' php --version || return
  2418. [[ $_p9k__ret == (#b)(*$'\n')#'PHP '([[:digit:].]##)* ]] || return
  2419. local v=$match[2]
  2420. _p9k_prompt_segment "$0" "fuchsia" "grey93" 'PHP_ICON' 0 '' "${v//\%/%%}"
  2421. }
  2422. _p9k_prompt_php_version_init() {
  2423. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[php]'
  2424. }
  2425. ################################################################
  2426. # Segment to display free RAM and used Swap
  2427. prompt_ram() {
  2428. local -i len=$#_p9k__prompt _p9k__has_upglob
  2429. _p9k_prompt_segment $0 yellow "$_p9k_color1" RAM_ICON 1 '$_p9k__ram_free' '$_p9k__ram_free'
  2430. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  2431. }
  2432. function _p9k_prompt_ram_init() {
  2433. if [[ $_p9k_os == OSX && $+commands[vm_stat] == 0 ||
  2434. $_p9k_os == BSD && ! -r /var/run/dmesg.boot ||
  2435. $_p9k_os != (OSX|BSD) && ! -r /proc/meminfo ]]; then
  2436. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
  2437. return
  2438. fi
  2439. typeset -g _p9k__ram_free=
  2440. _p9k__async_segments_compute+='_p9k_worker_invoke ram _p9k_prompt_ram_compute'
  2441. }
  2442. _p9k_prompt_ram_compute() {
  2443. _p9k_worker_async _p9k_prompt_ram_async _p9k_prompt_ram_sync
  2444. }
  2445. _p9k_prompt_ram_async() {
  2446. local -F free_bytes
  2447. case $_p9k_os in
  2448. OSX)
  2449. (( $+commands[vm_stat] )) || return
  2450. local stat && stat="$(vm_stat 2>/dev/null)" || return
  2451. [[ $stat =~ 'Pages free:[[:space:]]+([0-9]+)' ]] || return
  2452. (( free_bytes += match[1] ))
  2453. [[ $stat =~ 'Pages inactive:[[:space:]]+([0-9]+)' ]] || return
  2454. (( free_bytes += match[1] ))
  2455. if (( ! $+_p9k__ram_pagesize )); then
  2456. local p
  2457. (( $+commands[pagesize] )) && p=$(pagesize 2>/dev/null) && [[ $p == <1-> ]] || p=4096
  2458. typeset -gi _p9k__ram_pagesize=p
  2459. _p9k_print_params _p9k__ram_pagesize
  2460. fi
  2461. (( free_bytes *= _p9k__ram_pagesize ))
  2462. ;;
  2463. BSD)
  2464. local stat && stat="$(grep -F 'avail memory' /var/run/dmesg.boot 2>/dev/null)" || return
  2465. free_bytes=${${(A)=stat}[4]}
  2466. ;;
  2467. *)
  2468. [[ -r /proc/meminfo ]] || return
  2469. local stat && stat="$(</proc/meminfo)" || return
  2470. [[ $stat == (#b)*(MemAvailable:|MemFree:)[[:space:]]#(<->)* ]] || return
  2471. free_bytes=$(( $match[2] * 1024 ))
  2472. ;;
  2473. esac
  2474. _p9k_human_readable_bytes $free_bytes
  2475. [[ $_p9k__ret != $_p9k__ram_free ]] || return
  2476. _p9k__ram_free=$_p9k__ret
  2477. _p9k_print_params _p9k__ram_free
  2478. echo -E - 'reset=1'
  2479. }
  2480. _p9k_prompt_ram_sync() {
  2481. eval $REPLY
  2482. _p9k_worker_reply $REPLY
  2483. }
  2484. function _p9k_rbenv_global_version() {
  2485. _p9k_read_word ${RBENV_ROOT:-$HOME/.rbenv}/version || _p9k__ret=system
  2486. }
  2487. ################################################################
  2488. # Segment to display rbenv information
  2489. # https://github.com/rbenv/rbenv#choosing-the-ruby-version
  2490. prompt_rbenv() {
  2491. if [[ -n $RBENV_VERSION ]]; then
  2492. (( ${_POWERLEVEL9K_RBENV_SOURCES[(I)shell]} )) || return
  2493. local v=$RBENV_VERSION
  2494. else
  2495. (( ${_POWERLEVEL9K_RBENV_SOURCES[(I)local|global]} )) || return
  2496. _p9k__ret=
  2497. if [[ $RBENV_DIR != (|.) ]]; then
  2498. [[ $RBENV_DIR == /* ]] && local dir=$RBENV_DIR || local dir="$_p9k__cwd_a/$RBENV_DIR"
  2499. dir=${dir:A}
  2500. if [[ $dir != $_p9k__cwd_a ]]; then
  2501. while true; do
  2502. if _p9k_read_word $dir/.ruby-version; then
  2503. (( ${_POWERLEVEL9K_RBENV_SOURCES[(I)local]} )) || return
  2504. break
  2505. fi
  2506. [[ $dir == (/|.) ]] && break
  2507. dir=${dir:h}
  2508. done
  2509. fi
  2510. fi
  2511. if [[ -z $_p9k__ret ]]; then
  2512. _p9k_upglob .ruby-version
  2513. local -i idx=$?
  2514. if (( idx )) && _p9k_read_word $_p9k__parent_dirs[idx]/.ruby-version; then
  2515. (( ${_POWERLEVEL9K_RBENV_SOURCES[(I)local]} )) || return
  2516. else
  2517. _p9k__ret=
  2518. fi
  2519. fi
  2520. if [[ -z $_p9k__ret ]]; then
  2521. (( _POWERLEVEL9K_RBENV_PROMPT_ALWAYS_SHOW )) || return
  2522. (( ${_POWERLEVEL9K_RBENV_SOURCES[(I)global]} )) || return
  2523. _p9k_rbenv_global_version
  2524. fi
  2525. local v=$_p9k__ret
  2526. fi
  2527. if (( !_POWERLEVEL9K_RBENV_PROMPT_ALWAYS_SHOW )); then
  2528. _p9k_rbenv_global_version
  2529. [[ $v == $_p9k__ret ]] && return
  2530. fi
  2531. if (( !_POWERLEVEL9K_RBENV_SHOW_SYSTEM )); then
  2532. [[ $v == system ]] && return
  2533. fi
  2534. _p9k_prompt_segment "$0" "red" "$_p9k_color1" 'RUBY_ICON' 0 '' "${v//\%/%%}"
  2535. }
  2536. _p9k_prompt_rbenv_init() {
  2537. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[rbenv]:-${${+functions[rbenv]}:#0}}'
  2538. }
  2539. function _p9k_phpenv_global_version() {
  2540. _p9k_read_word ${PHPENV_ROOT:-$HOME/.phpenv}/version || _p9k__ret=system
  2541. }
  2542. function _p9k_scalaenv_global_version() {
  2543. _p9k_read_word ${SCALAENV_ROOT:-$HOME/.scalaenv}/version || _p9k__ret=system
  2544. }
  2545. # https://github.com/scalaenv/scalaenv
  2546. prompt_scalaenv() {
  2547. if [[ -n $SCALAENV_VERSION ]]; then
  2548. (( ${_POWERLEVEL9K_SCALAENV_SOURCES[(I)shell]} )) || return
  2549. local v=$SCALAENV_VERSION
  2550. else
  2551. (( ${_POWERLEVEL9K_SCALAENV_SOURCES[(I)local|global]} )) || return
  2552. _p9k__ret=
  2553. if [[ $SCALAENV_DIR != (|.) ]]; then
  2554. [[ $SCALAENV_DIR == /* ]] && local dir=$SCALAENV_DIR || local dir="$_p9k__cwd_a/$SCALAENV_DIR"
  2555. dir=${dir:A}
  2556. if [[ $dir != $_p9k__cwd_a ]]; then
  2557. while true; do
  2558. if _p9k_read_word $dir/.scala-version; then
  2559. (( ${_POWERLEVEL9K_SCALAENV_SOURCES[(I)local]} )) || return
  2560. break
  2561. fi
  2562. [[ $dir == (/|.) ]] && break
  2563. dir=${dir:h}
  2564. done
  2565. fi
  2566. fi
  2567. if [[ -z $_p9k__ret ]]; then
  2568. _p9k_upglob .scala-version
  2569. local -i idx=$?
  2570. if (( idx )) && _p9k_read_word $_p9k__parent_dirs[idx]/.scala-version; then
  2571. (( ${_POWERLEVEL9K_SCALAENV_SOURCES[(I)local]} )) || return
  2572. else
  2573. _p9k__ret=
  2574. fi
  2575. fi
  2576. if [[ -z $_p9k__ret ]]; then
  2577. (( _POWERLEVEL9K_SCALAENV_PROMPT_ALWAYS_SHOW )) || return
  2578. (( ${_POWERLEVEL9K_SCALAENV_SOURCES[(I)global]} )) || return
  2579. _p9k_scalaenv_global_version
  2580. fi
  2581. local v=$_p9k__ret
  2582. fi
  2583. if (( !_POWERLEVEL9K_SCALAENV_PROMPT_ALWAYS_SHOW )); then
  2584. _p9k_scalaenv_global_version
  2585. [[ $v == $_p9k__ret ]] && return
  2586. fi
  2587. if (( !_POWERLEVEL9K_SCALAENV_SHOW_SYSTEM )); then
  2588. [[ $v == system ]] && return
  2589. fi
  2590. _p9k_prompt_segment "$0" "red" "$_p9k_color1" 'SCALA_ICON' 0 '' "${v//\%/%%}"
  2591. }
  2592. _p9k_prompt_scalaenv_init() {
  2593. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[scalaenv]:-${${+functions[scalaenv]}:#0}}'
  2594. }
  2595. function _p9k_phpenv_global_version() {
  2596. _p9k_read_word ${PHPENV_ROOT:-$HOME/.phpenv}/version || _p9k__ret=system
  2597. }
  2598. prompt_phpenv() {
  2599. if [[ -n $PHPENV_VERSION ]]; then
  2600. (( ${_POWERLEVEL9K_PHPENV_SOURCES[(I)shell]} )) || return
  2601. local v=$PHPENV_VERSION
  2602. else
  2603. (( ${_POWERLEVEL9K_PHPENV_SOURCES[(I)local|global]} )) || return
  2604. _p9k__ret=
  2605. if [[ $PHPENV_DIR != (|.) ]]; then
  2606. [[ $PHPENV_DIR == /* ]] && local dir=$PHPENV_DIR || local dir="$_p9k__cwd_a/$PHPENV_DIR"
  2607. dir=${dir:A}
  2608. if [[ $dir != $_p9k__cwd_a ]]; then
  2609. while true; do
  2610. if _p9k_read_word $dir/.php-version; then
  2611. (( ${_POWERLEVEL9K_PHPENV_SOURCES[(I)local]} )) || return
  2612. break
  2613. fi
  2614. [[ $dir == (/|.) ]] && break
  2615. dir=${dir:h}
  2616. done
  2617. fi
  2618. fi
  2619. if [[ -z $_p9k__ret ]]; then
  2620. _p9k_upglob .php-version
  2621. local -i idx=$?
  2622. if (( idx )) && _p9k_read_word $_p9k__parent_dirs[idx]/.php-version; then
  2623. (( ${_POWERLEVEL9K_PHPENV_SOURCES[(I)local]} )) || return
  2624. else
  2625. _p9k__ret=
  2626. fi
  2627. fi
  2628. if [[ -z $_p9k__ret ]]; then
  2629. (( _POWERLEVEL9K_PHPENV_PROMPT_ALWAYS_SHOW )) || return
  2630. (( ${_POWERLEVEL9K_PHPENV_SOURCES[(I)global]} )) || return
  2631. _p9k_phpenv_global_version
  2632. fi
  2633. local v=$_p9k__ret
  2634. fi
  2635. if (( !_POWERLEVEL9K_PHPENV_PROMPT_ALWAYS_SHOW )); then
  2636. _p9k_phpenv_global_version
  2637. [[ $v == $_p9k__ret ]] && return
  2638. fi
  2639. if (( !_POWERLEVEL9K_PHPENV_SHOW_SYSTEM )); then
  2640. [[ $v == system ]] && return
  2641. fi
  2642. _p9k_prompt_segment "$0" "magenta" "$_p9k_color1" 'PHP_ICON' 0 '' "${v//\%/%%}"
  2643. }
  2644. _p9k_prompt_phpenv_init() {
  2645. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[phpenv]:-${${+functions[phpenv]}:#0}}'
  2646. }
  2647. function _p9k_luaenv_global_version() {
  2648. _p9k_read_word ${LUAENV_ROOT:-$HOME/.luaenv}/version || _p9k__ret=system
  2649. }
  2650. ################################################################
  2651. # Segment to display luaenv information
  2652. # https://github.com/cehoffman/luaenv
  2653. prompt_luaenv() {
  2654. if [[ -n $LUAENV_VERSION ]]; then
  2655. (( ${_POWERLEVEL9K_LUAENV_SOURCES[(I)shell]} )) || return
  2656. local v=$LUAENV_VERSION
  2657. else
  2658. (( ${_POWERLEVEL9K_LUAENV_SOURCES[(I)local|global]} )) || return
  2659. _p9k__ret=
  2660. if [[ $LUAENV_DIR != (|.) ]]; then
  2661. [[ $LUAENV_DIR == /* ]] && local dir=$LUAENV_DIR || local dir="$_p9k__cwd_a/$LUAENV_DIR"
  2662. dir=${dir:A}
  2663. if [[ $dir != $_p9k__cwd_a ]]; then
  2664. while true; do
  2665. if _p9k_read_word $dir/.lua-version; then
  2666. (( ${_POWERLEVEL9K_LUAENV_SOURCES[(I)local]} )) || return
  2667. break
  2668. fi
  2669. [[ $dir == (/|.) ]] && break
  2670. dir=${dir:h}
  2671. done
  2672. fi
  2673. fi
  2674. if [[ -z $_p9k__ret ]]; then
  2675. _p9k_upglob .lua-version
  2676. local -i idx=$?
  2677. if (( idx )) && _p9k_read_word $_p9k__parent_dirs[idx]/.lua-version; then
  2678. (( ${_POWERLEVEL9K_LUAENV_SOURCES[(I)local]} )) || return
  2679. else
  2680. _p9k__ret=
  2681. fi
  2682. fi
  2683. if [[ -z $_p9k__ret ]]; then
  2684. (( _POWERLEVEL9K_LUAENV_PROMPT_ALWAYS_SHOW )) || return
  2685. (( ${_POWERLEVEL9K_LUAENV_SOURCES[(I)global]} )) || return
  2686. _p9k_luaenv_global_version
  2687. fi
  2688. local v=$_p9k__ret
  2689. fi
  2690. if (( !_POWERLEVEL9K_LUAENV_PROMPT_ALWAYS_SHOW )); then
  2691. _p9k_luaenv_global_version
  2692. [[ $v == $_p9k__ret ]] && return
  2693. fi
  2694. if (( !_POWERLEVEL9K_LUAENV_SHOW_SYSTEM )); then
  2695. [[ $v == system ]] && return
  2696. fi
  2697. _p9k_prompt_segment "$0" blue "$_p9k_color1" 'LUA_ICON' 0 '' "${v//\%/%%}"
  2698. }
  2699. _p9k_prompt_luaenv_init() {
  2700. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[luaenv]:-${${+functions[luaenv]}:#0}}'
  2701. }
  2702. function _p9k_jenv_global_version() {
  2703. _p9k_read_word ${JENV_ROOT:-$HOME/.jenv}/version || _p9k__ret=system
  2704. }
  2705. ################################################################
  2706. # Segment to display jenv information
  2707. # https://github.com/jenv/jenv
  2708. prompt_jenv() {
  2709. if [[ -n $JENV_VERSION ]]; then
  2710. (( ${_POWERLEVEL9K_JENV_SOURCES[(I)shell]} )) || return
  2711. local v=$JENV_VERSION
  2712. else
  2713. (( ${_POWERLEVEL9K_JENV_SOURCES[(I)local|global]} )) || return
  2714. _p9k__ret=
  2715. if [[ $JENV_DIR != (|.) ]]; then
  2716. [[ $JENV_DIR == /* ]] && local dir=$JENV_DIR || local dir="$_p9k__cwd_a/$JENV_DIR"
  2717. dir=${dir:A}
  2718. if [[ $dir != $_p9k__cwd_a ]]; then
  2719. while true; do
  2720. if _p9k_read_word $dir/.java-version; then
  2721. (( ${_POWERLEVEL9K_JENV_SOURCES[(I)local]} )) || return
  2722. break
  2723. fi
  2724. [[ $dir == (/|.) ]] && break
  2725. dir=${dir:h}
  2726. done
  2727. fi
  2728. fi
  2729. if [[ -z $_p9k__ret ]]; then
  2730. _p9k_upglob .java-version
  2731. local -i idx=$?
  2732. if (( idx )) && _p9k_read_word $_p9k__parent_dirs[idx]/.java-version; then
  2733. (( ${_POWERLEVEL9K_JENV_SOURCES[(I)local]} )) || return
  2734. else
  2735. _p9k__ret=
  2736. fi
  2737. fi
  2738. if [[ -z $_p9k__ret ]]; then
  2739. (( _POWERLEVEL9K_JENV_PROMPT_ALWAYS_SHOW )) || return
  2740. (( ${_POWERLEVEL9K_JENV_SOURCES[(I)global]} )) || return
  2741. _p9k_jenv_global_version
  2742. fi
  2743. local v=$_p9k__ret
  2744. fi
  2745. if (( !_POWERLEVEL9K_JENV_PROMPT_ALWAYS_SHOW )); then
  2746. _p9k_jenv_global_version
  2747. [[ $v == $_p9k__ret ]] && return
  2748. fi
  2749. if (( !_POWERLEVEL9K_JENV_SHOW_SYSTEM )); then
  2750. [[ $v == system ]] && return
  2751. fi
  2752. _p9k_prompt_segment "$0" white red 'JAVA_ICON' 0 '' "${v//\%/%%}"
  2753. }
  2754. _p9k_prompt_jenv_init() {
  2755. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[jenv]:-${${+functions[jenv]}:#0}}'
  2756. }
  2757. function _p9k_plenv_global_version() {
  2758. _p9k_read_word ${PLENV_ROOT:-$HOME/.plenv}/version || _p9k__ret=system
  2759. }
  2760. ################################################################
  2761. # Segment to display plenv information
  2762. # https://github.com/plenv/plenv#choosing-the-perl-version
  2763. prompt_plenv() {
  2764. if [[ -n $PLENV_VERSION ]]; then
  2765. (( ${_POWERLEVEL9K_PLENV_SOURCES[(I)shell]} )) || return
  2766. local v=$PLENV_VERSION
  2767. else
  2768. (( ${_POWERLEVEL9K_PLENV_SOURCES[(I)local|global]} )) || return
  2769. _p9k__ret=
  2770. if [[ $PLENV_DIR != (|.) ]]; then
  2771. [[ $PLENV_DIR == /* ]] && local dir=$PLENV_DIR || local dir="$_p9k__cwd_a/$PLENV_DIR"
  2772. dir=${dir:A}
  2773. if [[ $dir != $_p9k__cwd_a ]]; then
  2774. while true; do
  2775. if _p9k_read_word $dir/.perl-version; then
  2776. (( ${_POWERLEVEL9K_PLENV_SOURCES[(I)local]} )) || return
  2777. break
  2778. fi
  2779. [[ $dir == (/|.) ]] && break
  2780. dir=${dir:h}
  2781. done
  2782. fi
  2783. fi
  2784. if [[ -z $_p9k__ret ]]; then
  2785. _p9k_upglob .perl-version
  2786. local -i idx=$?
  2787. if (( idx )) && _p9k_read_word $_p9k__parent_dirs[idx]/.perl-version; then
  2788. (( ${_POWERLEVEL9K_PLENV_SOURCES[(I)local]} )) || return
  2789. else
  2790. _p9k__ret=
  2791. fi
  2792. fi
  2793. if [[ -z $_p9k__ret ]]; then
  2794. (( _POWERLEVEL9K_PLENV_PROMPT_ALWAYS_SHOW )) || return
  2795. (( ${_POWERLEVEL9K_PLENV_SOURCES[(I)global]} )) || return
  2796. _p9k_plenv_global_version
  2797. fi
  2798. local v=$_p9k__ret
  2799. fi
  2800. if (( !_POWERLEVEL9K_PLENV_PROMPT_ALWAYS_SHOW )); then
  2801. _p9k_plenv_global_version
  2802. [[ $v == $_p9k__ret ]] && return
  2803. fi
  2804. if (( !_POWERLEVEL9K_PLENV_SHOW_SYSTEM )); then
  2805. [[ $v == system ]] && return
  2806. fi
  2807. _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PERL_ICON' 0 '' "${v//\%/%%}"
  2808. }
  2809. _p9k_prompt_plenv_init() {
  2810. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[plenv]:-${${+functions[plenv]}:#0}}'
  2811. }
  2812. ################################################################
  2813. # Segment to display chruby information
  2814. # see https://github.com/postmodern/chruby/issues/245 for chruby_auto issue with ZSH
  2815. prompt_chruby() {
  2816. local v
  2817. (( _POWERLEVEL9K_CHRUBY_SHOW_ENGINE )) && v=$RUBY_ENGINE
  2818. if [[ $_POWERLEVEL9K_CHRUBY_SHOW_VERSION == 1 && -n $RUBY_VERSION ]] && v+=${v:+ }$RUBY_VERSION
  2819. _p9k_prompt_segment "$0" "red" "$_p9k_color1" 'RUBY_ICON' 0 '' "${v//\%/%%}"
  2820. }
  2821. _p9k_prompt_chruby_init() {
  2822. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$RUBY_ENGINE'
  2823. }
  2824. ################################################################
  2825. # Segment to print an icon if user is root.
  2826. prompt_root_indicator() {
  2827. local -i len=$#_p9k__prompt _p9k__has_upglob
  2828. _p9k_prompt_segment "$0" "$_p9k_color1" "yellow" 'ROOT_ICON' 0 '${${(%):-%#}:#\%}' ''
  2829. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  2830. }
  2831. instant_prompt_root_indicator() { prompt_root_indicator; }
  2832. ################################################################
  2833. # Segment to display Rust version number
  2834. prompt_rust_version() {
  2835. unset P9K_RUST_VERSION
  2836. if (( _POWERLEVEL9K_RUST_VERSION_PROJECT_ONLY )); then
  2837. _p9k_upglob Cargo.toml && return
  2838. fi
  2839. local rustc=$commands[rustc] toolchain deps=()
  2840. if (( $+commands[ldd] )); then
  2841. if ! _p9k_cache_stat_get $0_so $rustc; then
  2842. local line so
  2843. for line in "${(@f)$(ldd $rustc 2>/dev/null)}"; do
  2844. [[ $line == (#b)[[:space:]]#librustc_driver[^[:space:]]#.so' => '(*)' (0x'[[:xdigit:]]#')' ]] || continue
  2845. so=$match[1]
  2846. break
  2847. done
  2848. _p9k_cache_stat_set "$so"
  2849. fi
  2850. deps+=$_p9k__cache_val[1]
  2851. fi
  2852. if (( $+commands[rustup] )); then
  2853. local rustup=$commands[rustup]
  2854. local rustup_home=${RUSTUP_HOME:-~/.rustup}
  2855. local cfg=($rustup_home/settings.toml(.N))
  2856. deps+=($cfg $rustup_home/update-hashes/*(.N))
  2857. if [[ -z ${toolchain::=$RUSTUP_TOOLCHAIN} ]]; then
  2858. if ! _p9k_cache_stat_get $0_overrides $rustup $cfg; then
  2859. local lines=(${(f)"$(rustup override list 2>/dev/null)"})
  2860. if [[ $lines[1] == "no overrides" ]]; then
  2861. _p9k_cache_stat_set
  2862. else
  2863. local MATCH
  2864. local keys=(${(@)${lines%%[[:space:]]#[^[:space:]]#}/(#m)*/${(b)MATCH}/})
  2865. local vals=(${(@)lines/(#m)*/$MATCH[(I)/] ${MATCH##*[[:space:]]}})
  2866. _p9k_cache_stat_set ${keys:^vals}
  2867. fi
  2868. fi
  2869. local -A overrides=($_p9k__cache_val)
  2870. _p9k_upglob rust-toolchain
  2871. local dir=$_p9k__parent_dirs[$?]
  2872. local -i n m=${dir[(I)/]}
  2873. local pair
  2874. for pair in ${overrides[(K)$_p9k__cwd/]}; do
  2875. n=${pair%% *}
  2876. (( n <= m )) && continue
  2877. m=n
  2878. toolchain=${pair#* }
  2879. done
  2880. if [[ -z $toolchain && -n $dir ]]; then
  2881. _p9k_read_word $dir/rust-toolchain
  2882. toolchain=$_p9k__ret
  2883. fi
  2884. fi
  2885. fi
  2886. if ! _p9k_cache_stat_get $0_v$toolchain $rustc $deps; then
  2887. _p9k_cache_stat_set "$($rustc --version 2>/dev/null)"
  2888. fi
  2889. local v=${${_p9k__cache_val[1]#rustc }%% *}
  2890. [[ -n $v ]] || return
  2891. typeset -g P9K_RUST_VERSION=$_p9k__cache_val[1]
  2892. _p9k_prompt_segment "$0" "darkorange" "$_p9k_color1" 'RUST_ICON' 0 '' "${v//\%/%%}"
  2893. }
  2894. _p9k_prompt_rust_version_init() {
  2895. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[rustc]'
  2896. }
  2897. # RSpec test ratio
  2898. prompt_rspec_stats() {
  2899. if [[ -d app && -d spec ]]; then
  2900. local -a code=(app/**/*.rb(N))
  2901. (( $#code )) || return
  2902. local tests=(spec/**/*.rb(N))
  2903. _p9k_build_test_stats "$0" "$#code" "$#tests" "RSpec" 'TEST_ICON'
  2904. fi
  2905. }
  2906. ################################################################
  2907. # Segment to display Ruby Version Manager information
  2908. prompt_rvm() {
  2909. [[ $GEM_HOME == *rvm* && $ruby_string != $rvm_path/bin/ruby ]] || return
  2910. local v=${GEM_HOME:t}
  2911. (( _POWERLEVEL9K_RVM_SHOW_GEMSET )) || v=${v%%${rvm_gemset_separator:-@}*}
  2912. (( _POWERLEVEL9K_RVM_SHOW_PREFIX )) || v=${v#*-}
  2913. [[ -n $v ]] || return
  2914. _p9k_prompt_segment "$0" "240" "$_p9k_color1" 'RUBY_ICON' 0 '' "${v//\%/%%}"
  2915. }
  2916. _p9k_prompt_rvm_init() {
  2917. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[rvm-prompt]:-${${+functions[rvm-prompt]}:#0}}'
  2918. }
  2919. ################################################################
  2920. # Segment to display SSH icon when connected
  2921. prompt_ssh() {
  2922. local -i len=$#_p9k__prompt _p9k__has_upglob
  2923. _p9k_prompt_segment "$0" "$_p9k_color1" "yellow" 'SSH_ICON' 0 '' ''
  2924. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  2925. }
  2926. _p9k_prompt_ssh_init() {
  2927. if (( ! P9K_SSH )); then
  2928. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
  2929. fi
  2930. }
  2931. instant_prompt_ssh() {
  2932. if (( ! P9K_SSH )); then
  2933. return
  2934. fi
  2935. prompt_ssh
  2936. }
  2937. ################################################################
  2938. # Status: When an error occur, return the error code, or a cross icon if option is set
  2939. # Display an ok icon when no error occur, or hide the segment if option is set to false
  2940. prompt_status() {
  2941. if ! _p9k_cache_get $0 $_p9k__status $_p9k__pipestatus; then
  2942. (( _p9k__status )) && local state=ERROR || local state=OK
  2943. if (( _POWERLEVEL9K_STATUS_EXTENDED_STATES )); then
  2944. if (( _p9k__status )); then
  2945. if (( $#_p9k__pipestatus > 1 )); then
  2946. state+=_PIPE
  2947. elif (( _p9k__status > 128 )); then
  2948. state+=_SIGNAL
  2949. fi
  2950. elif [[ "$_p9k__pipestatus" == *[1-9]* ]]; then
  2951. state+=_PIPE
  2952. fi
  2953. fi
  2954. _p9k__cache_val=(:)
  2955. if (( _POWERLEVEL9K_STATUS_$state )); then
  2956. if (( _POWERLEVEL9K_STATUS_SHOW_PIPESTATUS )); then
  2957. local text=${(j:|:)${(@)_p9k__pipestatus:/(#b)(*)/$_p9k_exitcode2str[$match[1]+1]}}
  2958. else
  2959. local text=$_p9k_exitcode2str[_p9k__status+1]
  2960. fi
  2961. if (( _p9k__status )); then
  2962. if (( !_POWERLEVEL9K_STATUS_CROSS && _POWERLEVEL9K_STATUS_VERBOSE )); then
  2963. _p9k__cache_val=($0_$state red yellow1 CARRIAGE_RETURN_ICON 0 '' "$text")
  2964. else
  2965. _p9k__cache_val=($0_$state $_p9k_color1 red FAIL_ICON 0 '' '')
  2966. fi
  2967. elif (( _POWERLEVEL9K_STATUS_VERBOSE || _POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE )); then
  2968. [[ $state == OK ]] && text=''
  2969. _p9k__cache_val=($0_$state "$_p9k_color1" green OK_ICON 0 '' "$text")
  2970. fi
  2971. fi
  2972. if (( $#_p9k__pipestatus < 3 )); then
  2973. _p9k_cache_set "${(@)_p9k__cache_val}"
  2974. fi
  2975. fi
  2976. _p9k_prompt_segment "${(@)_p9k__cache_val}"
  2977. }
  2978. instant_prompt_status() {
  2979. if (( _POWERLEVEL9K_STATUS_OK )); then
  2980. _p9k_prompt_segment prompt_status_OK "$_p9k_color1" green OK_ICON 0 '' ''
  2981. fi
  2982. }
  2983. prompt_prompt_char() {
  2984. local saved=$_p9k__prompt_char_saved[$_p9k__prompt_side$_p9k__segment_index$((!_p9k__status))]
  2985. if [[ -n $saved ]]; then
  2986. _p9k__prompt+=$saved
  2987. return
  2988. fi
  2989. local -i len=$#_p9k__prompt _p9k__has_upglob
  2990. if (( __p9k_sh_glob )); then
  2991. if (( _p9k__status )); then
  2992. if (( _POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE )); then
  2993. _p9k_prompt_segment $0_ERROR_VIINS "$_p9k_color1" 196 '' 0 '${${${${${${:-$_p9k__keymap.$_p9k__zle_state}:#vicmd.*}:#vivis.*}:#vivli.*}:#*.*overwrite*}}' '❯'
  2994. _p9k_prompt_segment $0_ERROR_VIOWR "$_p9k_color1" 196 '' 0 '${${${${${${:-$_p9k__keymap.$_p9k__zle_state}:#vicmd.*}:#vivis.*}:#vivli.*}:#*.*insert*}}' '▶'
  2995. else
  2996. _p9k_prompt_segment $0_ERROR_VIINS "$_p9k_color1" 196 '' 0 '${${${${_p9k__keymap:#vicmd}:#vivis}:#vivli}}' '❯'
  2997. fi
  2998. _p9k_prompt_segment $0_ERROR_VICMD "$_p9k_color1" 196 '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#vicmd0}' '❮'
  2999. _p9k_prompt_segment $0_ERROR_VIVIS "$_p9k_color1" 196 '' 0 '${$((! ${#${${${${:-$_p9k__keymap$_p9k__region_active}:#vicmd1}:#vivis?}:#vivli?}})):#0}' 'Ⅴ'
  3000. else
  3001. if (( _POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE )); then
  3002. _p9k_prompt_segment $0_OK_VIINS "$_p9k_color1" 76 '' 0 '${${${${${${:-$_p9k__keymap.$_p9k__zle_state}:#vicmd.*}:#vivis.*}:#vivli.*}:#*.*overwrite*}}' '❯'
  3003. _p9k_prompt_segment $0_OK_VIOWR "$_p9k_color1" 76 '' 0 '${${${${${${:-$_p9k__keymap.$_p9k__zle_state}:#vicmd.*}:#vivis.*}:#vivli.*}:#*.*insert*}}' '▶'
  3004. else
  3005. _p9k_prompt_segment $0_OK_VIINS "$_p9k_color1" 76 '' 0 '${${${${_p9k__keymap:#vicmd}:#vivis}:#vivli}}' '❯'
  3006. fi
  3007. _p9k_prompt_segment $0_OK_VICMD "$_p9k_color1" 76 '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#vicmd0}' '❮'
  3008. _p9k_prompt_segment $0_OK_VIVIS "$_p9k_color1" 76 '' 0 '${$((! ${#${${${${:-$_p9k__keymap$_p9k__region_active}:#vicmd1}:#vivis?}:#vivli?}})):#0}' 'Ⅴ'
  3009. fi
  3010. else
  3011. if (( _p9k__status )); then
  3012. if (( _POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE )); then
  3013. _p9k_prompt_segment $0_ERROR_VIINS "$_p9k_color1" 196 '' 0 '${${:-$_p9k__keymap.$_p9k__zle_state}:#(vicmd.*|vivis.*|vivli.*|*.*overwrite*)}' '❯'
  3014. _p9k_prompt_segment $0_ERROR_VIOWR "$_p9k_color1" 196 '' 0 '${${:-$_p9k__keymap.$_p9k__zle_state}:#(vicmd.*|vivis.*|vivli.*|*.*insert*)}' '▶'
  3015. else
  3016. _p9k_prompt_segment $0_ERROR_VIINS "$_p9k_color1" 196 '' 0 '${_p9k__keymap:#(vicmd|vivis|vivli)}' '❯'
  3017. fi
  3018. _p9k_prompt_segment $0_ERROR_VICMD "$_p9k_color1" 196 '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#vicmd0}' '❮'
  3019. _p9k_prompt_segment $0_ERROR_VIVIS "$_p9k_color1" 196 '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#(vicmd1|vivis?|vivli?)}' 'Ⅴ'
  3020. else
  3021. if (( _POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE )); then
  3022. _p9k_prompt_segment $0_OK_VIINS "$_p9k_color1" 76 '' 0 '${${:-$_p9k__keymap.$_p9k__zle_state}:#(vicmd.*|vivis.*|vivli.*|*.*overwrite*)}' '❯'
  3023. _p9k_prompt_segment $0_OK_VIOWR "$_p9k_color1" 76 '' 0 '${${:-$_p9k__keymap.$_p9k__zle_state}:#(vicmd.*|vivis.*|vivli.*|*.*insert*)}' '▶'
  3024. else
  3025. _p9k_prompt_segment $0_OK_VIINS "$_p9k_color1" 76 '' 0 '${_p9k__keymap:#(vicmd|vivis|vivli)}' '❯'
  3026. fi
  3027. _p9k_prompt_segment $0_OK_VICMD "$_p9k_color1" 76 '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#vicmd0}' '❮'
  3028. _p9k_prompt_segment $0_OK_VIVIS "$_p9k_color1" 76 '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#(vicmd1|vivis?|vivli?)}' 'Ⅴ'
  3029. fi
  3030. fi
  3031. (( _p9k__has_upglob )) || _p9k__prompt_char_saved[$_p9k__prompt_side$_p9k__segment_index$((!_p9k__status))]=$_p9k__prompt[len+1,-1]
  3032. }
  3033. instant_prompt_prompt_char() {
  3034. _p9k_prompt_segment prompt_prompt_char_OK_VIINS "$_p9k_color1" 76 '' 0 '' '❯'
  3035. }
  3036. ################################################################
  3037. # Segment to display Swap information
  3038. prompt_swap() {
  3039. local -i len=$#_p9k__prompt _p9k__has_upglob
  3040. _p9k_prompt_segment $0 yellow "$_p9k_color1" SWAP_ICON 1 '$_p9k__swap_used' '$_p9k__swap_used'
  3041. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  3042. }
  3043. function _p9k_prompt_swap_init() {
  3044. if [[ $_p9k_os == OSX && $+commands[sysctl] == 0 || $_p9k_os != OSX && ! -r /proc/meminfo ]]; then
  3045. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
  3046. return
  3047. fi
  3048. typeset -g _p9k__swap_used=
  3049. _p9k__async_segments_compute+='_p9k_worker_invoke swap _p9k_prompt_swap_compute'
  3050. }
  3051. _p9k_prompt_swap_compute() {
  3052. _p9k_worker_async _p9k_prompt_swap_async _p9k_prompt_swap_sync
  3053. }
  3054. _p9k_prompt_swap_async() {
  3055. local -F used_bytes
  3056. if [[ "$_p9k_os" == "OSX" ]]; then
  3057. (( $+commands[sysctl] )) || return
  3058. [[ "$(sysctl vm.swapusage 2>/dev/null)" =~ "used = ([0-9,.]+)([A-Z]+)" ]] || return
  3059. used_bytes=${match[1]//,/.}
  3060. case ${match[2]} in
  3061. 'K') (( used_bytes *= 1024 ));;
  3062. 'M') (( used_bytes *= 1048576 ));;
  3063. 'G') (( used_bytes *= 1073741824 ));;
  3064. 'T') (( used_bytes *= 1099511627776 ));;
  3065. *) return 0;;
  3066. esac
  3067. else
  3068. local meminfo && meminfo="$(grep -F 'Swap' /proc/meminfo 2>/dev/null)" || return
  3069. [[ $meminfo =~ 'SwapTotal:[[:space:]]+([0-9]+)' ]] || return
  3070. (( used_bytes+=match[1] ))
  3071. [[ $meminfo =~ 'SwapFree:[[:space:]]+([0-9]+)' ]] || return
  3072. (( used_bytes-=match[1] ))
  3073. (( used_bytes *= 1024 ))
  3074. fi
  3075. (( used_bytes >= 0 || (used_bytes = 0) ))
  3076. _p9k_human_readable_bytes $used_bytes
  3077. [[ $_p9k__ret != $_p9k__swap_used ]] || return
  3078. _p9k__swap_used=$_p9k__ret
  3079. _p9k_print_params _p9k__swap_used
  3080. echo -E - 'reset=1'
  3081. }
  3082. _p9k_prompt_swap_sync() {
  3083. eval $REPLY
  3084. _p9k_worker_reply $REPLY
  3085. }
  3086. ################################################################
  3087. # Symfony2-PHPUnit test ratio
  3088. prompt_symfony2_tests() {
  3089. if [[ -d src && -d app && -f app/AppKernel.php ]]; then
  3090. local -a all=(src/**/*.php(N))
  3091. local -a code=(${(@)all##*Tests*})
  3092. (( $#code )) || return
  3093. _p9k_build_test_stats "$0" "$#code" "$(($#all - $#code))" "SF2" 'TEST_ICON'
  3094. fi
  3095. }
  3096. ################################################################
  3097. # Segment to display Symfony2-Version
  3098. prompt_symfony2_version() {
  3099. if [[ -r app/bootstrap.php.cache ]]; then
  3100. local v="${$(grep -F " VERSION " app/bootstrap.php.cache 2>/dev/null)//[![:digit:].]}"
  3101. _p9k_prompt_segment "$0" "grey35" "$_p9k_color1" 'SYMFONY_ICON' 0 '' "${v//\%/%%}"
  3102. fi
  3103. }
  3104. ################################################################
  3105. # Show a ratio of tests vs code
  3106. _p9k_build_test_stats() {
  3107. local code_amount="$2"
  3108. local tests_amount="$3"
  3109. local headline="$4"
  3110. (( code_amount > 0 )) || return
  3111. local -F 2 ratio=$(( 100. * tests_amount / code_amount ))
  3112. (( ratio >= 75 )) && _p9k_prompt_segment "${1}_GOOD" "cyan" "$_p9k_color1" "$5" 0 '' "$headline: $ratio%%"
  3113. (( ratio >= 50 && ratio < 75 )) && _p9k_prompt_segment "$1_AVG" "yellow" "$_p9k_color1" "$5" 0 '' "$headline: $ratio%%"
  3114. (( ratio < 50 )) && _p9k_prompt_segment "$1_BAD" "red" "$_p9k_color1" "$5" 0 '' "$headline: $ratio%%"
  3115. }
  3116. ################################################################
  3117. # System time
  3118. prompt_time() {
  3119. if (( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )); then
  3120. _p9k_prompt_segment "$0" "$_p9k_color2" "$_p9k_color1" "TIME_ICON" 0 '' "$_POWERLEVEL9K_TIME_FORMAT"
  3121. else
  3122. if [[ $_p9k__refresh_reason == precmd ]]; then
  3123. if [[ $+__p9k_instant_prompt_active == 1 && $__p9k_instant_prompt_time_format == $_POWERLEVEL9K_TIME_FORMAT ]]; then
  3124. _p9k__time=${__p9k_instant_prompt_time//\%/%%}
  3125. else
  3126. _p9k__time=${${(%)_POWERLEVEL9K_TIME_FORMAT}//\%/%%}
  3127. fi
  3128. fi
  3129. if (( _POWERLEVEL9K_TIME_UPDATE_ON_COMMAND )); then
  3130. _p9k_escape $_p9k__time
  3131. local t=$_p9k__ret
  3132. _p9k_escape $_POWERLEVEL9K_TIME_FORMAT
  3133. _p9k_prompt_segment "$0" "$_p9k_color2" "$_p9k_color1" "TIME_ICON" 1 '' \
  3134. "\${_p9k__line_finished-$t}\${_p9k__line_finished+$_p9k__ret}"
  3135. else
  3136. _p9k_prompt_segment "$0" "$_p9k_color2" "$_p9k_color1" "TIME_ICON" 0 '' $_p9k__time
  3137. fi
  3138. fi
  3139. }
  3140. instant_prompt_time() {
  3141. _p9k_escape $_POWERLEVEL9K_TIME_FORMAT
  3142. local stash='${${__p9k_instant_prompt_time::=${(%)${__p9k_instant_prompt_time_format::='$_p9k__ret'}}}+}'
  3143. _p9k_escape $_POWERLEVEL9K_TIME_FORMAT
  3144. _p9k_prompt_segment prompt_time "$_p9k_color2" "$_p9k_color1" "TIME_ICON" 1 '' $stash$_p9k__ret
  3145. }
  3146. _p9k_prompt_time_init() {
  3147. (( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )) || return
  3148. _p9k__async_segments_compute+='_p9k_worker_invoke time _p9k_prompt_time_compute'
  3149. }
  3150. _p9k_prompt_time_compute() {
  3151. _p9k_worker_async _p9k_prompt_time_async _p9k_prompt_time_sync
  3152. }
  3153. _p9k_prompt_time_async() {
  3154. sleep 1 || true
  3155. }
  3156. _p9k_prompt_time_sync() {
  3157. _p9k_worker_reply '_p9k_worker_invoke _p9k_prompt_time_compute _p9k_prompt_time_compute; reset=1'
  3158. }
  3159. ################################################################
  3160. # System date
  3161. prompt_date() {
  3162. if [[ $_p9k__refresh_reason == precmd ]]; then
  3163. if [[ $+__p9k_instant_prompt_active == 1 && $__p9k_instant_prompt_date_format == $_POWERLEVEL9K_DATE_FORMAT ]]; then
  3164. _p9k__date=${__p9k_instant_prompt_date//\%/%%}
  3165. else
  3166. _p9k__date=${${(%)_POWERLEVEL9K_DATE_FORMAT}//\%/%%}
  3167. fi
  3168. fi
  3169. _p9k_prompt_segment "$0" "$_p9k_color2" "$_p9k_color1" "DATE_ICON" 0 '' "$_p9k__date"
  3170. }
  3171. instant_prompt_date() {
  3172. _p9k_escape $_POWERLEVEL9K_DATE_FORMAT
  3173. local stash='${${__p9k_instant_prompt_date::=${(%)${__p9k_instant_prompt_date_format::='$_p9k__ret'}}}+}'
  3174. _p9k_escape $_POWERLEVEL9K_DATE_FORMAT
  3175. _p9k_prompt_segment prompt_date "$_p9k_color2" "$_p9k_color1" "DATE_ICON" 1 '' $stash$_p9k__ret
  3176. }
  3177. ################################################################
  3178. # todo.sh: shows the number of tasks in your todo.sh file
  3179. prompt_todo() {
  3180. unset P9K_TODO_TOTAL_TASK_COUNT P9K_TODO_FILTERED_TASK_COUNT
  3181. [[ -r $_p9k__todo_file && -x $_p9k__todo_command ]] || return
  3182. if ! _p9k_cache_stat_get $0 $_p9k__todo_file; then
  3183. local count="$($_p9k__todo_command -p ls | command tail -1)"
  3184. if [[ $count == (#b)'TODO: '([[:digit:]]##)' of '([[:digit:]]##)' '* ]]; then
  3185. _p9k_cache_stat_set 1 $match[1] $match[2]
  3186. else
  3187. _p9k_cache_stat_set 0
  3188. fi
  3189. fi
  3190. (( $_p9k__cache_val[1] )) || return
  3191. typeset -gi P9K_TODO_FILTERED_TASK_COUNT=$_p9k__cache_val[2]
  3192. typeset -gi P9K_TODO_TOTAL_TASK_COUNT=$_p9k__cache_val[3]
  3193. if (( (P9K_TODO_TOTAL_TASK_COUNT || !_POWERLEVEL9K_TODO_HIDE_ZERO_TOTAL) &&
  3194. (P9K_TODO_FILTERED_TASK_COUNT || !_POWERLEVEL9K_TODO_HIDE_ZERO_FILTERED) )); then
  3195. if (( P9K_TODO_TOTAL_TASK_COUNT == P9K_TODO_FILTERED_TASK_COUNT )); then
  3196. local text=$P9K_TODO_TOTAL_TASK_COUNT
  3197. else
  3198. local text="$P9K_TODO_FILTERED_TASK_COUNT/$P9K_TODO_TOTAL_TASK_COUNT"
  3199. fi
  3200. _p9k_prompt_segment "$0" "grey50" "$_p9k_color1" 'TODO_ICON' 0 '' "$text"
  3201. fi
  3202. }
  3203. _p9k_prompt_todo_init() {
  3204. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$_p9k__todo_file'
  3205. }
  3206. ################################################################
  3207. # VCS segment: shows the state of your repository, if you are in a folder under
  3208. # version control
  3209. # The vcs segment can have 4 different states - defaults to 'CLEAN'.
  3210. typeset -gA __p9k_vcs_states=(
  3211. 'CLEAN' '2'
  3212. 'MODIFIED' '3'
  3213. 'UNTRACKED' '2'
  3214. 'LOADING' '8'
  3215. 'CONFLICTED' '3'
  3216. )
  3217. function +vi-git-untracked() {
  3218. [[ -z "${vcs_comm[gitdir]}" || "${vcs_comm[gitdir]}" == "." ]] && return
  3219. # get the root for the current repo or submodule
  3220. local repoTopLevel="$(git rev-parse --show-toplevel 2> /dev/null)"
  3221. # dump out if we're outside a git repository (which includes being in the .git folder)
  3222. [[ $? != 0 || -z $repoTopLevel ]] && return
  3223. local untrackedFiles="$(git ls-files --others --exclude-standard "${repoTopLevel}" 2> /dev/null)"
  3224. if [[ -z $untrackedFiles && $_POWERLEVEL9K_VCS_SHOW_SUBMODULE_DIRTY == 1 ]]; then
  3225. untrackedFiles+="$(git submodule foreach --quiet --recursive 'git ls-files --others --exclude-standard' 2> /dev/null)"
  3226. fi
  3227. [[ -z $untrackedFiles ]] && return
  3228. hook_com[unstaged]+=" $(print_icon 'VCS_UNTRACKED_ICON')"
  3229. VCS_WORKDIR_HALF_DIRTY=true
  3230. }
  3231. function +vi-git-aheadbehind() {
  3232. local ahead behind
  3233. local -a gitstatus
  3234. # for git prior to 1.7
  3235. # ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l)
  3236. ahead="$(git rev-list --count "${hook_com[branch]}"@{upstream}..HEAD 2>/dev/null)"
  3237. (( ahead )) && gitstatus+=( " $(print_icon 'VCS_OUTGOING_CHANGES_ICON')${ahead// /}" )
  3238. # for git prior to 1.7
  3239. # behind=$(git rev-list HEAD..origin/${hook_com[branch]} | wc -l)
  3240. behind="$(git rev-list --count HEAD.."${hook_com[branch]}"@{upstream} 2>/dev/null)"
  3241. (( behind )) && gitstatus+=( " $(print_icon 'VCS_INCOMING_CHANGES_ICON')${behind// /}" )
  3242. hook_com[misc]+=${(j::)gitstatus}
  3243. }
  3244. function +vi-git-remotebranch() {
  3245. local remote
  3246. local branch_name="${hook_com[branch]}"
  3247. # Are we on a remote-tracking branch?
  3248. remote="$(git rev-parse --verify HEAD@{upstream} --symbolic-full-name 2>/dev/null)"
  3249. remote=${remote/refs\/(remotes|heads)\/}
  3250. if (( $+_POWERLEVEL9K_VCS_SHORTEN_LENGTH && $+_POWERLEVEL9K_VCS_SHORTEN_MIN_LENGTH )); then
  3251. if (( ${#hook_com[branch]} > _POWERLEVEL9K_VCS_SHORTEN_MIN_LENGTH && ${#hook_com[branch]} > _POWERLEVEL9K_VCS_SHORTEN_LENGTH )); then
  3252. case $_POWERLEVEL9K_VCS_SHORTEN_STRATEGY in
  3253. truncate_middle)
  3254. hook_com[branch]="${branch_name:0:$_POWERLEVEL9K_VCS_SHORTEN_LENGTH}${_POWERLEVEL9K_VCS_SHORTEN_DELIMITER}${branch_name: -$_POWERLEVEL9K_VCS_SHORTEN_LENGTH}"
  3255. ;;
  3256. truncate_from_right)
  3257. hook_com[branch]="${branch_name:0:$_POWERLEVEL9K_VCS_SHORTEN_LENGTH}${_POWERLEVEL9K_VCS_SHORTEN_DELIMITER}"
  3258. ;;
  3259. esac
  3260. fi
  3261. fi
  3262. if (( _POWERLEVEL9K_HIDE_BRANCH_ICON )); then
  3263. hook_com[branch]="${hook_com[branch]}"
  3264. else
  3265. hook_com[branch]="$(print_icon 'VCS_BRANCH_ICON')${hook_com[branch]}"
  3266. fi
  3267. # Always show the remote
  3268. #if [[ -n ${remote} ]] ; then
  3269. # Only show the remote if it differs from the local
  3270. if [[ -n ${remote} ]] && [[ "${remote#*/}" != "${branch_name}" ]] ; then
  3271. hook_com[branch]+="$(print_icon 'VCS_REMOTE_BRANCH_ICON')${remote// /}"
  3272. fi
  3273. }
  3274. function +vi-git-tagname() {
  3275. if (( !_POWERLEVEL9K_VCS_HIDE_TAGS )); then
  3276. # If we are on a tag, append the tagname to the current branch string.
  3277. local tag
  3278. tag="$(git describe --tags --exact-match HEAD 2>/dev/null)"
  3279. if [[ -n "${tag}" ]] ; then
  3280. # There is a tag that points to our current commit. Need to determine if we
  3281. # are also on a branch, or are in a DETACHED_HEAD state.
  3282. if [[ -z "$(git symbolic-ref HEAD 2>/dev/null)" ]]; then
  3283. # DETACHED_HEAD state. We want to append the tag name to the commit hash
  3284. # and print it. Unfortunately, `vcs_info` blows away the hash when a tag
  3285. # exists, so we have to manually retrieve it and clobber the branch
  3286. # string.
  3287. local revision
  3288. revision="$(git rev-list -n 1 --abbrev-commit --abbrev=${_POWERLEVEL9K_CHANGESET_HASH_LENGTH} HEAD)"
  3289. if (( _POWERLEVEL9K_HIDE_BRANCH_ICON )); then
  3290. hook_com[branch]="${revision} $(print_icon 'VCS_TAG_ICON')${tag}"
  3291. else
  3292. hook_com[branch]="$(print_icon 'VCS_BRANCH_ICON')${revision} $(print_icon 'VCS_TAG_ICON')${tag}"
  3293. fi
  3294. else
  3295. # We are on both a tag and a branch; print both by appending the tag name.
  3296. hook_com[branch]+=" $(print_icon 'VCS_TAG_ICON')${tag}"
  3297. fi
  3298. fi
  3299. fi
  3300. }
  3301. # Show count of stashed changes
  3302. # Port from https://github.com/whiteinge/dotfiles/blob/5dfd08d30f7f2749cfc60bc55564c6ea239624d9/.zsh_shouse_prompt#L268
  3303. function +vi-git-stash() {
  3304. if [[ -s "${vcs_comm[gitdir]}/logs/refs/stash" ]] ; then
  3305. local -a stashes=( "${(@f)"$(<${vcs_comm[gitdir]}/logs/refs/stash)"}" )
  3306. hook_com[misc]+=" $(print_icon 'VCS_STASH_ICON')${#stashes}"
  3307. fi
  3308. }
  3309. function +vi-hg-bookmarks() {
  3310. if [[ -n "${hgbmarks[@]}" ]]; then
  3311. hook_com[hg-bookmark-string]=" $(print_icon 'VCS_BOOKMARK_ICON')${hgbmarks[@]}"
  3312. # To signal that we want to use the sting we just generated, set the special
  3313. # variable `ret' to something other than the default zero:
  3314. ret=1
  3315. return 0
  3316. fi
  3317. }
  3318. function +vi-vcs-detect-changes() {
  3319. if [[ "${hook_com[vcs]}" == "git" ]]; then
  3320. local remote="$(git ls-remote --get-url 2> /dev/null)"
  3321. if [[ "$remote" =~ "github" ]] then
  3322. vcs_visual_identifier='VCS_GIT_GITHUB_ICON'
  3323. elif [[ "$remote" =~ "bitbucket" ]] then
  3324. vcs_visual_identifier='VCS_GIT_BITBUCKET_ICON'
  3325. elif [[ "$remote" =~ "stash" ]] then
  3326. vcs_visual_identifier='VCS_GIT_BITBUCKET_ICON'
  3327. elif [[ "$remote" =~ "gitlab" ]] then
  3328. vcs_visual_identifier='VCS_GIT_GITLAB_ICON'
  3329. else
  3330. vcs_visual_identifier='VCS_GIT_ICON'
  3331. fi
  3332. elif [[ "${hook_com[vcs]}" == "hg" ]]; then
  3333. vcs_visual_identifier='VCS_HG_ICON'
  3334. elif [[ "${hook_com[vcs]}" == "svn" ]]; then
  3335. vcs_visual_identifier='VCS_SVN_ICON'
  3336. fi
  3337. if [[ -n "${hook_com[staged]}" ]] || [[ -n "${hook_com[unstaged]}" ]]; then
  3338. VCS_WORKDIR_DIRTY=true
  3339. else
  3340. VCS_WORKDIR_DIRTY=false
  3341. fi
  3342. }
  3343. function +vi-svn-detect-changes() {
  3344. local svn_status="$(svn status)"
  3345. if [[ -n "$(echo "$svn_status" | \grep \^\?)" ]]; then
  3346. hook_com[unstaged]+=" $(print_icon 'VCS_UNTRACKED_ICON')"
  3347. VCS_WORKDIR_HALF_DIRTY=true
  3348. fi
  3349. if [[ -n "$(echo "$svn_status" | \grep \^\M)" ]]; then
  3350. hook_com[unstaged]+=" $(print_icon 'VCS_UNSTAGED_ICON')"
  3351. VCS_WORKDIR_DIRTY=true
  3352. fi
  3353. if [[ -n "$(echo "$svn_status" | \grep \^\A)" ]]; then
  3354. hook_com[staged]+=" $(print_icon 'VCS_STAGED_ICON')"
  3355. VCS_WORKDIR_DIRTY=true
  3356. fi
  3357. }
  3358. _p9k_vcs_info_init() {
  3359. autoload -Uz vcs_info
  3360. local prefix=''
  3361. if (( _POWERLEVEL9K_SHOW_CHANGESET )); then
  3362. _p9k_get_icon '' VCS_COMMIT_ICON
  3363. prefix="$_p9k__ret%0.${_POWERLEVEL9K_CHANGESET_HASH_LENGTH}i "
  3364. fi
  3365. zstyle ':vcs_info:*' check-for-changes true
  3366. zstyle ':vcs_info:*' formats "$prefix%b%c%u%m"
  3367. zstyle ':vcs_info:*' actionformats "%b %F{$_POWERLEVEL9K_VCS_ACTIONFORMAT_FOREGROUND}| %a%f"
  3368. _p9k_get_icon '' VCS_STAGED_ICON
  3369. zstyle ':vcs_info:*' stagedstr " $_p9k__ret"
  3370. _p9k_get_icon '' VCS_UNSTAGED_ICON
  3371. zstyle ':vcs_info:*' unstagedstr " $_p9k__ret"
  3372. zstyle ':vcs_info:git*+set-message:*' hooks $_POWERLEVEL9K_VCS_GIT_HOOKS
  3373. zstyle ':vcs_info:hg*+set-message:*' hooks $_POWERLEVEL9K_VCS_HG_HOOKS
  3374. zstyle ':vcs_info:svn*+set-message:*' hooks $_POWERLEVEL9K_VCS_SVN_HOOKS
  3375. # For Hg, only show the branch name
  3376. if (( _POWERLEVEL9K_HIDE_BRANCH_ICON )); then
  3377. zstyle ':vcs_info:hg*:*' branchformat "%b"
  3378. else
  3379. _p9k_get_icon '' VCS_BRANCH_ICON
  3380. zstyle ':vcs_info:hg*:*' branchformat "$_p9k__ret%b"
  3381. fi
  3382. # The `get-revision` function must be turned on for dirty-check to work for Hg
  3383. zstyle ':vcs_info:hg*:*' get-revision true
  3384. zstyle ':vcs_info:hg*:*' get-bookmarks true
  3385. zstyle ':vcs_info:hg*+gen-hg-bookmark-string:*' hooks hg-bookmarks
  3386. # TODO: fix the %b (branch) format for svn. Using %b breaks color-encoding of the foreground
  3387. # for the rest of the powerline.
  3388. zstyle ':vcs_info:svn*:*' formats "$prefix%c%u"
  3389. zstyle ':vcs_info:svn*:*' actionformats "$prefix%c%u %F{$_POWERLEVEL9K_VCS_ACTIONFORMAT_FOREGROUND}| %a%f"
  3390. if (( _POWERLEVEL9K_SHOW_CHANGESET )); then
  3391. zstyle ':vcs_info:*' get-revision true
  3392. else
  3393. zstyle ':vcs_info:*' get-revision false
  3394. fi
  3395. }
  3396. function _p9k_vcs_status_save() {
  3397. local z=$'\0'
  3398. _p9k__gitstatus_last[${${_p9k__git_dir:+GIT_DIR:$_p9k__git_dir}:-$VCS_STATUS_WORKDIR}]=\
  3399. $VCS_STATUS_COMMIT$z$VCS_STATUS_LOCAL_BRANCH$z$VCS_STATUS_REMOTE_BRANCH$z$VCS_STATUS_REMOTE_NAME$z\
  3400. $VCS_STATUS_REMOTE_URL$z$VCS_STATUS_ACTION$z$VCS_STATUS_INDEX_SIZE$z$VCS_STATUS_NUM_STAGED$z\
  3401. $VCS_STATUS_NUM_UNSTAGED$z$VCS_STATUS_NUM_CONFLICTED$z$VCS_STATUS_NUM_UNTRACKED$z\
  3402. $VCS_STATUS_HAS_STAGED$z$VCS_STATUS_HAS_UNSTAGED$z$VCS_STATUS_HAS_CONFLICTED$z\
  3403. $VCS_STATUS_HAS_UNTRACKED$z$VCS_STATUS_COMMITS_AHEAD$z$VCS_STATUS_COMMITS_BEHIND$z\
  3404. $VCS_STATUS_STASHES$z$VCS_STATUS_TAG$z$VCS_STATUS_NUM_UNSTAGED_DELETED$z\
  3405. $VCS_STATUS_NUM_STAGED_NEW$z$VCS_STATUS_NUM_STAGED_DELETED$z$VCS_STATUS_PUSH_REMOTE_NAME$z\
  3406. $VCS_STATUS_PUSH_REMOTE_URL$z$VCS_STATUS_PUSH_COMMITS_AHEAD$z$VCS_STATUS_PUSH_COMMITS_BEHIND$z\
  3407. $VCS_STATUS_NUM_SKIP_WORKTREE$z$VCS_STATUS_NUM_ASSUME_UNCHANGED
  3408. }
  3409. function _p9k_vcs_status_restore() {
  3410. for VCS_STATUS_COMMIT VCS_STATUS_LOCAL_BRANCH VCS_STATUS_REMOTE_BRANCH VCS_STATUS_REMOTE_NAME \
  3411. VCS_STATUS_REMOTE_URL VCS_STATUS_ACTION VCS_STATUS_INDEX_SIZE VCS_STATUS_NUM_STAGED \
  3412. VCS_STATUS_NUM_UNSTAGED VCS_STATUS_NUM_CONFLICTED VCS_STATUS_NUM_UNTRACKED \
  3413. VCS_STATUS_HAS_STAGED VCS_STATUS_HAS_UNSTAGED VCS_STATUS_HAS_CONFLICTED \
  3414. VCS_STATUS_HAS_UNTRACKED VCS_STATUS_COMMITS_AHEAD VCS_STATUS_COMMITS_BEHIND \
  3415. VCS_STATUS_STASHES VCS_STATUS_TAG VCS_STATUS_NUM_UNSTAGED_DELETED VCS_STATUS_NUM_STAGED_NEW \
  3416. VCS_STATUS_NUM_STAGED_DELETED VCS_STATUS_PUSH_REMOTE_NAME VCS_STATUS_PUSH_REMOTE_URL \
  3417. VCS_STATUS_PUSH_COMMITS_AHEAD VCS_STATUS_PUSH_COMMITS_BEHIND VCS_STATUS_NUM_SKIP_WORKTREE \
  3418. VCS_STATUS_NUM_ASSUME_UNCHANGED
  3419. in "${(@0)1}"; do done
  3420. }
  3421. function _p9k_vcs_status_for_dir() {
  3422. if [[ -n $GIT_DIR ]]; then
  3423. _p9k__ret=$_p9k__gitstatus_last[GIT_DIR:$GIT_DIR]
  3424. [[ -n $_p9k__ret ]]
  3425. else
  3426. local dir=$_p9k__cwd_a
  3427. while true; do
  3428. _p9k__ret=$_p9k__gitstatus_last[$dir]
  3429. [[ -n $_p9k__ret ]] && return 0
  3430. [[ $dir == (/|.) ]] && return 1
  3431. dir=${dir:h}
  3432. done
  3433. fi
  3434. }
  3435. function _p9k_vcs_status_purge() {
  3436. if [[ -n $_p9k__git_dir ]]; then
  3437. _p9k__gitstatus_last[GIT_DIR:$_p9k__git_dir]=""
  3438. else
  3439. local dir=$1
  3440. while true; do
  3441. # unset doesn't work if $dir contains weird shit
  3442. _p9k__gitstatus_last[$dir]=""
  3443. _p9k_git_slow[$dir]=""
  3444. [[ $dir == (/|.) ]] && break
  3445. dir=${dir:h}
  3446. done
  3447. fi
  3448. }
  3449. function _p9k_vcs_icon() {
  3450. case "$VCS_STATUS_REMOTE_URL" in
  3451. *github*) _p9k__ret=VCS_GIT_GITHUB_ICON;;
  3452. *bitbucket*) _p9k__ret=VCS_GIT_BITBUCKET_ICON;;
  3453. *stash*) _p9k__ret=VCS_GIT_BITBUCKET_ICON;;
  3454. *gitlab*) _p9k__ret=VCS_GIT_GITLAB_ICON;;
  3455. *) _p9k__ret=VCS_GIT_ICON;;
  3456. esac
  3457. }
  3458. function _p9k_vcs_render() {
  3459. local state
  3460. if (( $+_p9k__gitstatus_next_dir )); then
  3461. if _p9k_vcs_status_for_dir; then
  3462. _p9k_vcs_status_restore $_p9k__ret
  3463. state=LOADING
  3464. else
  3465. _p9k_prompt_segment prompt_vcs_LOADING "${__p9k_vcs_states[LOADING]}" "$_p9k_color1" VCS_LOADING_ICON 0 '' "$_POWERLEVEL9K_VCS_LOADING_TEXT"
  3466. return 0
  3467. fi
  3468. elif [[ $VCS_STATUS_RESULT != ok-* ]]; then
  3469. return 1
  3470. fi
  3471. if (( _POWERLEVEL9K_VCS_DISABLE_GITSTATUS_FORMATTING )); then
  3472. if [[ -z $state ]]; then
  3473. if [[ $VCS_STATUS_HAS_CONFLICTED == 1 && $_POWERLEVEL9K_VCS_CONFLICTED_STATE == 1 ]]; then
  3474. state=CONFLICTED
  3475. elif [[ $VCS_STATUS_HAS_STAGED != 0 || $VCS_STATUS_HAS_UNSTAGED != 0 ]]; then
  3476. state=MODIFIED
  3477. elif [[ $VCS_STATUS_HAS_UNTRACKED != 0 ]]; then
  3478. state=UNTRACKED
  3479. else
  3480. state=CLEAN
  3481. fi
  3482. fi
  3483. _p9k_vcs_icon
  3484. _p9k_prompt_segment prompt_vcs_$state "${__p9k_vcs_states[$state]}" "$_p9k_color1" "$_p9k__ret" 0 '' ""
  3485. return 0
  3486. fi
  3487. (( ${_POWERLEVEL9K_VCS_GIT_HOOKS[(I)git-untracked]} )) || VCS_STATUS_HAS_UNTRACKED=0
  3488. (( ${_POWERLEVEL9K_VCS_GIT_HOOKS[(I)git-aheadbehind]} )) || { VCS_STATUS_COMMITS_AHEAD=0 && VCS_STATUS_COMMITS_BEHIND=0 }
  3489. (( ${_POWERLEVEL9K_VCS_GIT_HOOKS[(I)git-stash]} )) || VCS_STATUS_STASHES=0
  3490. (( ${_POWERLEVEL9K_VCS_GIT_HOOKS[(I)git-remotebranch]} )) || VCS_STATUS_REMOTE_BRANCH=""
  3491. (( ${_POWERLEVEL9K_VCS_GIT_HOOKS[(I)git-tagname]} )) || VCS_STATUS_TAG=""
  3492. (( _POWERLEVEL9K_VCS_COMMITS_AHEAD_MAX_NUM >= 0 && VCS_STATUS_COMMITS_AHEAD > _POWERLEVEL9K_VCS_COMMITS_AHEAD_MAX_NUM )) &&
  3493. VCS_STATUS_COMMITS_AHEAD=$_POWERLEVEL9K_VCS_COMMITS_AHEAD_MAX_NUM
  3494. (( _POWERLEVEL9K_VCS_COMMITS_BEHIND_MAX_NUM >= 0 && VCS_STATUS_COMMITS_BEHIND > _POWERLEVEL9K_VCS_COMMITS_BEHIND_MAX_NUM )) &&
  3495. VCS_STATUS_COMMITS_BEHIND=$_POWERLEVEL9K_VCS_COMMITS_BEHIND_MAX_NUM
  3496. local -a cache_key=(
  3497. "$VCS_STATUS_LOCAL_BRANCH"
  3498. "$VCS_STATUS_REMOTE_BRANCH"
  3499. "$VCS_STATUS_REMOTE_URL"
  3500. "$VCS_STATUS_ACTION"
  3501. "$VCS_STATUS_NUM_STAGED"
  3502. "$VCS_STATUS_NUM_UNSTAGED"
  3503. "$VCS_STATUS_NUM_UNTRACKED"
  3504. "$VCS_STATUS_HAS_CONFLICTED"
  3505. "$VCS_STATUS_HAS_STAGED"
  3506. "$VCS_STATUS_HAS_UNSTAGED"
  3507. "$VCS_STATUS_HAS_UNTRACKED"
  3508. "$VCS_STATUS_COMMITS_AHEAD"
  3509. "$VCS_STATUS_COMMITS_BEHIND"
  3510. "$VCS_STATUS_STASHES"
  3511. "$VCS_STATUS_TAG"
  3512. "$VCS_STATUS_NUM_UNSTAGED_DELETED"
  3513. )
  3514. if [[ $_POWERLEVEL9K_SHOW_CHANGESET == 1 || -z $VCS_STATUS_LOCAL_BRANCH ]]; then
  3515. cache_key+=$VCS_STATUS_COMMIT
  3516. fi
  3517. if ! _p9k_cache_ephemeral_get "$state" "${(@)cache_key}"; then
  3518. local icon
  3519. local content
  3520. if (( ${_POWERLEVEL9K_VCS_GIT_HOOKS[(I)vcs-detect-changes]} )); then
  3521. if [[ $VCS_STATUS_HAS_CONFLICTED == 1 && $_POWERLEVEL9K_VCS_CONFLICTED_STATE == 1 ]]; then
  3522. : ${state:=CONFLICTED}
  3523. elif [[ $VCS_STATUS_HAS_STAGED != 0 || $VCS_STATUS_HAS_UNSTAGED != 0 ]]; then
  3524. : ${state:=MODIFIED}
  3525. elif [[ $VCS_STATUS_HAS_UNTRACKED != 0 ]]; then
  3526. : ${state:=UNTRACKED}
  3527. fi
  3528. # It's weird that removing vcs-detect-changes from POWERLEVEL9K_VCS_GIT_HOOKS gets rid
  3529. # of the GIT icon. That's what vcs_info does, so we do the same in the name of compatiblity.
  3530. _p9k_vcs_icon
  3531. icon=$_p9k__ret
  3532. fi
  3533. : ${state:=CLEAN}
  3534. function _$0_fmt() {
  3535. _p9k_vcs_style $state $1
  3536. content+="$_p9k__ret$2"
  3537. }
  3538. local ws
  3539. if [[ $_POWERLEVEL9K_SHOW_CHANGESET == 1 || -z $VCS_STATUS_LOCAL_BRANCH ]]; then
  3540. _p9k_get_icon prompt_vcs_$state VCS_COMMIT_ICON
  3541. _$0_fmt COMMIT "$_p9k__ret${${VCS_STATUS_COMMIT:0:$_POWERLEVEL9K_CHANGESET_HASH_LENGTH}:-HEAD}"
  3542. ws=' '
  3543. fi
  3544. if [[ -n $VCS_STATUS_LOCAL_BRANCH ]]; then
  3545. local branch=$ws
  3546. if (( !_POWERLEVEL9K_HIDE_BRANCH_ICON )); then
  3547. _p9k_get_icon prompt_vcs_$state VCS_BRANCH_ICON
  3548. branch+=$_p9k__ret
  3549. fi
  3550. if (( $+_POWERLEVEL9K_VCS_SHORTEN_LENGTH && $+_POWERLEVEL9K_VCS_SHORTEN_MIN_LENGTH &&
  3551. $#VCS_STATUS_LOCAL_BRANCH > _POWERLEVEL9K_VCS_SHORTEN_MIN_LENGTH &&
  3552. $#VCS_STATUS_LOCAL_BRANCH > _POWERLEVEL9K_VCS_SHORTEN_LENGTH )) &&
  3553. [[ $_POWERLEVEL9K_VCS_SHORTEN_STRATEGY == (truncate_middle|truncate_from_right) ]]; then
  3554. branch+=${VCS_STATUS_LOCAL_BRANCH[1,_POWERLEVEL9K_VCS_SHORTEN_LENGTH]//\%/%%}${_POWERLEVEL9K_VCS_SHORTEN_DELIMITER}
  3555. if [[ $_POWERLEVEL9K_VCS_SHORTEN_STRATEGY == truncate_middle ]]; then
  3556. _p9k_vcs_style $state BRANCH
  3557. branch+=${_p9k__ret}${VCS_STATUS_LOCAL_BRANCH[-_POWERLEVEL9K_VCS_SHORTEN_LENGTH,-1]//\%/%%}
  3558. fi
  3559. else
  3560. branch+=${VCS_STATUS_LOCAL_BRANCH//\%/%%}
  3561. fi
  3562. _$0_fmt BRANCH $branch
  3563. fi
  3564. if [[ $_POWERLEVEL9K_VCS_HIDE_TAGS == 0 && -n $VCS_STATUS_TAG ]]; then
  3565. _p9k_get_icon prompt_vcs_$state VCS_TAG_ICON
  3566. _$0_fmt TAG " $_p9k__ret${VCS_STATUS_TAG//\%/%%}"
  3567. fi
  3568. if [[ -n $VCS_STATUS_ACTION ]]; then
  3569. _$0_fmt ACTION " | ${VCS_STATUS_ACTION//\%/%%}"
  3570. else
  3571. if [[ -n $VCS_STATUS_REMOTE_BRANCH &&
  3572. $VCS_STATUS_LOCAL_BRANCH != $VCS_STATUS_REMOTE_BRANCH ]]; then
  3573. _p9k_get_icon prompt_vcs_$state VCS_REMOTE_BRANCH_ICON
  3574. _$0_fmt REMOTE_BRANCH " $_p9k__ret${VCS_STATUS_REMOTE_BRANCH//\%/%%}"
  3575. fi
  3576. if [[ $VCS_STATUS_HAS_STAGED == 1 || $VCS_STATUS_HAS_UNSTAGED == 1 || $VCS_STATUS_HAS_UNTRACKED == 1 ]]; then
  3577. _p9k_get_icon prompt_vcs_$state VCS_DIRTY_ICON
  3578. _$0_fmt DIRTY "$_p9k__ret"
  3579. if [[ $VCS_STATUS_HAS_STAGED == 1 ]]; then
  3580. _p9k_get_icon prompt_vcs_$state VCS_STAGED_ICON
  3581. (( _POWERLEVEL9K_VCS_STAGED_MAX_NUM != 1 )) && _p9k__ret+=$VCS_STATUS_NUM_STAGED
  3582. _$0_fmt STAGED " $_p9k__ret"
  3583. fi
  3584. if [[ $VCS_STATUS_HAS_UNSTAGED == 1 ]]; then
  3585. _p9k_get_icon prompt_vcs_$state VCS_UNSTAGED_ICON
  3586. (( _POWERLEVEL9K_VCS_UNSTAGED_MAX_NUM != 1 )) && _p9k__ret+=$VCS_STATUS_NUM_UNSTAGED
  3587. _$0_fmt UNSTAGED " $_p9k__ret"
  3588. fi
  3589. if [[ $VCS_STATUS_HAS_UNTRACKED == 1 ]]; then
  3590. _p9k_get_icon prompt_vcs_$state VCS_UNTRACKED_ICON
  3591. (( _POWERLEVEL9K_VCS_UNTRACKED_MAX_NUM != 1 )) && _p9k__ret+=$VCS_STATUS_NUM_UNTRACKED
  3592. _$0_fmt UNTRACKED " $_p9k__ret"
  3593. fi
  3594. fi
  3595. if [[ $VCS_STATUS_COMMITS_BEHIND -gt 0 ]]; then
  3596. _p9k_get_icon prompt_vcs_$state VCS_INCOMING_CHANGES_ICON
  3597. (( _POWERLEVEL9K_VCS_COMMITS_BEHIND_MAX_NUM != 1 )) && _p9k__ret+=$VCS_STATUS_COMMITS_BEHIND
  3598. _$0_fmt INCOMING_CHANGES " $_p9k__ret"
  3599. fi
  3600. if [[ $VCS_STATUS_COMMITS_AHEAD -gt 0 ]]; then
  3601. _p9k_get_icon prompt_vcs_$state VCS_OUTGOING_CHANGES_ICON
  3602. (( _POWERLEVEL9K_VCS_COMMITS_AHEAD_MAX_NUM != 1 )) && _p9k__ret+=$VCS_STATUS_COMMITS_AHEAD
  3603. _$0_fmt OUTGOING_CHANGES " $_p9k__ret"
  3604. fi
  3605. if [[ $VCS_STATUS_STASHES -gt 0 ]]; then
  3606. _p9k_get_icon prompt_vcs_$state VCS_STASH_ICON
  3607. _$0_fmt STASH " $_p9k__ret$VCS_STATUS_STASHES"
  3608. fi
  3609. fi
  3610. _p9k_cache_ephemeral_set "prompt_vcs_$state" "${__p9k_vcs_states[$state]}" "$_p9k_color1" "$icon" 0 '' "$content"
  3611. fi
  3612. _p9k_prompt_segment "$_p9k__cache_val[@]"
  3613. return 0
  3614. }
  3615. function _p9k_maybe_ignore_git_repo() {
  3616. if [[ $VCS_STATUS_RESULT == ok-* && $VCS_STATUS_WORKDIR == $~_POWERLEVEL9K_VCS_DISABLED_WORKDIR_PATTERN ]]; then
  3617. VCS_STATUS_RESULT=norepo${VCS_STATUS_RESULT#ok}
  3618. fi
  3619. }
  3620. function _p9k_vcs_resume() {
  3621. eval "$__p9k_intro"
  3622. _p9k_maybe_ignore_git_repo
  3623. if [[ $VCS_STATUS_RESULT == ok-async ]]; then
  3624. local latency=$((EPOCHREALTIME - _p9k__gitstatus_start_time))
  3625. if (( latency > _POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS )); then
  3626. _p9k_git_slow[${${_p9k__git_dir:+GIT_DIR:$_p9k__git_dir}:-$VCS_STATUS_WORKDIR}]=1
  3627. elif (( $1 && latency < 0.8 * _POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS )); then # 0.8 to avoid flip-flopping
  3628. _p9k_git_slow[${${_p9k__git_dir:+GIT_DIR:$_p9k__git_dir}:-$VCS_STATUS_WORKDIR}]=0
  3629. fi
  3630. _p9k_vcs_status_save
  3631. fi
  3632. if [[ -z $_p9k__gitstatus_next_dir ]]; then
  3633. unset _p9k__gitstatus_next_dir
  3634. case $VCS_STATUS_RESULT in
  3635. norepo-async) (( $1 )) && _p9k_vcs_status_purge $_p9k__cwd_a;;
  3636. ok-async) (( $1 )) || _p9k__gitstatus_next_dir=$_p9k__cwd_a;;
  3637. esac
  3638. fi
  3639. if [[ -n $_p9k__gitstatus_next_dir ]]; then
  3640. _p9k__git_dir=$GIT_DIR
  3641. if ! gitstatus_query_p9k_ -d $_p9k__gitstatus_next_dir -t 0 -c '_p9k_vcs_resume 1' POWERLEVEL9K; then
  3642. unset _p9k__gitstatus_next_dir
  3643. unset VCS_STATUS_RESULT
  3644. else
  3645. _p9k_maybe_ignore_git_repo
  3646. case $VCS_STATUS_RESULT in
  3647. tout) _p9k__gitstatus_next_dir=''; _p9k__gitstatus_start_time=$EPOCHREALTIME;;
  3648. norepo-sync) _p9k_vcs_status_purge $_p9k__gitstatus_next_dir; unset _p9k__gitstatus_next_dir;;
  3649. ok-sync) _p9k_vcs_status_save; unset _p9k__gitstatus_next_dir;;
  3650. esac
  3651. fi
  3652. fi
  3653. if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then
  3654. local _p9k__prompt _p9k__prompt_side=$_p9k_vcs_side _p9k__segment_name=vcs
  3655. local -i _p9k__has_upglob _p9k__segment_index=_p9k_vcs_index _p9k__line_index=_p9k_vcs_line_index
  3656. _p9k_vcs_render
  3657. typeset -g _p9k__vcs=$_p9k__prompt
  3658. else
  3659. _p9k__refresh_reason=gitstatus
  3660. _p9k_set_prompt
  3661. _p9k__refresh_reason=''
  3662. fi
  3663. _p9k_reset_prompt
  3664. }
  3665. function _p9k_vcs_gitstatus() {
  3666. if [[ $_p9k__refresh_reason == precmd ]] && (( !_p9k__vcs_called )); then
  3667. typeset -gi _p9k__vcs_called=1
  3668. if (( $+_p9k__gitstatus_next_dir )); then
  3669. _p9k__gitstatus_next_dir=$_p9k__cwd_a
  3670. else
  3671. local -F timeout=_POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS
  3672. if ! _p9k_vcs_status_for_dir; then
  3673. _p9k__git_dir=$GIT_DIR
  3674. gitstatus_query_p9k_ -d $_p9k__cwd_a -t $timeout -p -c '_p9k_vcs_resume 0' POWERLEVEL9K || return 1
  3675. _p9k_maybe_ignore_git_repo
  3676. case $VCS_STATUS_RESULT in
  3677. tout) _p9k__gitstatus_next_dir=''; _p9k__gitstatus_start_time=$EPOCHREALTIME; return 0;;
  3678. norepo-sync) return 0;;
  3679. ok-sync) _p9k_vcs_status_save;;
  3680. esac
  3681. else
  3682. if [[ -n $GIT_DIR ]]; then
  3683. [[ $_p9k_git_slow[GIT_DIR:$GIT_DIR] == 1 ]] && timeout=0
  3684. else
  3685. local dir=$_p9k__cwd_a
  3686. while true; do
  3687. case $_p9k_git_slow[$dir] in
  3688. "") [[ $dir == (/|.) ]] && break; dir=${dir:h};;
  3689. 0) break;;
  3690. 1) timeout=0; break;;
  3691. esac
  3692. done
  3693. fi
  3694. fi
  3695. (( _p9k__prompt_idx == 1 )) && timeout=0
  3696. _p9k__git_dir=$GIT_DIR
  3697. if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then
  3698. if ! gitstatus_query_p9k_ -d $_p9k__cwd_a -t 0 -c '_p9k_vcs_resume 1' POWERLEVEL9K; then
  3699. unset VCS_STATUS_RESULT
  3700. return 1
  3701. fi
  3702. typeset -gF _p9k__vcs_timeout=timeout
  3703. _p9k__gitstatus_next_dir=''
  3704. _p9k__gitstatus_start_time=$EPOCHREALTIME
  3705. return 0
  3706. fi
  3707. if ! gitstatus_query_p9k_ -d $_p9k__cwd_a -t $timeout -c '_p9k_vcs_resume 1' POWERLEVEL9K; then
  3708. unset VCS_STATUS_RESULT
  3709. return 1
  3710. fi
  3711. _p9k_maybe_ignore_git_repo
  3712. case $VCS_STATUS_RESULT in
  3713. tout) _p9k__gitstatus_next_dir=''; _p9k__gitstatus_start_time=$EPOCHREALTIME;;
  3714. norepo-sync) _p9k_vcs_status_purge $_p9k__cwd_a;;
  3715. ok-sync) _p9k_vcs_status_save;;
  3716. esac
  3717. fi
  3718. fi
  3719. return 0
  3720. }
  3721. ################################################################
  3722. # Segment to show VCS information
  3723. prompt_vcs() {
  3724. if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then
  3725. _p9k__prompt+='${(e)_p9k__vcs}'
  3726. return
  3727. fi
  3728. local -a backends=($_POWERLEVEL9K_VCS_BACKENDS)
  3729. if (( ${backends[(I)git]} && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )) && _p9k_vcs_gitstatus; then
  3730. _p9k_vcs_render && return
  3731. backends=(${backends:#git})
  3732. fi
  3733. if (( $#backends )); then
  3734. VCS_WORKDIR_DIRTY=false
  3735. VCS_WORKDIR_HALF_DIRTY=false
  3736. local current_state=""
  3737. # Actually invoke vcs_info manually to gather all information.
  3738. zstyle ':vcs_info:*' enable ${backends}
  3739. vcs_info
  3740. local vcs_prompt="${vcs_info_msg_0_}"
  3741. if [[ -n "$vcs_prompt" ]]; then
  3742. if [[ "$VCS_WORKDIR_DIRTY" == true ]]; then
  3743. # $vcs_visual_identifier gets set in +vi-vcs-detect-changes in functions/vcs.zsh,
  3744. # as we have there access to vcs_info internal hooks.
  3745. current_state='MODIFIED'
  3746. else
  3747. if [[ "$VCS_WORKDIR_HALF_DIRTY" == true ]]; then
  3748. current_state='UNTRACKED'
  3749. else
  3750. current_state='CLEAN'
  3751. fi
  3752. fi
  3753. _p9k_prompt_segment "${0}_${${(U)current_state}//İ/I}" "${__p9k_vcs_states[$current_state]}" "$_p9k_color1" "$vcs_visual_identifier" 0 '' "$vcs_prompt"
  3754. fi
  3755. fi
  3756. }
  3757. ################################################################
  3758. # Vi Mode: show editing mode (NORMAL|INSERT|VISUAL)
  3759. prompt_vi_mode() {
  3760. local -i len=$#_p9k__prompt _p9k__has_upglob
  3761. if (( __p9k_sh_glob )); then
  3762. if (( $+_POWERLEVEL9K_VI_OVERWRITE_MODE_STRING )); then
  3763. if [[ -n $_POWERLEVEL9K_VI_INSERT_MODE_STRING ]]; then
  3764. _p9k_prompt_segment $0_INSERT "$_p9k_color1" blue '' 0 '${${${${${${:-$_p9k__keymap.$_p9k__zle_state}:#vicmd.*}:#vivis.*}:#vivli.*}:#*.*overwrite*}}' "$_POWERLEVEL9K_VI_INSERT_MODE_STRING"
  3765. fi
  3766. _p9k_prompt_segment $0_OVERWRITE "$_p9k_color1" blue '' 0 '${${${${${${:-$_p9k__keymap.$_p9k__zle_state}:#vicmd.*}:#vivis.*}:#vivli.*}:#*.*insert*}}' "$_POWERLEVEL9K_VI_OVERWRITE_MODE_STRING"
  3767. else
  3768. if [[ -n $_POWERLEVEL9K_VI_INSERT_MODE_STRING ]]; then
  3769. _p9k_prompt_segment $0_INSERT "$_p9k_color1" blue '' 0 '${${${${_p9k__keymap:#vicmd}:#vivis}:#vivli}}' "$_POWERLEVEL9K_VI_INSERT_MODE_STRING"
  3770. fi
  3771. fi
  3772. if (( $+_POWERLEVEL9K_VI_VISUAL_MODE_STRING )); then
  3773. _p9k_prompt_segment $0_NORMAL "$_p9k_color1" white '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#vicmd0}' "$_POWERLEVEL9K_VI_COMMAND_MODE_STRING"
  3774. _p9k_prompt_segment $0_VISUAL "$_p9k_color1" white '' 0 '${$((! ${#${${${${:-$_p9k__keymap$_p9k__region_active}:#vicmd1}:#vivis?}:#vivli?}})):#0}' "$_POWERLEVEL9K_VI_VISUAL_MODE_STRING"
  3775. else
  3776. _p9k_prompt_segment $0_NORMAL "$_p9k_color1" white '' 0 '${$((! ${#${${${_p9k__keymap:#vicmd}:#vivis}:#vivli}})):#0}' "$_POWERLEVEL9K_VI_COMMAND_MODE_STRING"
  3777. fi
  3778. else
  3779. if (( $+_POWERLEVEL9K_VI_OVERWRITE_MODE_STRING )); then
  3780. if [[ -n $_POWERLEVEL9K_VI_INSERT_MODE_STRING ]]; then
  3781. _p9k_prompt_segment $0_INSERT "$_p9k_color1" blue '' 0 '${${:-$_p9k__keymap.$_p9k__zle_state}:#(vicmd.*|vivis.*|vivli.*|*.*overwrite*)}' "$_POWERLEVEL9K_VI_INSERT_MODE_STRING"
  3782. fi
  3783. _p9k_prompt_segment $0_OVERWRITE "$_p9k_color1" blue '' 0 '${${:-$_p9k__keymap.$_p9k__zle_state}:#(vicmd.*|vivis.*|vivli.*|*.*insert*)}' "$_POWERLEVEL9K_VI_OVERWRITE_MODE_STRING"
  3784. else
  3785. if [[ -n $_POWERLEVEL9K_VI_INSERT_MODE_STRING ]]; then
  3786. _p9k_prompt_segment $0_INSERT "$_p9k_color1" blue '' 0 '${_p9k__keymap:#(vicmd|vivis|vivli)}' "$_POWERLEVEL9K_VI_INSERT_MODE_STRING"
  3787. fi
  3788. fi
  3789. if (( $+_POWERLEVEL9K_VI_VISUAL_MODE_STRING )); then
  3790. _p9k_prompt_segment $0_NORMAL "$_p9k_color1" white '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#vicmd0}' "$_POWERLEVEL9K_VI_COMMAND_MODE_STRING"
  3791. _p9k_prompt_segment $0_VISUAL "$_p9k_color1" white '' 0 '${(M)${:-$_p9k__keymap$_p9k__region_active}:#(vicmd1|vivis?|vivli?)}' "$_POWERLEVEL9K_VI_VISUAL_MODE_STRING"
  3792. else
  3793. _p9k_prompt_segment $0_NORMAL "$_p9k_color1" white '' 0 '${(M)_p9k__keymap:#(vicmd|vivis|vivli)}' "$_POWERLEVEL9K_VI_COMMAND_MODE_STRING"
  3794. fi
  3795. fi
  3796. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  3797. }
  3798. instant_prompt_vi_mode() {
  3799. if [[ -n $_POWERLEVEL9K_VI_INSERT_MODE_STRING ]]; then
  3800. _p9k_prompt_segment prompt_vi_mode_INSERT "$_p9k_color1" blue '' 0 '' "$_POWERLEVEL9K_VI_INSERT_MODE_STRING"
  3801. fi
  3802. }
  3803. ################################################################
  3804. # Virtualenv: current working virtualenv
  3805. # More information on virtualenv (Python):
  3806. # https://virtualenv.pypa.io/en/latest/
  3807. prompt_virtualenv() {
  3808. local msg=''
  3809. if (( _POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION )) && _p9k_python_version; then
  3810. msg="${_p9k__ret//\%/%%} "
  3811. fi
  3812. local v=${VIRTUAL_ENV:t}
  3813. if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($v) " ]]; then
  3814. v=$VIRTUAL_ENV_PROMPT[2,-3]
  3815. elif [[ $v == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then
  3816. v=${VIRTUAL_ENV:h:t}
  3817. fi
  3818. msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${v//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER"
  3819. case $_POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV in
  3820. false)
  3821. _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '${(M)${#P9K_PYENV_PYTHON_VERSION}:#0}' "$msg"
  3822. ;;
  3823. if-different)
  3824. _p9k_escape $v
  3825. _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '${${:-'$_p9k__ret'}:#$_p9k__pyenv_version}' "$msg"
  3826. ;;
  3827. *)
  3828. _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '' "$msg"
  3829. ;;
  3830. esac
  3831. }
  3832. _p9k_prompt_virtualenv_init() {
  3833. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$VIRTUAL_ENV'
  3834. }
  3835. # _p9k_read_pyenv_like_version_file <filepath> [prefix]
  3836. function _p9k_read_pyenv_like_version_file() {
  3837. local -a stat
  3838. zstat -A stat +mtime -- $1 2>/dev/null || stat=(-1)
  3839. local cached=$_p9k__read_pyenv_like_version_file_cache[$1:$2]
  3840. if [[ $cached == $stat[1]:* ]]; then
  3841. _p9k__ret=${cached#*:}
  3842. else
  3843. local fd content
  3844. {
  3845. { sysopen -r -u fd -- $1 && sysread -i $fd -s 1024 content } 2>/dev/null
  3846. } always {
  3847. [[ -n $fd ]] && exec {fd}>&-
  3848. }
  3849. local MATCH
  3850. local versions=(${${${${(f)content}/(#m)*/${MATCH[(w)1]}}##\#*}#$2})
  3851. _p9k__ret=${(j.:.)versions}
  3852. _p9k__read_pyenv_like_version_file_cache[$1:$2]=$stat[1]:$_p9k__ret
  3853. fi
  3854. [[ -n $_p9k__ret ]]
  3855. }
  3856. function _p9k_pyenv_global_version() {
  3857. _p9k_read_pyenv_like_version_file ${PYENV_ROOT:-$HOME/.pyenv}/version python- || _p9k__ret=system
  3858. }
  3859. function _p9k_pyenv_compute() {
  3860. unset P9K_PYENV_PYTHON_VERSION _p9k__pyenv_version
  3861. local v=${(j.:.)${(@)${(s.:.)PYENV_VERSION}#python-}}
  3862. if [[ -n $v ]]; then
  3863. (( ${_POWERLEVEL9K_PYENV_SOURCES[(I)shell]} )) || return
  3864. else
  3865. (( ${_POWERLEVEL9K_PYENV_SOURCES[(I)local|global]} )) || return
  3866. _p9k__ret=
  3867. if [[ $PYENV_DIR != (|.) ]]; then
  3868. [[ $PYENV_DIR == /* ]] && local dir=$PYENV_DIR || local dir="$_p9k__cwd_a/$PYENV_DIR"
  3869. dir=${dir:A}
  3870. if [[ $dir != $_p9k__cwd_a ]]; then
  3871. while true; do
  3872. if _p9k_read_pyenv_like_version_file $dir/.python-version python-; then
  3873. (( ${_POWERLEVEL9K_PYENV_SOURCES[(I)local]} )) || return
  3874. break
  3875. fi
  3876. [[ $dir == (/|.) ]] && break
  3877. dir=${dir:h}
  3878. done
  3879. fi
  3880. fi
  3881. if [[ -z $_p9k__ret ]]; then
  3882. _p9k_upglob .python-version
  3883. local -i idx=$?
  3884. if (( idx )) && _p9k_read_pyenv_like_version_file $_p9k__parent_dirs[idx]/.python-version python-; then
  3885. (( ${_POWERLEVEL9K_PYENV_SOURCES[(I)local]} )) || return
  3886. else
  3887. _p9k__ret=
  3888. fi
  3889. fi
  3890. if [[ -z $_p9k__ret ]]; then
  3891. (( _POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW )) || return
  3892. (( ${_POWERLEVEL9K_PYENV_SOURCES[(I)global]} )) || return
  3893. _p9k_pyenv_global_version
  3894. fi
  3895. v=$_p9k__ret
  3896. fi
  3897. if (( !_POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW )); then
  3898. _p9k_pyenv_global_version
  3899. [[ $v == $_p9k__ret ]] && return 1
  3900. fi
  3901. if (( !_POWERLEVEL9K_PYENV_SHOW_SYSTEM )); then
  3902. [[ $v == system ]] && return 1
  3903. fi
  3904. local versions=${PYENV_ROOT:-$HOME/.pyenv}/versions
  3905. versions=${versions:A}
  3906. local name version
  3907. for name in ${(s.:.)v}; do
  3908. version=$versions/$name
  3909. version=${version:A}
  3910. if [[ $version(#qN/) == (#b)$versions/([^/]##)* ]]; then
  3911. typeset -g P9K_PYENV_PYTHON_VERSION=$match[1]
  3912. break
  3913. fi
  3914. done
  3915. typeset -g _p9k__pyenv_version=$v
  3916. }
  3917. ################################################################
  3918. # Segment to display pyenv information
  3919. # https://github.com/pyenv/pyenv#choosing-the-python-version
  3920. prompt_pyenv() {
  3921. _p9k_pyenv_compute || return
  3922. _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '' "${_p9k__pyenv_version//\%/%%}"
  3923. }
  3924. _p9k_prompt_pyenv_init() {
  3925. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[pyenv]:-${${+functions[pyenv]}:#0}}'
  3926. }
  3927. function _p9k_goenv_global_version() {
  3928. _p9k_read_pyenv_like_version_file ${GOENV_ROOT:-$HOME/.goenv}/version go- || _p9k__ret=system
  3929. }
  3930. ################################################################
  3931. # Segment to display goenv information: https://github.com/syndbg/goenv
  3932. prompt_goenv() {
  3933. local v=${(j.:.)${(@)${(s.:.)GOENV_VERSION}#go-}}
  3934. if [[ -n $v ]]; then
  3935. (( ${_POWERLEVEL9K_GOENV_SOURCES[(I)shell]} )) || return
  3936. else
  3937. (( ${_POWERLEVEL9K_GOENV_SOURCES[(I)local|global]} )) || return
  3938. _p9k__ret=
  3939. if [[ $GOENV_DIR != (|.) ]]; then
  3940. [[ $GOENV_DIR == /* ]] && local dir=$GOENV_DIR || local dir="$_p9k__cwd_a/$GOENV_DIR"
  3941. dir=${dir:A}
  3942. if [[ $dir != $_p9k__cwd_a ]]; then
  3943. while true; do
  3944. if _p9k_read_pyenv_like_version_file $dir/.go-version go-; then
  3945. (( ${_POWERLEVEL9K_GOENV_SOURCES[(I)local]} )) || return
  3946. break
  3947. fi
  3948. [[ $dir == (/|.) ]] && break
  3949. dir=${dir:h}
  3950. done
  3951. fi
  3952. fi
  3953. if [[ -z $_p9k__ret ]]; then
  3954. _p9k_upglob .go-version
  3955. local -i idx=$?
  3956. if (( idx )) && _p9k_read_pyenv_like_version_file $_p9k__parent_dirs[idx]/.go-version go-; then
  3957. (( ${_POWERLEVEL9K_GOENV_SOURCES[(I)local]} )) || return
  3958. else
  3959. _p9k__ret=
  3960. fi
  3961. fi
  3962. if [[ -z $_p9k__ret ]]; then
  3963. (( _POWERLEVEL9K_GOENV_PROMPT_ALWAYS_SHOW )) || return
  3964. (( ${_POWERLEVEL9K_GOENV_SOURCES[(I)global]} )) || return
  3965. _p9k_goenv_global_version
  3966. fi
  3967. v=$_p9k__ret
  3968. fi
  3969. if (( !_POWERLEVEL9K_GOENV_PROMPT_ALWAYS_SHOW )); then
  3970. _p9k_goenv_global_version
  3971. [[ $v == $_p9k__ret ]] && return
  3972. fi
  3973. if (( !_POWERLEVEL9K_GOENV_SHOW_SYSTEM )); then
  3974. [[ $v == system ]] && return
  3975. fi
  3976. _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'GO_ICON' 0 '' "${v//\%/%%}"
  3977. }
  3978. _p9k_prompt_goenv_init() {
  3979. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[goenv]:-${${+functions[goenv]}:#0}}'
  3980. }
  3981. ################################################################
  3982. # Display openfoam information
  3983. prompt_openfoam() {
  3984. if [[ -z "$WM_FORK" ]] ; then
  3985. _p9k_prompt_segment "$0" "yellow" "$_p9k_color1" '' 0 '' "OF: ${${WM_PROJECT_VERSION:t}//\%/%%}"
  3986. else
  3987. _p9k_prompt_segment "$0" "yellow" "$_p9k_color1" '' 0 '' "F-X: ${${WM_PROJECT_VERSION:t}//\%/%%}"
  3988. fi
  3989. }
  3990. _p9k_prompt_openfoam_init() {
  3991. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$WM_PROJECT_VERSION'
  3992. }
  3993. ################################################################
  3994. # Segment to display Swift version
  3995. prompt_swift_version() {
  3996. _p9k_cached_cmd 0 '' swift --version || return
  3997. [[ $_p9k__ret == (#b)[^[:digit:]]#([[:digit:].]##)* ]] || return
  3998. _p9k_prompt_segment "$0" "magenta" "white" 'SWIFT_ICON' 0 '' "${match[1]//\%/%%}"
  3999. }
  4000. _p9k_prompt_swift_version_init() {
  4001. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[swift]'
  4002. }
  4003. ################################################################
  4004. # dir_writable: Display information about the user's permission to write in the current directory
  4005. prompt_dir_writable() {
  4006. if [[ ! -w "$_p9k__cwd_a" ]]; then
  4007. _p9k_prompt_segment "$0_FORBIDDEN" "red" "yellow1" 'LOCK_ICON' 0 '' ''
  4008. fi
  4009. }
  4010. instant_prompt_dir_writable() { prompt_dir_writable; }
  4011. ################################################################
  4012. # Kubernetes Current Context/Namespace
  4013. prompt_kubecontext() {
  4014. if ! _p9k_cache_stat_get $0 ${(s.:.)${KUBECONFIG:-$HOME/.kube/config}}; then
  4015. local name namespace cluster user cloud_name cloud_account cloud_zone cloud_cluster text state
  4016. () {
  4017. local cfg && cfg=(${(f)"$(kubectl config view -o=yaml 2>/dev/null)"}) || return
  4018. local qstr='"*"'
  4019. local str='([^"'\''|>]*|'$qstr')'
  4020. local ctx=(${(@M)cfg:#current-context: $~str})
  4021. (( $#ctx == 1 )) || return
  4022. name=${ctx[1]#current-context: }
  4023. local -i pos=${cfg[(i)contexts:]}
  4024. {
  4025. (( pos <= $#cfg )) || return
  4026. shift $pos cfg
  4027. pos=${cfg[(i) name: $name]}
  4028. (( pos <= $#cfg )) || return
  4029. (( --pos ))
  4030. for ((; pos > 0; --pos)); do
  4031. local line=$cfg[pos]
  4032. if [[ $line == '- context:' ]]; then
  4033. return 0
  4034. elif [[ $line == (#b)' cluster: '($~str) ]]; then
  4035. cluster=$match[1]
  4036. [[ $cluster == $~qstr ]] && cluster=$cluster[2,-2]
  4037. elif [[ $line == (#b)' namespace: '($~str) ]]; then
  4038. namespace=$match[1]
  4039. [[ $namespace == $~qstr ]] && namespace=$namespace[2,-2]
  4040. elif [[ $line == (#b)' user: '($~str) ]]; then
  4041. user=$match[1]
  4042. [[ $user == $~qstr ]] && user=$user[2,-2]
  4043. fi
  4044. done
  4045. } always {
  4046. [[ $name == $~qstr ]] && name=$name[2,-2]
  4047. }
  4048. }
  4049. if [[ -n $name ]]; then
  4050. : ${namespace:=default}
  4051. # gke_my-account_us-east1-a_cluster-01
  4052. # gke_my-account_us-east1_cluster-01
  4053. if [[ $cluster == (#b)gke_(?*)_(asia|australia|europe|northamerica|southamerica|us)-([a-z]##<->)(-[a-z]|)_(?*) ]]; then
  4054. cloud_name=gke
  4055. cloud_account=$match[1]
  4056. cloud_zone=$match[2]-$match[3]$match[4]
  4057. cloud_cluster=$match[5]
  4058. if (( ${_POWERLEVEL9K_KUBECONTEXT_SHORTEN[(I)gke]} )); then
  4059. text=$cloud_cluster
  4060. fi
  4061. # arn:aws:eks:us-east-1:123456789012:cluster/cluster-01
  4062. elif [[ $cluster == (#b)arn:aws:eks:([[:alnum:]-]##):([[:digit:]]##):cluster/(?*) ]]; then
  4063. cloud_name=eks
  4064. cloud_zone=$match[1]
  4065. cloud_account=$match[2]
  4066. cloud_cluster=$match[3]
  4067. if (( ${_POWERLEVEL9K_KUBECONTEXT_SHORTEN[(I)eks]} )); then
  4068. text=$cloud_cluster
  4069. fi
  4070. fi
  4071. if [[ -z $text ]]; then
  4072. text=$name
  4073. if [[ $_POWERLEVEL9K_KUBECONTEXT_SHOW_DEFAULT_NAMESPACE == 1 || $namespace != (default|$name) ]]; then
  4074. text+="/$namespace"
  4075. fi
  4076. fi
  4077. local pat class
  4078. for pat class in "${_POWERLEVEL9K_KUBECONTEXT_CLASSES[@]}"; do
  4079. if [[ $text == ${~pat} ]]; then
  4080. [[ -n $class ]] && state=_${${(U)class}//İ/I}
  4081. break
  4082. fi
  4083. done
  4084. fi
  4085. _p9k_cache_stat_set "${(g::)name}" "${(g::)namespace}" "${(g::)cluster}" "${(g::)user}" "${(g::)cloud_name}" "${(g::)cloud_account}" "${(g::)cloud_zone}" "${(g::)cloud_cluster}" "${(g::)text}" "$state"
  4086. fi
  4087. typeset -g P9K_KUBECONTEXT_NAME=$_p9k__cache_val[1]
  4088. typeset -g P9K_KUBECONTEXT_NAMESPACE=$_p9k__cache_val[2]
  4089. typeset -g P9K_KUBECONTEXT_CLUSTER=$_p9k__cache_val[3]
  4090. typeset -g P9K_KUBECONTEXT_USER=$_p9k__cache_val[4]
  4091. typeset -g P9K_KUBECONTEXT_CLOUD_NAME=$_p9k__cache_val[5]
  4092. typeset -g P9K_KUBECONTEXT_CLOUD_ACCOUNT=$_p9k__cache_val[6]
  4093. typeset -g P9K_KUBECONTEXT_CLOUD_ZONE=$_p9k__cache_val[7]
  4094. typeset -g P9K_KUBECONTEXT_CLOUD_CLUSTER=$_p9k__cache_val[8]
  4095. [[ -n $_p9k__cache_val[9] ]] || return
  4096. _p9k_prompt_segment $0$_p9k__cache_val[10] magenta white KUBERNETES_ICON 0 '' "${_p9k__cache_val[9]//\%/%%}"
  4097. }
  4098. _p9k_prompt_kubecontext_init() {
  4099. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[kubectl]'
  4100. }
  4101. ################################################################
  4102. # Dropbox status
  4103. prompt_dropbox() {
  4104. # The first column is just the directory, so cut it
  4105. local dropbox_status="$(dropbox-cli filestatus . | cut -d\ -f2-)"
  4106. # Only show if the folder is tracked and dropbox is running
  4107. if [[ "$dropbox_status" != 'unwatched' && "$dropbox_status" != "isn't running!" ]]; then
  4108. # If "up to date", only show the icon
  4109. if [[ "$dropbox_status" =~ 'up to date' ]]; then
  4110. dropbox_status=""
  4111. fi
  4112. _p9k_prompt_segment "$0" "white" "blue" "DROPBOX_ICON" 0 '' "${dropbox_status//\%/%%}"
  4113. fi
  4114. }
  4115. _p9k_prompt_dropbox_init() {
  4116. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[dropbox-cli]'
  4117. }
  4118. # print Java version number
  4119. prompt_java_version() {
  4120. if (( _POWERLEVEL9K_JAVA_VERSION_PROJECT_ONLY )); then
  4121. _p9k_upglob 'pom.xml|build.gradle.kts|build.sbt|deps.edn|project.clj|build.boot|*.(java|class|jar|gradle|clj|cljc)' && return
  4122. fi
  4123. local java=$commands[java]
  4124. if ! _p9k_cache_stat_get $0 $java ${JAVA_HOME:+$JAVA_HOME/release}; then
  4125. local v
  4126. v="$(java -fullversion 2>&1)" || v=
  4127. v=${${v#*\"}%\"*}
  4128. (( _POWERLEVEL9K_JAVA_VERSION_FULL )) || v=${v%%-*}
  4129. _p9k_cache_stat_set "${v//\%/%%}"
  4130. fi
  4131. [[ -n $_p9k__cache_val[1] ]] || return
  4132. _p9k_prompt_segment "$0" "red" "white" "JAVA_ICON" 0 '' $_p9k__cache_val[1]
  4133. }
  4134. _p9k_prompt_java_version_init() {
  4135. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[java]'
  4136. }
  4137. prompt_azure() {
  4138. local cfg=${AZURE_CONFIG_DIR:-$HOME/.azure}/azureProfile.json
  4139. if ! _p9k_cache_stat_get $0 $cfg; then
  4140. local name
  4141. if (( $+commands[jq] )) && name="$(jq -r '[.subscriptions[]|select(.isDefault==true)|.name][]|strings' $cfg 2>/dev/null)"; then
  4142. name=${name%%$'\n'*}
  4143. elif ! name="$(az account show --query name --output tsv 2>/dev/null)"; then
  4144. name=
  4145. fi
  4146. _p9k_cache_stat_set "$name"
  4147. fi
  4148. local pat class state
  4149. for pat class in "${_POWERLEVEL9K_AZURE_CLASSES[@]}"; do
  4150. if [[ $name == ${~pat} ]]; then
  4151. [[ -n $class ]] && state=_${${(U)class}//İ/I}
  4152. break
  4153. fi
  4154. done
  4155. [[ -n $_p9k__cache_val[1] ]] || return
  4156. _p9k_prompt_segment "$0$state" "blue" "white" "AZURE_ICON" 0 '' "${_p9k__cache_val[1]//\%/%%}"
  4157. }
  4158. _p9k_prompt_azure_init() {
  4159. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[az]'
  4160. }
  4161. prompt_gcloud() {
  4162. local -i len=$#_p9k__prompt _p9k__has_upglob
  4163. _p9k_prompt_segment \
  4164. $0_PARTIAL blue white GCLOUD_ICON 1 \
  4165. '${${(M)${#P9K_GCLOUD_PROJECT_NAME}:#0}:+$P9K_GCLOUD_ACCOUNT$P9K_GCLOUD_PROJECT_ID}' \
  4166. '${P9K_GCLOUD_ACCOUNT//\%/%%}:${P9K_GCLOUD_PROJECT_ID//\%/%%}'
  4167. _p9k_prompt_segment \
  4168. $0_COMPLETE blue white GCLOUD_ICON 1 \
  4169. '$P9K_GCLOUD_PROJECT_NAME' \
  4170. '${P9K_GCLOUD_ACCOUNT//\%/%%}:${P9K_GCLOUD_PROJECT_ID//\%/%%}'
  4171. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  4172. }
  4173. _p9k_gcloud_prefetch() {
  4174. # P9K_GCLOUD_PROJECT is deprecated; it's always equal to P9K_GCLOUD_PROJECT_ID
  4175. unset P9K_GCLOUD_CONFIGURATION P9K_GCLOUD_ACCOUNT P9K_GCLOUD_PROJECT P9K_GCLOUD_PROJECT_ID P9K_GCLOUD_PROJECT_NAME
  4176. (( $+commands[gcloud] )) || return
  4177. _p9k_read_word ~/.config/gcloud/active_config || return
  4178. P9K_GCLOUD_CONFIGURATION=$_p9k__ret
  4179. if ! _p9k_cache_stat_get $0 ~/.config/gcloud/configurations/config_$P9K_GCLOUD_CONFIGURATION; then
  4180. local pair account project_id
  4181. pair="$(gcloud config configurations describe $P9K_GCLOUD_CONFIGURATION \
  4182. --format=$'value[separator="\1"](properties.core.account,properties.core.project)')"
  4183. (( ! $? )) && IFS=$'\1' read account project_id <<<$pair
  4184. _p9k_cache_stat_set "$account" "$project_id"
  4185. fi
  4186. if [[ -n $_p9k__cache_val[1] ]]; then
  4187. P9K_GCLOUD_ACCOUNT=$_p9k__cache_val[1]
  4188. fi
  4189. if [[ -n $_p9k__cache_val[2] ]]; then
  4190. P9K_GCLOUD_PROJECT_ID=$_p9k__cache_val[2]
  4191. P9K_GCLOUD_PROJECT=$P9K_GCLOUD_PROJECT_ID # deprecated parameter; set for backward compatibility
  4192. fi
  4193. if [[ $P9K_GCLOUD_CONFIGURATION == $_p9k_gcloud_configuration &&
  4194. $P9K_GCLOUD_ACCOUNT == $_p9k_gcloud_account &&
  4195. $P9K_GCLOUD_PROJECT_ID == $_p9k_gcloud_project_id ]]; then
  4196. [[ -n $_p9k_gcloud_project_name ]] && P9K_GCLOUD_PROJECT_NAME=$_p9k_gcloud_project_name
  4197. if (( _POWERLEVEL9K_GCLOUD_REFRESH_PROJECT_NAME_SECONDS < 0 ||
  4198. _p9k__gcloud_last_fetch_ts + _POWERLEVEL9K_GCLOUD_REFRESH_PROJECT_NAME_SECONDS > EPOCHREALTIME )); then
  4199. return
  4200. fi
  4201. else
  4202. _p9k_gcloud_configuration=$P9K_GCLOUD_CONFIGURATION
  4203. _p9k_gcloud_account=$P9K_GCLOUD_ACCOUNT
  4204. _p9k_gcloud_project_id=$P9K_GCLOUD_PROJECT_ID
  4205. _p9k_gcloud_project_name=
  4206. _p9k__state_dump_scheduled=1
  4207. fi
  4208. [[ -n $P9K_GCLOUD_CONFIGURATION && -n $P9K_GCLOUD_ACCOUNT && -n $P9K_GCLOUD_PROJECT_ID ]] || return
  4209. _p9k__gcloud_last_fetch_ts=EPOCHREALTIME
  4210. _p9k_worker_invoke gcloud "_p9k_prompt_gcloud_compute ${(q)commands[gcloud]} ${(q)P9K_GCLOUD_CONFIGURATION} ${(q)P9K_GCLOUD_ACCOUNT} ${(q)P9K_GCLOUD_PROJECT_ID}"
  4211. }
  4212. _p9k_prompt_gcloud_init() {
  4213. _p9k__async_segments_compute+=_p9k_gcloud_prefetch
  4214. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[gcloud]'
  4215. }
  4216. _p9k_prompt_gcloud_compute() {
  4217. local gcloud=$1
  4218. P9K_GCLOUD_CONFIGURATION=$2
  4219. P9K_GCLOUD_ACCOUNT=$3
  4220. P9K_GCLOUD_PROJECT_ID=$4
  4221. _p9k_worker_async "_p9k_prompt_gcloud_async ${(q)gcloud}" _p9k_prompt_gcloud_sync
  4222. }
  4223. _p9k_prompt_gcloud_async() {
  4224. local gcloud=$1
  4225. $gcloud projects describe $P9K_GCLOUD_PROJECT_ID --configuration=$P9K_GCLOUD_CONFIGURATION \
  4226. --account=$P9K_GCLOUD_ACCOUNT --format='value(name)'
  4227. }
  4228. _p9k_prompt_gcloud_sync() {
  4229. _p9k_worker_reply "_p9k_prompt_gcloud_update ${(q)P9K_GCLOUD_CONFIGURATION} ${(q)P9K_GCLOUD_ACCOUNT} ${(q)P9K_GCLOUD_PROJECT_ID} ${(q)REPLY%$'\n'}"
  4230. }
  4231. _p9k_prompt_gcloud_update() {
  4232. [[ $1 == $P9K_GCLOUD_CONFIGURATION &&
  4233. $2 == $P9K_GCLOUD_ACCOUNT &&
  4234. $3 == $P9K_GCLOUD_PROJECT_ID &&
  4235. $4 != $P9K_GCLOUD_PROJECT_NAME ]] || return
  4236. [[ -n $4 ]] && P9K_GCLOUD_PROJECT_NAME=$4 || unset P9K_GCLOUD_PROJECT_NAME
  4237. _p9k_gcloud_project_name=$P9K_GCLOUD_PROJECT_NAME
  4238. _p9k__state_dump_scheduled=1
  4239. reset=1
  4240. }
  4241. prompt_google_app_cred() {
  4242. unset P9K_GOOGLE_APP_CRED_{TYPE,PROJECT_ID,CLIENT_EMAIL}
  4243. if ! _p9k_cache_stat_get $0 $GOOGLE_APPLICATION_CREDENTIALS; then
  4244. local -a lines
  4245. local q='[.type//"", .project_id//"", .client_email//"", 0][]'
  4246. if lines=("${(@f)$(jq -r $q <$GOOGLE_APPLICATION_CREDENTIALS 2>/dev/null)}") && (( $#lines == 4 )); then
  4247. local text="${(j.:.)lines[1,-2]}"
  4248. local pat class state
  4249. for pat class in "${_POWERLEVEL9K_GOOGLE_APP_CRED_CLASSES[@]}"; do
  4250. if [[ $text == ${~pat} ]]; then
  4251. [[ -n $class ]] && state=_${${(U)class}//İ/I}
  4252. break
  4253. fi
  4254. done
  4255. _p9k_cache_stat_set 1 "${(@)lines[1,-2]}" "$text" "$state"
  4256. else
  4257. _p9k_cache_stat_set 0
  4258. fi
  4259. fi
  4260. (( _p9k__cache_val[1] )) || return
  4261. P9K_GOOGLE_APP_CRED_TYPE=$_p9k__cache_val[2]
  4262. P9K_GOOGLE_APP_CRED_PROJECT_ID=$_p9k__cache_val[3]
  4263. P9K_GOOGLE_APP_CRED_CLIENT_EMAIL=$_p9k__cache_val[4]
  4264. _p9k_prompt_segment "$0$_p9k__cache_val[6]" "blue" "white" "GCLOUD_ICON" 0 '' "$_p9k__cache_val[5]"
  4265. }
  4266. _p9k_prompt_google_app_cred_init() {
  4267. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${GOOGLE_APPLICATION_CREDENTIALS:+$commands[jq]}'
  4268. }
  4269. typeset -gra __p9k_nordvpn_tag=(
  4270. P9K_NORDVPN_STATUS
  4271. P9K_NORDVPN_TECHNOLOGY
  4272. P9K_NORDVPN_PROTOCOL
  4273. P9K_NORDVPN_IP_ADDRESS
  4274. P9K_NORDVPN_SERVER
  4275. P9K_NORDVPN_COUNTRY
  4276. P9K_NORDVPN_CITY
  4277. )
  4278. function _p9k_fetch_nordvpn_status() {
  4279. setopt err_return no_multi_byte
  4280. local REPLY
  4281. zsocket /run/nordvpn/nordvpnd.sock
  4282. local -i fd=REPLY
  4283. {
  4284. print -nu $fd 'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n\0\0\0\4\1\0\0\0\0\0\0;\1\4\0\0\0\1\203\206E\213b\270\327\2762\322z\230\326j\246A\206\240\344\35\23\235\t_\213\35u\320b\r&=LMedz\212\232\312\310\264\307`+\262\332\340@\2te\206M\2035\5\261\37\0\0\5\0\1\0\0\0\1\0\0\0\0\0\0\0\25\1\4\0\0\0\3\203\206E\215b\270\327\2762\322z\230\334\221\246\324\177\302\301\300\277\0\0\5\0\1\0\0\0\3\0\0\0\0\0'
  4285. local val
  4286. local -i len n wire tag
  4287. {
  4288. IFS='' read -t 0.25 -r val
  4289. val=$'\n'
  4290. while true; do
  4291. tag=$((#val))
  4292. wire='tag & 7'
  4293. (( (tag >>= 3) && tag <= $#__p9k_nordvpn_tag )) || break
  4294. if (( wire == 0 )); then
  4295. # varint
  4296. sysread -s 1 -t 0.25 val
  4297. n=$((#val))
  4298. (( n < 128 )) || break # bail on multi-byte varints
  4299. if (( tag == 2 )); then
  4300. # P9K_NORDVPN_TECHNOLOGY
  4301. case $n in
  4302. 1) typeset -g P9K_NORDVPN_TECHNOLOGY=OPENVPN;;
  4303. 2) typeset -g P9K_NORDVPN_TECHNOLOGY=NORDLYNX;;
  4304. 3) typeset -g P9K_NORDVPN_TECHNOLOGY=SKYLARK;;
  4305. *) typeset -g P9K_NORDVPN_TECHNOLOGY=UNKNOWN;;
  4306. esac
  4307. elif (( tag == 3 )); then
  4308. # P9K_NORDVPN_PROTOCOL
  4309. case $n in
  4310. 1) typeset -g P9K_NORDVPN_PROTOCOL=UDP;;
  4311. 2) typeset -g P9K_NORDVPN_PROTOCOL=TCP;;
  4312. *) typeset -g P9K_NORDVPN_PROTOCOL=UNKNOWN;;
  4313. esac
  4314. else
  4315. break
  4316. fi
  4317. else
  4318. # length-delimited
  4319. (( wire == 2 )) || break
  4320. (( tag != 2 && tag != 3 )) || break
  4321. [[ -t $fd ]] || true # https://www.zsh.org/mla/workers/2020/msg00207.html
  4322. sysread -s 1 -t 0.25 val
  4323. len=$((#val))
  4324. val=
  4325. while (( $#val < len )); do
  4326. [[ -t $fd ]] || true # https://www.zsh.org/mla/workers/2020/msg00207.html
  4327. sysread -s $(( len - $#val )) -t 0.25 'val[$#val+1]'
  4328. done
  4329. typeset -g $__p9k_nordvpn_tag[tag]=$val
  4330. fi
  4331. [[ -t $fd ]] || true # https://www.zsh.org/mla/workers/2020/msg00207.html
  4332. sysread -s 1 -t 0.25 val
  4333. done
  4334. } <&$fd
  4335. } always {
  4336. exec {fd}>&-
  4337. }
  4338. }
  4339. # Shows the state of NordVPN connection. Works only on Linux. Can be in the following 5 states.
  4340. #
  4341. # CONNECTED: NordVPN is connected. By default shows NORDVPN_ICON as icon and country code as
  4342. # content. In addition, the following variables are set for the use by
  4343. # POWERLEVEL9K_NORDVPN_CONNECTED_VISUAL_IDENTIFIER_EXPANSION and
  4344. # POWERLEVEL9K_NORDVPN_CONNECTED_CONTENT_EXPANSION:
  4345. #
  4346. # - P9K_NORDVPN_STATUS
  4347. # - P9K_NORDVPN_PROTOCOL
  4348. # - P9K_NORDVPN_TECHNOLOGY
  4349. # - P9K_NORDVPN_IP_ADDRESS
  4350. # - P9K_NORDVPN_SERVER
  4351. # - P9K_NORDVPN_COUNTRY
  4352. # - P9K_NORDVPN_CITY
  4353. # - P9K_NORDVPN_COUNTRY_CODE
  4354. #
  4355. # The last variable is trivially derived from P9K_NORDVPN_SERVER. The rest correspond to the output
  4356. # lines of `nordvpn status` command. Example of using these variables:
  4357. #
  4358. # # Display the name of the city where VPN servers are located when connected to NordVPN.
  4359. # POWERLEVEL9K_NORDVPN_CONNECTED_CONTENT_EXPANSION='${P9K_NORDVPN_CITY}'
  4360. #
  4361. # DISCONNECTED, CONNECTING, DISCONNECTING: NordVPN is disconnected/connecting/disconnecting. By
  4362. # default shows NORDVPN_ICON as icon and FAIL_ICON as content. In state CONNECTING the same
  4363. # P9K_NORDVPN_* variables are set as in CONNECTED. In states DISCONNECTED and DISCONNECTING only
  4364. # P9K_NORDVPN_STATUS is set. Example customizations:
  4365. #
  4366. # # Hide NordVPN segment when disconnected (segments with no icon and no content are not shown).
  4367. # POWERLEVEL9K_NORDVPN_DISCONNECTED_CONTENT_EXPANSION=
  4368. # POWERLEVEL9K_NORDVPN_DISCONNECTED_VISUAL_IDENTIFIER_EXPANSION=
  4369. #
  4370. # # When NordVPN is connecting, show country code on cyan background.
  4371. # POWERLEVEL9K_NORDVPN_CONNECTING_CONTENT_EXPANSION='${P9K_NORDVPN_COUNTRY_CODE}'
  4372. # POWERLEVEL9K_NORDVPN_CONNECTING_BACKGROUND=cyan
  4373. function prompt_nordvpn() {
  4374. unset $__p9k_nordvpn_tag P9K_NORDVPN_COUNTRY_CODE
  4375. [[ -e /run/nordvpn/nordvpnd.sock ]] || return
  4376. _p9k_fetch_nordvpn_status 2>/dev/null || return
  4377. if [[ $P9K_NORDVPN_SERVER == (#b)([[:alpha:]]##)[[:digit:]]##.nordvpn.com ]]; then
  4378. typeset -g P9K_NORDVPN_COUNTRY_CODE=${${(U)match[1]}//İ/I}
  4379. fi
  4380. case $P9K_NORDVPN_STATUS in
  4381. Connected)
  4382. _p9k_prompt_segment $0_CONNECTED blue white NORDVPN_ICON 0 '' "$P9K_NORDVPN_COUNTRY_CODE"
  4383. ;;
  4384. Disconnected|Connecting|Disconnecting)
  4385. local state=${${(U)P9K_NORDVPN_STATUS}//İ/I}
  4386. _p9k_get_icon $0_$state FAIL_ICON
  4387. _p9k_prompt_segment $0_$state yellow white NORDVPN_ICON 0 '' "$_p9k__ret"
  4388. ;;
  4389. *)
  4390. return
  4391. ;;
  4392. esac
  4393. }
  4394. _p9k_prompt_nordvpn_init() {
  4395. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[nordvpn]'
  4396. }
  4397. function prompt_ranger() {
  4398. _p9k_prompt_segment $0 $_p9k_color1 yellow RANGER_ICON 0 '' $RANGER_LEVEL
  4399. }
  4400. _p9k_prompt_ranger_init() {
  4401. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$RANGER_LEVEL'
  4402. }
  4403. function instant_prompt_ranger() {
  4404. _p9k_prompt_segment prompt_ranger $_p9k_color1 yellow RANGER_ICON 1 '$RANGER_LEVEL' '$RANGER_LEVEL'
  4405. }
  4406. function prompt_midnight_commander() {
  4407. local -i len=$#_p9k__prompt _p9k__has_upglob
  4408. _p9k_prompt_segment $0 $_p9k_color1 yellow MIDNIGHT_COMMANDER_ICON 0 '' ''
  4409. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  4410. }
  4411. _p9k_prompt_midnight_commander_init() {
  4412. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$MC_TMPDIR'
  4413. }
  4414. function instant_prompt_midnight_commander() {
  4415. _p9k_prompt_segment prompt_midnight_commander $_p9k_color1 yellow MIDNIGHT_COMMANDER_ICON 0 '$MC_TMPDIR' ''
  4416. }
  4417. function prompt_nnn() {
  4418. _p9k_prompt_segment $0 6 $_p9k_color1 NNN_ICON 0 '' $NNNLVL
  4419. }
  4420. _p9k_prompt_nnn_init() {
  4421. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${NNNLVL:#0}'
  4422. }
  4423. function instant_prompt_nnn() {
  4424. _p9k_prompt_segment prompt_nnn 6 $_p9k_color1 NNN_ICON 1 '${NNNLVL:#0}' '$NNNLVL'
  4425. }
  4426. function prompt_xplr() {
  4427. local -i len=$#_p9k__prompt _p9k__has_upglob
  4428. _p9k_prompt_segment $0 6 $_p9k_color1 XPLR_ICON 0 '' ''
  4429. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  4430. }
  4431. _p9k_prompt_xplr_init() {
  4432. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$XPLR_PID'
  4433. }
  4434. function instant_prompt_xplr() {
  4435. _p9k_prompt_segment prompt_xplr 6 $_p9k_color1 XPLR_ICON 0 '$XPLR_PID' ''
  4436. }
  4437. function prompt_vim_shell() {
  4438. local -i len=$#_p9k__prompt _p9k__has_upglob
  4439. _p9k_prompt_segment $0 green $_p9k_color1 VIM_ICON 0 '' ''
  4440. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  4441. }
  4442. _p9k_prompt_vim_shell_init() {
  4443. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$VIMRUNTIME'
  4444. }
  4445. function instant_prompt_vim_shell() {
  4446. _p9k_prompt_segment prompt_vim_shell green $_p9k_color1 VIM_ICON 0 '$VIMRUNTIME' ''
  4447. }
  4448. function prompt_nix_shell() {
  4449. _p9k_prompt_segment $0 4 $_p9k_color1 NIX_SHELL_ICON 0 '' "${(M)IN_NIX_SHELL:#(pure|impure)}"
  4450. }
  4451. _p9k_prompt_nix_shell_init() {
  4452. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${IN_NIX_SHELL:#0}'
  4453. }
  4454. function instant_prompt_nix_shell() {
  4455. _p9k_prompt_segment prompt_nix_shell 4 $_p9k_color1 NIX_SHELL_ICON 1 '${IN_NIX_SHELL:#0}' '${(M)IN_NIX_SHELL:#(pure|impure)}'
  4456. }
  4457. function prompt_terraform() {
  4458. local ws=$TF_WORKSPACE
  4459. if [[ -z $TF_WORKSPACE ]]; then
  4460. _p9k_read_word ${${TF_DATA_DIR:-.terraform}:A}/environment && ws=$_p9k__ret
  4461. fi
  4462. [[ -z $ws || $ws == default && $_POWERLEVEL9K_TERRAFORM_SHOW_DEFAULT == 0 ]] && return
  4463. local pat class state
  4464. for pat class in "${_POWERLEVEL9K_TERRAFORM_CLASSES[@]}"; do
  4465. if [[ $ws == ${~pat} ]]; then
  4466. [[ -n $class ]] && state=_${${(U)class}//İ/I}
  4467. break
  4468. fi
  4469. done
  4470. _p9k_prompt_segment "$0$state" $_p9k_color1 blue TERRAFORM_ICON 0 '' $ws
  4471. }
  4472. _p9k_prompt_terraform_init() {
  4473. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[terraform]'
  4474. }
  4475. function prompt_terraform_version() {
  4476. _p9k_cached_cmd 0 '' terraform --version || return
  4477. local v=${_p9k__ret#Terraform v}
  4478. (( $#v < $#_p9k__ret )) || return
  4479. v=${v%%$'\n'*}
  4480. [[ -n $v ]] || return
  4481. _p9k_prompt_segment $0 $_p9k_color1 blue TERRAFORM_ICON 0 '' $v
  4482. }
  4483. _p9k_prompt_terraform_version_init() {
  4484. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[terraform]'
  4485. }
  4486. function prompt_proxy() {
  4487. local -U p=(
  4488. $all_proxy $http_proxy $https_proxy $ftp_proxy
  4489. $ALL_PROXY $HTTP_PROXY $HTTPS_PROXY $FTP_PROXY)
  4490. p=(${(@)${(@)${(@)p#*://}##*@}%%/*})
  4491. (( $#p == 1 )) || p=("")
  4492. _p9k_prompt_segment $0 $_p9k_color1 blue PROXY_ICON 0 '' "$p[1]"
  4493. }
  4494. _p9k_prompt_proxy_init() {
  4495. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$all_proxy$http_proxy$https_proxy$ftp_proxy$ALL_PROXY$HTTP_PROXY$HTTPS_PROXY$FTP_PROXY'
  4496. }
  4497. function prompt_direnv() {
  4498. local -i len=$#_p9k__prompt _p9k__has_upglob
  4499. _p9k_prompt_segment $0 $_p9k_color1 yellow DIRENV_ICON 0 '$DIRENV_DIR' ''
  4500. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  4501. }
  4502. _p9k_prompt_direnv_init() {
  4503. # DIRENV_DIR is set in a precmd hook. If our hook isn't the last, DIRENV_DIR might
  4504. # still get set before prompt is expanded.
  4505. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${DIRENV_DIR:-${precmd_functions[-1]:#_p9k_precmd}}'
  4506. }
  4507. function instant_prompt_direnv() {
  4508. if [[ -n $DIRENV_DIR && $precmd_functions[-1] == _p9k_precmd ]]; then
  4509. _p9k_prompt_segment prompt_direnv $_p9k_color1 yellow DIRENV_ICON 0 '' ''
  4510. fi
  4511. }
  4512. function _p9k_timewarrior_clear() {
  4513. [[ -z $_p9k_timewarrior_dir ]] && return
  4514. _p9k_timewarrior_dir=
  4515. _p9k_timewarrior_dir_mtime=0
  4516. _p9k_timewarrior_file_mtime=0
  4517. _p9k_timewarrior_file_name=
  4518. unset _p9k_timewarrior_tags
  4519. _p9k__state_dump_scheduled=1
  4520. }
  4521. function prompt_timewarrior() {
  4522. local -a stat
  4523. local dir=${TIMEWARRIORDB:-~/.timewarrior}/data
  4524. [[ $dir == $_p9k_timewarrior_dir ]] || _p9k_timewarrior_clear
  4525. if [[ -n $_p9k_timewarrior_file_name ]]; then
  4526. zstat -A stat +mtime -- $dir $_p9k_timewarrior_file_name 2>/dev/null || stat=()
  4527. if [[ $stat[1] == $_p9k_timewarrior_dir_mtime && $stat[2] == $_p9k_timewarrior_file_mtime ]]; then
  4528. if (( $+_p9k_timewarrior_tags )); then
  4529. _p9k_prompt_segment $0 grey 255 TIMEWARRIOR_ICON 0 '' "${_p9k_timewarrior_tags//\%/%%}"
  4530. fi
  4531. return
  4532. fi
  4533. fi
  4534. if [[ ! -d $dir ]]; then
  4535. _p9k_timewarrior_clear
  4536. return
  4537. fi
  4538. _p9k_timewarrior_dir=$dir
  4539. if [[ $stat[1] != $_p9k_timewarrior_dir_mtime ]]; then
  4540. local -a files=($dir/<->-<->.data(.N))
  4541. if (( ! $#files )); then
  4542. if (( $#stat )) || zstat -A stat +mtime -- $dir 2>/dev/null; then
  4543. _p9k_timewarrior_dir_mtime=$stat[1]
  4544. _p9k_timewarrior_file_mtime=$stat[1]
  4545. _p9k_timewarrior_file_name=$dir # sic
  4546. unset _p9k_timewarrior_tags
  4547. _p9k__state_dump_scheduled=1
  4548. else
  4549. _p9k_timewarrior_clear
  4550. fi
  4551. return
  4552. fi
  4553. _p9k_timewarrior_file_name=${${(AO)files}[1]}
  4554. fi
  4555. if ! zstat -A stat +mtime -- $dir $_p9k_timewarrior_file_name 2>/dev/null; then
  4556. _p9k_timewarrior_clear
  4557. return
  4558. fi
  4559. _p9k_timewarrior_dir_mtime=$stat[1]
  4560. _p9k_timewarrior_file_mtime=$stat[2]
  4561. { local tail=${${(Af)"$(<$_p9k_timewarrior_file_name)"}[-1]} } 2>/dev/null
  4562. if [[ $tail == (#b)'inc '[^\ ]##(|\ #\#(*)) ]]; then
  4563. _p9k_timewarrior_tags=${${match[2]## #}%% #}
  4564. _p9k_prompt_segment $0 grey 255 TIMEWARRIOR_ICON 0 '' "${_p9k_timewarrior_tags//\%/%%}"
  4565. else
  4566. unset _p9k_timewarrior_tags
  4567. fi
  4568. _p9k__state_dump_scheduled=1
  4569. }
  4570. function _p9k_prompt_timewarrior_init() {
  4571. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[timew]'
  4572. }
  4573. function _p9k_taskwarrior_check_meta() {
  4574. [[ -n $_p9k_taskwarrior_meta_sig ]] || return
  4575. [[ -z $^_p9k_taskwarrior_meta_non_files(#qN) ]] || return
  4576. local -a stat
  4577. if (( $#_p9k_taskwarrior_meta_files )); then
  4578. zstat -A stat +mtime -- $_p9k_taskwarrior_meta_files 2>/dev/null || return
  4579. fi
  4580. [[ $_p9k_taskwarrior_meta_sig == ${(pj:\0:)stat}$'\0'$TASKRC$'\0'$TASKDATA ]] || return
  4581. }
  4582. function _p9k_taskwarrior_init_meta() {
  4583. local last_sig=$_p9k_taskwarrior_meta_sig
  4584. {
  4585. local cfg
  4586. cfg="$(command task show data.location rc.color=0 rc._forcecolor=0 </dev/null 2>/dev/null)" || return
  4587. local lines=(${(@M)${(f)cfg}:#data.location[[:space:]]##[^[:space:]]*})
  4588. (( $#lines == 1 )) || return
  4589. local dir=${lines[1]##data.location[[:space:]]#}
  4590. : ${dir::=$~dir} # `task` can give us path with `~`` in it; expand it
  4591. local -a stat files=(${TASKRC:-~/.taskrc})
  4592. _p9k_taskwarrior_meta_files=($^files(N))
  4593. _p9k_taskwarrior_meta_non_files=(${files:|_p9k_taskwarrior_meta_files})
  4594. if (( $#_p9k_taskwarrior_meta_files )); then
  4595. zstat -A stat +mtime -- $_p9k_taskwarrior_meta_files 2>/dev/null || stat=(-1)
  4596. fi
  4597. _p9k_taskwarrior_meta_sig=${(pj:\0:)stat}$'\0'$TASKRC$'\0'$TASKDATA
  4598. _p9k_taskwarrior_data_dir=$dir
  4599. } always {
  4600. if (( $? == 0 )); then
  4601. _p9k__state_dump_scheduled=1
  4602. return
  4603. fi
  4604. [[ -n $last_sig ]] && _p9k__state_dump_scheduled=1
  4605. _p9k_taskwarrior_meta_files=()
  4606. _p9k_taskwarrior_meta_non_files=()
  4607. _p9k_taskwarrior_meta_sig=
  4608. _p9k_taskwarrior_data_dir=
  4609. _p9k__taskwarrior_functional=
  4610. }
  4611. }
  4612. function _p9k_taskwarrior_check_data() {
  4613. [[ -n $_p9k_taskwarrior_data_sig ]] || return
  4614. [[ -z $^_p9k_taskwarrior_data_non_files(#qN) ]] || return
  4615. local -a stat
  4616. if (( $#_p9k_taskwarrior_data_files )); then
  4617. zstat -A stat +mtime -- $_p9k_taskwarrior_data_files 2>/dev/null || return
  4618. fi
  4619. [[ $_p9k_taskwarrior_data_sig == ${(pj:\0:)stat}$'\0'$TASKRC$'\0'$TASKDATA ]] || return
  4620. (( _p9k_taskwarrior_next_due == 0 || _p9k_taskwarrior_next_due > EPOCHSECONDS )) || return
  4621. }
  4622. function _p9k_taskwarrior_init_data() {
  4623. local -a stat files=($_p9k_taskwarrior_data_dir/{pending,completed}.data)
  4624. _p9k_taskwarrior_data_files=($^files(N))
  4625. _p9k_taskwarrior_data_non_files=(${files:|_p9k_taskwarrior_data_files})
  4626. if (( $#_p9k_taskwarrior_data_files )); then
  4627. zstat -A stat +mtime -- $_p9k_taskwarrior_data_files 2>/dev/null || stat=(-1)
  4628. _p9k_taskwarrior_data_sig=${(pj:\0:)stat}$'\0'
  4629. else
  4630. _p9k_taskwarrior_data_sig=
  4631. fi
  4632. _p9k_taskwarrior_data_files+=($_p9k_taskwarrior_meta_files)
  4633. _p9k_taskwarrior_data_non_files+=($_p9k_taskwarrior_meta_non_files)
  4634. _p9k_taskwarrior_data_sig+=$_p9k_taskwarrior_meta_sig
  4635. local name val
  4636. for name in PENDING OVERDUE; do
  4637. val="$(command task +$name count rc.color=0 rc._forcecolor=0 </dev/null 2>/dev/null)" || continue
  4638. [[ $val == <1-> ]] || continue
  4639. _p9k_taskwarrior_counters[$name]=$val
  4640. done
  4641. _p9k_taskwarrior_next_due=0
  4642. if (( _p9k_taskwarrior_counters[PENDING] > _p9k_taskwarrior_counters[OVERDUE] )); then
  4643. local -a ts
  4644. ts=($(command task +PENDING -OVERDUE list rc.verbose=nothing rc.color=0 rc._forcecolor=0 \
  4645. rc.report.list.labels= rc.report.list.columns=due.epoch </dev/null 2>/dev/null)) || ts=()
  4646. if (( $#ts )); then
  4647. _p9k_taskwarrior_next_due=${${(on)ts}[1]}
  4648. (( _p9k_taskwarrior_next_due > EPOCHSECONDS )) || _p9k_taskwarrior_next_due=$((EPOCHSECONDS+60))
  4649. fi
  4650. fi
  4651. _p9k__state_dump_scheduled=1
  4652. }
  4653. function prompt_taskwarrior() {
  4654. unset P9K_TASKWARRIOR_PENDING_COUNT P9K_TASKWARRIOR_OVERDUE_COUNT
  4655. if ! _p9k_taskwarrior_check_data; then
  4656. _p9k_taskwarrior_data_files=()
  4657. _p9k_taskwarrior_data_non_files=()
  4658. _p9k_taskwarrior_data_sig=
  4659. _p9k_taskwarrior_counters=()
  4660. _p9k_taskwarrior_next_due=0
  4661. _p9k_taskwarrior_check_meta || _p9k_taskwarrior_init_meta || return
  4662. _p9k_taskwarrior_init_data
  4663. fi
  4664. (( $#_p9k_taskwarrior_counters )) || return
  4665. local text c=$_p9k_taskwarrior_counters[OVERDUE]
  4666. if [[ -n $c ]]; then
  4667. typeset -g P9K_TASKWARRIOR_OVERDUE_COUNT=$c
  4668. text+="!$c"
  4669. fi
  4670. c=$_p9k_taskwarrior_counters[PENDING]
  4671. if [[ -n $c ]]; then
  4672. typeset -g P9K_TASKWARRIOR_PENDING_COUNT=$c
  4673. [[ -n $text ]] && text+='/'
  4674. text+=$c
  4675. fi
  4676. [[ -n $text ]] || return
  4677. _p9k_prompt_segment $0 6 $_p9k_color1 TASKWARRIOR_ICON 0 '' $text
  4678. }
  4679. function _p9k_prompt_taskwarrior_init() {
  4680. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[task]:+$_p9k__taskwarrior_functional}'
  4681. }
  4682. prompt_wifi() {
  4683. local -i len=$#_p9k__prompt _p9k__has_upglob
  4684. _p9k_prompt_segment $0 green $_p9k_color1 WIFI_ICON 1 '$_p9k__wifi_on' '$P9K_WIFI_LAST_TX_RATE Mbps'
  4685. (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
  4686. }
  4687. _p9k_prompt_wifi_init() {
  4688. if [[ -x /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport ||
  4689. -r /proc/net/wireless && -n $commands[iw] ]]; then
  4690. typeset -g _p9k__wifi_on=
  4691. typeset -g P9K_WIFI_LAST_TX_RATE=
  4692. typeset -g P9K_WIFI_SSID=
  4693. typeset -g P9K_WIFI_LINK_AUTH=
  4694. typeset -g P9K_WIFI_RSSI=
  4695. typeset -g P9K_WIFI_NOISE=
  4696. typeset -g P9K_WIFI_BARS=
  4697. _p9k__async_segments_compute+='_p9k_worker_invoke wifi _p9k_prompt_wifi_compute'
  4698. else
  4699. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
  4700. fi
  4701. }
  4702. _p9k_prompt_wifi_compute() {
  4703. _p9k_worker_async _p9k_prompt_wifi_async _p9k_prompt_wifi_sync
  4704. }
  4705. _p9k_prompt_wifi_async() {
  4706. local airport=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
  4707. local last_tx_rate ssid link_auth rssi noise bars on out line v state iface
  4708. {
  4709. if [[ -x $airport ]]; then
  4710. out="$($airport -I)" || return 0
  4711. for line in ${${${(f)out}##[[:space:]]#}%%[[:space:]]#}; do
  4712. v=${line#*: }
  4713. case $line[1,-$#v-3] in
  4714. agrCtlRSSI) rssi=$v;;
  4715. agrCtlNoise) noise=$v;;
  4716. state) state=$v;;
  4717. lastTxRate) last_tx_rate=$v;;
  4718. link\ auth) link_auth=$v;;
  4719. SSID) ssid=$v;;
  4720. esac
  4721. done
  4722. [[ $state == running && $rssi == (0|-<->) && $noise == (0|-<->) ]] || return 0
  4723. elif [[ -r /proc/net/wireless && -n $commands[iw] ]]; then
  4724. # Content example (https://github.com/romkatv/powerlevel10k/pull/973#issuecomment-680251804):
  4725. #
  4726. # Inter-| sta-| Quality | Discarded packets | Missed | WE
  4727. # face | tus | link level noise | nwid crypt frag retry misc | beacon | 22
  4728. # wlp3s0: 0000 58. -52. -256 0 0 0 0 76 0
  4729. local -a lines
  4730. lines=(${${(f)"$(</proc/net/wireless)"}:#*\|*}) || return 0
  4731. (( $#lines == 1 )) || return 0
  4732. local parts=(${=lines[1]})
  4733. iface=${parts[1]%:}
  4734. state=${parts[2]}
  4735. rssi=${parts[4]%.*}
  4736. noise=${parts[5]%.*}
  4737. [[ -n $iface && $state == 0## && $rssi == (0|-<->) && $noise == (0|-<->) ]] || return 0
  4738. # Output example (https://github.com/romkatv/powerlevel10k/pull/973#issuecomment-680251804):
  4739. #
  4740. # Connected to 74:83:c2:be:76:da (on wlp3s0)
  4741. # SSID: DailyGrindGuest1
  4742. # freq: 5745
  4743. # RX: 35192066 bytes (27041 packets)
  4744. # TX: 4090471 bytes (24287 packets)
  4745. # signal: -52 dBm
  4746. # rx bitrate: 243.0 MBit/s VHT-MCS 6 40MHz VHT-NSS 2
  4747. # tx bitrate: 240.0 MBit/s VHT-MCS 5 40MHz short GI VHT-NSS 2
  4748. #
  4749. # bss flags: short-slot-time
  4750. # dtim period: 1
  4751. # beacon int: 100
  4752. lines=(${(f)"$(command iw dev $iface link)"}) || return 0
  4753. local -a match mbegin mend
  4754. for line in $lines; do
  4755. if [[ $line == (#b)[[:space:]]#SSID:[[:space:]]##(*) ]]; then
  4756. ssid=$match[1]
  4757. elif [[ $line == (#b)[[:space:]]#'tx bitrate:'[[:space:]]##([^[:space:]]##)' MBit/s'* ]]; then
  4758. last_tx_rate=$match[1]
  4759. [[ $last_tx_rate == <->.<-> ]] && last_tx_rate=${${last_tx_rate%%0#}%.}
  4760. fi
  4761. done
  4762. [[ -n $ssid && -n $last_tx_rate ]] || return 0
  4763. else
  4764. return 0
  4765. fi
  4766. # https://www.speedguide.net/faq/how-to-read-rssisignal-and-snrnoise-ratings-440
  4767. # http://www.wireless-nets.com/resources/tutorials/define_SNR_values.html
  4768. local -i snr_margin='rssi - noise'
  4769. if (( snr_margin >= 40 )); then
  4770. bars=4
  4771. elif (( snr_margin >= 25 )); then
  4772. bars=3
  4773. elif (( snr_margin >= 15 )); then
  4774. bars=2
  4775. elif (( snr_margin >= 10 )); then
  4776. bars=1
  4777. else
  4778. bars=0
  4779. fi
  4780. on=1
  4781. } always {
  4782. if (( ! on )); then
  4783. rssi=
  4784. noise=
  4785. ssid=
  4786. last_tx_rate=
  4787. bars=
  4788. link_auth=
  4789. fi
  4790. if [[ $_p9k__wifi_on != $on ||
  4791. $P9K_WIFI_LAST_TX_RATE != $last_tx_rate ||
  4792. $P9K_WIFI_SSID != $ssid ||
  4793. $P9K_WIFI_LINK_AUTH != $link_auth ||
  4794. $P9K_WIFI_RSSI != $rssi ||
  4795. $P9K_WIFI_NOISE != $noise ||
  4796. $P9K_WIFI_BARS != $bars ]]; then
  4797. _p9k__wifi_on=$on
  4798. P9K_WIFI_LAST_TX_RATE=$last_tx_rate
  4799. P9K_WIFI_SSID=$ssid
  4800. P9K_WIFI_LINK_AUTH=$link_auth
  4801. P9K_WIFI_RSSI=$rssi
  4802. P9K_WIFI_NOISE=$noise
  4803. P9K_WIFI_BARS=$bars
  4804. _p9k_print_params \
  4805. _p9k__wifi_on \
  4806. P9K_WIFI_LAST_TX_RATE \
  4807. P9K_WIFI_SSID \
  4808. P9K_WIFI_LINK_AUTH \
  4809. P9K_WIFI_RSSI \
  4810. P9K_WIFI_NOISE \
  4811. P9K_WIFI_BARS
  4812. echo -E - 'reset=1'
  4813. fi
  4814. }
  4815. }
  4816. _p9k_prompt_wifi_sync() {
  4817. if [[ -n $REPLY ]]; then
  4818. eval $REPLY
  4819. _p9k_worker_reply $REPLY
  4820. fi
  4821. }
  4822. function _p9k_asdf_check_meta() {
  4823. [[ -n $_p9k_asdf_meta_sig ]] || return
  4824. [[ -z $^_p9k_asdf_meta_non_files(#qN) ]] || return
  4825. local -a stat
  4826. if (( $#_p9k_asdf_meta_files )); then
  4827. zstat -A stat +mtime -- $_p9k_asdf_meta_files 2>/dev/null || return
  4828. fi
  4829. [[ $_p9k_asdf_meta_sig == $ASDF_CONFIG_FILE$'\0'$ASDF_DATA_DIR$'\0'${(pj:\0:)stat} ]] || return
  4830. }
  4831. function _p9k_asdf_init_meta() {
  4832. local last_sig=$_p9k_asdf_meta_sig
  4833. {
  4834. local -a files
  4835. local -i legacy_enabled
  4836. _p9k_asdf_plugins=()
  4837. _p9k_asdf_file_info=()
  4838. local cfg=${ASDF_CONFIG_FILE:-~/.asdfrc}
  4839. files+=$cfg
  4840. if [[ -f $cfg && -r $cfg ]]; then
  4841. # Config parser in adsf is very strange.
  4842. #
  4843. # This gives "yes":
  4844. #
  4845. # legacy_version_file = yes = no
  4846. #
  4847. # This gives "no":
  4848. #
  4849. # legacy_version_file = yes
  4850. # legacy_version_file = yes
  4851. #
  4852. # We do the same.
  4853. local lines=(${(@M)${(@)${(f)"$(<$cfg)"}%$'\r'}:#[[:space:]]#legacy_version_file[[:space:]]#=*})
  4854. if [[ $#lines == 1 && ${${(s:=:)lines[1]}[2]} == [[:space:]]#yes[[:space:]]# ]]; then
  4855. legacy_enabled=1
  4856. fi
  4857. fi
  4858. local root=${ASDF_DATA_DIR:-~/.asdf}
  4859. files+=$root/plugins
  4860. if [[ -d $root/plugins ]]; then
  4861. local plugin
  4862. for plugin in $root/plugins/[^[:space:]]##(/N); do
  4863. files+=$root/installs/${plugin:t}
  4864. local -aU installed=($root/installs/${plugin:t}/[^[:space:]]##(/N:t) system)
  4865. _p9k_asdf_plugins[${plugin:t}]=${(j:|:)${(@b)installed}}
  4866. (( legacy_enabled )) || continue
  4867. if [[ ! -e $plugin/bin ]]; then
  4868. files+=$plugin/bin
  4869. else
  4870. local list_names=$plugin/bin/list-legacy-filenames
  4871. files+=$list_names
  4872. if [[ -x $list_names ]]; then
  4873. local parse=$plugin/bin/parse-legacy-file
  4874. local -i has_parse=0
  4875. files+=$parse
  4876. [[ -x $parse ]] && has_parse=1
  4877. local name
  4878. for name in ${$($list_names 2>/dev/null)%$'\r'}; do
  4879. [[ $name == (*/*|.tool-versions) ]] && continue
  4880. _p9k_asdf_file_info[$name]+="${plugin:t} $has_parse "
  4881. done
  4882. fi
  4883. fi
  4884. done
  4885. fi
  4886. _p9k_asdf_meta_files=($^files(N))
  4887. _p9k_asdf_meta_non_files=(${files:|_p9k_asdf_meta_files})
  4888. local -a stat
  4889. if (( $#_p9k_asdf_meta_files )); then
  4890. zstat -A stat +mtime -- $_p9k_asdf_meta_files 2>/dev/null || return
  4891. fi
  4892. _p9k_asdf_meta_sig=$ASDF_CONFIG_FILE$'\0'$ASDF_DATA_DIR$'\0'${(pj:\0:)stat}
  4893. _p9k__asdf_dir2files=()
  4894. _p9k_asdf_file2versions=()
  4895. } always {
  4896. if (( $? == 0 )); then
  4897. _p9k__state_dump_scheduled=1
  4898. return
  4899. fi
  4900. [[ -n $last_sig ]] && _p9k__state_dump_scheduled=1
  4901. _p9k_asdf_meta_files=()
  4902. _p9k_asdf_meta_non_files=()
  4903. _p9k_asdf_meta_sig=
  4904. _p9k_asdf_plugins=()
  4905. _p9k_asdf_file_info=()
  4906. _p9k__asdf_dir2files=()
  4907. _p9k_asdf_file2versions=()
  4908. }
  4909. }
  4910. # Usage: _p9k_asdf_parse_version_file <file> <is_legacy>
  4911. #
  4912. # Mutates `versions` on success.
  4913. function _p9k_asdf_parse_version_file() {
  4914. local file=$1
  4915. local is_legacy=$2
  4916. local -a stat
  4917. zstat -A stat +mtime $file 2>/dev/null || return
  4918. if (( is_legacy )); then
  4919. local plugin has_parse
  4920. for plugin has_parse in $=_p9k_asdf_file_info[$file:t]; do
  4921. local cached=$_p9k_asdf_file2versions[$plugin:$file]
  4922. if [[ $cached == $stat[1]:* ]]; then
  4923. local v=${cached#*:}
  4924. else
  4925. if (( has_parse )); then
  4926. local v=($(${ASDF_DATA_DIR:-~/.asdf}/plugins/$plugin/bin/parse-legacy-file $file 2>/dev/null))
  4927. else
  4928. { local v=($(<$file)) } 2>/dev/null
  4929. fi
  4930. v=(${v%$'\r'})
  4931. v=${v[(r)$_p9k_asdf_plugins[$plugin]]:-$v[1]}
  4932. _p9k_asdf_file2versions[$plugin:$file]=$stat[1]:"$v"
  4933. _p9k__state_dump_scheduled=1
  4934. fi
  4935. [[ -n $v ]] && : ${versions[$plugin]="$v"}
  4936. done
  4937. else
  4938. local cached=$_p9k_asdf_file2versions[:$file]
  4939. if [[ $cached == $stat[1]:* ]]; then
  4940. local file_versions=(${(0)${cached#*:}})
  4941. else
  4942. local file_versions=()
  4943. { local lines=(${(@)${(@)${(f)"$(<$file)"}%$'\r'}/\#*}) } 2>/dev/null
  4944. local line
  4945. for line in $lines; do
  4946. local words=($=line)
  4947. (( $#words > 1 )) || continue
  4948. local installed=$_p9k_asdf_plugins[$words[1]]
  4949. [[ -n $installed ]] || continue
  4950. file_versions+=($words[1] ${${words:1}[(r)$installed]:-$words[2]})
  4951. done
  4952. _p9k_asdf_file2versions[:$file]=$stat[1]:${(pj:\0:)file_versions}
  4953. _p9k__state_dump_scheduled=1
  4954. fi
  4955. local plugin version
  4956. for plugin version in $file_versions; do
  4957. : ${versions[$plugin]=$version}
  4958. done
  4959. fi
  4960. return 0
  4961. }
  4962. function prompt_asdf() {
  4963. _p9k_asdf_check_meta || _p9k_asdf_init_meta || return
  4964. local -A versions
  4965. local -a stat
  4966. local -i has_global
  4967. local dirs=($_p9k__parent_dirs)
  4968. local mtimes=($_p9k__parent_mtimes)
  4969. if [[ $dirs[-1] != ~ ]]; then
  4970. zstat -A stat +mtime ~ 2>/dev/null || return
  4971. dirs+=(~)
  4972. mtimes+=($stat[1])
  4973. fi
  4974. local elem
  4975. for elem in ${(@)${:-{1..$#dirs}}/(#m)*/${${:-$MATCH:$_p9k__asdf_dir2files[$dirs[MATCH]]}#$MATCH:$mtimes[MATCH]:}}; do
  4976. if [[ $elem == *:* ]]; then
  4977. local dir=$dirs[${elem%%:*}]
  4978. zstat -A stat +mtime $dir 2>/dev/null || return
  4979. local files=($dir/.tool-versions(N) $dir/${(k)^_p9k_asdf_file_info}(N))
  4980. _p9k__asdf_dir2files[$dir]=$stat[1]:${(pj:\0:)files}
  4981. else
  4982. local files=(${(0)elem})
  4983. fi
  4984. if [[ ${files[1]:h} == ~ ]]; then
  4985. has_global=1
  4986. local -A local_versions=(${(kv)versions})
  4987. versions=()
  4988. fi
  4989. local file
  4990. for file in $files; do
  4991. [[ $file == */.tool-versions ]]
  4992. _p9k_asdf_parse_version_file $file $? || return
  4993. done
  4994. done
  4995. if (( ! has_global )); then
  4996. has_global=1
  4997. local -A local_versions=(${(kv)versions})
  4998. versions=()
  4999. fi
  5000. if [[ -r $ASDF_DEFAULT_TOOL_VERSIONS_FILENAME ]]; then
  5001. _p9k_asdf_parse_version_file $ASDF_DEFAULT_TOOL_VERSIONS_FILENAME 0 || return
  5002. fi
  5003. local plugin
  5004. for plugin in ${(k)_p9k_asdf_plugins}; do
  5005. local upper=${${(U)plugin//-/_}//İ/I}
  5006. if (( $+parameters[_POWERLEVEL9K_ASDF_${upper}_SOURCES] )); then
  5007. local sources=(${(P)${:-_POWERLEVEL9K_ASDF_${upper}_SOURCES}})
  5008. else
  5009. local sources=($_POWERLEVEL9K_ASDF_SOURCES)
  5010. fi
  5011. local version="${(P)${:-ASDF_${upper}_VERSION}}"
  5012. if [[ -n $version ]]; then
  5013. (( $sources[(I)shell] )) || continue
  5014. else
  5015. version=$local_versions[$plugin]
  5016. if [[ -n $version ]]; then
  5017. (( $sources[(I)local] )) || continue
  5018. else
  5019. version=$versions[$plugin]
  5020. [[ -n $version ]] || continue
  5021. (( $sources[(I)global] )) || continue
  5022. fi
  5023. fi
  5024. if [[ $version == $versions[$plugin] ]]; then
  5025. if (( $+parameters[_POWERLEVEL9K_ASDF_${upper}_PROMPT_ALWAYS_SHOW] )); then
  5026. (( _POWERLEVEL9K_ASDF_${upper}_PROMPT_ALWAYS_SHOW )) || continue
  5027. else
  5028. (( _POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW )) || continue
  5029. fi
  5030. fi
  5031. if [[ $version == system ]]; then
  5032. if (( $+parameters[_POWERLEVEL9K_ASDF_${upper}_SHOW_SYSTEM] )); then
  5033. (( _POWERLEVEL9K_ASDF_${upper}_SHOW_SYSTEM )) || continue
  5034. else
  5035. (( _POWERLEVEL9K_ASDF_SHOW_SYSTEM )) || continue
  5036. fi
  5037. fi
  5038. _p9k_get_icon $0_$upper ${upper}_ICON $plugin
  5039. _p9k_prompt_segment $0_$upper green $_p9k_color1 $'\1'$_p9k__ret 0 '' ${version//\%/%%}
  5040. done
  5041. }
  5042. _p9k_prompt_asdf_init() {
  5043. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[asdf]:-${${+functions[asdf]}:#0}}'
  5044. }
  5045. _p9k_haskell_stack_version() {
  5046. if ! _p9k_cache_stat_get $0 $1 ${STACK_ROOT:-~/.stack}/{pantry/pantry.sqlite3,stack.sqlite3}; then
  5047. local v
  5048. v="$(STACK_YAML=$1 stack \
  5049. --silent \
  5050. --no-install-ghc \
  5051. --skip-ghc-check \
  5052. --no-terminal \
  5053. --color=never \
  5054. --lock-file=read-only \
  5055. query compiler actual)" || v=
  5056. _p9k_cache_stat_set "$v"
  5057. fi
  5058. _p9k__ret=$_p9k__cache_val[1]
  5059. }
  5060. prompt_haskell_stack() {
  5061. if [[ -n $STACK_YAML ]]; then
  5062. (( ${_POWERLEVEL9K_HASKELL_STACK_SOURCES[(I)shell]} )) || return
  5063. _p9k_haskell_stack_version $STACK_YAML
  5064. else
  5065. (( ${_POWERLEVEL9K_HASKELL_STACK_SOURCES[(I)local|global]} )) || return
  5066. if _p9k_upglob stack.yaml; then
  5067. (( _POWERLEVEL9K_HASKELL_STACK_PROMPT_ALWAYS_SHOW )) || return
  5068. (( ${_POWERLEVEL9K_HASKELL_STACK_SOURCES[(I)global]} )) || return
  5069. _p9k_haskell_stack_version ${STACK_ROOT:-~/.stack}/global-project/stack.yaml
  5070. else
  5071. local -i idx=$?
  5072. (( ${_POWERLEVEL9K_HASKELL_STACK_SOURCES[(I)local]} )) || return
  5073. _p9k_haskell_stack_version $_p9k__parent_dirs[idx]/stack.yaml
  5074. fi
  5075. fi
  5076. [[ -n $_p9k__ret ]] || return
  5077. local v=$_p9k__ret
  5078. if (( !_POWERLEVEL9K_HASKELL_STACK_PROMPT_ALWAYS_SHOW )); then
  5079. _p9k_haskell_stack_version ${STACK_ROOT:-~/.stack}/global-project/stack.yaml
  5080. [[ $v == $_p9k__ret ]] && return
  5081. fi
  5082. _p9k_prompt_segment "$0" "yellow" "$_p9k_color1" 'HASKELL_ICON' 0 '' "${v//\%/%%}"
  5083. }
  5084. _p9k_prompt_haskell_stack_init() {
  5085. typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[stack]'
  5086. }
  5087. # Use two preexec hooks to survive https://github.com/MichaelAquilina/zsh-you-should-use with
  5088. # YSU_HARDCORE=1. See https://github.com/romkatv/powerlevel10k/issues/427.
  5089. _p9k_preexec1() {
  5090. _p9k_restore_special_params
  5091. unset __p9k_trapint
  5092. trap - INT
  5093. }
  5094. _p9k_preexec2() {
  5095. typeset -g _p9k__preexec_cmd=$2
  5096. _p9k__timer_start=EPOCHREALTIME
  5097. P9K_TTY=old
  5098. }
  5099. function _p9k_prompt_net_iface_init() {
  5100. typeset -g _p9k__public_ip_vpn=
  5101. typeset -g _p9k__public_ip_not_vpn=
  5102. typeset -g P9K_IP_IP=
  5103. typeset -g P9K_IP_INTERFACE=
  5104. typeset -g P9K_IP_TX_BYTES=
  5105. typeset -g P9K_IP_RX_BYTES=
  5106. typeset -g P9K_IP_TX_BYTES_DELTA=
  5107. typeset -g P9K_IP_RX_BYTES_DELTA=
  5108. typeset -g P9K_IP_TX_RATE=
  5109. typeset -g P9K_IP_RX_RATE=
  5110. typeset -g _p9__ip_timestamp=
  5111. typeset -g _p9k__vpn_ip_ips=()
  5112. [[ -z $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]] && _p9k__public_ip_not_vpn=1
  5113. _p9k__async_segments_compute+='_p9k_worker_invoke net_iface _p9k_prompt_net_iface_compute'
  5114. }
  5115. # reads `iface2ip` and sets `ifaces` and `ips`
  5116. function _p9k_prompt_net_iface_match() {
  5117. local iface_regex="^($1)\$" iface ip
  5118. ips=()
  5119. ifaces=()
  5120. for iface ip in "${(@)iface2ip}"; do
  5121. [[ $iface =~ $iface_regex ]] || continue
  5122. ifaces+=$iface
  5123. ips+=$ip
  5124. done
  5125. return $(($#ips == 0))
  5126. }
  5127. function _p9k_prompt_net_iface_compute() {
  5128. _p9k_worker_async _p9k_prompt_net_iface_async _p9k_prompt_net_iface_sync
  5129. }
  5130. function _p9k_prompt_net_iface_async() {
  5131. # netstat -inbI en0
  5132. local iface ip line var
  5133. typeset -a iface2ip ips ifaces
  5134. if (( $+commands[ifconfig] )); then
  5135. for line in ${(f)"$(command ifconfig 2>/dev/null)"}; do
  5136. if [[ $line == (#b)([^[:space:]]##):[[:space:]]##flags=([[:xdigit:]]##)'<'* ]]; then
  5137. [[ $match[2] == *[13579bdfBDF] ]] && iface=$match[1] || iface=
  5138. elif [[ -n $iface && $line == (#b)[[:space:]]##inet[[:space:]]##([0-9.]##)* ]]; then
  5139. iface2ip+=($iface $match[1])
  5140. iface=
  5141. fi
  5142. done
  5143. elif (( $+commands[ip] )); then
  5144. for line in ${(f)"$(command ip -4 a show 2>/dev/null)"}; do
  5145. if [[ $line == (#b)<->:[[:space:]]##([^:]##):[[:space:]]##\<([^\>]#)\>* ]]; then
  5146. [[ ,$match[2], == *,UP,* ]] && iface=$match[1] || iface=
  5147. elif [[ -n $iface && $line == (#b)[[:space:]]##inet[[:space:]]##([0-9.]##)* ]]; then
  5148. iface2ip+=($iface $match[1])
  5149. iface=
  5150. fi
  5151. done
  5152. fi
  5153. if _p9k_prompt_net_iface_match $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE; then
  5154. local public_ip_vpn=1
  5155. local public_ip_not_vpn=
  5156. else
  5157. local public_ip_vpn=
  5158. local public_ip_not_vpn=1
  5159. fi
  5160. if _p9k_prompt_net_iface_match $_POWERLEVEL9K_IP_INTERFACE; then
  5161. local ip_ip=$ips[1] ip_interface=$ifaces[1] ip_timestamp=$EPOCHREALTIME
  5162. local ip_tx_bytes ip_rx_bytes ip_tx_rate ip_rx_rate
  5163. if [[ $_p9k_os == (Linux|Android) ]]; then
  5164. if [[ -r /sys/class/net/$ifaces[1]/statistics/tx_bytes &&
  5165. -r /sys/class/net/$ifaces[1]/statistics/rx_bytes ]]; then
  5166. _p9k_read_file /sys/class/net/$ifaces[1]/statistics/tx_bytes &&
  5167. [[ $_p9k__ret == <-> ]] && ip_tx_bytes=$_p9k__ret &&
  5168. _p9k_read_file /sys/class/net/$ifaces[1]/statistics/rx_bytes &&
  5169. [[ $_p9k__ret == <-> ]] && ip_rx_bytes=$_p9k__ret || { ip_tx_bytes=; ip_rx_bytes=; }
  5170. fi
  5171. elif [[ $_p9k_os == (BSD|OSX) && $+commands[netstat] == 1 ]]; then
  5172. local -a lines
  5173. if lines=(${(f)"$(netstat -inbI $ifaces[1])"}); then
  5174. local header=($=lines[1])
  5175. local -i rx_idx=$header[(Ie)Ibytes]
  5176. local -i tx_idx=$header[(Ie)Obytes]
  5177. if (( rx_idx && tx_idx )); then
  5178. ip_tx_bytes=0
  5179. ip_rx_bytes=0
  5180. for line in ${lines:1}; do
  5181. (( ip_rx_bytes += ${line[(w)rx_idx]} ))
  5182. (( ip_tx_bytes += ${line[(w)tx_idx]} ))
  5183. done
  5184. fi
  5185. fi
  5186. fi
  5187. if [[ -n $ip_rx_bytes ]]; then
  5188. if [[ $ip_ip == $P9K_IP_IP && $ifaces[1] == $P9K_IP_INTERFACE ]]; then
  5189. local -F t='ip_timestamp - _p9__ip_timestamp'
  5190. if (( t <= 0 )); then
  5191. ip_tx_rate=${P9K_IP_TX_RATE:-0 B/s}
  5192. ip_rx_rate=${P9K_IP_RX_RATE:-0 B/s}
  5193. else
  5194. _p9k_human_readable_bytes $(((ip_tx_bytes - P9K_IP_TX_BYTES) / t))
  5195. [[ $_p9k__ret == *B ]] && ip_tx_rate="$_p9k__ret[1,-2] B/s" || ip_tx_rate="$_p9k__ret[1,-2] $_p9k__ret[-1]iB/s"
  5196. _p9k_human_readable_bytes $(((ip_rx_bytes - P9K_IP_RX_BYTES) / t))
  5197. [[ $_p9k__ret == *B ]] && ip_rx_rate="$_p9k__ret[1,-2] B/s" || ip_rx_rate="$_p9k__ret[1,-2] $_p9k__ret[-1]iB/s"
  5198. fi
  5199. else
  5200. ip_tx_rate='0 B/s'
  5201. ip_rx_rate='0 B/s'
  5202. fi
  5203. fi
  5204. else
  5205. local ip_ip= ip_interface= ip_tx_bytes= ip_rx_bytes= ip_tx_rate= ip_rx_rate= ip_timestamp=
  5206. fi
  5207. if _p9k_prompt_net_iface_match $_POWERLEVEL9K_VPN_IP_INTERFACE; then
  5208. if (( _POWERLEVEL9K_VPN_IP_SHOW_ALL )); then
  5209. local vpn_ip_ips=($ips)
  5210. else
  5211. local vpn_ip_ips=($ips[1])
  5212. fi
  5213. else
  5214. local vpn_ip_ips=()
  5215. fi
  5216. [[ $_p9k__public_ip_vpn == $public_ip_vpn &&
  5217. $_p9k__public_ip_not_vpn == $public_ip_not_vpn &&
  5218. $P9K_IP_IP == $ip_ip &&
  5219. $P9K_IP_INTERFACE == $ip_interface &&
  5220. $P9K_IP_TX_BYTES == $ip_tx_bytes &&
  5221. $P9K_IP_RX_BYTES == $ip_rx_bytes &&
  5222. $P9K_IP_TX_RATE == $ip_tx_rate &&
  5223. $P9K_IP_RX_RATE == $ip_rx_rate &&
  5224. "$_p9k__vpn_ip_ips" == "$vpn_ip_ips" ]] && return 1
  5225. if [[ "$_p9k__vpn_ip_ips" == "$vpn_ip_ips" ]]; then
  5226. echo -n 0
  5227. else
  5228. echo -n 1
  5229. fi
  5230. _p9k__public_ip_vpn=$public_ip_vpn
  5231. _p9k__public_ip_not_vpn=$public_ip_not_vpn
  5232. P9K_IP_IP=$ip_ip
  5233. P9K_IP_INTERFACE=$ip_interface
  5234. if [[ -n $ip_tx_bytes && -n $P9K_IP_TX_BYTES ]]; then
  5235. P9K_IP_TX_BYTES_DELTA=$((ip_tx_bytes - P9K_IP_TX_BYTES))
  5236. else
  5237. P9K_IP_TX_BYTES_DELTA=
  5238. fi
  5239. if [[ -n $ip_rx_bytes && -n $P9K_IP_RX_BYTES ]]; then
  5240. P9K_IP_RX_BYTES_DELTA=$((ip_rx_bytes - P9K_IP_RX_BYTES))
  5241. else
  5242. P9K_IP_RX_BYTES_DELTA=
  5243. fi
  5244. P9K_IP_TX_BYTES=$ip_tx_bytes
  5245. P9K_IP_RX_BYTES=$ip_rx_bytes
  5246. P9K_IP_TX_RATE=$ip_tx_rate
  5247. P9K_IP_RX_RATE=$ip_rx_rate
  5248. _p9__ip_timestamp=$ip_timestamp
  5249. _p9k__vpn_ip_ips=($vpn_ip_ips)
  5250. _p9k_print_params \
  5251. _p9k__public_ip_vpn \
  5252. _p9k__public_ip_not_vpn \
  5253. P9K_IP_IP \
  5254. P9K_IP_INTERFACE \
  5255. P9K_IP_TX_BYTES \
  5256. P9K_IP_RX_BYTES \
  5257. P9K_IP_TX_BYTES_DELTA \
  5258. P9K_IP_RX_BYTES_DELTA \
  5259. P9K_IP_TX_RATE \
  5260. P9K_IP_RX_RATE \
  5261. _p9__ip_timestamp \
  5262. _p9k__vpn_ip_ips
  5263. echo -E - 'reset=1'
  5264. }
  5265. _p9k_prompt_net_iface_sync() {
  5266. local -i vpn_ip_changed=$REPLY[1]
  5267. REPLY[1]=""
  5268. eval $REPLY
  5269. (( vpn_ip_changed )) && REPLY+='; _p9k_vpn_ip_render'
  5270. _p9k_worker_reply $REPLY
  5271. }
  5272. function _p9k_set_prompt() {
  5273. local -i _p9k__vcs_called
  5274. PROMPT=
  5275. RPROMPT=
  5276. [[ $1 == instant_ ]] || PROMPT+='${$((_p9k_on_expand()))+}%{${_p9k__raw_msg-}${_p9k__raw_msg::=}%}'
  5277. PROMPT+=$_p9k_prompt_prefix_left
  5278. local -i _p9k__has_upglob
  5279. local -i left_idx=1 right_idx=1 num_lines=$#_p9k_line_segments_left
  5280. for _p9k__line_index in {1..$num_lines}; do
  5281. local right=
  5282. if (( !_POWERLEVEL9K_DISABLE_RPROMPT )); then
  5283. _p9k__dir=
  5284. _p9k__prompt=
  5285. _p9k__segment_index=right_idx
  5286. _p9k__prompt_side=right
  5287. if [[ $1 == instant_ ]]; then
  5288. for _p9k__segment_name in ${${(0)_p9k_line_segments_right[_p9k__line_index]}%_joined}; do
  5289. if (( $+functions[instant_prompt_$_p9k__segment_name] )); then
  5290. local disabled=_POWERLEVEL9K_${${(U)_p9k__segment_name}//İ/I}_DISABLED_DIR_PATTERN
  5291. if [[ $_p9k__cwd != ${(P)~disabled} ]]; then
  5292. local -i len=$#_p9k__prompt
  5293. _p9k__non_hermetic_expansion=0
  5294. instant_prompt_$_p9k__segment_name
  5295. if (( _p9k__non_hermetic_expansion )); then
  5296. _p9k__prompt[len+1,-1]=
  5297. fi
  5298. fi
  5299. fi
  5300. ((++_p9k__segment_index))
  5301. done
  5302. else
  5303. for _p9k__segment_name in ${${(0)_p9k_line_segments_right[_p9k__line_index]}%_joined}; do
  5304. local cond=$_p9k__segment_cond_right[_p9k__segment_index]
  5305. if [[ -z $cond || -n ${(e)cond} ]]; then
  5306. local disabled=_POWERLEVEL9K_${${(U)_p9k__segment_name}//İ/I}_DISABLED_DIR_PATTERN
  5307. if [[ $_p9k__cwd != ${(P)~disabled} ]]; then
  5308. local val=$_p9k__segment_val_right[_p9k__segment_index]
  5309. if [[ -n $val ]]; then
  5310. _p9k__prompt+=$val
  5311. else
  5312. if [[ $_p9k__segment_name == custom_* ]]; then
  5313. _p9k_custom_prompt $_p9k__segment_name[8,-1]
  5314. elif (( $+functions[prompt_$_p9k__segment_name] )); then
  5315. prompt_$_p9k__segment_name
  5316. fi
  5317. fi
  5318. fi
  5319. fi
  5320. ((++_p9k__segment_index))
  5321. done
  5322. fi
  5323. _p9k__prompt=${${_p9k__prompt//$' %{\b'/'%{%G'}//$' \b'}
  5324. right_idx=_p9k__segment_index
  5325. if [[ -n $_p9k__prompt || $_p9k_line_never_empty_right[_p9k__line_index] == 1 ]]; then
  5326. right=$_p9k_line_prefix_right[_p9k__line_index]$_p9k__prompt$_p9k_line_suffix_right[_p9k__line_index]
  5327. fi
  5328. fi
  5329. unset _p9k__dir
  5330. _p9k__prompt=$_p9k_line_prefix_left[_p9k__line_index]
  5331. _p9k__segment_index=left_idx
  5332. _p9k__prompt_side=left
  5333. if [[ $1 == instant_ ]]; then
  5334. for _p9k__segment_name in ${${(0)_p9k_line_segments_left[_p9k__line_index]}%_joined}; do
  5335. if (( $+functions[instant_prompt_$_p9k__segment_name] )); then
  5336. local disabled=_POWERLEVEL9K_${${(U)_p9k__segment_name}//İ/I}_DISABLED_DIR_PATTERN
  5337. if [[ $_p9k__cwd != ${(P)~disabled} ]]; then
  5338. local -i len=$#_p9k__prompt
  5339. _p9k__non_hermetic_expansion=0
  5340. instant_prompt_$_p9k__segment_name
  5341. if (( _p9k__non_hermetic_expansion )); then
  5342. _p9k__prompt[len+1,-1]=
  5343. fi
  5344. fi
  5345. fi
  5346. ((++_p9k__segment_index))
  5347. done
  5348. else
  5349. for _p9k__segment_name in ${${(0)_p9k_line_segments_left[_p9k__line_index]}%_joined}; do
  5350. local cond=$_p9k__segment_cond_left[_p9k__segment_index]
  5351. if [[ -z $cond || -n ${(e)cond} ]]; then
  5352. local disabled=_POWERLEVEL9K_${${(U)_p9k__segment_name}//İ/I}_DISABLED_DIR_PATTERN
  5353. if [[ $_p9k__cwd != ${(P)~disabled} ]]; then
  5354. local val=$_p9k__segment_val_left[_p9k__segment_index]
  5355. if [[ -n $val ]]; then
  5356. _p9k__prompt+=$val
  5357. else
  5358. if [[ $_p9k__segment_name == custom_* ]]; then
  5359. _p9k_custom_prompt $_p9k__segment_name[8,-1]
  5360. elif (( $+functions[prompt_$_p9k__segment_name] )); then
  5361. prompt_$_p9k__segment_name
  5362. fi
  5363. fi
  5364. fi
  5365. fi
  5366. ((++_p9k__segment_index))
  5367. done
  5368. fi
  5369. _p9k__prompt=${${_p9k__prompt//$' %{\b'/'%{%G'}//$' \b'}
  5370. left_idx=_p9k__segment_index
  5371. _p9k__prompt+=$_p9k_line_suffix_left[_p9k__line_index]
  5372. if (( $+_p9k__dir || (_p9k__line_index != num_lines && $#right) )); then
  5373. _p9k__prompt='${${:-${_p9k__d::=0}${_p9k__rprompt::='$right'}${_p9k__lprompt::='$_p9k__prompt'}}+}'
  5374. _p9k__prompt+=$_p9k_gap_pre
  5375. if (( $+_p9k__dir )); then
  5376. if (( _p9k__line_index == num_lines && (_POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS > 0 || _POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS_PCT > 0) )); then
  5377. local a=$_POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS
  5378. local f=$((0.01*_POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS_PCT))'*_p9k__clm'
  5379. _p9k__prompt+="\${\${_p9k__h::=$((($a<$f)*$f+($a>=$f)*$a))}+}"
  5380. else
  5381. _p9k__prompt+='${${_p9k__h::=0}+}'
  5382. fi
  5383. if [[ $_POWERLEVEL9K_DIR_MAX_LENGTH == <->('%'|) ]]; then
  5384. local lim=
  5385. if [[ $_POWERLEVEL9K_DIR_MAX_LENGTH[-1] == '%' ]]; then
  5386. lim="$_p9k__dir_len-$((0.01*$_POWERLEVEL9K_DIR_MAX_LENGTH[1,-2]))*_p9k__clm"
  5387. else
  5388. lim=$((_p9k__dir_len-_POWERLEVEL9K_DIR_MAX_LENGTH))
  5389. ((lim <= 0)) && lim=
  5390. fi
  5391. if [[ -n $lim ]]; then
  5392. _p9k__prompt+='${${${$((_p9k__h<_p9k__m+'$lim')):#1}:-${_p9k__h::=$((_p9k__m+'$lim'))}}+}'
  5393. fi
  5394. fi
  5395. _p9k__prompt+='${${_p9k__d::=$((_p9k__m-_p9k__h))}+}'
  5396. _p9k__prompt+='${_p9k__lprompt/\%\{d\%\}*\%\{d\%\}/${_p9k__'$_p9k__line_index'ldir-'$_p9k__dir'}}'
  5397. _p9k__prompt+='${${_p9k__m::=$((_p9k__d+_p9k__h))}+}'
  5398. else
  5399. _p9k__prompt+='${_p9k__lprompt}'
  5400. fi
  5401. ((_p9k__line_index != num_lines && $#right)) && _p9k__prompt+=$_p9k_line_gap_post[_p9k__line_index]
  5402. fi
  5403. if (( _p9k__line_index == num_lines )); then
  5404. [[ -n $right ]] && RPROMPT=$_p9k_prompt_prefix_right$right$_p9k_prompt_suffix_right
  5405. _p9k__prompt='${_p9k__'$_p9k__line_index'-'$_p9k__prompt'}'$_p9k_prompt_suffix_left
  5406. [[ $1 == instant_ ]] || PROMPT+=$_p9k__prompt
  5407. else
  5408. [[ -n $right ]] || _p9k__prompt+=$'\n'
  5409. PROMPT+='${_p9k__'$_p9k__line_index'-'$_p9k__prompt'}'
  5410. fi
  5411. done
  5412. _p9k__prompt_side=
  5413. (( $#_p9k_cache < _POWERLEVEL9K_MAX_CACHE_SIZE )) || _p9k_cache=()
  5414. (( $#_p9k__cache_ephemeral < _POWERLEVEL9K_MAX_CACHE_SIZE )) || _p9k__cache_ephemeral=()
  5415. [[ -n $RPROMPT ]] || unset RPROMPT
  5416. }
  5417. _p9k_set_instant_prompt() {
  5418. local saved_prompt=$PROMPT
  5419. local saved_rprompt=$RPROMPT
  5420. _p9k_set_prompt instant_
  5421. typeset -g _p9k__instant_prompt=$PROMPT$'\x1f'$_p9k__prompt$'\x1f'$RPROMPT
  5422. PROMPT=$saved_prompt
  5423. RPROMPT=$saved_rprompt
  5424. [[ -n $RPROMPT ]] || unset RPROMPT
  5425. }
  5426. typeset -gri __p9k_instant_prompt_version=45
  5427. _p9k_dump_instant_prompt() {
  5428. local user=${(%):-%n}
  5429. local root_dir=${__p9k_dump_file:h}
  5430. local prompt_dir=${root_dir}/p10k-$user
  5431. local root_file=$root_dir/p10k-instant-prompt-$user.zsh
  5432. local prompt_file=$prompt_dir/prompt-${#_p9k__cwd}
  5433. [[ -d $prompt_dir ]] || mkdir -p $prompt_dir || return
  5434. [[ -w $root_dir && -w $prompt_dir ]] || return
  5435. if [[ ! -e $root_file ]]; then
  5436. local tmp=$root_file.tmp.$$
  5437. local -i fd
  5438. sysopen -a -m 600 -o creat,trunc -u fd -- $tmp || return
  5439. {
  5440. [[ $TERM == (screen*|tmux*) ]] && local screen='-n' || local screen='-z'
  5441. local -a display_v=("${_p9k__display_v[@]}")
  5442. local -i i
  5443. for ((i = 6; i <= $#display_v; i+=2)); do display_v[i]=show; done
  5444. display_v[2]=hide
  5445. display_v[4]=hide
  5446. local gitstatus_dir=${${_POWERLEVEL9K_GITSTATUS_DIR:A}:-${__p9k_root_dir}/gitstatus}
  5447. local gitstatus_header
  5448. if [[ -r $gitstatus_dir/install.info ]]; then
  5449. IFS= read -r gitstatus_header <$gitstatus_dir/install.info || return
  5450. fi
  5451. >&$fd print -r -- '[[ -t 0 && -t 1 && -t 2 && -o interactive && -o zle && -o no_xtrace ]] &&
  5452. ! (( ${+__p9k_instant_prompt_disabled} || ZSH_SUBSHELL || ${+ZSH_SCRIPT} || ${+ZSH_EXECUTION_STRING} )) || return 0'
  5453. >&$fd print -r -- "() {
  5454. $__p9k_intro_no_locale
  5455. typeset -gi __p9k_instant_prompt_disabled=1
  5456. [[ \$ZSH_VERSION == ${(q)ZSH_VERSION} && \$ZSH_PATCHLEVEL == ${(q)ZSH_PATCHLEVEL} &&
  5457. $screen \${(M)TERM:#(screen*|tmux*)} &&
  5458. \${#\${(M)VTE_VERSION:#(<1-4602>|4801)}} == ${#${(M)VTE_VERSION:#(<1-4602>|4801)}} &&
  5459. \$POWERLEVEL9K_DISABLE_INSTANT_PROMPT != 'true' &&
  5460. \$POWERLEVEL9K_INSTANT_PROMPT != 'off' ]] || return
  5461. typeset -g __p9k_instant_prompt_param_sig=${(q+)_p9k__param_sig}
  5462. local gitstatus_dir=${(q)gitstatus_dir}
  5463. local gitstatus_header=${(q)gitstatus_header}
  5464. local -i ZLE_RPROMPT_INDENT=${ZLE_RPROMPT_INDENT:-1}
  5465. local PROMPT_EOL_MARK=${(q)PROMPT_EOL_MARK-%B%S%#%s%b}
  5466. [[ -n \$SSH_CLIENT || -n \$SSH_TTY || -n \$SSH_CONNECTION ]] && local ssh=1 || local ssh=0
  5467. local cr=\$'\r' lf=\$'\n' esc=\$'\e[' rs=$'\x1e' us=$'\x1f'
  5468. local -i height=${_POWERLEVEL9K_INSTANT_PROMPT_COMMAND_LINES-1}
  5469. local prompt_dir=${(q)prompt_dir}"
  5470. if (( ! ${+_POWERLEVEL9K_INSTANT_PROMPT_COMMAND_LINES} )); then
  5471. >&$fd print -r -- '
  5472. (( _z4h_can_save_restore_screen == 1 )) && height=0'
  5473. fi
  5474. >&$fd print -r -- '
  5475. local real_gitstatus_header
  5476. if [[ -r $gitstatus_dir/install.info ]]; then
  5477. IFS= read -r real_gitstatus_header <$gitstatus_dir/install.info || real_gitstatus_header=borked
  5478. fi
  5479. [[ $real_gitstatus_header == $gitstatus_header ]] || return
  5480. zmodload zsh/langinfo zsh/terminfo zsh/system || return
  5481. if [[ $langinfo[CODESET] != (utf|UTF)(-|)8 ]]; then
  5482. local loc_cmd=$commands[locale]
  5483. [[ -z $loc_cmd ]] && loc_cmd='${(q)commands[locale]}'
  5484. if [[ -x $loc_cmd ]]; then
  5485. local -a locs
  5486. if locs=(${(@M)$(locale -a 2>/dev/null):#*.(utf|UTF)(-|)8}) && (( $#locs )); then
  5487. local loc=${locs[(r)(#i)C.UTF(-|)8]:-${locs[(r)(#i)en_US.UTF(-|)8]:-$locs[1]}}
  5488. [[ -n $LC_ALL ]] && local LC_ALL=$loc || local LC_CTYPE=$loc
  5489. fi
  5490. fi
  5491. fi
  5492. (( terminfo[colors] == '${terminfo[colors]:-0}' )) || return
  5493. (( $+terminfo[cuu] && $+terminfo[cuf] && $+terminfo[ed] && $+terminfo[sc] && $+terminfo[rc] )) || return
  5494. local pwd=${(%):-%/}
  5495. [[ $pwd == /* ]] || return
  5496. local prompt_file=$prompt_dir/prompt-${#pwd}
  5497. local key=$pwd:$ssh:${(%):-%#}
  5498. local content
  5499. if [[ ! -e $prompt_file ]]; then
  5500. typeset -gi __p9k_instant_prompt_sourced='$__p9k_instant_prompt_version'
  5501. return 1
  5502. fi
  5503. { content="$(<$prompt_file)" } 2>/dev/null || return
  5504. local tail=${content##*$rs$key$us}
  5505. if (( ${#tail} == ${#content} )); then
  5506. typeset -gi __p9k_instant_prompt_sourced='$__p9k_instant_prompt_version'
  5507. return 1
  5508. fi
  5509. local _p9k__ipe
  5510. local P9K_PROMPT=instant
  5511. if [[ -z $P9K_TTY || $P9K_TTY == old && -n ${_P9K_TTY:#$TTY} ]]; then'
  5512. if (( _POWERLEVEL9K_NEW_TTY_MAX_AGE_SECONDS < 0 )); then
  5513. >&$fd print -r -- ' typeset -gx P9K_TTY=new'
  5514. else
  5515. >&$fd print -r -- '
  5516. typeset -gx P9K_TTY=old
  5517. zmodload -F zsh/stat b:zstat || return
  5518. zmodload zsh/datetime || return
  5519. local -a stat
  5520. if zstat -A stat +ctime -- $TTY 2>/dev/null &&
  5521. (( EPOCHREALTIME - stat[1] < '$_POWERLEVEL9K_NEW_TTY_MAX_AGE_SECONDS' )); then
  5522. P9K_TTY=new
  5523. fi'
  5524. fi
  5525. >&$fd print -r -- ' fi
  5526. typeset -gx _P9K_TTY=$TTY
  5527. local -i _p9k__empty_line_i=3 _p9k__ruler_i=3
  5528. local -A _p9k_display_k=('${(j: :)${(@q)${(kv)_p9k_display_k}}}')
  5529. local -a _p9k__display_v=('${(j: :)${(@q)display_v}}')
  5530. function p10k() {
  5531. '$__p9k_intro'
  5532. [[ $1 == display ]] || return
  5533. shift
  5534. local -i k dump
  5535. local opt prev new pair list name var
  5536. while getopts ":ha" opt; do
  5537. case $opt in
  5538. a) dump=1;;
  5539. h) return 0;;
  5540. ?) return 1;;
  5541. esac
  5542. done
  5543. if (( dump )); then
  5544. reply=()
  5545. shift $((OPTIND-1))
  5546. (( ARGC )) || set -- "*"
  5547. for opt; do
  5548. for k in ${(u@)_p9k_display_k[(I)$opt]:/(#m)*/$_p9k_display_k[$MATCH]}; do
  5549. reply+=($_p9k__display_v[k,k+1])
  5550. done
  5551. done
  5552. return 0
  5553. fi
  5554. for opt in "${@:$OPTIND}"; do
  5555. pair=(${(s:=:)opt})
  5556. list=(${(s:,:)${pair[2]}})
  5557. if [[ ${(b)pair[1]} == $pair[1] ]]; then
  5558. local ks=($_p9k_display_k[$pair[1]])
  5559. else
  5560. local ks=(${(u@)_p9k_display_k[(I)$pair[1]]:/(#m)*/$_p9k_display_k[$MATCH]})
  5561. fi
  5562. for k in $ks; do
  5563. if (( $#list == 1 )); then
  5564. [[ $_p9k__display_v[k+1] == $list[1] ]] && continue
  5565. new=$list[1]
  5566. else
  5567. new=${list[list[(I)$_p9k__display_v[k+1]]+1]:-$list[1]}
  5568. [[ $_p9k__display_v[k+1] == $new ]] && continue
  5569. fi
  5570. _p9k__display_v[k+1]=$new
  5571. name=$_p9k__display_v[k]
  5572. if [[ $name == (empty_line|ruler) ]]; then
  5573. var=_p9k__${name}_i
  5574. [[ $new == hide ]] && typeset -gi $var=3 || unset $var
  5575. elif [[ $name == (#b)(<->)(*) ]]; then
  5576. var=_p9k__${match[1]}${${${${match[2]//\/}/#left/l}/#right/r}/#gap/g}
  5577. [[ $new == hide ]] && typeset -g $var= || unset $var
  5578. fi
  5579. done
  5580. done
  5581. }'
  5582. if (( _POWERLEVEL9K_PROMPT_ADD_NEWLINE )); then
  5583. >&$fd print -r -- ' [[ $P9K_TTY == old ]] && { unset _p9k__empty_line_i; _p9k__display_v[2]=print }'
  5584. fi
  5585. if (( _POWERLEVEL9K_SHOW_RULER )); then
  5586. >&$fd print -r -- '[[ $P9K_TTY == old ]] && { unset _p9k__ruler_i; _p9k__display_v[4]=print }'
  5587. fi
  5588. if (( $+functions[p10k-on-init] )); then
  5589. >&$fd print -r -- '
  5590. p10k-on-init() { '$functions[p10k-on-init]' }'
  5591. fi
  5592. if (( $+functions[p10k-on-pre-prompt] )); then
  5593. >&$fd print -r -- '
  5594. p10k-on-pre-prompt() { '$functions[p10k-on-pre-prompt]' }'
  5595. fi
  5596. if (( $+functions[p10k-on-post-prompt] )); then
  5597. >&$fd print -r -- '
  5598. p10k-on-post-prompt() { '$functions[p10k-on-post-prompt]' }'
  5599. fi
  5600. if (( $+functions[p10k-on-post-widget] )); then
  5601. >&$fd print -r -- '
  5602. p10k-on-post-widget() { '$functions[p10k-on-post-widget]' }'
  5603. fi
  5604. if (( $+functions[p10k-on-init] )); then
  5605. >&$fd print -r -- '
  5606. p10k-on-init'
  5607. fi
  5608. local pat idx var
  5609. for pat idx var in $_p9k_show_on_command; do
  5610. >&$fd print -r -- "
  5611. local $var=
  5612. _p9k__display_v[$idx]=hide"
  5613. done
  5614. if (( $+functions[p10k-on-pre-prompt] )); then
  5615. >&$fd print -r -- '
  5616. p10k-on-pre-prompt'
  5617. fi
  5618. if (( $+functions[p10k-on-init] )); then
  5619. >&$fd print -r -- '
  5620. unfunction p10k-on-init'
  5621. fi
  5622. if (( $+functions[p10k-on-pre-prompt] )); then
  5623. >&$fd print -r -- '
  5624. unfunction p10k-on-pre-prompt'
  5625. fi
  5626. if (( $+functions[p10k-on-post-prompt] )); then
  5627. >&$fd print -r -- '
  5628. unfunction p10k-on-post-prompt'
  5629. fi
  5630. if (( $+functions[p10k-on-post-widget] )); then
  5631. >&$fd print -r -- '
  5632. unfunction p10k-on-post-widget'
  5633. fi
  5634. >&$fd print -r -- '
  5635. () {
  5636. '$functions[_p9k_init_toolbox]'
  5637. }
  5638. trap "unset -m _p9k__\*; unfunction p10k" EXIT
  5639. local -a _p9k_t=("${(@ps:$us:)${tail%%$rs*}}")
  5640. if [[ $+VTE_VERSION == 1 || $TERM_PROGRAM == Hyper ]] && (( $+commands[stty] )); then
  5641. if [[ $TERM_PROGRAM == Hyper ]]; then
  5642. local bad_lines=40 bad_columns=100
  5643. else
  5644. local bad_lines=24 bad_columns=80
  5645. fi
  5646. if (( LINES == bad_lines && COLUMNS == bad_columns )); then
  5647. zmodload -F zsh/stat b:zstat || return
  5648. zmodload zsh/datetime || return
  5649. local -a tty_ctime
  5650. if ! zstat -A tty_ctime +ctime -- $TTY 2>/dev/null || (( tty_ctime[1] + 2 > EPOCHREALTIME )); then
  5651. local -F deadline=$((EPOCHREALTIME+0.025))
  5652. local tty_size
  5653. while true; do
  5654. if (( EPOCHREALTIME > deadline )) || ! tty_size="$(command stty size 2>/dev/null)" || [[ $tty_size != <->" "<-> ]]; then
  5655. (( $+_p9k__ruler_i )) || local -i _p9k__ruler_i=1
  5656. local _p9k__g= _p9k__'$#_p9k_line_segments_right'r= _p9k__'$#_p9k_line_segments_right'r_frame=
  5657. break
  5658. fi
  5659. if [[ $tty_size != "$bad_lines $bad_columns" ]]; then
  5660. local lines_columns=(${=tty_size})
  5661. local LINES=$lines_columns[1]
  5662. local COLUMNS=$lines_columns[2]
  5663. break
  5664. fi
  5665. done
  5666. fi
  5667. fi
  5668. fi'
  5669. (( __p9k_ksh_arrays )) && >&$fd print -r -- ' setopt ksh_arrays'
  5670. (( __p9k_sh_glob )) && >&$fd print -r -- ' setopt sh_glob'
  5671. >&$fd print -r -- ' typeset -ga __p9k_used_instant_prompt=("${(@e)_p9k_t[-3,-1]}")'
  5672. (( __p9k_ksh_arrays )) && >&$fd print -r -- ' unsetopt ksh_arrays'
  5673. (( __p9k_sh_glob )) && >&$fd print -r -- ' unsetopt sh_glob'
  5674. >&$fd print -r -- '
  5675. local -i prompt_height=${#${__p9k_used_instant_prompt[1]//[^$lf]}}
  5676. (( height += prompt_height ))
  5677. local _p9k__ret
  5678. function _p9k_prompt_length() {
  5679. local -i COLUMNS=1024
  5680. local -i x y=${#1} m
  5681. if (( y )); then
  5682. while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
  5683. x=y
  5684. (( y *= 2 ))
  5685. done
  5686. while (( y > x + 1 )); do
  5687. (( m = x + (y - x) / 2 ))
  5688. (( ${${(%):-$1%$m(l.x.y)}[-1]} = m ))
  5689. done
  5690. fi
  5691. typeset -g _p9k__ret=$x
  5692. }
  5693. local out=${(%):-%b%k%f%s%u}
  5694. if [[ $P9K_TTY == old && ( $+VTE_VERSION == 0 && $TERM_PROGRAM != Hyper || $+_p9k__g == 0 ) ]]; then
  5695. local mark=${(e)PROMPT_EOL_MARK}
  5696. [[ $mark == "%B%S%#%s%b" ]] && _p9k__ret=1 || _p9k_prompt_length $mark
  5697. local -i fill=$((COLUMNS > _p9k__ret ? COLUMNS - _p9k__ret : 0))
  5698. out+="${(%):-$mark${(pl.$fill.. .)}$cr%b%k%f%s%u%E}"
  5699. else
  5700. out+="${(%):-$cr%E}"
  5701. fi
  5702. if (( _z4h_can_save_restore_screen != 1 )); then
  5703. (( height )) && out+="${(pl.$height..$lf.)}$esc${height}A"
  5704. out+="$terminfo[sc]"
  5705. fi
  5706. out+=${(%):-"$__p9k_used_instant_prompt[1]$__p9k_used_instant_prompt[2]"}
  5707. if [[ -n $__p9k_used_instant_prompt[3] ]]; then
  5708. _p9k_prompt_length "$__p9k_used_instant_prompt[2]"
  5709. local -i left_len=_p9k__ret
  5710. _p9k_prompt_length "$__p9k_used_instant_prompt[3]"
  5711. if (( _p9k__ret )); then
  5712. local -i gap=$((COLUMNS - left_len - _p9k__ret - ZLE_RPROMPT_INDENT))
  5713. if (( gap >= 40 )); then
  5714. out+="${(pl.$gap.. .)}${(%):-${__p9k_used_instant_prompt[3]}%b%k%f%s%u}$cr$esc${left_len}C"
  5715. fi
  5716. fi
  5717. fi
  5718. if (( _z4h_can_save_restore_screen == 1 )); then
  5719. if (( height )); then
  5720. out+="$cr${(pl:$((height-prompt_height))::\n:)}$esc${height}A$terminfo[sc]$out"
  5721. else
  5722. out+="$cr${(pl:$((height-prompt_height))::\n:)}$terminfo[sc]$out"
  5723. fi
  5724. fi
  5725. typeset -g __p9k_instant_prompt_output=${TMPDIR:-/tmp}/p10k-instant-prompt-output-${(%):-%n}-$$
  5726. { echo -n > $__p9k_instant_prompt_output } || return
  5727. print -rn -- "${out}${esc}?2004h" || return
  5728. if (( $+commands[stty] )); then
  5729. command stty -icanon 2>/dev/null
  5730. fi
  5731. local fd_null
  5732. sysopen -ru fd_null /dev/null || return
  5733. exec {__p9k_fd_0}<&0 {__p9k_fd_1}>&1 {__p9k_fd_2}>&2 0<&$fd_null 1>$__p9k_instant_prompt_output
  5734. exec 2>&1 {fd_null}>&-
  5735. typeset -gi __p9k_instant_prompt_active=1
  5736. if (( _z4h_can_save_restore_screen == 1 )); then
  5737. typeset -g _z4h_saved_screen
  5738. -z4h-save-screen
  5739. fi
  5740. typeset -g __p9k_instant_prompt_dump_file=${XDG_CACHE_HOME:-~/.cache}/p10k-dump-${(%):-%n}.zsh
  5741. if builtin source $__p9k_instant_prompt_dump_file 2>/dev/null && (( $+functions[_p9k_preinit] )); then
  5742. _p9k_preinit
  5743. fi
  5744. function _p9k_instant_prompt_cleanup() {
  5745. (( ZSH_SUBSHELL == 0 && ${+__p9k_instant_prompt_active} )) || return 0
  5746. '$__p9k_intro_no_locale'
  5747. unset __p9k_instant_prompt_active
  5748. exec 0<&$__p9k_fd_0 1>&$__p9k_fd_1 2>&$__p9k_fd_2 {__p9k_fd_0}>&- {__p9k_fd_1}>&- {__p9k_fd_2}>&-
  5749. unset __p9k_fd_0 __p9k_fd_1 __p9k_fd_2
  5750. typeset -gi __p9k_instant_prompt_erased=1
  5751. if (( _z4h_can_save_restore_screen == 1 && __p9k_instant_prompt_sourced >= 35 )); then
  5752. -z4h-restore-screen
  5753. unset _z4h_saved_screen
  5754. fi
  5755. print -rn -- $terminfo[rc]${(%):-%b%k%f%s%u}$terminfo[ed]
  5756. if [[ -s $__p9k_instant_prompt_output ]]; then
  5757. command cat $__p9k_instant_prompt_output 2>/dev/null
  5758. if (( $1 )); then
  5759. local _p9k__ret mark="${(e)${PROMPT_EOL_MARK-%B%S%#%s%b}}"
  5760. _p9k_prompt_length $mark
  5761. local -i fill=$((COLUMNS > _p9k__ret ? COLUMNS - _p9k__ret : 0))
  5762. echo -nE - "${(%):-%b%k%f%s%u$mark${(pl.$fill.. .)}$cr%b%k%f%s%u%E}"
  5763. fi
  5764. fi
  5765. zshexit_functions=(${zshexit_functions:#_p9k_instant_prompt_cleanup})
  5766. zmodload -F zsh/files b:zf_rm || return
  5767. local user=${(%):-%n}
  5768. local root_dir=${__p9k_instant_prompt_dump_file:h}
  5769. zf_rm -f -- $__p9k_instant_prompt_output $__p9k_instant_prompt_dump_file{,.zwc} $root_dir/p10k-instant-prompt-$user.zsh{,.zwc} $root_dir/p10k-$user/prompt-*(N) 2>/dev/null
  5770. }
  5771. function _p9k_instant_prompt_precmd_first() {
  5772. '$__p9k_intro'
  5773. function _p9k_instant_prompt_sched_last() {
  5774. (( ${+__p9k_instant_prompt_active} )) || return 0
  5775. _p9k_instant_prompt_cleanup 1
  5776. setopt no_local_options prompt_cr prompt_sp
  5777. }
  5778. zmodload zsh/sched
  5779. sched +0 _p9k_instant_prompt_sched_last
  5780. precmd_functions=(${(@)precmd_functions:#_p9k_instant_prompt_precmd_first})
  5781. }
  5782. zshexit_functions=(_p9k_instant_prompt_cleanup $zshexit_functions)
  5783. precmd_functions=(_p9k_instant_prompt_precmd_first $precmd_functions)
  5784. DISABLE_UPDATE_PROMPT=true
  5785. } && unsetopt prompt_cr prompt_sp && typeset -gi __p9k_instant_prompt_sourced='$__p9k_instant_prompt_version' ||
  5786. typeset -gi __p9k_instant_prompt_sourced=${__p9k_instant_prompt_sourced:-0}'
  5787. } always {
  5788. exec {fd}>&-
  5789. }
  5790. {
  5791. (( ! $? )) || return
  5792. # `zf_mv -f src dst` fails on NTFS if `dst` is not writable, hence `zf_rm`.
  5793. zf_rm -f -- $root_file.zwc || return
  5794. zf_mv -f -- $tmp $root_file || return
  5795. zcompile -R -- $tmp.zwc $root_file || return
  5796. zf_mv -f -- $tmp.zwc $root_file.zwc || return
  5797. } always {
  5798. (( $? )) && zf_rm -f -- $tmp $tmp.zwc 2>/dev/null
  5799. }
  5800. fi
  5801. local tmp=$prompt_file.tmp.$$
  5802. zf_mv -f -- $prompt_file $tmp 2>/dev/null
  5803. if [[ "$(<$tmp)" == *$'\x1e'$_p9k__instant_prompt_sig$'\x1f'* ]] 2>/dev/null; then
  5804. echo -n >$tmp || return
  5805. fi
  5806. local -i fd
  5807. sysopen -a -m 600 -o creat -u fd -- $tmp || return
  5808. {
  5809. {
  5810. print -rnu $fd -- $'\x1e'$_p9k__instant_prompt_sig$'\x1f'${(pj:\x1f:)_p9k_t}$'\x1f'$_p9k__instant_prompt || return
  5811. } always {
  5812. exec {fd}>&-
  5813. }
  5814. zf_mv -f -- $tmp $prompt_file || return
  5815. } always {
  5816. (( $? )) && zf_rm -f -- $tmp 2>/dev/null
  5817. }
  5818. }
  5819. typeset -gi __p9k_sh_glob
  5820. typeset -gi __p9k_ksh_arrays
  5821. typeset -gi __p9k_new_status
  5822. typeset -ga __p9k_new_pipestatus
  5823. _p9k_save_status() {
  5824. local -i pipe
  5825. if (( !$+_p9k__line_finished )); then
  5826. : # SIGINT
  5827. elif (( !$+_p9k__preexec_cmd )); then
  5828. # Empty line, comment or parse error.
  5829. #
  5830. # This case is handled incorrectly:
  5831. #
  5832. # true | false
  5833. # |
  5834. #
  5835. # Here status=1 and pipestatus=(0 1). Ideally we should ignore pipestatus but we won't.
  5836. #
  5837. # This works though (unless pipefail is set):
  5838. #
  5839. # false | true
  5840. # |
  5841. #
  5842. # We get status=1 and pipestatus=(1 0) and correctly ignore pipestatus.
  5843. (( _p9k__status == __p9k_new_status )) && return
  5844. elif (( $__p9k_new_pipestatus[(I)$__p9k_new_status] )); then # just in case
  5845. local cmd=(${(z)_p9k__preexec_cmd})
  5846. if [[ $#cmd != 0 && $cmd[1] != '!' && ${(Q)cmd[1]} != coproc ]]; then
  5847. local arg
  5848. for arg in ${(z)_p9k__preexec_cmd}; do
  5849. # '()' is for functions, *';' is for complex commands.
  5850. if [[ $arg == ('()'|'&&'|'||'|'&'|'&|'|'&!'|*';') ]]; then
  5851. pipe=0
  5852. break
  5853. elif [[ $arg == *('|'|'|&')* ]]; then
  5854. pipe=1
  5855. fi
  5856. done
  5857. fi
  5858. fi
  5859. _p9k__status=$__p9k_new_status
  5860. if (( pipe )); then
  5861. _p9k__pipestatus=($__p9k_new_pipestatus)
  5862. else
  5863. _p9k__pipestatus=($_p9k__status)
  5864. fi
  5865. }
  5866. function _p9k_dump_state() {
  5867. local dir=${__p9k_dump_file:h}
  5868. [[ -d $dir ]] || mkdir -p -- $dir || return
  5869. [[ -w $dir ]] || return
  5870. local tmp=$__p9k_dump_file.tmp.$$
  5871. local -i fd
  5872. sysopen -a -m 600 -o creat,trunc -u fd -- $tmp || return
  5873. {
  5874. {
  5875. typeset -g __p9k_cached_param_pat=$_p9k__param_pat
  5876. typeset -g __p9k_cached_param_sig=$_p9k__param_sig
  5877. typeset -pm __p9k_cached_param_pat __p9k_cached_param_sig >&$fd || return
  5878. unset __p9k_cached_param_pat __p9k_cached_param_sig
  5879. (( $+_p9k_preinit )) && { print -r -- $_p9k_preinit >&$fd || return }
  5880. print -r -- '_p9k_restore_state_impl() {' >&$fd || return
  5881. typeset -pm '_POWERLEVEL9K_*|_p9k_[^_]*|icons|OS|DEFAULT_COLOR|DEFAULT_COLOR_INVERTED' >&$fd || return
  5882. print -r -- '}' >&$fd || return
  5883. } always {
  5884. exec {fd}>&-
  5885. }
  5886. # `zf_mv -f src dst` fails on NTFS if `dst` is not writable, hence `zf_rm`.
  5887. zf_rm -f -- $__p9k_dump_file.zwc || return
  5888. zf_mv -f -- $tmp $__p9k_dump_file || return
  5889. zcompile -R -- $tmp.zwc $__p9k_dump_file || return
  5890. zf_mv -f -- $tmp.zwc $__p9k_dump_file.zwc || return
  5891. } always {
  5892. (( $? )) && zf_rm -f -- $tmp $tmp.zwc 2>/dev/null
  5893. }
  5894. }
  5895. function _p9k_delete_instant_prompt() {
  5896. local user=${(%):-%n}
  5897. local root_dir=${__p9k_dump_file:h}
  5898. zf_rm -f -- $root_dir/p10k-instant-prompt-$user.zsh{,.zwc} ${root_dir}/p10k-$user/prompt-*(N) 2>/dev/null
  5899. }
  5900. function _p9k_restore_state() {
  5901. {
  5902. [[ $__p9k_cached_param_pat == $_p9k__param_pat && $__p9k_cached_param_sig == $_p9k__param_sig ]] || return
  5903. (( $+functions[_p9k_restore_state_impl] )) || return
  5904. _p9k_restore_state_impl
  5905. return 0
  5906. } always {
  5907. if (( $? )); then
  5908. if (( $+functions[_p9k_preinit] )); then
  5909. unfunction _p9k_preinit
  5910. (( $+functions[gitstatus_stop_p9k_] )) && gitstatus_stop_p9k_ POWERLEVEL9K
  5911. fi
  5912. _p9k_delete_instant_prompt
  5913. zf_rm -f -- $__p9k_dump_file{,.zwc} 2>/dev/null
  5914. elif [[ $__p9k_instant_prompt_param_sig != $_p9k__param_sig ]]; then
  5915. _p9k_delete_instant_prompt
  5916. _p9k_dumped_instant_prompt_sigs=()
  5917. fi
  5918. unset __p9k_cached_param_sig
  5919. }
  5920. }
  5921. function _p9k_clear_instant_prompt() {
  5922. if (( $+__p9k_fd_0 )); then
  5923. exec 0<&$__p9k_fd_0 {__p9k_fd_0}>&-
  5924. unset __p9k_fd_0
  5925. fi
  5926. exec 1>&$__p9k_fd_1 2>&$__p9k_fd_2 {__p9k_fd_1}>&- {__p9k_fd_2}>&-
  5927. unset __p9k_fd_1 __p9k_fd_2
  5928. zshexit_functions=(${zshexit_functions:#_p9k_instant_prompt_cleanup})
  5929. if (( _p9k__can_hide_cursor )); then
  5930. echoti civis
  5931. _p9k__cursor_hidden=1
  5932. fi
  5933. if [[ -s $__p9k_instant_prompt_output ]]; then
  5934. {
  5935. local content
  5936. [[ $_POWERLEVEL9K_INSTANT_PROMPT == verbose ]] && content="$(<$__p9k_instant_prompt_output)"
  5937. local mark="${(e)${PROMPT_EOL_MARK-%B%S%#%s%b}}"
  5938. _p9k_prompt_length $mark
  5939. local -i fill=$((COLUMNS > _p9k__ret ? COLUMNS - _p9k__ret : 0))
  5940. local cr=$'\r'
  5941. local sp="${(%):-%b%k%f%s%u$mark${(pl.$fill.. .)}$cr%b%k%f%s%u%E}"
  5942. if (( _z4h_can_save_restore_screen == 1 && __p9k_instant_prompt_sourced >= 35 )); then
  5943. -z4h-restore-screen
  5944. unset _z4h_saved_screen
  5945. fi
  5946. print -rn -- $terminfo[rc]${(%):-%b%k%f%s%u}$terminfo[ed]
  5947. local unexpected=${${${(S)content//$'\e[?'<->'c'}//$'\e['<->' q'}//$'\e'[^$'\a\e']#($'\a'|$'\e\\')}
  5948. if [[ -n $unexpected ]]; then
  5949. local omz1='[Oh My Zsh] Would you like to update? [Y/n]: '
  5950. local omz2='Updating Oh My Zsh'
  5951. local omz3='https://shop.planetargon.com/collections/oh-my-zsh'
  5952. local omz4='There was an error updating. Try again later?'
  5953. if [[ $unexpected != ($omz1|)$omz2*($omz3|$omz4)[^$'\n']#($'\n'|) ]]; then
  5954. echo -E - ""
  5955. echo -E - "${(%):-[%3FWARNING%f]: Console output during zsh initialization detected.}"
  5956. echo -E - ""
  5957. echo -E - "${(%):-When using Powerlevel10k with instant prompt, console output during zsh}"
  5958. echo -E - "${(%):-initialization may indicate issues.}"
  5959. echo -E - ""
  5960. echo -E - "${(%):-You can:}"
  5961. echo -E - ""
  5962. echo -E - "${(%):- - %BRecommended%b: Change %B$__p9k_zshrc_u%b so that it does not perform console I/O}"
  5963. echo -E - "${(%):- after the instant prompt preamble. See the link below for details.}"
  5964. echo -E - ""
  5965. echo -E - "${(%):- * You %Bwill not%b see this error message again.}"
  5966. echo -E - "${(%):- * Zsh will start %Bquickly%b and prompt will update %Bsmoothly%b.}"
  5967. echo -E - ""
  5968. echo -E - "${(%):- - Suppress this warning either by running %Bp10k configure%b or by manually}"
  5969. echo -E - "${(%):- defining the following parameter:}"
  5970. echo -E - ""
  5971. echo -E - "${(%):- %3Ftypeset%f -g POWERLEVEL9K_INSTANT_PROMPT=quiet}"
  5972. echo -E - ""
  5973. echo -E - "${(%):- * You %Bwill not%b see this error message again.}"
  5974. echo -E - "${(%):- * Zsh will start %Bquickly%b but prompt will %Bjump down%b after initialization.}"
  5975. echo -E - ""
  5976. echo -E - "${(%):- - Disable instant prompt either by running %Bp10k configure%b or by manually}"
  5977. echo -E - "${(%):- defining the following parameter:}"
  5978. echo -E - ""
  5979. echo -E - "${(%):- %3Ftypeset%f -g POWERLEVEL9K_INSTANT_PROMPT=off}"
  5980. echo -E - ""
  5981. echo -E - "${(%):- * You %Bwill not%b see this error message again.}"
  5982. echo -E - "${(%):- * Zsh will start %Bslowly%b.}"
  5983. echo -E - ""
  5984. echo -E - "${(%):- - Do nothing.}"
  5985. echo -E - ""
  5986. echo -E - "${(%):- * You %Bwill%b see this error message every time you start zsh.}"
  5987. echo -E - "${(%):- * Zsh will start %Bquickly%b but prompt will %Bjump down%b after initialization.}"
  5988. echo -E - ""
  5989. echo -E - "${(%):-For details, see:}"
  5990. if (( _p9k_term_has_href )); then
  5991. echo - "${(%):-\e]8;;https://github.com/romkatv/powerlevel10k/blob/master/README.md#instant-prompt\ahttps://github.com/romkatv/powerlevel10k/blob/master/README.md#instant-prompt\e]8;;\a}"
  5992. else
  5993. echo - "${(%):-https://github.com/romkatv/powerlevel10k/blob/master/README.md#instant-prompt}"
  5994. fi
  5995. echo -E - ""
  5996. echo - "${(%):-%3F-- console output produced during zsh initialization follows --%f}"
  5997. echo -E - ""
  5998. fi
  5999. fi
  6000. command cat -- $__p9k_instant_prompt_output
  6001. echo -nE - $sp
  6002. zf_rm -f -- $__p9k_instant_prompt_output
  6003. } 2>/dev/null
  6004. else
  6005. zf_rm -f -- $__p9k_instant_prompt_output 2>/dev/null
  6006. if (( _z4h_can_save_restore_screen == 1 && __p9k_instant_prompt_sourced >= 35 )); then
  6007. -z4h-restore-screen
  6008. unset _z4h_saved_screen
  6009. fi
  6010. print -rn -- $terminfo[rc]${(%):-%b%k%f%s%u}$terminfo[ed]
  6011. fi
  6012. prompt_opts=(percent subst sp cr)
  6013. if [[ $_POWERLEVEL9K_DISABLE_INSTANT_PROMPT == 0 && $__p9k_instant_prompt_active == 2 ]]; then
  6014. >&2 echo -E - ""
  6015. >&2 echo -E - "${(%):-[%1FERROR%f]: When using Powerlevel10k with instant prompt, %Bprompt_cr%b must be unset.}"
  6016. >&2 echo -E - ""
  6017. >&2 echo -E - "${(%):-You can:}"
  6018. >&2 echo -E - ""
  6019. >&2 echo -E - "${(%):- - %BRecommended%b: call %Bp10k finalize%b at the end of %B$__p9k_zshrc_u%b.}"
  6020. >&2 echo -E - "${(%):- You can do this by running the following command:}"
  6021. >&2 echo -E - ""
  6022. >&2 echo -E - "${(%):- %2Fecho%f %3F'(( ! \${+functions[p10k]\} )) || p10k finalize'%f >>! $__p9k_zshrc_u}"
  6023. >&2 echo -E - ""
  6024. >&2 echo -E - "${(%):- * You %Bwill not%b see this error message again.}"
  6025. >&2 echo -E - "${(%):- * Zsh will start %Bquickly%b and %Bwithout%b prompt flickering.}"
  6026. >&2 echo -E - ""
  6027. >&2 echo -E - "${(%):- - Find where %Bprompt_cr%b option gets sets in your zsh configs and stop setting it.}"
  6028. >&2 echo -E - ""
  6029. >&2 echo -E - "${(%):- * You %Bwill not%b see this error message again.}"
  6030. >&2 echo -E - "${(%):- * Zsh will start %Bquickly%b and %Bwithout%b prompt flickering.}"
  6031. >&2 echo -E - ""
  6032. >&2 echo -E - "${(%):- - Disable instant prompt either by running %Bp10k configure%b or by manually}"
  6033. >&2 echo -E - "${(%):- defining the following parameter:}"
  6034. >&2 echo -E - ""
  6035. >&2 echo -E - "${(%):- %3Ftypeset%f -g POWERLEVEL9K_INSTANT_PROMPT=off}"
  6036. >&2 echo -E - ""
  6037. >&2 echo -E - "${(%):- * You %Bwill not%b see this error message again.}"
  6038. >&2 echo -E - "${(%):- * Zsh will start %Bslowly%b.}"
  6039. >&2 echo -E - ""
  6040. >&2 echo -E - "${(%):- - Do nothing.}"
  6041. >&2 echo -E - ""
  6042. >&2 echo -E - "${(%):- * You %Bwill%b see this error message every time you start zsh.}"
  6043. >&2 echo -E - "${(%):- * Zsh will start %Bquckly%b but %Bwith%b prompt flickering.}"
  6044. >&2 echo -E - ""
  6045. fi
  6046. }
  6047. function _p9k_do_dump() {
  6048. eval "$__p9k_intro"
  6049. zle -F $1
  6050. exec {1}>&-
  6051. (( _p9k__state_dump_fd )) || return
  6052. if (( ! _p9k__instant_prompt_disabled )); then
  6053. _p9k__instant_prompt_sig=$_p9k__cwd:$P9K_SSH:${(%):-%#}
  6054. _p9k_set_instant_prompt
  6055. _p9k_dump_instant_prompt
  6056. _p9k_dumped_instant_prompt_sigs[$_p9k__instant_prompt_sig]=1
  6057. fi
  6058. _p9k_dump_state
  6059. _p9k__state_dump_scheduled=0
  6060. _p9k__state_dump_fd=0
  6061. }
  6062. function _p9k_should_dump() {
  6063. (( __p9k_dumps_enabled && ! _p9k__state_dump_fd )) || return
  6064. (( _p9k__state_dump_scheduled || _p9k__prompt_idx == 1 )) && return
  6065. _p9k__instant_prompt_sig=$_p9k__cwd:$P9K_SSH:${(%):-%#}
  6066. (( ! $+_p9k_dumped_instant_prompt_sigs[$_p9k__instant_prompt_sig] ))
  6067. }
  6068. # Must not run under `eval "$__p9k_intro_locale"`. Safe to run with any options.
  6069. function _p9k_restore_special_params() {
  6070. (( ! ${+_p9k__real_zle_rprompt_indent} )) || {
  6071. [[ -n "$_p9k__real_zle_rprompt_indent" ]] &&
  6072. ZLE_RPROMPT_INDENT="$_p9k__real_zle_rprompt_indent" ||
  6073. unset ZLE_RPROMPT_INDENT
  6074. unset _p9k__real_zle_rprompt_indent
  6075. }
  6076. (( ! ${+_p9k__real_lc_ctype} )) || {
  6077. LC_CTYPE="$_p9k__real_lc_ctype"
  6078. unset _p9k__real_lc_ctype
  6079. }
  6080. (( ! ${+_p9k__real_lc_all} )) || {
  6081. LC_ALL="$_p9k__real_lc_all"
  6082. unset _p9k__real_lc_all
  6083. }
  6084. }
  6085. function _p9k_on_expand() {
  6086. (( _p9k__expanded && ! ${+__p9k_instant_prompt_active} )) && [[ "${langinfo[CODESET]}" == (utf|UTF)(-|)8 ]] && return
  6087. eval "$__p9k_intro_no_locale"
  6088. if [[ $langinfo[CODESET] != (utf|UTF)(-|)8 ]]; then
  6089. _p9k_restore_special_params
  6090. if [[ $langinfo[CODESET] != (utf|UTF)(-|)8 ]] && _p9k_init_locale; then
  6091. if [[ -n $LC_ALL ]]; then
  6092. _p9k__real_lc_all=$LC_ALL
  6093. LC_ALL=$__p9k_locale
  6094. else
  6095. _p9k__real_lc_ctype=$LC_CTYPE
  6096. LC_CTYPE=$__p9k_locale
  6097. fi
  6098. fi
  6099. fi
  6100. (( _p9k__expanded && ! $+__p9k_instant_prompt_active )) && return
  6101. eval "$__p9k_intro_locale"
  6102. if (( ! _p9k__expanded )); then
  6103. if _p9k_should_dump; then
  6104. sysopen -o cloexec -ru _p9k__state_dump_fd /dev/null
  6105. zle -F $_p9k__state_dump_fd _p9k_do_dump
  6106. fi
  6107. if [[ -z $P9K_TTY || $P9K_TTY == old && -n ${_P9K_TTY:#$TTY} ]]; then
  6108. typeset -gx P9K_TTY=old
  6109. if (( _POWERLEVEL9K_NEW_TTY_MAX_AGE_SECONDS < 0 )); then
  6110. P9K_TTY=new
  6111. else
  6112. local -a stat
  6113. if zstat -A stat +ctime -- $TTY 2>/dev/null &&
  6114. (( EPOCHREALTIME - stat[1] < _POWERLEVEL9K_NEW_TTY_MAX_AGE_SECONDS )); then
  6115. P9K_TTY=new
  6116. fi
  6117. fi
  6118. fi
  6119. typeset -gx _P9K_TTY=$TTY
  6120. __p9k_reset_state=1
  6121. if (( _POWERLEVEL9K_PROMPT_ADD_NEWLINE )); then
  6122. if [[ $P9K_TTY == new ]]; then
  6123. _p9k__empty_line_i=3
  6124. _p9k__display_v[2]=hide
  6125. elif [[ -z $_p9k_transient_prompt && $+functions[p10k-on-post-prompt] == 0 ]]; then
  6126. _p9k__empty_line_i=3
  6127. _p9k__display_v[2]=print
  6128. else
  6129. unset _p9k__empty_line_i
  6130. _p9k__display_v[2]=show
  6131. fi
  6132. fi
  6133. if (( _POWERLEVEL9K_SHOW_RULER )); then
  6134. if [[ $P9K_TTY == new ]]; then
  6135. _p9k__ruler_i=3
  6136. _p9k__display_v[4]=hide
  6137. elif [[ -z $_p9k_transient_prompt && $+functions[p10k-on-post-prompt] == 0 ]]; then
  6138. _p9k__ruler_i=3
  6139. _p9k__display_v[4]=print
  6140. else
  6141. unset _p9k__ruler_i
  6142. _p9k__display_v[4]=show
  6143. fi
  6144. fi
  6145. (( _p9k__fully_initialized )) || _p9k_wrap_widgets
  6146. fi
  6147. if (( $+__p9k_instant_prompt_active )); then
  6148. _p9k_clear_instant_prompt
  6149. unset __p9k_instant_prompt_active
  6150. fi
  6151. if (( ! _p9k__expanded )); then
  6152. _p9k__expanded=1
  6153. (( _p9k__fully_initialized || ! $+functions[p10k-on-init] )) || p10k-on-init
  6154. local pat idx var
  6155. for pat idx var in $_p9k_show_on_command; do
  6156. _p9k_display_segment $idx $var hide
  6157. done
  6158. (( $+functions[p10k-on-pre-prompt] )) && p10k-on-pre-prompt
  6159. if zle; then
  6160. local -a P9K_COMMANDS=($_p9k__last_commands)
  6161. local pat idx var
  6162. for pat idx var in $_p9k_show_on_command; do
  6163. if (( $P9K_COMMANDS[(I)$pat] )); then
  6164. _p9k_display_segment $idx $var show
  6165. else
  6166. _p9k_display_segment $idx $var hide
  6167. fi
  6168. done
  6169. if (( $+functions[p10k-on-post-widget] )); then
  6170. local -h WIDGET
  6171. unset WIDGET
  6172. p10k-on-post-widget
  6173. fi
  6174. else
  6175. if [[ $_p9k__display_v[2] == print && -n $_p9k_t[_p9k_empty_line_idx] ]]; then
  6176. print -rnP -- '%b%k%f%E'$_p9k_t[_p9k_empty_line_idx]
  6177. fi
  6178. if [[ $_p9k__display_v[4] == print ]]; then
  6179. () {
  6180. local ruler=$_p9k_t[_p9k_ruler_idx]
  6181. local -i _p9k__clm=COLUMNS _p9k__ind=${ZLE_RPROMPT_INDENT:-1}
  6182. (( __p9k_ksh_arrays )) && setopt ksh_arrays
  6183. (( __p9k_sh_glob )) && setopt sh_glob
  6184. setopt prompt_subst
  6185. print -rnP -- '%b%k%f%E'$ruler
  6186. }
  6187. fi
  6188. fi
  6189. __p9k_reset_state=0
  6190. _p9k__fully_initialized=1
  6191. fi
  6192. }
  6193. functions -M _p9k_on_expand
  6194. _p9k_precmd_impl() {
  6195. eval "$__p9k_intro"
  6196. (( __p9k_enabled )) || return
  6197. if ! zle || [[ -z $_p9k__param_sig ]]; then
  6198. if zle; then
  6199. __p9k_new_status=0
  6200. __p9k_new_pipestatus=(0)
  6201. else
  6202. _p9k__must_restore_prompt=0
  6203. fi
  6204. if _p9k_must_init; then
  6205. local -i instant_prompt_disabled
  6206. if (( !__p9k_configured )); then
  6207. __p9k_configured=1
  6208. if [[ -z "${parameters[(I)POWERLEVEL9K_*~POWERLEVEL9K_(MODE|CONFIG_FILE|GITSTATUS_DIR)]}" ]]; then
  6209. _p9k_can_configure -q
  6210. local -i ret=$?
  6211. if (( ret == 2 && $+__p9k_instant_prompt_active )); then
  6212. _p9k_clear_instant_prompt
  6213. unset __p9k_instant_prompt_active
  6214. _p9k_delete_instant_prompt
  6215. zf_rm -f -- $__p9k_dump_file{,.zwc} 2>/dev/null
  6216. () {
  6217. local key
  6218. while true; do
  6219. [[ -t 2 ]]
  6220. read -t0 -k key || break
  6221. done 2>/dev/null
  6222. }
  6223. _p9k_can_configure -q
  6224. ret=$?
  6225. fi
  6226. if (( ret == 0 )); then
  6227. if (( $+commands[git] )); then
  6228. (
  6229. local -i pid
  6230. {
  6231. { /bin/sh "$__p9k_root_dir"/gitstatus/install </dev/null &>/dev/null & } && pid=$!
  6232. ( builtin source "$__p9k_root_dir"/internal/wizard.zsh )
  6233. } always {
  6234. if (( pid )); then
  6235. kill -- $pid 2>/dev/null
  6236. wait -- $pid 2>/dev/null
  6237. fi
  6238. }
  6239. )
  6240. else
  6241. ( builtin source "$__p9k_root_dir"/internal/wizard.zsh )
  6242. fi
  6243. if (( $? )); then
  6244. instant_prompt_disabled=1
  6245. else
  6246. builtin source "$__p9k_cfg_path"
  6247. _p9k__force_must_init=1
  6248. _p9k_must_init
  6249. fi
  6250. fi
  6251. fi
  6252. fi
  6253. typeset -gi _p9k__instant_prompt_disabled=instant_prompt_disabled
  6254. _p9k_init
  6255. fi
  6256. if (( _p9k__timer_start )); then
  6257. typeset -gF P9K_COMMAND_DURATION_SECONDS=$((EPOCHREALTIME - _p9k__timer_start))
  6258. else
  6259. unset P9K_COMMAND_DURATION_SECONDS
  6260. fi
  6261. _p9k_save_status
  6262. if [[ $_p9k__preexec_cmd == [[:space:]]#(clear([[:space:]]##-(|x)(|T[a-zA-Z0-9-_\'\"]#))#|reset)[[:space:]]# &&
  6263. $_p9k__status == 0 ]]; then
  6264. P9K_TTY=new
  6265. elif [[ $P9K_TTY == new && $_p9k__fully_initialized == 1 ]] && ! zle; then
  6266. P9K_TTY=old
  6267. fi
  6268. _p9k__timer_start=0
  6269. _p9k__region_active=0
  6270. unset _p9k__line_finished _p9k__preexec_cmd
  6271. _p9k__keymap=main
  6272. _p9k__zle_state=insert
  6273. (( ++_p9k__prompt_idx ))
  6274. fi
  6275. _p9k_fetch_cwd
  6276. _p9k__refresh_reason=precmd
  6277. __p9k_reset_state=1
  6278. local -i fast_vcs
  6279. if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then
  6280. if [[ $_p9k__cwd != $~_POWERLEVEL9K_VCS_DISABLED_DIR_PATTERN ]]; then
  6281. local -F start_time=EPOCHREALTIME
  6282. unset _p9k__vcs
  6283. unset _p9k__vcs_timeout
  6284. local -i _p9k__vcs_called
  6285. _p9k_vcs_gitstatus
  6286. local -i fast_vcs=1
  6287. fi
  6288. fi
  6289. (( $+functions[_p9k_async_segments_compute] )) && _p9k_async_segments_compute
  6290. _p9k__expanded=0
  6291. _p9k_set_prompt
  6292. _p9k__refresh_reason=''
  6293. if [[ $precmd_functions[1] != _p9k_do_nothing && $precmd_functions[(I)_p9k_do_nothing] != 0 ]]; then
  6294. precmd_functions=(_p9k_do_nothing ${(@)precmd_functions:#_p9k_do_nothing})
  6295. fi
  6296. if [[ $precmd_functions[-1] != _p9k_precmd && $precmd_functions[(I)_p9k_precmd] != 0 ]]; then
  6297. precmd_functions=(${(@)precmd_functions:#_p9k_precmd} _p9k_precmd)
  6298. fi
  6299. if [[ $preexec_functions[1] != _p9k_preexec1 && $preexec_functions[(I)_p9k_preexec1] != 0 ]]; then
  6300. preexec_functions=(_p9k_preexec1 ${(@)preexec_functions:#_p9k_preexec1})
  6301. fi
  6302. if [[ $preexec_functions[-1] != _p9k_preexec2 && $preexec_functions[(I)_p9k_preexec2] != 0 ]]; then
  6303. preexec_functions=(${(@)preexec_functions:#_p9k_preexec2} _p9k_preexec2)
  6304. fi
  6305. if (( fast_vcs && _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then
  6306. if (( $+_p9k__vcs_timeout )); then
  6307. (( _p9k__vcs_timeout = _POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS + start_time - EPOCHREALTIME ))
  6308. (( _p9k__vcs_timeout >= 0 )) || (( _p9k__vcs_timeout = 0 ))
  6309. gitstatus_process_results_p9k_ -t $_p9k__vcs_timeout POWERLEVEL9K
  6310. fi
  6311. if (( ! $+_p9k__vcs )); then
  6312. local _p9k__prompt _p9k__prompt_side=$_p9k_vcs_side _p9k__segment_name=vcs
  6313. local -i _p9k__has_upglob _p9k__segment_index=_p9k_vcs_index _p9k__line_index=_p9k_vcs_line_index
  6314. _p9k_vcs_render
  6315. typeset -g _p9k__vcs=$_p9k__prompt
  6316. fi
  6317. fi
  6318. _p9k_worker_receive
  6319. __p9k_reset_state=0
  6320. }
  6321. _p9k_trapint() {
  6322. if (( __p9k_enabled )); then
  6323. eval "$__p9k_intro"
  6324. _p9k_deschedule_redraw
  6325. zle && _p9k_on_widget_zle-line-finish int
  6326. fi
  6327. return 0
  6328. }
  6329. _p9k_precmd() {
  6330. __p9k_new_status=$?
  6331. __p9k_new_pipestatus=($pipestatus)
  6332. trap ":" INT
  6333. [[ -o ksh_arrays ]] && __p9k_ksh_arrays=1 || __p9k_ksh_arrays=0
  6334. [[ -o sh_glob ]] && __p9k_sh_glob=1 || __p9k_sh_glob=0
  6335. _p9k_restore_special_params
  6336. _p9k_precmd_impl
  6337. [[ ${+__p9k_instant_prompt_active} == 0 || -o no_prompt_cr ]] || __p9k_instant_prompt_active=2
  6338. setopt no_local_options no_prompt_bang prompt_percent prompt_subst prompt_cr prompt_sp
  6339. # See https://www.zsh.org/mla/workers/2020/msg00612.html for the reason behind __p9k_trapint.
  6340. typeset -g __p9k_trapint='_p9k_trapint; return 130'
  6341. trap "$__p9k_trapint" INT
  6342. : ${(%):-%b%k%s%u}
  6343. }
  6344. function _p9k_reset_prompt() {
  6345. if (( __p9k_reset_state != 1 )) && zle && [[ -z $_p9k__line_finished ]]; then
  6346. __p9k_reset_state=0
  6347. setopt prompt_subst
  6348. (( __p9k_ksh_arrays )) && setopt ksh_arrays
  6349. (( __p9k_sh_glob )) && setopt sh_glob
  6350. {
  6351. (( _p9k__can_hide_cursor )) && echoti civis
  6352. zle .reset-prompt
  6353. (( ${+functions[z4h]} )) || zle -R
  6354. } always {
  6355. (( _p9k__can_hide_cursor )) && echoti cnorm
  6356. _p9k__cursor_hidden=0
  6357. }
  6358. fi
  6359. }
  6360. # Does ZSH have a certain off-by-one bug that triggers when PROMPT overflows to a new line?
  6361. #
  6362. # Bug: https://github.com/zsh-users/zsh/commit/d8d9fee137a5aa2cf9bf8314b06895bfc2a05518.
  6363. # ZSH_PATCHLEVEL=zsh-5.4.2-159-gd8d9fee13. Released in 5.5.
  6364. #
  6365. # Fix: https://github.com/zsh-users/zsh/commit/64d13738357c9b9c212adbe17f271716abbcf6ea.
  6366. # ZSH_PATCHLEVEL=zsh-5.7.1-50-g64d137383.
  6367. #
  6368. # Test: PROMPT="${(pl:$((COLUMNS))::-:)}<%1(l.%2(l.FAIL.PASS).FAIL)> " zsh -dfis <<<exit
  6369. # Workaround: PROMPT="${(pl:$((COLUMNS))::-:)}%{%G%}<%1(l.%2(l.FAIL.PASS).FAIL)> " zsh -dfis <<<exit
  6370. function _p9k_prompt_overflow_bug() {
  6371. [[ $ZSH_PATCHLEVEL =~ '^zsh-5\.4\.2-([0-9]+)-' ]] && return $(( match[1] < 159 ))
  6372. [[ $ZSH_PATCHLEVEL =~ '^zsh-5\.7\.1-([0-9]+)-' ]] && return $(( match[1] >= 50 ))
  6373. is-at-least 5.5 && ! is-at-least 5.7.2
  6374. }
  6375. typeset -g _p9k__param_pat
  6376. typeset -g _p9k__param_sig
  6377. _p9k_init_vars() {
  6378. typeset -gF _p9k__gcloud_last_fetch_ts
  6379. typeset -g _p9k_gcloud_configuration
  6380. typeset -g _p9k_gcloud_account
  6381. typeset -g _p9k_gcloud_project_id
  6382. typeset -g _p9k_gcloud_project_name
  6383. typeset -gi _p9k_term_has_href
  6384. typeset -gi _p9k_vcs_index
  6385. typeset -gi _p9k_vcs_line_index
  6386. typeset -g _p9k_vcs_side
  6387. typeset -ga _p9k_taskwarrior_meta_files
  6388. typeset -ga _p9k_taskwarrior_meta_non_files
  6389. typeset -g _p9k_taskwarrior_meta_sig
  6390. typeset -g _p9k_taskwarrior_data_dir
  6391. typeset -g _p9k__taskwarrior_functional=1
  6392. typeset -ga _p9k_taskwarrior_data_files
  6393. typeset -ga _p9k_taskwarrior_data_non_files
  6394. typeset -g _p9k_taskwarrior_data_sig
  6395. typeset -gA _p9k_taskwarrior_counters
  6396. typeset -gF _p9k_taskwarrior_next_due
  6397. typeset -ga _p9k_asdf_meta_files
  6398. typeset -ga _p9k_asdf_meta_non_files
  6399. typeset -g _p9k_asdf_meta_sig
  6400. # plugin => installed_version_pattern
  6401. # example: (ruby '2.7.0|2.6.3|system' lua 'system' chubaka '1.0.0|system')
  6402. typeset -gA _p9k_asdf_plugins
  6403. # example: (.ruby-version "ruby 1 chubaka 0")
  6404. #
  6405. # - "1" means parse-legacy-file is present
  6406. # - "chubaka" is another plugin that claims to be able to parse .ruby-version
  6407. typeset -gA _p9k_asdf_file_info
  6408. # dir => mtime ':' ${(pj:\0:)files}
  6409. typeset -gA _p9k__asdf_dir2files
  6410. # :file => mtime ':' ${(pj:\0:)tool_versions}
  6411. # plugin:file => mtime ':' version
  6412. typeset -gA _p9k_asdf_file2versions
  6413. # filepath => mtime ':' word
  6414. typeset -gA _p9k__read_word_cache
  6415. # filepath:prefix => mtime ':' versions
  6416. typeset -gA _p9k__read_pyenv_like_version_file_cache
  6417. # _p9k__parent_dirs and _p9k__parent_mtimes are parallel arrays. They are updated
  6418. # together with _p9k__cwd. _p9k__parent_mtimes[i] is mtime for _p9k__parent_dirs[i].
  6419. #
  6420. # When _p9k__cwd is / or ~, both arrays are empty. When _p9k__cwd is ~/foo/bar,
  6421. # _p9k__parent_dirs is (/home/user/foo/bar /home/user/foo). When _p9k__cwd is
  6422. # /foo/bar, it's (/foo/bar /foo).
  6423. #
  6424. # $_p9k__parent_mtimes_i[i] == "$i:$_p9k__parent_mtimes[i]"
  6425. # $_p9k__parent_mtimes_s == "$_p9k__parent_mtimes_i".
  6426. typeset -ga _p9k__parent_dirs
  6427. typeset -ga _p9k__parent_mtimes
  6428. typeset -ga _p9k__parent_mtimes_i
  6429. typeset -g _p9k__parent_mtimes_s
  6430. typeset -g _p9k__cwd
  6431. typeset -g _p9k__cwd_a
  6432. # dir/pattern => dir mtime ':' num_matches
  6433. typeset -gA _p9k__glob_cache
  6434. # dir/pattern => space-separated parent dir mtimes ' :' the first matching parent dir
  6435. # Note: ' :' is indeed the delimiter.
  6436. typeset -gA _p9k__upsearch_cache
  6437. typeset -g _p9k_timewarrior_dir
  6438. typeset -gi _p9k_timewarrior_dir_mtime
  6439. typeset -gi _p9k_timewarrior_file_mtime
  6440. typeset -g _p9k_timewarrior_file_name
  6441. typeset -gA _p9k__prompt_char_saved
  6442. typeset -g _p9k__worker_pid
  6443. typeset -g _p9k__worker_req_fd
  6444. typeset -g _p9k__worker_resp_fd
  6445. typeset -g _p9k__worker_shell_pid
  6446. typeset -g _p9k__worker_file_prefix
  6447. typeset -gA _p9k__worker_request_map
  6448. typeset -ga _p9k__segment_cond_left
  6449. typeset -ga _p9k__segment_cond_right
  6450. typeset -ga _p9k__segment_val_left
  6451. typeset -ga _p9k__segment_val_right
  6452. typeset -ga _p9k_show_on_command
  6453. typeset -g _p9k__last_buffer
  6454. typeset -ga _p9k__last_commands
  6455. typeset -gi _p9k__fully_initialized
  6456. typeset -gi _p9k__must_restore_prompt
  6457. typeset -gi _p9k__restore_prompt_fd
  6458. typeset -gi _p9k__redraw_fd
  6459. typeset -gi _p9k__can_hide_cursor=$(( $+terminfo[civis] && $+terminfo[cnorm] ))
  6460. typeset -gi _p9k__cursor_hidden
  6461. typeset -gi _p9k__non_hermetic_expansion
  6462. typeset -g _p9k__time
  6463. typeset -g _p9k__date
  6464. typeset -gA _p9k_dumped_instant_prompt_sigs
  6465. typeset -g _p9k__instant_prompt_sig
  6466. typeset -g _p9k__instant_prompt
  6467. typeset -gi _p9k__state_dump_scheduled
  6468. typeset -gi _p9k__state_dump_fd
  6469. typeset -gi _p9k__prompt_idx
  6470. typeset -gi _p9k_reset_on_line_finish
  6471. typeset -gF _p9k__timer_start
  6472. typeset -gi _p9k__status
  6473. typeset -ga _p9k__pipestatus
  6474. typeset -g _p9k__ret
  6475. typeset -g _p9k__cache_key
  6476. typeset -ga _p9k__cache_val
  6477. typeset -g _p9k__cache_stat_meta
  6478. typeset -g _p9k__cache_stat_fprint
  6479. typeset -g _p9k__cache_fprint_key
  6480. typeset -gA _p9k_cache
  6481. typeset -gA _p9k__cache_ephemeral
  6482. typeset -ga _p9k_t
  6483. typeset -g _p9k__n
  6484. typeset -gi _p9k__i
  6485. typeset -g _p9k__bg
  6486. typeset -ga _p9k_left_join
  6487. typeset -ga _p9k_right_join
  6488. typeset -g _p9k__public_ip
  6489. typeset -g _p9k__todo_command
  6490. typeset -g _p9k__todo_file
  6491. typeset -g _p9k__git_dir
  6492. # git workdir => 1 if gitstatus is slow on it, 0 if it's fast.
  6493. typeset -gA _p9k_git_slow
  6494. # git workdir => the last state we've seen for it
  6495. typeset -gA _p9k__gitstatus_last
  6496. typeset -gF _p9k__gitstatus_start_time
  6497. typeset -g _p9k__prompt
  6498. typeset -g _p9k__rprompt
  6499. typeset -g _p9k__lprompt
  6500. typeset -g _p9k__prompt_side
  6501. typeset -g _p9k__segment_name
  6502. typeset -gi _p9k__segment_index
  6503. typeset -gi _p9k__line_index
  6504. typeset -g _p9k__refresh_reason
  6505. typeset -gi _p9k__region_active
  6506. typeset -ga _p9k_line_segments_left
  6507. typeset -ga _p9k_line_segments_right
  6508. typeset -ga _p9k_line_prefix_left
  6509. typeset -ga _p9k_line_prefix_right
  6510. typeset -ga _p9k_line_suffix_left
  6511. typeset -ga _p9k_line_suffix_right
  6512. typeset -ga _p9k_line_never_empty_right
  6513. typeset -ga _p9k_line_gap_post
  6514. typeset -g _p9k__xy
  6515. typeset -g _p9k__clm
  6516. typeset -g _p9k__p
  6517. typeset -gi _p9k__x
  6518. typeset -gi _p9k__y
  6519. typeset -gi _p9k__m
  6520. typeset -gi _p9k__d
  6521. typeset -gi _p9k__h
  6522. typeset -gi _p9k__ind
  6523. typeset -g _p9k_gap_pre
  6524. typeset -gi _p9k__ruler_i=3
  6525. typeset -gi _p9k_ruler_idx
  6526. typeset -gi _p9k__empty_line_i=3
  6527. typeset -gi _p9k_empty_line_idx
  6528. typeset -g _p9k_prompt_prefix_left
  6529. typeset -g _p9k_prompt_prefix_right
  6530. typeset -g _p9k_prompt_suffix_left
  6531. typeset -g _p9k_prompt_suffix_right
  6532. typeset -gi _p9k_emulate_zero_rprompt_indent
  6533. typeset -gA _p9k_battery_states
  6534. typeset -g _p9k_os
  6535. typeset -g _p9k_os_icon
  6536. typeset -g _p9k_color1
  6537. typeset -g _p9k_color2
  6538. typeset -g _p9k__s
  6539. typeset -g _p9k__ss
  6540. typeset -g _p9k__sss
  6541. typeset -g _p9k__v
  6542. typeset -g _p9k__c
  6543. typeset -g _p9k__e
  6544. typeset -g _p9k__w
  6545. typeset -gi _p9k__dir_len
  6546. typeset -gi _p9k_num_cpus
  6547. typeset -g _p9k__keymap
  6548. typeset -g _p9k__zle_state
  6549. typeset -g _p9k_uname
  6550. typeset -g _p9k_uname_o
  6551. typeset -g _p9k_uname_m
  6552. typeset -g _p9k_transient_prompt
  6553. typeset -g _p9k__last_prompt_pwd
  6554. typeset -gA _p9k_display_k
  6555. typeset -ga _p9k__display_v
  6556. typeset -gA _p9k__dotnet_stat_cache
  6557. typeset -gA _p9k__dir_stat_cache
  6558. typeset -gi _p9k__expanded
  6559. typeset -gi _p9k__force_must_init
  6560. typeset -g P9K_VISUAL_IDENTIFIER
  6561. typeset -g P9K_CONTENT
  6562. typeset -g P9K_GAP
  6563. typeset -g P9K_PROMPT=regular
  6564. }
  6565. _p9k_init_params() {
  6566. _p9k_declare -F POWERLEVEL9K_GCLOUD_REFRESH_PROJECT_NAME_SECONDS 60
  6567. # invarint: _POWERLEVEL9K_INSTANT_PROMPT == (verbose|quiet|off)
  6568. # invariant: [[ ($_POWERLEVEL9K_INSTANT_PROMPT == off) == $_POWERLEVEL9K_DISABLE_INSTANT_PROMPT ]]
  6569. _p9k_declare -s POWERLEVEL9K_INSTANT_PROMPT # verbose, quiet, off
  6570. if [[ $_POWERLEVEL9K_INSTANT_PROMPT == off ]]; then
  6571. typeset -gi _POWERLEVEL9K_DISABLE_INSTANT_PROMPT=1
  6572. else
  6573. _p9k_declare -b POWERLEVEL9K_DISABLE_INSTANT_PROMPT 0
  6574. if (( _POWERLEVEL9K_DISABLE_INSTANT_PROMPT )); then
  6575. _POWERLEVEL9K_INSTANT_PROMPT=off
  6576. elif [[ $_POWERLEVEL9K_INSTANT_PROMPT != quiet ]]; then
  6577. _POWERLEVEL9K_INSTANT_PROMPT=verbose
  6578. fi
  6579. fi
  6580. (( _POWERLEVEL9K_DISABLE_INSTANT_PROMPT )) && _p9k__instant_prompt_disabled=1
  6581. _p9k_declare -s POWERLEVEL9K_TRANSIENT_PROMPT off
  6582. [[ $_POWERLEVEL9K_TRANSIENT_PROMPT == (off|always|same-dir) ]] || _POWERLEVEL9K_TRANSIENT_PROMPT=off
  6583. _p9k_declare -b POWERLEVEL9K_TERM_SHELL_INTEGRATION 0
  6584. if [[ __p9k_force_term_shell_integration -eq 1 || $ITERM_SHELL_INTEGRATION_INSTALLED == Yes ]]; then
  6585. _POWERLEVEL9K_TERM_SHELL_INTEGRATION=1
  6586. fi
  6587. _p9k_declare -s POWERLEVEL9K_WORKER_LOG_LEVEL
  6588. _p9k_declare -i POWERLEVEL9K_COMMANDS_MAX_TOKEN_COUNT 64
  6589. _p9k_declare -a POWERLEVEL9K_HOOK_WIDGETS --
  6590. _p9k_declare -b POWERLEVEL9K_TODO_HIDE_ZERO_TOTAL 0
  6591. _p9k_declare -b POWERLEVEL9K_TODO_HIDE_ZERO_FILTERED 0
  6592. _p9k_declare -b POWERLEVEL9K_DISABLE_HOT_RELOAD 0
  6593. _p9k_declare -F POWERLEVEL9K_NEW_TTY_MAX_AGE_SECONDS 5
  6594. _p9k_declare -i POWERLEVEL9K_INSTANT_PROMPT_COMMAND_LINES
  6595. _p9k_declare -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS -- context dir vcs
  6596. _p9k_declare -a POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS -- status root_indicator background_jobs history time
  6597. _p9k_declare -b POWERLEVEL9K_DISABLE_RPROMPT 0
  6598. _p9k_declare -b POWERLEVEL9K_PROMPT_ADD_NEWLINE 0
  6599. _p9k_declare -b POWERLEVEL9K_PROMPT_ON_NEWLINE 0
  6600. _p9k_declare -b POWERLEVEL9K_RPROMPT_ON_NEWLINE 0
  6601. _p9k_declare -b POWERLEVEL9K_SHOW_RULER 0
  6602. _p9k_declare -i POWERLEVEL9K_PROMPT_ADD_NEWLINE_COUNT 1
  6603. _p9k_declare -s POWERLEVEL9K_COLOR_SCHEME dark
  6604. _p9k_declare -s POWERLEVEL9K_GITSTATUS_DIR ""
  6605. _p9k_declare -s POWERLEVEL9K_VCS_DISABLED_WORKDIR_PATTERN
  6606. _p9k_declare -b POWERLEVEL9K_VCS_SHOW_SUBMODULE_DIRTY 0
  6607. _p9k_declare -i POWERLEVEL9K_VCS_SHORTEN_LENGTH
  6608. _p9k_declare -i POWERLEVEL9K_VCS_SHORTEN_MIN_LENGTH
  6609. _p9k_declare -s POWERLEVEL9K_VCS_SHORTEN_STRATEGY
  6610. if [[ $langinfo[CODESET] == (utf|UTF)(-|)8 ]]; then
  6611. _p9k_declare -e POWERLEVEL9K_VCS_SHORTEN_DELIMITER '\u2026'
  6612. else
  6613. _p9k_declare -e POWERLEVEL9K_VCS_SHORTEN_DELIMITER '..'
  6614. fi
  6615. _p9k_declare -b POWERLEVEL9K_VCS_CONFLICTED_STATE 0
  6616. _p9k_declare -b POWERLEVEL9K_HIDE_BRANCH_ICON 0
  6617. _p9k_declare -b POWERLEVEL9K_VCS_HIDE_TAGS 0
  6618. _p9k_declare -i POWERLEVEL9K_CHANGESET_HASH_LENGTH 8
  6619. # Specifies the maximum number of elements in the cache. When the cache grows over this limit,
  6620. # it gets cleared. This is meant to avoid memory leaks when a rogue prompt is filling the cache
  6621. # with data.
  6622. _p9k_declare -i POWERLEVEL9K_MAX_CACHE_SIZE 10000
  6623. _p9k_declare -e POWERLEVEL9K_ANACONDA_LEFT_DELIMITER "("
  6624. _p9k_declare -e POWERLEVEL9K_ANACONDA_RIGHT_DELIMITER ")"
  6625. _p9k_declare -b POWERLEVEL9K_ANACONDA_SHOW_PYTHON_VERSION 1
  6626. _p9k_declare -b POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE 1
  6627. _p9k_declare -b POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS 0
  6628. _p9k_declare -b POWERLEVEL9K_DISK_USAGE_ONLY_WARNING 0
  6629. _p9k_declare -i POWERLEVEL9K_DISK_USAGE_WARNING_LEVEL 90
  6630. _p9k_declare -i POWERLEVEL9K_DISK_USAGE_CRITICAL_LEVEL 95
  6631. _p9k_declare -i POWERLEVEL9K_BATTERY_LOW_THRESHOLD 10
  6632. _p9k_declare -i POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD 999
  6633. _p9k_declare -b POWERLEVEL9K_BATTERY_VERBOSE 1
  6634. _p9k_declare -a POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND --
  6635. _p9k_declare -a POWERLEVEL9K_BATTERY_LEVEL_FOREGROUND --
  6636. case $parameters[POWERLEVEL9K_BATTERY_STAGES] in
  6637. scalar*) typeset -ga _POWERLEVEL9K_BATTERY_STAGES=("${(@s::)${(g::)POWERLEVEL9K_BATTERY_STAGES}}");;
  6638. array*) typeset -ga _POWERLEVEL9K_BATTERY_STAGES=("${(@g::)POWERLEVEL9K_BATTERY_STAGES}");;
  6639. *) typeset -ga _POWERLEVEL9K_BATTERY_STAGES=();;
  6640. esac
  6641. local state
  6642. for state in CHARGED CHARGING LOW DISCONNECTED; do
  6643. _p9k_declare -i POWERLEVEL9K_BATTERY_${state}_HIDE_ABOVE_THRESHOLD $_POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD
  6644. local var=POWERLEVEL9K_BATTERY_${state}_STAGES
  6645. case $parameters[$var] in
  6646. scalar*) eval "typeset -ga _$var=(${(@qq)${(@s::)${(g::)${(P)var}}}})";;
  6647. array*) eval "typeset -ga _$var=(${(@qq)${(@g::)${(@P)var}}})";;
  6648. *) eval "typeset -ga _$var=(${(@qq)_POWERLEVEL9K_BATTERY_STAGES})";;
  6649. esac
  6650. local var=POWERLEVEL9K_BATTERY_${state}_LEVEL_BACKGROUND
  6651. case $parameters[$var] in
  6652. array*) eval "typeset -ga _$var=(${(@qq)${(@P)var}})";;
  6653. *) eval "typeset -ga _$var=(${(@qq)_POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND})";;
  6654. esac
  6655. local var=POWERLEVEL9K_BATTERY_${state}_LEVEL_FOREGROUND
  6656. case $parameters[$var] in
  6657. array*) eval "typeset -ga _$var=(${(@qq)${(@P)var}})";;
  6658. *) eval "typeset -ga _$var=(${(@qq)_POWERLEVEL9K_BATTERY_LEVEL_FOREGROUND})";;
  6659. esac
  6660. done
  6661. _p9k_declare -F POWERLEVEL9K_PUBLIC_IP_TIMEOUT 300
  6662. _p9k_declare -a POWERLEVEL9K_PUBLIC_IP_METHODS -- dig curl wget
  6663. _p9k_declare -e POWERLEVEL9K_PUBLIC_IP_NONE ""
  6664. _p9k_declare -s POWERLEVEL9K_PUBLIC_IP_HOST "https://v4.ident.me/"
  6665. _p9k_declare -s POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ""
  6666. _p9k_segment_in_use public_ip || _POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE=
  6667. _p9k_declare -b POWERLEVEL9K_ALWAYS_SHOW_CONTEXT 0
  6668. _p9k_declare -b POWERLEVEL9K_ALWAYS_SHOW_USER 0
  6669. _p9k_declare -e POWERLEVEL9K_CONTEXT_TEMPLATE "%n@%m"
  6670. _p9k_declare -e POWERLEVEL9K_USER_TEMPLATE "%n"
  6671. _p9k_declare -e POWERLEVEL9K_HOST_TEMPLATE "%m"
  6672. _p9k_declare -F POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD 3
  6673. _p9k_declare -i POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION 2
  6674. # Other options: "d h m s".
  6675. _p9k_declare -s POWERLEVEL9K_COMMAND_EXECUTION_TIME_FORMAT "H:M:S"
  6676. _p9k_declare -e POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~"
  6677. _p9k_declare -b POWERLEVEL9K_DIR_PATH_ABSOLUTE 0
  6678. _p9k_declare -s POWERLEVEL9K_DIR_SHOW_WRITABLE ''
  6679. case $_POWERLEVEL9K_DIR_SHOW_WRITABLE in
  6680. true) _POWERLEVEL9K_DIR_SHOW_WRITABLE=1;;
  6681. v2) _POWERLEVEL9K_DIR_SHOW_WRITABLE=2;;
  6682. v3) _POWERLEVEL9K_DIR_SHOW_WRITABLE=3;;
  6683. *) _POWERLEVEL9K_DIR_SHOW_WRITABLE=0;;
  6684. esac
  6685. typeset -gi _POWERLEVEL9K_DIR_SHOW_WRITABLE
  6686. _p9k_declare -b POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER 0
  6687. _p9k_declare -b POWERLEVEL9K_DIR_HYPERLINK 0
  6688. _p9k_declare -s POWERLEVEL9K_SHORTEN_STRATEGY ""
  6689. local markers=(
  6690. .bzr
  6691. .citc
  6692. .git
  6693. .hg
  6694. .node-version
  6695. .python-version
  6696. .ruby-version
  6697. .shorten_folder_marker
  6698. .svn
  6699. .terraform
  6700. CVS
  6701. Cargo.toml
  6702. composer.json
  6703. go.mod
  6704. package.json
  6705. )
  6706. _p9k_declare -s POWERLEVEL9K_SHORTEN_FOLDER_MARKER "(${(j:|:)markers})"
  6707. # Shorten directory if it's longer than this even if there is space for it.
  6708. # The value can be either absolute (e.g., '80') or a percentage of terminal
  6709. # width (e.g, '50%'). If empty, directory will be shortened only when prompt
  6710. # doesn't fit. Applies only when POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_unique.
  6711. _p9k_declare -s POWERLEVEL9K_DIR_MAX_LENGTH 0
  6712. # Individual elements are patterns. They are expanded with the options set
  6713. # by `emulate zsh && setopt extended_glob`.
  6714. _p9k_declare -a POWERLEVEL9K_DIR_PACKAGE_FILES -- package.json composer.json
  6715. # When dir is on the last prompt line, try to shorten it enough to leave at least this many
  6716. # columns for typing commands. Applies only when POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_unique.
  6717. _p9k_declare -i POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS 40
  6718. # When dir is on the last prompt line, try to shorten it enough to leave at least
  6719. # COLUMNS * POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS_PCT * 0.01 columns for typing commands. Applies
  6720. # only when POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_unique.
  6721. _p9k_declare -F POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS_PCT 50
  6722. # POWERLEVEL9K_DIR_CLASSES allow you to specify custom styling and icons for different
  6723. # directories.
  6724. #
  6725. # POWERLEVEL9K_DIR_CLASSES must be an array with 3 * N elements. Each triplet consists of:
  6726. #
  6727. # 1. A pattern against which the current directory is matched. Matching is done with
  6728. # extended_glob option enabled.
  6729. # 2. Directory class for the purpose of styling.
  6730. # 3. Icon.
  6731. #
  6732. # Triplets are tried in order. The first triplet whose pattern matches $PWD wins. If there are no
  6733. # matches, there will be no icon and the styling is done according to POWERLEVEL9K_DIR_BACKGROUND,
  6734. # POWERLEVEL9K_DIR_FOREGROUND, etc.
  6735. #
  6736. # Example:
  6737. #
  6738. # POWERLEVEL9K_DIR_CLASSES=(
  6739. # '~/work(/*)#' WORK '(╯°□°)╯︵ ┻━┻'
  6740. # '~(/*)#' HOME '⌂'
  6741. # '*' DEFAULT '')
  6742. #
  6743. # POWERLEVEL9K_DIR_WORK_BACKGROUND=red
  6744. # POWERLEVEL9K_DIR_HOME_BACKGROUND=blue
  6745. # POWERLEVEL9K_DIR_DEFAULT_BACKGROUND=yellow
  6746. #
  6747. # With these settings, the current directory in the prompt may look like this:
  6748. #
  6749. # (╯°□°)╯︵ ┻━┻ ~/work/projects/important/urgent
  6750. #
  6751. # ⌂ ~/best/powerlevel10k
  6752. _p9k_declare -a POWERLEVEL9K_DIR_CLASSES
  6753. _p9k_declare -i POWERLEVEL9K_SHORTEN_DELIMITER_LENGTH
  6754. _p9k_declare -e POWERLEVEL9K_SHORTEN_DELIMITER
  6755. _p9k_declare -s POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER ''
  6756. case $_POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER in
  6757. first|last) _POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER+=:0;;
  6758. (first|last):(|-)<->);;
  6759. *) _POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER=;;
  6760. esac
  6761. [[ -z $_POWERLEVEL9K_SHORTEN_FOLDER_MARKER ]] && _POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER=
  6762. _p9k_declare -i POWERLEVEL9K_SHORTEN_DIR_LENGTH
  6763. _p9k_declare -s POWERLEVEL9K_IP_INTERFACE ""
  6764. : ${_POWERLEVEL9K_IP_INTERFACE:='.*'}
  6765. _p9k_segment_in_use ip || _POWERLEVEL9K_IP_INTERFACE=
  6766. _p9k_declare -s POWERLEVEL9K_VPN_IP_INTERFACE "(gpd|wg|(.*tun)|tailscale)[0-9]*"
  6767. : ${_POWERLEVEL9K_VPN_IP_INTERFACE:='.*'}
  6768. _p9k_segment_in_use vpn_ip || _POWERLEVEL9K_VPN_IP_INTERFACE=
  6769. _p9k_declare -b POWERLEVEL9K_VPN_IP_SHOW_ALL 0
  6770. _p9k_declare -i POWERLEVEL9K_LOAD_WHICH 5
  6771. case $_POWERLEVEL9K_LOAD_WHICH in
  6772. 1) _POWERLEVEL9K_LOAD_WHICH=1;;
  6773. 15) _POWERLEVEL9K_LOAD_WHICH=3;;
  6774. *) _POWERLEVEL9K_LOAD_WHICH=2;;
  6775. esac
  6776. _p9k_declare -F POWERLEVEL9K_LOAD_WARNING_PCT 50
  6777. _p9k_declare -F POWERLEVEL9K_LOAD_CRITICAL_PCT 70
  6778. _p9k_declare -b POWERLEVEL9K_NODE_VERSION_PROJECT_ONLY 0
  6779. _p9k_declare -b POWERLEVEL9K_PHP_VERSION_PROJECT_ONLY 0
  6780. _p9k_declare -b POWERLEVEL9K_DOTNET_VERSION_PROJECT_ONLY 1
  6781. _p9k_declare -b POWERLEVEL9K_GO_VERSION_PROJECT_ONLY 1
  6782. _p9k_declare -b POWERLEVEL9K_RUST_VERSION_PROJECT_ONLY 1
  6783. _p9k_declare -b POWERLEVEL9K_JAVA_VERSION_PROJECT_ONLY 0
  6784. _p9k_declare -b POWERLEVEL9K_NODENV_PROMPT_ALWAYS_SHOW 0
  6785. _p9k_declare -a POWERLEVEL9K_NODENV_SOURCES -- shell local global
  6786. _p9k_declare -b POWERLEVEL9K_NODENV_SHOW_SYSTEM 1
  6787. _p9k_declare -b POWERLEVEL9K_RBENV_PROMPT_ALWAYS_SHOW 0
  6788. _p9k_declare -a POWERLEVEL9K_RBENV_SOURCES -- shell local global
  6789. _p9k_declare -b POWERLEVEL9K_RBENV_SHOW_SYSTEM 1
  6790. _p9k_declare -b POWERLEVEL9K_SCALAENV_PROMPT_ALWAYS_SHOW 0
  6791. _p9k_declare -a POWERLEVEL9K_SCALAENV_SOURCES -- shell local global
  6792. _p9k_declare -b POWERLEVEL9K_SCALAENV_SHOW_SYSTEM 1
  6793. _p9k_declare -b POWERLEVEL9K_PHPENV_PROMPT_ALWAYS_SHOW 0
  6794. _p9k_declare -a POWERLEVEL9K_PHPENV_SOURCES -- shell local global
  6795. _p9k_declare -b POWERLEVEL9K_PHPENV_SHOW_SYSTEM 1
  6796. _p9k_declare -b POWERLEVEL9K_LUAENV_PROMPT_ALWAYS_SHOW 0
  6797. _p9k_declare -a POWERLEVEL9K_LUAENV_SOURCES -- shell local global
  6798. _p9k_declare -b POWERLEVEL9K_LUAENV_SHOW_SYSTEM 1
  6799. _p9k_declare -b POWERLEVEL9K_JENV_PROMPT_ALWAYS_SHOW 0
  6800. _p9k_declare -a POWERLEVEL9K_JENV_SOURCES -- shell local global
  6801. _p9k_declare -b POWERLEVEL9K_JENV_SHOW_SYSTEM 1
  6802. _p9k_declare -b POWERLEVEL9K_PLENV_PROMPT_ALWAYS_SHOW 0
  6803. _p9k_declare -a POWERLEVEL9K_PLENV_SOURCES -- shell local global
  6804. _p9k_declare -b POWERLEVEL9K_PLENV_SHOW_SYSTEM 1
  6805. _p9k_declare -b POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW 0
  6806. _p9k_declare -b POWERLEVEL9K_PYENV_SHOW_SYSTEM 1
  6807. _p9k_declare -a POWERLEVEL9K_PYENV_SOURCES -- shell local global
  6808. _p9k_declare -b POWERLEVEL9K_GOENV_PROMPT_ALWAYS_SHOW 0
  6809. _p9k_declare -a POWERLEVEL9K_GOENV_SOURCES -- shell local global
  6810. _p9k_declare -b POWERLEVEL9K_GOENV_SHOW_SYSTEM 1
  6811. _p9k_declare -b POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW 0
  6812. _p9k_declare -b POWERLEVEL9K_ASDF_SHOW_SYSTEM 1
  6813. _p9k_declare -a POWERLEVEL9K_ASDF_SOURCES -- shell local global
  6814. local var
  6815. for var in ${parameters[(I)POWERLEVEL9K_ASDF_*_PROMPT_ALWAYS_SHOW]}; do
  6816. _p9k_declare -b $var $_POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW
  6817. done
  6818. for var in ${parameters[(I)POWERLEVEL9K_ASDF_*_SHOW_SYSTEM]}; do
  6819. _p9k_declare -b $var $_POWERLEVEL9K_ASDF_SHOW_SYSTEM
  6820. done
  6821. for var in ${parameters[(I)POWERLEVEL9K_ASDF_*_SOURCES]}; do
  6822. _p9k_declare -a $var -- $_POWERLEVEL9K_ASDF_SOURCES
  6823. done
  6824. _p9k_declare -b POWERLEVEL9K_HASKELL_STACK_PROMPT_ALWAYS_SHOW 1
  6825. _p9k_declare -a POWERLEVEL9K_HASKELL_STACK_SOURCES -- shell local
  6826. _p9k_declare -b POWERLEVEL9K_RVM_SHOW_GEMSET 0
  6827. _p9k_declare -b POWERLEVEL9K_RVM_SHOW_PREFIX 0
  6828. _p9k_declare -b POWERLEVEL9K_CHRUBY_SHOW_VERSION 1
  6829. _p9k_declare -b POWERLEVEL9K_CHRUBY_SHOW_ENGINE 1
  6830. _p9k_declare -b POWERLEVEL9K_STATUS_CROSS 0
  6831. _p9k_declare -b POWERLEVEL9K_STATUS_OK 1
  6832. _p9k_declare -b POWERLEVEL9K_STATUS_OK_PIPE 1
  6833. _p9k_declare -b POWERLEVEL9K_STATUS_ERROR 1
  6834. _p9k_declare -b POWERLEVEL9K_STATUS_ERROR_PIPE 1
  6835. _p9k_declare -b POWERLEVEL9K_STATUS_ERROR_SIGNAL 1
  6836. _p9k_declare -b POWERLEVEL9K_STATUS_SHOW_PIPESTATUS 1
  6837. _p9k_declare -b POWERLEVEL9K_STATUS_HIDE_SIGNAME 0
  6838. _p9k_declare -b POWERLEVEL9K_STATUS_VERBOSE_SIGNAME 1
  6839. _p9k_declare -b POWERLEVEL9K_STATUS_EXTENDED_STATES 0
  6840. _p9k_declare -b POWERLEVEL9K_STATUS_VERBOSE 1
  6841. _p9k_declare -b POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE 0
  6842. _p9k_declare -e POWERLEVEL9K_DATE_FORMAT "%D{%d.%m.%y}"
  6843. _p9k_declare -s POWERLEVEL9K_VCS_ACTIONFORMAT_FOREGROUND 1
  6844. _p9k_declare -b POWERLEVEL9K_SHOW_CHANGESET 0
  6845. _p9k_declare -e POWERLEVEL9K_VCS_LOADING_TEXT loading
  6846. _p9k_declare -a POWERLEVEL9K_VCS_GIT_HOOKS -- vcs-detect-changes git-untracked git-aheadbehind git-stash git-remotebranch git-tagname
  6847. _p9k_declare -a POWERLEVEL9K_VCS_HG_HOOKS -- vcs-detect-changes
  6848. _p9k_declare -a POWERLEVEL9K_VCS_SVN_HOOKS -- vcs-detect-changes svn-detect-changes
  6849. # If it takes longer than this to fetch git repo status, display the prompt with a greyed out
  6850. # vcs segment and fix it asynchronously when the results come it.
  6851. _p9k_declare -F POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS 0.01
  6852. (( POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS >= 0 )) || (( POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS = 0 ))
  6853. _p9k_declare -a POWERLEVEL9K_VCS_BACKENDS -- git
  6854. (( $+commands[git] )) || _POWERLEVEL9K_VCS_BACKENDS=(${_POWERLEVEL9K_VCS_BACKENDS:#git})
  6855. _p9k_declare -b POWERLEVEL9K_VCS_DISABLE_GITSTATUS_FORMATTING 0
  6856. _p9k_declare -i POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY -1
  6857. _p9k_declare -i POWERLEVEL9K_VCS_STAGED_MAX_NUM 1
  6858. _p9k_declare -i POWERLEVEL9K_VCS_UNSTAGED_MAX_NUM 1
  6859. _p9k_declare -i POWERLEVEL9K_VCS_UNTRACKED_MAX_NUM 1
  6860. _p9k_declare -i POWERLEVEL9K_VCS_CONFLICTED_MAX_NUM 1
  6861. _p9k_declare -i POWERLEVEL9K_VCS_COMMITS_AHEAD_MAX_NUM -1
  6862. _p9k_declare -i POWERLEVEL9K_VCS_COMMITS_BEHIND_MAX_NUM -1
  6863. _p9k_declare -b POWERLEVEL9K_VCS_RECURSE_UNTRACKED_DIRS 0
  6864. _p9k_declare -b POWERLEVEL9K_DISABLE_GITSTATUS 0
  6865. _p9k_declare -e POWERLEVEL9K_VI_INSERT_MODE_STRING "INSERT"
  6866. _p9k_declare -e POWERLEVEL9K_VI_COMMAND_MODE_STRING "NORMAL"
  6867. # VISUAL mode is shown as NORMAL unless POWERLEVEL9K_VI_VISUAL_MODE_STRING is explicitly set.
  6868. _p9k_declare -e POWERLEVEL9K_VI_VISUAL_MODE_STRING
  6869. # OVERWRITE mode is shown as INSERT unless POWERLEVEL9K_VI_OVERWRITE_MODE_STRING is explicitly set.
  6870. _p9k_declare -e POWERLEVEL9K_VI_OVERWRITE_MODE_STRING
  6871. _p9k_declare -s POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV true
  6872. _p9k_declare -b POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION 1
  6873. _p9k_declare -e POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER "("
  6874. _p9k_declare -e POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER ")"
  6875. _p9k_declare -a POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES -- virtualenv venv .venv env
  6876. _POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES="${(j.|.)_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES}"
  6877. _p9k_declare -b POWERLEVEL9K_NODEENV_SHOW_NODE_VERSION 1
  6878. _p9k_declare -e POWERLEVEL9K_NODEENV_LEFT_DELIMITER "["
  6879. _p9k_declare -e POWERLEVEL9K_NODEENV_RIGHT_DELIMITER "]"
  6880. _p9k_declare -b POWERLEVEL9K_KUBECONTEXT_SHOW_DEFAULT_NAMESPACE 1
  6881. _p9k_declare -a POWERLEVEL9K_KUBECONTEXT_SHORTEN --
  6882. # Defines context classes for the purpose of applying different styling to different contexts.
  6883. #
  6884. # POWERLEVEL9K_KUBECONTEXT_CLASSES must be an array with even number of elements. The first
  6885. # element in each pair defines a pattern against which the current context (in the format it is
  6886. # displayed in the prompt) gets matched. The second element defines context class. Patterns are
  6887. # tried in order. The first match wins.
  6888. #
  6889. # If a non-empty class <C> is assigned to a context, the segment is styled with
  6890. # POWERLEVEL9K_KUBECONTEXT_<U>_BACKGROUND and POWERLEVEL9K_KUBECONTEXT_<U>_FOREGROUND where <U> is
  6891. # uppercased <C>. Otherwise with POWERLEVEL9K_KUBECONTEXT_BACKGROUND and
  6892. # POWERLEVEL9K_KUBECONTEXT_FOREGROUND.
  6893. #
  6894. # Example: Use red background for contexts containing "prod", green for "testing" and yellow for
  6895. # everything else.
  6896. #
  6897. # POWERLEVEL9K_KUBECONTEXT_CLASSES=(
  6898. # '*prod*' prod
  6899. # '*testing*' testing
  6900. # '*' other)
  6901. #
  6902. # POWERLEVEL9K_KUBECONTEXT_PROD_BACKGROUND=red
  6903. # POWERLEVEL9K_KUBECONTEXT_TESTING_BACKGROUND=green
  6904. # POWERLEVEL9K_KUBECONTEXT_OTHER_BACKGROUND=yellow
  6905. _p9k_declare -a POWERLEVEL9K_KUBECONTEXT_CLASSES --
  6906. _p9k_declare -a POWERLEVEL9K_AWS_CLASSES --
  6907. _p9k_declare -a POWERLEVEL9K_AZURE_CLASSES --
  6908. _p9k_declare -a POWERLEVEL9K_TERRAFORM_CLASSES --
  6909. _p9k_declare -b POWERLEVEL9K_TERRAFORM_SHOW_DEFAULT 0
  6910. _p9k_declare -a POWERLEVEL9K_GOOGLE_APP_CRED_CLASSES -- 'service_account:*' SERVICE_ACCOUNT
  6911. # Specifies the format of java version.
  6912. #
  6913. # POWERLEVEL9K_JAVA_VERSION_FULL=true => 1.8.0_212-8u212-b03-0ubuntu1.18.04.1-b03
  6914. # POWERLEVEL9K_JAVA_VERSION_FULL=false => 1.8.0_212
  6915. #
  6916. # These correspond to `java -fullversion` and `java -version` respectively.
  6917. _p9k_declare -b POWERLEVEL9K_JAVA_VERSION_FULL 1
  6918. _p9k_declare -b POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE 0
  6919. # Format for the current time: 09:51:02. See `man 3 strftime`.
  6920. _p9k_declare -e POWERLEVEL9K_TIME_FORMAT "%D{%H:%M:%S}"
  6921. # If set to true, time will update when you hit enter. This way prompts for the past
  6922. # commands will contain the start times of their commands as opposed to the default
  6923. # behavior where they contain the end times of their preceding commands.
  6924. _p9k_declare -b POWERLEVEL9K_TIME_UPDATE_ON_COMMAND 0
  6925. # If set to true, time will update every second.
  6926. _p9k_declare -b POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME 0
  6927. local -i i=1
  6928. while (( i <= $#_POWERLEVEL9K_LEFT_PROMPT_ELEMENTS )); do
  6929. local segment=${${(U)_POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[i]}//İ/I}
  6930. local var=POWERLEVEL9K_${segment}_LEFT_DISABLED
  6931. (( $+parameters[$var] )) || var=POWERLEVEL9K_${segment}_DISABLED
  6932. if [[ ${(P)var} == true ]]; then
  6933. _POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[i,i]=()
  6934. else
  6935. (( ++i ))
  6936. fi
  6937. done
  6938. local -i i=1
  6939. while (( i <= $#_POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS )); do
  6940. local segment=${${(U)_POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[i]}//İ/I}
  6941. local var=POWERLEVEL9K_${segment}_RIGHT_DISABLED
  6942. (( $+parameters[$var] )) || var=POWERLEVEL9K_${segment}_DISABLED
  6943. if [[ ${(P)var} == true ]]; then
  6944. _POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[i,i]=()
  6945. else
  6946. (( ++i ))
  6947. fi
  6948. done
  6949. local var
  6950. for var in ${(@)${parameters[(I)POWERLEVEL9K_*]}/(#m)*/${(M)${parameters[_$MATCH]-$MATCH}:#$MATCH}}; do
  6951. case $parameters[$var] in
  6952. (scalar|integer|float)*) typeset -g _$var=${(P)var};;
  6953. array*) eval 'typeset -ga '_$var'=("${'$var'[@]}")';;
  6954. esac
  6955. done
  6956. }
  6957. function _p9k_on_widget_zle-keymap-select() { _p9k_check_visual_mode; __p9k_reset_state=2; }
  6958. function _p9k_on_widget_overwrite-mode() { _p9k_check_visual_mode; __p9k_reset_state=2; }
  6959. function _p9k_on_widget_vi-replace() { _p9k_check_visual_mode; __p9k_reset_state=2; }
  6960. if is-at-least 5.3; then
  6961. function _p9k_check_visual_mode() {
  6962. [[ ${KEYMAP:-} == vicmd ]] || return 0
  6963. local region=${${REGION_ACTIVE:-0}/2/1}
  6964. [[ $region != $_p9k__region_active ]] || return 0
  6965. _p9k__region_active=$region
  6966. __p9k_reset_state=2
  6967. }
  6968. else
  6969. function _p9k_check_visual_mode() {}
  6970. fi
  6971. function _p9k_on_widget_visual-mode() { _p9k_check_visual_mode; }
  6972. function _p9k_on_widget_visual-line-mode() { _p9k_check_visual_mode; }
  6973. function _p9k_on_widget_deactivate-region() { _p9k_check_visual_mode; }
  6974. function _p9k_on_widget_zle-line-init() {
  6975. (( _p9k__cursor_hidden )) || return 0
  6976. _p9k__cursor_hidden=0
  6977. echoti cnorm
  6978. }
  6979. function _p9k_on_widget_zle-line-finish() {
  6980. (( $+_p9k__line_finished )) && return
  6981. local P9K_PROMPT=transient
  6982. _p9k__line_finished=
  6983. (( _p9k_reset_on_line_finish )) && __p9k_reset_state=2
  6984. (( $+functions[p10k-on-post-prompt] )) && p10k-on-post-prompt
  6985. local -i optimized
  6986. if [[ -n $_p9k_transient_prompt ]]; then
  6987. if [[ $_POWERLEVEL9K_TRANSIENT_PROMPT == always || $_p9k__cwd == $_p9k__last_prompt_pwd ]]; then
  6988. optimized=1
  6989. __p9k_reset_state=2
  6990. else
  6991. _p9k__last_prompt_pwd=$_p9k__cwd
  6992. fi
  6993. fi
  6994. if [[ $1 == int ]]; then
  6995. _p9k__must_restore_prompt=1
  6996. if (( !_p9k__restore_prompt_fd )); then
  6997. sysopen -o cloexec -ru _p9k__restore_prompt_fd /dev/null
  6998. zle -F $_p9k__restore_prompt_fd _p9k_restore_prompt
  6999. fi
  7000. fi
  7001. if (( __p9k_reset_state == 2 )); then
  7002. if (( optimized )); then
  7003. RPROMPT= PROMPT=$_p9k_transient_prompt _p9k_reset_prompt
  7004. else
  7005. _p9k_reset_prompt
  7006. fi
  7007. fi
  7008. _p9k__line_finished='%{%}'
  7009. }
  7010. function _p9k_on_widget_send-break() {
  7011. _p9k_on_widget_zle-line-finish int
  7012. }
  7013. # Usage example: _p9k_display_segment 58 _p9k__1rkubecontext hide
  7014. function _p9k_display_segment() {
  7015. [[ $_p9k__display_v[$1] == $3 ]] && return
  7016. _p9k__display_v[$1]=$3
  7017. [[ $3 == hide ]] && typeset -g $2= || unset $2
  7018. __p9k_reset_state=2
  7019. }
  7020. function _p9k_redraw() {
  7021. zle -F $1
  7022. exec {1}>&-
  7023. _p9k__redraw_fd=0
  7024. () {
  7025. local -h WIDGET=zle-line-pre-redraw
  7026. _p9k_widget_hook ''
  7027. }
  7028. }
  7029. function _p9k_deschedule_redraw() {
  7030. (( _p9k__redraw_fd )) || return
  7031. zle -F $_p9k__redraw_fd
  7032. exec {_p9k__redraw_fd}>&-
  7033. _p9k__redraw_fd=0
  7034. }
  7035. function _p9k_widget_hook() {
  7036. _p9k_deschedule_redraw
  7037. if (( ${+functions[p10k-on-post-widget]} || ${#_p9k_show_on_command} )); then
  7038. local -a P9K_COMMANDS
  7039. if [[ "$_p9k__last_buffer" == "$PREBUFFER$BUFFER" ]]; then
  7040. P9K_COMMANDS=(${_p9k__last_commands[@]})
  7041. else
  7042. _p9k__last_buffer="$PREBUFFER$BUFFER"
  7043. if [[ -n "$_p9k__last_buffer" ]]; then
  7044. # this must run with user options
  7045. _p9k_parse_buffer "$_p9k__last_buffer" $_POWERLEVEL9K_COMMANDS_MAX_TOKEN_COUNT
  7046. fi
  7047. _p9k__last_commands=(${P9K_COMMANDS[@]})
  7048. fi
  7049. fi
  7050. eval "$__p9k_intro"
  7051. (( _p9k__restore_prompt_fd )) && _p9k_restore_prompt $_p9k__restore_prompt_fd
  7052. if [[ $1 == (clear-screen|z4h-clear-screen-*-top) ]]; then
  7053. P9K_TTY=new
  7054. _p9k__expanded=0
  7055. _p9k_reset_prompt
  7056. fi
  7057. __p9k_reset_state=1
  7058. _p9k_check_visual_mode
  7059. local pat idx var
  7060. for pat idx var in $_p9k_show_on_command; do
  7061. if (( $P9K_COMMANDS[(I)$pat] )); then
  7062. _p9k_display_segment $idx $var show
  7063. else
  7064. _p9k_display_segment $idx $var hide
  7065. fi
  7066. done
  7067. (( $+functions[p10k-on-post-widget] )) && p10k-on-post-widget "${@:2}"
  7068. (( $+functions[_p9k_on_widget_$1] )) && _p9k_on_widget_$1
  7069. (( __p9k_reset_state == 2 )) && _p9k_reset_prompt
  7070. __p9k_reset_state=0
  7071. }
  7072. function _p9k_widget() {
  7073. local f=${widgets[._p9k_orig_$1]:-}
  7074. local -i res
  7075. [[ -z $f ]] || {
  7076. [[ $f == user:-z4h-* ]] && {
  7077. "${f#user:}" "${@:2}"
  7078. res=$?
  7079. } || {
  7080. zle ._p9k_orig_$1 -- "${@:2}"
  7081. res=$?
  7082. }
  7083. }
  7084. (( ! __p9k_enabled )) || [[ $CONTEXT != start ]] || _p9k_widget_hook "$@"
  7085. return res
  7086. }
  7087. function _p9k_widget_zle-line-pre-redraw-impl() {
  7088. (( __p9k_enabled )) && [[ $CONTEXT == start ]] || return 0
  7089. ! (( ${+functions[p10k-on-post-widget]} || ${#_p9k_show_on_command} || _p9k__restore_prompt_fd || _p9k__redraw_fd )) &&
  7090. [[ ${KEYMAP:-} != vicmd ]] &&
  7091. return
  7092. (( PENDING || KEYS_QUEUED_COUNT )) && {
  7093. (( _p9k__redraw_fd )) || {
  7094. sysopen -o cloexec -ru _p9k__redraw_fd /dev/null
  7095. zle -F $_p9k__redraw_fd _p9k_redraw
  7096. }
  7097. return
  7098. }
  7099. _p9k_widget_hook zle-line-pre-redraw
  7100. }
  7101. function _p9k_widget_send-break() {
  7102. (( ! __p9k_enabled )) || [[ $CONTEXT != start ]] || {
  7103. _p9k_widget_hook send-break "$@"
  7104. }
  7105. local f=${widgets[._p9k_orig_send-break]:-}
  7106. [[ -z $f ]] || zle ._p9k_orig_send-break -- "$@"
  7107. }
  7108. typeset -gi __p9k_widgets_wrapped=0
  7109. function _p9k_wrap_widgets() {
  7110. (( __p9k_widgets_wrapped )) && return
  7111. typeset -gir __p9k_widgets_wrapped=1
  7112. local -a widget_list
  7113. if is-at-least 5.3; then
  7114. local -aU widget_list=(
  7115. zle-line-pre-redraw
  7116. zle-line-init
  7117. zle-line-finish
  7118. zle-keymap-select
  7119. overwrite-mode
  7120. vi-replace
  7121. visual-mode
  7122. visual-line-mode
  7123. deactivate-region
  7124. clear-screen
  7125. z4h-clear-screen-soft-top
  7126. z4h-clear-screen-hard-top
  7127. send-break
  7128. $_POWERLEVEL9K_HOOK_WIDGETS
  7129. )
  7130. else
  7131. # There is no zle-line-pre-redraw in zsh < 5.3, so we have to wrap all widgets
  7132. # with key bindings. This costs extra 3ms: 1.5ms to fetch the list of widgets and
  7133. # another 1.5ms to wrap them.
  7134. local keymap tmp=${TMPDIR:-/tmp}/p10k.bindings.$sysparams[pid]
  7135. {
  7136. for keymap in $keymaps; do bindkey -M $keymap; done >$tmp
  7137. local -aU widget_list=(
  7138. zle-isearch-exit
  7139. zle-isearch-update
  7140. zle-line-init
  7141. zle-line-finish
  7142. zle-history-line-set
  7143. zle-keymap-select
  7144. send-break
  7145. $_POWERLEVEL9K_HOOK_WIDGETS
  7146. ${${${(f)"$(<$tmp)"}##* }:#(*\"|.*)}
  7147. )
  7148. } always {
  7149. zf_rm -f -- $tmp
  7150. }
  7151. fi
  7152. local widget
  7153. for widget in $widget_list; do
  7154. if (( ! $+functions[_p9k_widget_$widget] )); then
  7155. functions[_p9k_widget_$widget]='_p9k_widget '${(q)widget}' "$@"'
  7156. fi
  7157. if [[ $widget == zle-* &&
  7158. $widgets[$widget] == user:azhw:* &&
  7159. $functions[add-zle-hook-widget] ]]; then
  7160. add-zle-hook-widget $widget _p9k_widget_$widget
  7161. else
  7162. # The leading dot is to work around bugs in zsh-syntax-highlighting.
  7163. zle -A $widget ._p9k_orig_$widget
  7164. zle -N $widget _p9k_widget_$widget
  7165. fi
  7166. done 2>/dev/null # `zle -A` fails for inexisting widgets and complains to stderr
  7167. case ${widgets[._p9k_orig_zle-line-pre-redraw]:-} in
  7168. user:-z4h-zle-line-pre-redraw)
  7169. function _p9k_widget_zle-line-pre-redraw() {
  7170. -z4h-zle-line-pre-redraw "$@"
  7171. _p9k_widget_zle-line-pre-redraw-impl
  7172. }
  7173. ;;
  7174. ?*)
  7175. function _p9k_widget_zle-line-pre-redraw() {
  7176. zle ._p9k_orig_zle-line-pre-redraw -- "$@"
  7177. local -i res=$?
  7178. _p9k_widget_zle-line-pre-redraw-impl
  7179. return res
  7180. }
  7181. ;;
  7182. '')
  7183. function _p9k_widget_zle-line-pre-redraw() {
  7184. _p9k_widget_zle-line-pre-redraw-impl
  7185. }
  7186. ;;
  7187. esac
  7188. }
  7189. function _p9k_restore_prompt() {
  7190. eval "$__p9k_intro"
  7191. zle -F $1
  7192. exec {1}>&-
  7193. _p9k__restore_prompt_fd=0
  7194. (( _p9k__must_restore_prompt )) || return 0
  7195. _p9k__must_restore_prompt=0
  7196. unset _p9k__line_finished
  7197. _p9k__refresh_reason=restore
  7198. _p9k_set_prompt
  7199. _p9k__refresh_reason=
  7200. _p9k__expanded=0
  7201. _p9k_reset_prompt
  7202. }
  7203. prompt__p9k_internal_nothing() { _p9k__prompt+='${_p9k__sss::=}'; }
  7204. instant_prompt__p9k_internal_nothing() { prompt__p9k_internal_nothing; }
  7205. # _p9k_build_gap_post line_number
  7206. _p9k_build_gap_post() {
  7207. if [[ $1 == 1 ]]; then
  7208. local kind_l=first kind_u=FIRST
  7209. else
  7210. local kind_l=newline kind_u=NEWLINE
  7211. fi
  7212. _p9k_get_icon '' MULTILINE_${kind_u}_PROMPT_GAP_CHAR
  7213. local char=${_p9k__ret:- }
  7214. _p9k_prompt_length $char
  7215. if (( _p9k__ret != 1 || $#char != 1 )); then
  7216. >&2 print -rP -- "%F{red}WARNING!%f %BMULTILINE_${kind_u}_PROMPT_GAP_CHAR%b is not one character long. Will use ' '."
  7217. >&2 print -rP -- "Either change the value of %BPOWERLEVEL9K_MULTILINE_${kind_u}_PROMPT_GAP_CHAR%b or remove it."
  7218. char=' '
  7219. fi
  7220. local style
  7221. _p9k_color prompt_multiline_${kind_l}_prompt_gap BACKGROUND ""
  7222. [[ -n $_p9k__ret ]] && _p9k_background $_p9k__ret
  7223. style+=$_p9k__ret
  7224. _p9k_color prompt_multiline_${kind_l}_prompt_gap FOREGROUND ""
  7225. [[ -n $_p9k__ret ]] && _p9k_foreground $_p9k__ret
  7226. style+=$_p9k__ret
  7227. _p9k_escape_style $style
  7228. style=$_p9k__ret
  7229. local exp=_POWERLEVEL9K_MULTILINE_${kind_u}_PROMPT_GAP_EXPANSION
  7230. (( $+parameters[$exp] )) && exp=${(P)exp} || exp='${P9K_GAP}'
  7231. [[ $char == '.' ]] && local s=',' || local s='.'
  7232. _p9k__ret=$'${${_p9k__g+\n}:-'$style'${${${_p9k__m:#-*}:+'
  7233. _p9k__ret+='${${_p9k__'$1'g+${(pl.$((_p9k__m+1)).. .)}}:-'
  7234. if [[ $exp == '${P9K_GAP}' ]]; then
  7235. _p9k__ret+='${(pl'$s'$((_p9k__m+1))'$s$s$char$s')}'
  7236. else
  7237. _p9k__ret+='${${P9K_GAP::=${(pl'$s'$((_p9k__m+1))'$s$s$char$s')}}+}'
  7238. _p9k__ret+='${:-"'$exp'"}'
  7239. style=1
  7240. fi
  7241. _p9k__ret+='}'
  7242. if (( __p9k_ksh_arrays )); then
  7243. _p9k__ret+=$'$_p9k__rprompt${_p9k_t[$((!_p9k__ind))]}}:-\n}'
  7244. else
  7245. _p9k__ret+=$'$_p9k__rprompt${_p9k_t[$((1+!_p9k__ind))]}}:-\n}'
  7246. fi
  7247. [[ -n $style ]] && _p9k__ret+='%b%k%f'
  7248. _p9k__ret+='}'
  7249. }
  7250. _p9k_init_lines() {
  7251. local -a left_segments=($_POWERLEVEL9K_LEFT_PROMPT_ELEMENTS)
  7252. local -a right_segments=($_POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS)
  7253. if (( _POWERLEVEL9K_PROMPT_ON_NEWLINE )); then
  7254. left_segments+=(newline _p9k_internal_nothing)
  7255. fi
  7256. local -i num_left_lines=$((1 + ${#${(@M)left_segments:#newline}}))
  7257. local -i num_right_lines=$((1 + ${#${(@M)right_segments:#newline}}))
  7258. if (( num_right_lines > num_left_lines )); then
  7259. repeat $((num_right_lines - num_left_lines)) left_segments=(newline $left_segments)
  7260. local -i num_lines=num_right_lines
  7261. else
  7262. if (( _POWERLEVEL9K_RPROMPT_ON_NEWLINE )); then
  7263. repeat $((num_left_lines - num_right_lines)) right_segments=(newline $right_segments)
  7264. else
  7265. repeat $((num_left_lines - num_right_lines)) right_segments+=newline
  7266. fi
  7267. local -i num_lines=num_left_lines
  7268. fi
  7269. local -i i
  7270. for i in {1..$num_lines}; do
  7271. local -i left_end=${left_segments[(i)newline]}
  7272. local -i right_end=${right_segments[(i)newline]}
  7273. _p9k_line_segments_left+="${(pj:\0:)left_segments[1,left_end-1]}"
  7274. _p9k_line_segments_right+="${(pj:\0:)right_segments[1,right_end-1]}"
  7275. (( left_end > $#left_segments )) && left_segments=() || shift left_end left_segments
  7276. (( right_end > $#right_segments )) && right_segments=() || shift right_end right_segments
  7277. _p9k_get_icon '' LEFT_SEGMENT_SEPARATOR
  7278. _p9k_get_icon 'prompt_empty_line' LEFT_PROMPT_LAST_SEGMENT_END_SYMBOL $_p9k__ret
  7279. _p9k_escape $_p9k__ret
  7280. _p9k_line_prefix_left+='${_p9k__'$i'l-${${:-${_p9k__bg::=NONE}${_p9k__i::=0}${_p9k__sss::=%f'$_p9k__ret'}}+}'
  7281. _p9k_line_suffix_left+='%b%k$_p9k__sss%b%k%f'
  7282. _p9k_escape ${(g::)_POWERLEVEL9K_EMPTY_LINE_RIGHT_PROMPT_FIRST_SEGMENT_START_SYMBOL}
  7283. [[ -n $_p9k__ret ]] && _p9k_line_never_empty_right+=1 || _p9k_line_never_empty_right+=0
  7284. _p9k_line_prefix_right+='${_p9k__'$i'r-${${:-${_p9k__bg::=NONE}${_p9k__i::=0}${_p9k__sss::='$_p9k__ret'}}+}'
  7285. _p9k_line_suffix_right+='$_p9k__sss%b%k%f}' # gets overridden for _p9k_emulate_zero_rprompt_indent
  7286. if (( i == num_lines )); then
  7287. # it's safe to use _p9k_prompt_length on the last line because it cannot have prompt connection
  7288. _p9k_prompt_length ${(e)_p9k__ret}
  7289. (( _p9k__ret )) || _p9k_line_never_empty_right[-1]=0
  7290. fi
  7291. done
  7292. _p9k_get_icon '' LEFT_SEGMENT_END_SEPARATOR
  7293. if [[ -n $_p9k__ret ]]; then
  7294. _p9k__ret+=%b%k%f
  7295. # Not escaped for historical reasons.
  7296. _p9k__ret='${:-"'$_p9k__ret'"}'
  7297. if (( _POWERLEVEL9K_PROMPT_ON_NEWLINE )); then
  7298. _p9k_line_suffix_left[-2]+=$_p9k__ret
  7299. else
  7300. _p9k_line_suffix_left[-1]+=$_p9k__ret
  7301. fi
  7302. fi
  7303. for i in {1..$num_lines}; do _p9k_line_suffix_left[i]+='}'; done
  7304. if (( num_lines > 1 )); then
  7305. for i in {1..$((num_lines-1))}; do
  7306. _p9k_build_gap_post $i
  7307. _p9k_line_gap_post+=$_p9k__ret
  7308. done
  7309. if [[ $+_POWERLEVEL9K_MULTILINE_FIRST_PROMPT_PREFIX == 1 || $_POWERLEVEL9K_PROMPT_ON_NEWLINE == 1 ]]; then
  7310. _p9k_get_icon '' MULTILINE_FIRST_PROMPT_PREFIX
  7311. if [[ -n $_p9k__ret ]]; then
  7312. [[ _p9k__ret == *%* ]] && _p9k__ret+=%b%k%f
  7313. # Not escaped for historical reasons.
  7314. _p9k__ret='${_p9k__1l_frame-"'$_p9k__ret'"}'
  7315. _p9k_line_prefix_left[1]=$_p9k__ret$_p9k_line_prefix_left[1]
  7316. fi
  7317. fi
  7318. if [[ $+_POWERLEVEL9K_MULTILINE_LAST_PROMPT_PREFIX == 1 || $_POWERLEVEL9K_PROMPT_ON_NEWLINE == 1 ]]; then
  7319. _p9k_get_icon '' MULTILINE_LAST_PROMPT_PREFIX
  7320. if [[ -n $_p9k__ret ]]; then
  7321. [[ _p9k__ret == *%* ]] && _p9k__ret+=%b%k%f
  7322. # Not escaped for historical reasons.
  7323. _p9k__ret='${_p9k__'$num_lines'l_frame-"'$_p9k__ret'"}'
  7324. _p9k_line_prefix_left[-1]=$_p9k__ret$_p9k_line_prefix_left[-1]
  7325. fi
  7326. fi
  7327. _p9k_get_icon '' MULTILINE_FIRST_PROMPT_SUFFIX
  7328. if [[ -n $_p9k__ret ]]; then
  7329. [[ _p9k__ret == *%* ]] && _p9k__ret+=%b%k%f
  7330. _p9k_line_suffix_right[1]+='${_p9k__1r_frame-'${(qqq)_p9k__ret}'}'
  7331. _p9k_line_never_empty_right[1]=1
  7332. fi
  7333. _p9k_get_icon '' MULTILINE_LAST_PROMPT_SUFFIX
  7334. if [[ -n $_p9k__ret ]]; then
  7335. [[ _p9k__ret == *%* ]] && _p9k__ret+=%b%k%f
  7336. _p9k_line_suffix_right[-1]+='${_p9k__'$num_lines'r_frame-'${(qqq)_p9k__ret}'}'
  7337. # it's safe to use _p9k_prompt_length on the last line because it cannot have prompt connection
  7338. _p9k_prompt_length $_p9k__ret
  7339. (( _p9k__ret )) && _p9k_line_never_empty_right[-1]=1
  7340. fi
  7341. if (( num_lines > 2 )); then
  7342. if [[ $+_POWERLEVEL9K_MULTILINE_NEWLINE_PROMPT_PREFIX == 1 || $_POWERLEVEL9K_PROMPT_ON_NEWLINE == 1 ]]; then
  7343. _p9k_get_icon '' MULTILINE_NEWLINE_PROMPT_PREFIX
  7344. if [[ -n $_p9k__ret ]]; then
  7345. [[ _p9k__ret == *%* ]] && _p9k__ret+=%b%k%f
  7346. for i in {2..$((num_lines-1))}; do
  7347. # Not escaped for historical reasons.
  7348. _p9k_line_prefix_left[i]='${_p9k__'$i'l_frame-"'$_p9k__ret'"}'$_p9k_line_prefix_left[i]
  7349. done
  7350. fi
  7351. fi
  7352. _p9k_get_icon '' MULTILINE_NEWLINE_PROMPT_SUFFIX
  7353. if [[ -n $_p9k__ret ]]; then
  7354. [[ _p9k__ret == *%* ]] && _p9k__ret+=%b%k%f
  7355. for i in {2..$((num_lines-1))}; do
  7356. _p9k_line_suffix_right[i]+='${_p9k__'$i'r_frame-'${(qqq)_p9k__ret}'}'
  7357. done
  7358. _p9k_line_never_empty_right[2,-2]=${(@)_p9k_line_never_empty_right[2,-2]/0/1}
  7359. fi
  7360. fi
  7361. fi
  7362. }
  7363. _p9k_all_params_eq() {
  7364. local key
  7365. for key in ${parameters[(I)${~1}]}; do
  7366. [[ ${(P)key} == $2 ]] || return
  7367. done
  7368. }
  7369. _p9k_init_display() {
  7370. _p9k_display_k=(empty_line 1 ruler 3)
  7371. local -i n=3 i
  7372. local name
  7373. for i in {1..$#_p9k_line_segments_left}; do
  7374. local -i j=$((-$#_p9k_line_segments_left+i-1))
  7375. _p9k_display_k+=(
  7376. $i $((n+=2)) $j $n
  7377. $i/left_frame $((n+=2)) $j/left_frame $n
  7378. $i/right_frame $((n+=2)) $j/right_frame $n
  7379. $i/left $((n+=2)) $j/left $n
  7380. $i/right $((n+=2)) $j/right $n
  7381. $i/gap $((n+=2)) $j/gap $n)
  7382. for name in ${${(@0)_p9k_line_segments_left[i]}%_joined}; do
  7383. _p9k_display_k+=($i/left/$name $((n+=2)) $j/left/$name $n)
  7384. done
  7385. for name in ${${(@0)_p9k_line_segments_right[i]}%_joined}; do
  7386. _p9k_display_k+=($i/right/$name $((n+=2)) $j/right/$name $n)
  7387. done
  7388. done
  7389. }
  7390. _p9k_init_prompt() {
  7391. _p9k_t=($'\n' $'%{\n%}' '')
  7392. _p9k_prompt_overflow_bug && _p9k_t[2]=$'%{%G\n%}'
  7393. _p9k_init_lines
  7394. _p9k_gap_pre='${${:-${_p9k__x::=0}${_p9k__y::=1024}${_p9k__p::=$_p9k__lprompt$_p9k__rprompt}'
  7395. repeat 10; do
  7396. _p9k_gap_pre+='${_p9k__m::=$(((_p9k__x+_p9k__y)/2))}'
  7397. _p9k_gap_pre+='${_p9k__xy::=${${(%):-$_p9k__p%$_p9k__m(l./$_p9k__m;$_p9k__y./$_p9k__x;$_p9k__m)}##*/}}'
  7398. _p9k_gap_pre+='${_p9k__x::=${_p9k__xy%;*}}'
  7399. _p9k_gap_pre+='${_p9k__y::=${_p9k__xy#*;}}'
  7400. done
  7401. _p9k_gap_pre+='${_p9k__m::=$((_p9k__clm-_p9k__x-_p9k__ind-1))}'
  7402. _p9k_gap_pre+='}+}'
  7403. _p9k_prompt_prefix_left='${${_p9k__clm::=$COLUMNS}+}${${COLUMNS::=1024}+}'
  7404. _p9k_prompt_prefix_right='${_p9k__'$#_p9k_line_segments_left'-${${_p9k__clm::=$COLUMNS}+}${${COLUMNS::=1024}+}'
  7405. _p9k_prompt_suffix_left='${${COLUMNS::=$_p9k__clm}+}'
  7406. _p9k_prompt_suffix_right='${${COLUMNS::=$_p9k__clm}+}}'
  7407. if _p9k_segment_in_use vi_mode || _p9k_segment_in_use prompt_char; then
  7408. _p9k_prompt_prefix_left+='${${_p9k__keymap::=${KEYMAP:-$_p9k__keymap}}+}'
  7409. fi
  7410. if { _p9k_segment_in_use vi_mode && (( $+_POWERLEVEL9K_VI_OVERWRITE_MODE_STRING )) } ||
  7411. { _p9k_segment_in_use prompt_char && (( _POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE )) }; then
  7412. _p9k_prompt_prefix_left+='${${_p9k__zle_state::=${ZLE_STATE:-$_p9k__zle_state}}+}'
  7413. fi
  7414. _p9k_prompt_prefix_left+='%b%k%f'
  7415. # Bug fixed in: https://github.com/zsh-users/zsh/commit/3eea35d0853bddae13fa6f122669935a01618bf9.
  7416. # If affects most terminals when RPROMPT is non-empty and ZLE_RPROMPT_INDENT is zero.
  7417. # We can work around it as long as RPROMPT ends with a space.
  7418. if [[ -n $_p9k_line_segments_right[-1] && $_p9k_line_never_empty_right[-1] == 0 &&
  7419. $ZLE_RPROMPT_INDENT == 0 ]] &&
  7420. _p9k_all_params_eq '_POWERLEVEL9K_*WHITESPACE_BETWEEN_RIGHT_SEGMENTS' ' ' &&
  7421. _p9k_all_params_eq '_POWERLEVEL9K_*RIGHT_RIGHT_WHITESPACE' ' ' &&
  7422. _p9k_all_params_eq '_POWERLEVEL9K_*RIGHT_PROMPT_LAST_SEGMENT_END_SYMBOL' '' &&
  7423. ! is-at-least 5.7.2; then
  7424. _p9k_emulate_zero_rprompt_indent=1
  7425. _p9k_prompt_prefix_left+='${${:-${_p9k__real_zle_rprompt_indent:=$ZLE_RPROMPT_INDENT}${ZLE_RPROMPT_INDENT::=1}${_p9k__ind::=0}}+}'
  7426. _p9k_line_suffix_right[-1]='${_p9k__sss:+${_p9k__sss% }%E}}'
  7427. else
  7428. _p9k_emulate_zero_rprompt_indent=0
  7429. _p9k_prompt_prefix_left+='${${_p9k__ind::=${${ZLE_RPROMPT_INDENT:-1}/#-*/0}}+}'
  7430. fi
  7431. if (( _POWERLEVEL9K_TERM_SHELL_INTEGRATION )); then
  7432. _p9k_prompt_prefix_left+=$'%{\e]133;A\a%}'
  7433. _p9k_prompt_suffix_left+=$'%{\e]133;B\a%}'
  7434. if (( $+_z4h_iterm_cmd && _z4h_can_save_restore_screen == 1 )); then
  7435. _p9k_prompt_prefix_left+=$'%{\ePtmux;\e\e]133;A\a\e\\%}'
  7436. _p9k_prompt_suffix_left+=$'%{\ePtmux;\e\e]133;B\a\e\\%}'
  7437. fi
  7438. fi
  7439. if (( _POWERLEVEL9K_PROMPT_ADD_NEWLINE_COUNT > 0 )); then
  7440. _p9k_t+=${(pl.$_POWERLEVEL9K_PROMPT_ADD_NEWLINE_COUNT..\n.)}
  7441. else
  7442. _p9k_t+=''
  7443. fi
  7444. _p9k_empty_line_idx=$#_p9k_t
  7445. if (( __p9k_ksh_arrays )); then
  7446. _p9k_prompt_prefix_left+='${_p9k_t[${_p9k__empty_line_i:-'$#_p9k_t'}-1]}'
  7447. else
  7448. _p9k_prompt_prefix_left+='${_p9k_t[${_p9k__empty_line_i:-'$#_p9k_t'}]}'
  7449. fi
  7450. local -i num_lines=$#_p9k_line_segments_left
  7451. if (( $+terminfo[cuu1] )); then
  7452. _p9k_escape $terminfo[cuu1]
  7453. if (( __p9k_ksh_arrays )); then
  7454. local scroll=$'${_p9k_t[${_p9k__ruler_i:-1}-1]:+\n'$_p9k__ret'}'
  7455. else
  7456. local scroll=$'${_p9k_t[${_p9k__ruler_i:-1}]:+\n'$_p9k__ret'}'
  7457. fi
  7458. if (( num_lines > 1 )); then
  7459. local -i line_index=
  7460. for line_index in {1..$((num_lines-1))}; do
  7461. scroll='${_p9k__'$line_index-$'\n}'$scroll'${_p9k__'$line_index-$_p9k__ret'}'
  7462. done
  7463. fi
  7464. _p9k_prompt_prefix_left+='%{${_p9k__ipe-'$scroll'}%}'
  7465. fi
  7466. _p9k_get_icon '' RULER_CHAR
  7467. local ruler_char=$_p9k__ret
  7468. _p9k_prompt_length $ruler_char
  7469. (( _p9k__ret == 1 && $#ruler_char == 1 )) || ruler_char=' '
  7470. _p9k_color prompt_ruler BACKGROUND ""
  7471. if [[ -z $_p9k__ret && $ruler_char == ' ' ]]; then
  7472. local ruler=$'\n'
  7473. else
  7474. _p9k_background $_p9k__ret
  7475. local ruler=%b$_p9k__ret
  7476. _p9k_color prompt_ruler FOREGROUND ""
  7477. _p9k_foreground $_p9k__ret
  7478. ruler+=$_p9k__ret
  7479. [[ $ruler_char == '.' ]] && local sep=',' || local sep='.'
  7480. ruler+='${(pl'$sep'${$((_p9k__clm-_p9k__ind))/#-*/0}'$sep$sep$ruler_char$sep')}%k%f'
  7481. if (( __p9k_ksh_arrays )); then
  7482. ruler+='${_p9k_t[$((!_p9k__ind))]}'
  7483. else
  7484. ruler+='${_p9k_t[$((1+!_p9k__ind))]}'
  7485. fi
  7486. fi
  7487. _p9k_t+=$ruler
  7488. _p9k_ruler_idx=$#_p9k_t
  7489. if (( __p9k_ksh_arrays )); then
  7490. _p9k_prompt_prefix_left+='${(e)_p9k_t[${_p9k__ruler_i:-'$#_p9k_t'}-1]}'
  7491. else
  7492. _p9k_prompt_prefix_left+='${(e)_p9k_t[${_p9k__ruler_i:-'$#_p9k_t'}]}'
  7493. fi
  7494. ( _p9k_segment_in_use time && (( _POWERLEVEL9K_TIME_UPDATE_ON_COMMAND )) )
  7495. _p9k_reset_on_line_finish=$((!$?))
  7496. _p9k_t+=$_p9k_gap_pre
  7497. _p9k_gap_pre='${(e)_p9k_t['$(($#_p9k_t - __p9k_ksh_arrays))']}'
  7498. _p9k_t+=$_p9k_prompt_prefix_left
  7499. _p9k_prompt_prefix_left='${(e)_p9k_t['$(($#_p9k_t - __p9k_ksh_arrays))']}'
  7500. }
  7501. _p9k_init_ssh() {
  7502. # The following code is based on Pure:
  7503. # https://github.com/sindresorhus/pure/blob/e8abf9d37185ec9b7b4398ca9c5eba555a1028eb/pure.zsh.
  7504. #
  7505. # License: https://github.com/sindresorhus/pure/blob/e8abf9d37185ec9b7b4398ca9c5eba555a1028eb/license.
  7506. [[ -n $P9K_SSH ]] && return
  7507. typeset -gix P9K_SSH=0
  7508. if [[ -n $SSH_CLIENT || -n $SSH_TTY || -n $SSH_CONNECTION ]]; then
  7509. P9K_SSH=1
  7510. return 0
  7511. fi
  7512. # When changing user on a remote system, the $SSH_CONNECTION environment variable can be lost.
  7513. # Attempt detection via `who`.
  7514. (( $+commands[who] )) || return
  7515. local ipv6='(([0-9a-fA-F]+:)|:){2,}[0-9a-fA-F]+' # Simplified, only checks partial pattern.
  7516. local ipv4='([0-9]{1,3}\.){3}[0-9]+' # Simplified, allows invalid ranges.
  7517. # Assume two non-consecutive periods represents a hostname. Matches `x.y.z`, but not `x.y`.
  7518. local hostname='([.][^. ]+){2}'
  7519. local w
  7520. w="$(who -m 2>/dev/null)" || w=${(@M)${(f)"$(who 2>/dev/null)"}:#*[[:space:]]${TTY#/dev/}[[:space:]]*}
  7521. # Usually the remote address is surrounded by parenthesis but not on all systems (e.g., Busybox).
  7522. [[ $w =~ "\(?($ipv4|$ipv6|$hostname)\)?\$" ]] && P9K_SSH=1
  7523. }
  7524. _p9k_init_toolbox() {
  7525. [[ -z $P9K_TOOLBOX_NAME ]] || return 0
  7526. if [[ -f /run/.containerenv && -r /run/.containerenv ]]; then
  7527. local name=(${(Q)${${(@M)${(f)"$(</run/.containerenv)"}:#name=*}#name=}})
  7528. [[ ${#name} -eq 1 && -n ${name[1]} ]] || return 0
  7529. typeset -g P9K_TOOLBOX_NAME=${name[1]}
  7530. elif [[ -n $DISTROBOX_ENTER_PATH && -n $NAME ]]; then
  7531. local name=${(%):-%m}
  7532. if [[ $name == $NAME* ]]; then
  7533. typeset -g P9K_TOOLBOX_NAME=$name
  7534. fi
  7535. fi
  7536. }
  7537. _p9k_must_init() {
  7538. (( _POWERLEVEL9K_DISABLE_HOT_RELOAD && !_p9k__force_must_init )) && return 1
  7539. _p9k__force_must_init=0
  7540. local IFS sig
  7541. if [[ -n $_p9k__param_sig ]]; then
  7542. IFS=$'\2' sig="${(e)_p9k__param_pat}"
  7543. [[ $sig == $_p9k__param_sig ]] && return 1
  7544. _p9k_deinit
  7545. fi
  7546. _p9k__param_pat=$'v132\1'${(q)ZSH_VERSION}$'\1'${(q)ZSH_PATCHLEVEL}$'\1'
  7547. _p9k__param_pat+=$__p9k_force_term_shell_integration$'\1'
  7548. _p9k__param_pat+=$'${#parameters[(I)POWERLEVEL9K_*]}\1${(%):-%n%#}\1$GITSTATUS_LOG_LEVEL\1'
  7549. _p9k__param_pat+=$'$GITSTATUS_ENABLE_LOGGING\1$GITSTATUS_DAEMON\1$GITSTATUS_NUM_THREADS\1'
  7550. _p9k__param_pat+=$'$GITSTATUS_CACHE_DIR\1$GITSTATUS_AUTO_INSTALL\1${ZLE_RPROMPT_INDENT:-1}\1'
  7551. _p9k__param_pat+=$'$__p9k_sh_glob\1$__p9k_ksh_arrays\1$ITERM_SHELL_INTEGRATION_INSTALLED\1'
  7552. _p9k__param_pat+=$'${PROMPT_EOL_MARK-%B%S%#%s%b}\1$+commands[locale]\1$langinfo[CODESET]\1'
  7553. _p9k__param_pat+=$'${(M)VTE_VERSION:#(<1-4602>|4801)}\1$DEFAULT_USER\1$P9K_SSH\1$+commands[uname]\1'
  7554. _p9k__param_pat+=$'$__p9k_root_dir\1$functions[p10k-on-init]\1$functions[p10k-on-pre-prompt]\1'
  7555. _p9k__param_pat+=$'$functions[p10k-on-post-widget]\1$functions[p10k-on-post-prompt]\1'
  7556. _p9k__param_pat+=$'$+commands[git]\1$terminfo[colors]\1${+_z4h_iterm_cmd}\1'
  7557. _p9k__param_pat+=$'$_z4h_can_save_restore_screen'
  7558. local MATCH
  7559. IFS=$'\1' _p9k__param_pat+="${(@)${(@o)parameters[(I)POWERLEVEL9K_*]}:/(#m)*/\${${(q)MATCH}-$IFS\}}"
  7560. IFS=$'\2' _p9k__param_sig="${(e)_p9k__param_pat}"
  7561. }
  7562. function _p9k_set_os() {
  7563. _p9k_os=$1
  7564. _p9k_get_icon prompt_os_icon $2
  7565. _p9k_os_icon=$_p9k__ret
  7566. }
  7567. function _p9k_init_cacheable() {
  7568. _p9k_init_icons
  7569. _p9k_init_params
  7570. _p9k_init_prompt
  7571. _p9k_init_display
  7572. # https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#backward-compatibility
  7573. if [[ $VTE_VERSION != (<1-4602>|4801) ]]; then
  7574. _p9k_term_has_href=1
  7575. fi
  7576. local elem func
  7577. local -i i=0
  7578. for i in {1..$#_p9k_line_segments_left}; do
  7579. for elem in ${${${(@0)_p9k_line_segments_left[i]}%_joined}//-/_}; do
  7580. local var=POWERLEVEL9K_${${(U)elem}//İ/I}_SHOW_ON_COMMAND
  7581. (( $+parameters[$var] )) || continue
  7582. _p9k_show_on_command+=(
  7583. $'(|*[/\0])('${(j.|.)${(P)var}}')'
  7584. $((1+_p9k_display_k[$i/left/$elem]))
  7585. _p9k__${i}l$elem)
  7586. done
  7587. for elem in ${${${(@0)_p9k_line_segments_right[i]}%_joined}//-/_}; do
  7588. local var=POWERLEVEL9K_${${(U)elem}//İ/I}_SHOW_ON_COMMAND
  7589. (( $+parameters[$var] )) || continue
  7590. local cmds=(${(P)var})
  7591. _p9k_show_on_command+=(
  7592. $'(|*[/\0])('${(j.|.)${(P)var}}')'
  7593. $((1+$_p9k_display_k[$i/right/$elem]))
  7594. _p9k__${i}r$elem)
  7595. done
  7596. done
  7597. if [[ $_POWERLEVEL9K_TRANSIENT_PROMPT != off ]]; then
  7598. local sep=$'\1'
  7599. _p9k_transient_prompt='%b%k%s%u%(?'$sep
  7600. _p9k_color prompt_prompt_char_OK_VIINS FOREGROUND 76
  7601. _p9k_foreground $_p9k__ret
  7602. _p9k_transient_prompt+=$_p9k__ret
  7603. _p9k_transient_prompt+='${${P9K_CONTENT::="❯"}+}'
  7604. _p9k_param prompt_prompt_char_OK_VIINS CONTENT_EXPANSION '${P9K_CONTENT}'
  7605. _p9k_transient_prompt+='${:-"'$_p9k__ret'"}'
  7606. _p9k_transient_prompt+=$sep
  7607. _p9k_color prompt_prompt_char_ERROR_VIINS FOREGROUND 196
  7608. _p9k_foreground $_p9k__ret
  7609. _p9k_transient_prompt+=$_p9k__ret
  7610. _p9k_transient_prompt+='${${P9K_CONTENT::="❯"}+}'
  7611. _p9k_param prompt_prompt_char_ERROR_VIINS CONTENT_EXPANSION '${P9K_CONTENT}'
  7612. _p9k_transient_prompt+='${:-"'$_p9k__ret'"}'
  7613. _p9k_transient_prompt+=')%b%k%f%s%u '
  7614. if (( _POWERLEVEL9K_TERM_SHELL_INTEGRATION )); then
  7615. _p9k_transient_prompt=$'%{\e]133;A\a%}'$_p9k_transient_prompt$'%{\e]133;B\a%}'
  7616. if (( $+_z4h_iterm_cmd && _z4h_can_save_restore_screen == 1 )); then
  7617. _p9k_transient_prompt=$'%{\ePtmux;\e\e]133;A\a\e\\%}'$_p9k_transient_prompt$'%{\ePtmux;\e\e]133;B\a\e\\%}'
  7618. fi
  7619. fi
  7620. fi
  7621. _p9k_uname="$(uname)"
  7622. [[ $_p9k_uname == Linux ]] && _p9k_uname_o="$(uname -o 2>/dev/null)"
  7623. _p9k_uname_m="$(uname -m)"
  7624. if [[ $_p9k_uname == Linux && $_p9k_uname_o == Android ]]; then
  7625. _p9k_set_os Android ANDROID_ICON
  7626. else
  7627. case $_p9k_uname in
  7628. SunOS) _p9k_set_os Solaris SUNOS_ICON;;
  7629. Darwin) _p9k_set_os OSX APPLE_ICON;;
  7630. CYGWIN*|MSYS*|MINGW*) _p9k_set_os Windows WINDOWS_ICON;;
  7631. FreeBSD|OpenBSD|DragonFly) _p9k_set_os BSD FREEBSD_ICON;;
  7632. Linux)
  7633. _p9k_os='Linux'
  7634. local os_release_id
  7635. if [[ -r /etc/os-release ]]; then
  7636. local lines=(${(f)"$(</etc/os-release)"})
  7637. lines=(${(@M)lines:#ID=*})
  7638. (( $#lines == 1 )) && os_release_id=${lines[1]#ID=}
  7639. elif [[ -e /etc/artix-release ]]; then
  7640. os_release_id=artix
  7641. fi
  7642. case $os_release_id in
  7643. *arch*) _p9k_set_os Linux LINUX_ARCH_ICON;;
  7644. *debian*) _p9k_set_os Linux LINUX_DEBIAN_ICON;;
  7645. *raspbian*) _p9k_set_os Linux LINUX_RASPBIAN_ICON;;
  7646. *ubuntu*) _p9k_set_os Linux LINUX_UBUNTU_ICON;;
  7647. *elementary*) _p9k_set_os Linux LINUX_ELEMENTARY_ICON;;
  7648. *fedora*) _p9k_set_os Linux LINUX_FEDORA_ICON;;
  7649. *coreos*) _p9k_set_os Linux LINUX_COREOS_ICON;;
  7650. *gentoo*) _p9k_set_os Linux LINUX_GENTOO_ICON;;
  7651. *mageia*) _p9k_set_os Linux LINUX_MAGEIA_ICON;;
  7652. *centos*) _p9k_set_os Linux LINUX_CENTOS_ICON;;
  7653. *opensuse*|*tumbleweed*) _p9k_set_os Linux LINUX_OPENSUSE_ICON;;
  7654. *sabayon*) _p9k_set_os Linux LINUX_SABAYON_ICON;;
  7655. *slackware*) _p9k_set_os Linux LINUX_SLACKWARE_ICON;;
  7656. *linuxmint*) _p9k_set_os Linux LINUX_MINT_ICON;;
  7657. *alpine*) _p9k_set_os Linux LINUX_ALPINE_ICON;;
  7658. *aosc*) _p9k_set_os Linux LINUX_AOSC_ICON;;
  7659. *nixos*) _p9k_set_os Linux LINUX_NIXOS_ICON;;
  7660. *devuan*) _p9k_set_os Linux LINUX_DEVUAN_ICON;;
  7661. *manjaro*) _p9k_set_os Linux LINUX_MANJARO_ICON;;
  7662. *void*) _p9k_set_os Linux LINUX_VOID_ICON;;
  7663. *artix*) _p9k_set_os Linux LINUX_ARTIX_ICON;;
  7664. *rhel*) _p9k_set_os Linux LINUX_RHEL_ICON;;
  7665. amzn) _p9k_set_os Linux LINUX_AMZN_ICON;;
  7666. *) _p9k_set_os Linux LINUX_ICON;;
  7667. esac
  7668. ;;
  7669. esac
  7670. fi
  7671. if [[ $_POWERLEVEL9K_COLOR_SCHEME == light ]]; then
  7672. _p9k_color1=7
  7673. _p9k_color2=0
  7674. else
  7675. _p9k_color1=0
  7676. _p9k_color2=7
  7677. fi
  7678. # Someone might be using these.
  7679. typeset -g OS=$_p9k_os
  7680. typeset -g DEFAULT_COLOR=$_p9k_color1
  7681. typeset -g DEFAULT_COLOR_INVERTED=$_p9k_color2
  7682. _p9k_battery_states=(
  7683. 'LOW' 'red'
  7684. 'CHARGING' 'yellow'
  7685. 'CHARGED' 'green'
  7686. 'DISCONNECTED' "$_p9k_color2"
  7687. )
  7688. # This simpler construct doesn't work on zsh-5.1 with multi-line prompt:
  7689. #
  7690. # ${(@0)_p9k_line_segments_left[@]}
  7691. local -a left_segments=(${(@0)${(pj:\0:)_p9k_line_segments_left}})
  7692. _p9k_left_join=(1)
  7693. for ((i = 2; i <= $#left_segments; ++i)); do
  7694. elem=$left_segments[i]
  7695. if [[ $elem == *_joined ]]; then
  7696. _p9k_left_join+=$_p9k_left_join[((i-1))]
  7697. else
  7698. _p9k_left_join+=$i
  7699. fi
  7700. done
  7701. local -a right_segments=(${(@0)${(pj:\0:)_p9k_line_segments_right}})
  7702. _p9k_right_join=(1)
  7703. for ((i = 2; i <= $#right_segments; ++i)); do
  7704. elem=$right_segments[i]
  7705. if [[ $elem == *_joined ]]; then
  7706. _p9k_right_join+=$_p9k_right_join[((i-1))]
  7707. else
  7708. _p9k_right_join+=$i
  7709. fi
  7710. done
  7711. case $_p9k_os in
  7712. OSX) (( $+commands[sysctl] )) && _p9k_num_cpus="$(sysctl -n hw.logicalcpu 2>/dev/null)";;
  7713. BSD) (( $+commands[sysctl] )) && _p9k_num_cpus="$(sysctl -n hw.ncpu 2>/dev/null)";;
  7714. *) (( $+commands[nproc] )) && _p9k_num_cpus="$(nproc 2>/dev/null)";;
  7715. esac
  7716. (( _p9k_num_cpus )) || _p9k_num_cpus=1
  7717. if _p9k_segment_in_use dir; then
  7718. if (( $+_POWERLEVEL9K_DIR_CLASSES )); then
  7719. local -i i=3
  7720. for ((; i <= $#_POWERLEVEL9K_DIR_CLASSES; i+=3)); do
  7721. _POWERLEVEL9K_DIR_CLASSES[i]=${(g::)_POWERLEVEL9K_DIR_CLASSES[i]}
  7722. done
  7723. else
  7724. typeset -ga _POWERLEVEL9K_DIR_CLASSES=()
  7725. _p9k_get_icon prompt_dir_ETC ETC_ICON
  7726. _POWERLEVEL9K_DIR_CLASSES+=('/etc|/etc/*' ETC "$_p9k__ret")
  7727. _p9k_get_icon prompt_dir_HOME HOME_ICON
  7728. _POWERLEVEL9K_DIR_CLASSES+=('~' HOME "$_p9k__ret")
  7729. _p9k_get_icon prompt_dir_HOME_SUBFOLDER HOME_SUB_ICON
  7730. _POWERLEVEL9K_DIR_CLASSES+=('~/*' HOME_SUBFOLDER "$_p9k__ret")
  7731. _p9k_get_icon prompt_dir_DEFAULT FOLDER_ICON
  7732. _POWERLEVEL9K_DIR_CLASSES+=('*' DEFAULT "$_p9k__ret")
  7733. fi
  7734. fi
  7735. if _p9k_segment_in_use status; then
  7736. typeset -g _p9k_exitcode2str=({0..255})
  7737. local -i i=2
  7738. if (( !_POWERLEVEL9K_STATUS_HIDE_SIGNAME )); then
  7739. for ((; i <= $#signals; ++i)); do
  7740. local sig=$signals[i]
  7741. (( _POWERLEVEL9K_STATUS_VERBOSE_SIGNAME )) && sig="SIG${sig}($((i-1)))"
  7742. _p9k_exitcode2str[$((128+i))]=$sig
  7743. done
  7744. fi
  7745. fi
  7746. if [[ $#_POWERLEVEL9K_VCS_BACKENDS == 1 && $_POWERLEVEL9K_VCS_BACKENDS[1] == git ]]; then
  7747. local elem line
  7748. local -i i=0 line_idx=0
  7749. for line in $_p9k_line_segments_left; do
  7750. (( ++line_idx ))
  7751. for elem in ${${(0)line}%_joined}; do
  7752. (( ++i ))
  7753. if [[ $elem == vcs ]]; then
  7754. if (( _p9k_vcs_index )); then
  7755. _p9k_vcs_index=-1
  7756. else
  7757. _p9k_vcs_index=i
  7758. _p9k_vcs_line_index=line_idx
  7759. _p9k_vcs_side=left
  7760. fi
  7761. fi
  7762. done
  7763. done
  7764. i=0
  7765. line_idx=0
  7766. for line in $_p9k_line_segments_right; do
  7767. (( ++line_idx ))
  7768. for elem in ${${(0)line}%_joined}; do
  7769. (( ++i ))
  7770. if [[ $elem == vcs ]]; then
  7771. if (( _p9k_vcs_index )); then
  7772. _p9k_vcs_index=-1
  7773. else
  7774. _p9k_vcs_index=i
  7775. _p9k_vcs_line_index=line_idx
  7776. _p9k_vcs_side=right
  7777. fi
  7778. fi
  7779. done
  7780. done
  7781. if (( _p9k_vcs_index > 0 )); then
  7782. local state
  7783. for state in ${(k)__p9k_vcs_states}; do
  7784. _p9k_param prompt_vcs_$state CONTENT_EXPANSION x
  7785. if [[ -z $_p9k__ret ]]; then
  7786. _p9k_vcs_index=-1
  7787. break
  7788. fi
  7789. done
  7790. fi
  7791. if (( _p9k_vcs_index == -1 )); then
  7792. _p9k_vcs_index=0
  7793. _p9k_vcs_line_index=0
  7794. _p9k_vcs_side=
  7795. fi
  7796. fi
  7797. }
  7798. _p9k_init_vcs() {
  7799. if ! _p9k_segment_in_use vcs || (( ! $#_POWERLEVEL9K_VCS_BACKENDS )); then
  7800. (( $+functions[gitstatus_stop_p9k_] )) && gitstatus_stop_p9k_ POWERLEVEL9K
  7801. unset _p9k_preinit
  7802. return
  7803. fi
  7804. _p9k_vcs_info_init
  7805. if (( $+functions[_p9k_preinit] )); then
  7806. if (( $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then
  7807. () {
  7808. trap 'return 130' INT
  7809. {
  7810. gitstatus_start_p9k_ POWERLEVEL9K
  7811. } always {
  7812. trap ':' INT
  7813. }
  7814. }
  7815. fi
  7816. (( $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )) || _p9k__instant_prompt_disabled=1
  7817. return 0
  7818. fi
  7819. (( _POWERLEVEL9K_DISABLE_GITSTATUS )) && return
  7820. (( $_POWERLEVEL9K_VCS_BACKENDS[(I)git] )) || return
  7821. local gitstatus_dir=${_POWERLEVEL9K_GITSTATUS_DIR:-${__p9k_root_dir}/gitstatus}
  7822. typeset -g _p9k_preinit="function _p9k_preinit() {
  7823. (( $+commands[git] )) || { unfunction _p9k_preinit; return 1 }
  7824. [[ \$ZSH_VERSION == ${(q)ZSH_VERSION} ]] || return
  7825. [[ -r ${(q)gitstatus_dir}/gitstatus.plugin.zsh ]] || return
  7826. builtin source ${(q)gitstatus_dir}/gitstatus.plugin.zsh _p9k_ || return
  7827. GITSTATUS_AUTO_INSTALL=${(q)GITSTATUS_AUTO_INSTALL} \
  7828. GITSTATUS_DAEMON=${(q)GITSTATUS_DAEMON} \
  7829. GITSTATUS_CACHE_DIR=${(q)GITSTATUS_CACHE_DIR} \
  7830. GITSTATUS_NUM_THREADS=${(q)GITSTATUS_NUM_THREADS} \
  7831. GITSTATUS_LOG_LEVEL=${(q)GITSTATUS_LOG_LEVEL} \
  7832. GITSTATUS_ENABLE_LOGGING=${(q)GITSTATUS_ENABLE_LOGGING} \
  7833. gitstatus_start_p9k_ \
  7834. -s $_POWERLEVEL9K_VCS_STAGED_MAX_NUM \
  7835. -u $_POWERLEVEL9K_VCS_UNSTAGED_MAX_NUM \
  7836. -d $_POWERLEVEL9K_VCS_UNTRACKED_MAX_NUM \
  7837. -c $_POWERLEVEL9K_VCS_CONFLICTED_MAX_NUM \
  7838. -m $_POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY \
  7839. ${${_POWERLEVEL9K_VCS_RECURSE_UNTRACKED_DIRS:#0}:+-e} \
  7840. -a POWERLEVEL9K
  7841. }"
  7842. builtin source $gitstatus_dir/gitstatus.plugin.zsh _p9k_ || return
  7843. () {
  7844. trap 'return 130' INT
  7845. {
  7846. gitstatus_start_p9k_ \
  7847. -s $_POWERLEVEL9K_VCS_STAGED_MAX_NUM \
  7848. -u $_POWERLEVEL9K_VCS_UNSTAGED_MAX_NUM \
  7849. -d $_POWERLEVEL9K_VCS_UNTRACKED_MAX_NUM \
  7850. -c $_POWERLEVEL9K_VCS_CONFLICTED_MAX_NUM \
  7851. -m $_POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY \
  7852. ${${_POWERLEVEL9K_VCS_RECURSE_UNTRACKED_DIRS:#0}:+-e} \
  7853. POWERLEVEL9K
  7854. } always {
  7855. trap ':' INT
  7856. }
  7857. }
  7858. (( $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )) || _p9k__instant_prompt_disabled=1
  7859. }
  7860. _p9k_init() {
  7861. _p9k_init_vars
  7862. _p9k_restore_state || _p9k_init_cacheable
  7863. typeset -g P9K_OS_ICON=$_p9k_os_icon
  7864. local -a _p9k__async_segments_compute
  7865. local -i i
  7866. local elem
  7867. _p9k__prompt_side=left
  7868. _p9k__segment_index=1
  7869. for i in {1..$#_p9k_line_segments_left}; do
  7870. for elem in ${${(@0)_p9k_line_segments_left[i]}%_joined}; do
  7871. local f_init=_p9k_prompt_${elem}_init
  7872. (( $+functions[$f_init] )) && $f_init
  7873. (( ++_p9k__segment_index ))
  7874. done
  7875. done
  7876. _p9k__prompt_side=right
  7877. _p9k__segment_index=1
  7878. for i in {1..$#_p9k_line_segments_right}; do
  7879. for elem in ${${(@0)_p9k_line_segments_right[i]}%_joined}; do
  7880. local f_init=_p9k_prompt_${elem}_init
  7881. (( $+functions[$f_init] )) && $f_init
  7882. (( ++_p9k__segment_index ))
  7883. done
  7884. done
  7885. if [[ -n $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ||
  7886. -n $_POWERLEVEL9K_IP_INTERFACE ||
  7887. -n $_POWERLEVEL9K_VPN_IP_INTERFACE ]]; then
  7888. _p9k_prompt_net_iface_init
  7889. fi
  7890. if [[ -n $_p9k__async_segments_compute ]]; then
  7891. functions[_p9k_async_segments_compute]=${(pj:\n:)_p9k__async_segments_compute}
  7892. _p9k_worker_start
  7893. fi
  7894. local k v
  7895. for k v in ${(kv)_p9k_display_k}; do
  7896. [[ $k == -* ]] && continue
  7897. _p9k__display_v[v]=$k
  7898. _p9k__display_v[v+1]=show
  7899. done
  7900. _p9k__display_v[2]=hide
  7901. _p9k__display_v[4]=hide
  7902. if (( $+functions[iterm2_decorate_prompt] )); then
  7903. _p9k__iterm2_decorate_prompt=$functions[iterm2_decorate_prompt]
  7904. function iterm2_decorate_prompt() {
  7905. typeset -g ITERM2_PRECMD_PS1=$PROMPT
  7906. typeset -g ITERM2_SHOULD_DECORATE_PROMPT=
  7907. }
  7908. fi
  7909. if (( $+functions[iterm2_precmd] )); then
  7910. _p9k__iterm2_precmd=$functions[iterm2_precmd]
  7911. functions[iterm2_precmd]='local _p9k_status=$?; zle && return; () { return $_p9k_status; }; '$_p9k__iterm2_precmd
  7912. fi
  7913. if _p9k_segment_in_use todo; then
  7914. if [[ -n ${_p9k__todo_command::=${commands[todo.sh]}} ]]; then
  7915. local todo_global=/etc/todo/config
  7916. elif [[ -n ${_p9k__todo_command::=${commands[todo-txt]}} ]]; then
  7917. local todo_global=/etc/todo-txt/config
  7918. fi
  7919. if [[ -n $_p9k__todo_command ]]; then
  7920. _p9k__todo_file="$(exec -a $_p9k__todo_command ${commands[bash]:-:} 3>&1 &>/dev/null -c "
  7921. [ -e \"\$TODOTXT_CFG_FILE\" ] || TODOTXT_CFG_FILE=\$HOME/.todo/config
  7922. [ -e \"\$TODOTXT_CFG_FILE\" ] || TODOTXT_CFG_FILE=\$HOME/todo.cfg
  7923. [ -e \"\$TODOTXT_CFG_FILE\" ] || TODOTXT_CFG_FILE=\$HOME/.todo.cfg
  7924. [ -e \"\$TODOTXT_CFG_FILE\" ] || TODOTXT_CFG_FILE=\${XDG_CONFIG_HOME:-\$HOME/.config}/todo/config
  7925. [ -e \"\$TODOTXT_CFG_FILE\" ] || TODOTXT_CFG_FILE=${(qqq)_p9k__todo_command:h}/todo.cfg
  7926. [ -e \"\$TODOTXT_CFG_FILE\" ] || TODOTXT_CFG_FILE=\${TODOTXT_GLOBAL_CFG_FILE:-${(qqq)todo_global}}
  7927. [ -r \"\$TODOTXT_CFG_FILE\" ] || exit
  7928. source \"\$TODOTXT_CFG_FILE\"
  7929. printf "%s" \"\$TODO_FILE\" >&3")"
  7930. fi
  7931. fi
  7932. if _p9k_segment_in_use dir &&
  7933. [[ $_POWERLEVEL9K_SHORTEN_STRATEGY == truncate_with_package_name && $+commands[jq] == 0 ]]; then
  7934. print -rP -- '%F{yellow}WARNING!%f %BPOWERLEVEL9K_SHORTEN_STRATEGY=truncate_with_package_name%b requires %F{green}jq%f.'
  7935. print -rP -- 'Either install %F{green}jq%f or change the value of %BPOWERLEVEL9K_SHORTEN_STRATEGY%b.'
  7936. fi
  7937. _p9k_init_vcs
  7938. if (( _p9k__instant_prompt_disabled )); then
  7939. (( _POWERLEVEL9K_DISABLE_INSTANT_PROMPT )) && unset __p9k_instant_prompt_erased
  7940. _p9k_delete_instant_prompt
  7941. _p9k_dumped_instant_prompt_sigs=()
  7942. fi
  7943. if (( $+__p9k_instant_prompt_sourced && __p9k_instant_prompt_sourced != __p9k_instant_prompt_version )); then
  7944. _p9k_delete_instant_prompt
  7945. _p9k_dumped_instant_prompt_sigs=()
  7946. fi
  7947. if (( $+__p9k_instant_prompt_erased )); then
  7948. unset __p9k_instant_prompt_erased
  7949. if [[ -w $TTY ]]; then
  7950. local tty=$TTY
  7951. elif [[ -w /dev/tty ]]; then
  7952. local tty=/dev/tty
  7953. else
  7954. local tty=/dev/null
  7955. fi
  7956. {
  7957. >&2 echo -E - ""
  7958. >&2 echo -E - "${(%):-[%1FERROR%f]: When using instant prompt, Powerlevel10k must be loaded before the first prompt.}"
  7959. >&2 echo -E - ""
  7960. >&2 echo -E - "${(%):-You can:}"
  7961. >&2 echo -E - ""
  7962. >&2 echo -E - "${(%):- - %BRecommended%b: Change the way Powerlevel10k is loaded from %B$__p9k_zshrc_u%b.}"
  7963. if (( _p9k_term_has_href )); then
  7964. >&2 echo - "${(%):- See \e]8;;https://github.com/romkatv/powerlevel10k/blob/master/README.md#installation\ahttps://github.com/romkatv/powerlevel10k/blob/master/README.md#installation\e]8;;\a.}"
  7965. else
  7966. >&2 echo - "${(%):- See https://github.com/romkatv/powerlevel10k/blob/master/README.md#installation.}"
  7967. fi
  7968. if (( $+zsh_defer_options )); then
  7969. >&2 echo -E - ""
  7970. >&2 echo -E - "${(%):- NOTE: Do not use %1Fzsh-defer%f to load %Upowerlevel10k.zsh-theme%u.}"
  7971. elif (( $+functions[zinit] )); then
  7972. >&2 echo -E - ""
  7973. >&2 echo -E - "${(%):- NOTE: If using %2Fzinit%f to load %3F'romkatv/powerlevel10k'%f, %Bdo not apply%b %1Fice wait%f.}"
  7974. elif (( $+functions[zplugin] )); then
  7975. >&2 echo -E - ""
  7976. >&2 echo -E - "${(%):- NOTE: If using %2Fzplugin%f to load %3F'romkatv/powerlevel10k'%f, %Bdo not apply%b %1Fice wait%f.}"
  7977. fi
  7978. >&2 echo -E - ""
  7979. >&2 echo -E - "${(%):- * You %Bwill not%b see this error message again.}"
  7980. >&2 echo -E - "${(%):- * Zsh will start %Bquickly%b.}"
  7981. >&2 echo -E - ""
  7982. >&2 echo -E - "${(%):- - Disable instant prompt either by running %Bp10k configure%b or by manually}"
  7983. >&2 echo -E - "${(%):- defining the following parameter:}"
  7984. >&2 echo -E - ""
  7985. >&2 echo -E - "${(%):- %3Ftypeset%f -g POWERLEVEL9K_INSTANT_PROMPT=off}"
  7986. >&2 echo -E - ""
  7987. >&2 echo -E - "${(%):- * You %Bwill not%b see this error message again.}"
  7988. >&2 echo -E - "${(%):- * Zsh will start %Bslowly%b.}"
  7989. >&2 echo -E - ""
  7990. >&2 echo -E - "${(%):- - Do nothing.}"
  7991. >&2 echo -E - ""
  7992. >&2 echo -E - "${(%):- * You %Bwill%b see this error message every time you start zsh.}"
  7993. >&2 echo -E - "${(%):- * Zsh will start %Bslowly%b.}"
  7994. >&2 echo -E - ""
  7995. } 2>>$tty
  7996. fi
  7997. }
  7998. _p9k_deinit() {
  7999. (( $+functions[_p9k_preinit] )) && unfunction _p9k_preinit
  8000. (( $+functions[gitstatus_stop_p9k_] )) && gitstatus_stop_p9k_ POWERLEVEL9K
  8001. _p9k_worker_stop
  8002. if (( _p9k__state_dump_fd )); then
  8003. zle -F $_p9k__state_dump_fd
  8004. exec {_p9k__state_dump_fd}>&-
  8005. fi
  8006. if (( _p9k__restore_prompt_fd )); then
  8007. zle -F $_p9k__restore_prompt_fd
  8008. exec {_p9k__restore_prompt_fd}>&-
  8009. fi
  8010. if (( _p9k__redraw_fd )); then
  8011. zle -F $_p9k__redraw_fd
  8012. exec {_p9k__redraw_fd}>&-
  8013. fi
  8014. (( $+_p9k__iterm2_precmd )) && functions[iterm2_precmd]=$_p9k__iterm2_precmd
  8015. (( $+_p9k__iterm2_decorate_prompt )) && functions[iterm2_decorate_prompt]=$_p9k__iterm2_decorate_prompt
  8016. unset -m '(_POWERLEVEL9K_|P9K_|_p9k_)*~(P9K_SSH|P9K_TOOLBOX_NAME|P9K_TTY|_P9K_TTY)'
  8017. [[ -n $__p9k_locale ]] || unset __p9k_locale
  8018. }
  8019. typeset -gi __p9k_enabled=0
  8020. typeset -gi __p9k_configured=0
  8021. typeset -gri __p9k_instant_prompt_disabled=1
  8022. # `typeset -g` doesn't roundtrip in zsh prior to 5.4.
  8023. if is-at-least 5.4; then
  8024. typeset -gri __p9k_dumps_enabled=1
  8025. else
  8026. typeset -gri __p9k_dumps_enabled=0
  8027. fi
  8028. _p9k_do_nothing() { true; }
  8029. _p9k_precmd_first() {
  8030. eval "$__p9k_intro"
  8031. if [[ -n $KITTY_SHELL_INTEGRATION && KITTY_SHELL_INTEGRATION[(wIe)no-prompt-mark] -eq 0 ]]; then
  8032. KITTY_SHELL_INTEGRATION+=' no-prompt-mark'
  8033. (( $+__p9k_force_term_shell_integration )) || typeset -gri __p9k_force_term_shell_integration=1
  8034. fi
  8035. typeset -ga precmd_functions=(${precmd_functions:#_p9k_precmd_first})
  8036. }
  8037. _p9k_setup() {
  8038. (( __p9k_enabled )) && return
  8039. prompt_opts=(percent subst)
  8040. if (( ! $+__p9k_instant_prompt_active )); then
  8041. prompt_opts+=sp
  8042. prompt_opts+=cr
  8043. fi
  8044. prompt_powerlevel9k_teardown
  8045. __p9k_enabled=1
  8046. typeset -ga preexec_functions=(_p9k_preexec1 $preexec_functions _p9k_preexec2)
  8047. typeset -ga precmd_functions=(_p9k_do_nothing _p9k_precmd_first $precmd_functions _p9k_precmd)
  8048. }
  8049. prompt_powerlevel9k_setup() {
  8050. _p9k_restore_special_params
  8051. eval "$__p9k_intro"
  8052. _p9k_setup
  8053. }
  8054. prompt_powerlevel9k_teardown() {
  8055. _p9k_restore_special_params
  8056. eval "$__p9k_intro"
  8057. add-zsh-hook -D precmd '(_p9k_|powerlevel9k_)*'
  8058. add-zsh-hook -D preexec '(_p9k_|powerlevel9k_)*'
  8059. PROMPT='%m%# '
  8060. RPROMPT=
  8061. if (( __p9k_enabled )); then
  8062. _p9k_deinit
  8063. __p9k_enabled=0
  8064. fi
  8065. }
  8066. typeset -gr __p9k_p10k_usage="Usage: %2Fp10k%f %Bcommand%b [options]
  8067. Commands:
  8068. %Bconfigure%b run interactive configuration wizard
  8069. %Breload%b reload configuration
  8070. %Bsegment%b print a user-defined prompt segment
  8071. %Bdisplay%b show, hide or toggle prompt parts
  8072. %Bhelp%b print this help message
  8073. Print help for a specific command:
  8074. %2Fp10k%f %Bhelp%b command"
  8075. typeset -gr __p9k_p10k_segment_usage="Usage: %2Fp10k%f %Bsegment%b [-h] [{+|-}re] [-s state] [-b bg] [-f fg] [-i icon] [-c cond] [-t text]
  8076. Print a user-defined prompt segment. Can be called only during prompt rendering.
  8077. Options:
  8078. -t text segment's main content; will undergo prompt expansion: '%%F{blue}%%*%%f' will
  8079. show as %F{blue}%*%f; default is empty
  8080. -i icon segment's icon; default is empty
  8081. -r icon is a symbolic reference that needs to be resolved; for example, 'LOCK_ICON'
  8082. +r icon is already resolved and should be printed literally; for example, '⭐';
  8083. this is the default; you can also use \$'\u2B50' if you don't want to have
  8084. non-ascii characters in source code
  8085. -b bg background color; for example, 'blue', '4', or '#0000ff'; empty value means
  8086. transparent background, as in '%%k'; default is black
  8087. -f fg foreground color; for example, 'blue', '4', or '#0000ff'; empty value means
  8088. default foreground color, as in '%%f'; default is empty
  8089. -s state segment's state for the purpose of applying styling options; if you want to
  8090. to be able to use POWERLEVEL9K parameters to specify different colors or icons
  8091. depending on some property, use different states for different values of that
  8092. property
  8093. -c condition; if empty after parameter expansion and process substitution, the
  8094. segment is hidden; this is an advanced feature, use with caution; default is '1'
  8095. -e segment's main content will undergo parameter expansion and process
  8096. substitution; the content will be surrounded with double quotes and thus
  8097. should quote its own double quotes; this is an advanced feature, use with
  8098. caution
  8099. +e segment's main content should not undergo parameter expansion and process
  8100. substitution; this is the default
  8101. -h print this help message
  8102. Example: 'core' segment tells you if there is a file name 'core' in the current directory.
  8103. - Segment's icon is ''.
  8104. - Segment's text is the file's size in bytes.
  8105. - If you have permissions to delete the file, state is DELETABLE. If not, it's PROTECTED.
  8106. zmodload -F zsh/stat b:zstat
  8107. function prompt_core() {
  8108. local size=()
  8109. if ! zstat -A size +size core 2>/dev/null; then
  8110. # No 'core' file in the current directory.
  8111. return
  8112. fi
  8113. if [[ -w . ]]; then
  8114. local state=DELETABLE
  8115. else
  8116. local state=PROTECTED
  8117. fi
  8118. p10k segment -s \$state -i '⭐' -f blue -t \${size[1]}b
  8119. }
  8120. To enable this segment, add 'core' to POWERLEVEL9K_LEFT_PROMPT_ELEMENTS or
  8121. POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS.
  8122. Example customizations:
  8123. # Override default foreground.
  8124. POWERLEVEL9K_CORE_FOREGROUND=red
  8125. # Override foreground when DELETABLE.
  8126. POWERLEVEL9K_CORE_DELETABLE_BACKGROUND=green
  8127. # Override icon when PROTECTED.
  8128. POWERLEVEL9K_CORE_PROTECTED_VISUAL_IDENTIFIER_EXPANSION='❎'
  8129. # Don't show file size when PROTECTED.
  8130. POWERLEVEL9K_CORE_PROTECTED_CONTENT_EXPANSION=''"
  8131. typeset -gr __p9k_p10k_configure_usage="Usage: %2Fp10k%f %Bconfigure%b
  8132. Run interactive configuration wizard."
  8133. typeset -gr __p9k_p10k_reload_usage="Usage: %2Fp10k%f %Breload%b
  8134. Reload configuration."
  8135. typeset -gr __p9k_p10k_finalize_usage="Usage: %2Fp10k%f %Bfinalize%b
  8136. Perform the final stage of initialization. Must be called at the very end of zshrc."
  8137. typeset -gr __p9k_p10k_display_usage="Usage: %2Fp10k%f %Bdisplay%b part-pattern=state-list...
  8138. Show, hide or toggle prompt parts. If called from zle, the current
  8139. prompt is refreshed.
  8140. Usage: %2Fp10k%f %Bdisplay%b -a [part-pattern]...
  8141. Populate array \`reply\` with states of prompt parts matching the patterns.
  8142. If no patterns are supplied, assume \`*\`.
  8143. Usage: %2Fp10k%f %Bdisplay%b -r
  8144. Redisplay prompt.
  8145. Parts:
  8146. empty_line empty line (duh)
  8147. ruler ruler; if POWERLEVEL9K_RULER_CHAR=' ', it's essentially another
  8148. new_line
  8149. N prompt line number N, 1-based; counting from the top if positive,
  8150. from the bottom if negative
  8151. N/left_frame left frame on the Nth line
  8152. N/left left prompt on the Nth line
  8153. N/gap gap between left and right prompts on the Nth line
  8154. N/right right prompt on the Nth line
  8155. N/right_frame right frame on the Nth line
  8156. N/left/S segment S within N/left (dir, time, etc.)
  8157. N/right/S segment S within N/right (dir, time, etc.)
  8158. Part States:
  8159. show the part is displayed
  8160. hide the part is not displayed
  8161. print the part is printed in precmd; only applicable to empty_line and
  8162. ruler; unlike show, the effects of print cannot be undone with hide;
  8163. print used to look better after \`clear\` but this is no longer the
  8164. case; it's best to avoid it unless you know what you are doing
  8165. part-pattern is a glob pattern for parts. Examples:
  8166. */kubecontext all kubecontext prompt segments, regardless of where
  8167. they are
  8168. 1/(right|right_frame) all prompt segments and frame from the right side of
  8169. the first line
  8170. state-list is a comma-separated list of states. Must have at least one element.
  8171. If more than one, states will rotate.
  8172. Example: Bind Ctrl+P to toggle right prompt.
  8173. function toggle-right-prompt() { p10k display '*/right'=hide,show; }
  8174. zle -N toggle-right-prompt
  8175. bindkey '^P' toggle-right-prompt
  8176. Example: Print the current state of all prompt parts:
  8177. typeset -A reply
  8178. p10k display -a '*'
  8179. printf '%%-32s = %%q\n' \${(@kv)reply} | sort
  8180. "
  8181. # 0 -- reset-prompt not blocked
  8182. # 1 -- reset-prompt blocked and not needed
  8183. # 2 -- reset-prompt blocked and needed
  8184. typeset -gi __p9k_reset_state
  8185. function p10k() {
  8186. [[ $# != 1 || $1 != finalize ]] || { p10k-instant-prompt-finalize; return 0 }
  8187. eval "$__p9k_intro_no_reply"
  8188. if (( !ARGC )); then
  8189. print -rP -- $__p9k_p10k_usage >&2
  8190. return 1
  8191. fi
  8192. case $1 in
  8193. segment)
  8194. local REPLY
  8195. local -a reply
  8196. shift
  8197. local -i OPTIND
  8198. local OPTARG opt state bg=0 fg icon cond text ref=0 expand=0
  8199. while getopts ':s:b:f:i:c:t:reh' opt; do
  8200. case $opt in
  8201. s) state=$OPTARG;;
  8202. b) bg=$OPTARG;;
  8203. f) fg=$OPTARG;;
  8204. i) icon=$OPTARG;;
  8205. c) cond=${OPTARG:-'${:-}'};;
  8206. t) text=$OPTARG;;
  8207. r) ref=1;;
  8208. e) expand=1;;
  8209. +r) ref=0;;
  8210. +e) expand=0;;
  8211. h) print -rP -- $__p9k_p10k_segment_usage; return 0;;
  8212. ?) print -rP -- $__p9k_p10k_segment_usage >&2; return 1;;
  8213. esac
  8214. done
  8215. if (( OPTIND <= ARGC )); then
  8216. print -rP -- $__p9k_p10k_segment_usage >&2
  8217. return 1
  8218. fi
  8219. if [[ -z $_p9k__prompt_side ]]; then
  8220. print -rP -- "%1F[ERROR]%f %Bp10k segment%b: can be called only during prompt rendering." >&2
  8221. if (( !ARGC )); then
  8222. print -rP -- ""
  8223. print -rP -- "For help, type:" >&2
  8224. print -rP -- ""
  8225. print -rP -- " %2Fp10k%f %Bhelp%b %Bsegment%b" >&2
  8226. fi
  8227. return 1
  8228. fi
  8229. (( ref )) || icon=$'\1'$icon
  8230. typeset -i _p9k__has_upglob
  8231. "_p9k_${_p9k__prompt_side}_prompt_segment" "prompt_${_p9k__segment_name}${state:+_${${(U)state}//İ/I}}" \
  8232. "$bg" "${fg:-$_p9k_color1}" "$icon" "$expand" "$cond" "$text"
  8233. return 0
  8234. ;;
  8235. display)
  8236. if (( ARGC == 1 )); then
  8237. print -rP -- $__p9k_p10k_display_usage >&2
  8238. return 1
  8239. fi
  8240. shift
  8241. local -i k dump
  8242. local opt prev new pair list name var
  8243. while getopts ':har' opt; do
  8244. case $opt in
  8245. r)
  8246. if (( __p9k_reset_state > 0 )); then
  8247. __p9k_reset_state=2
  8248. else
  8249. __p9k_reset_state=-1
  8250. fi
  8251. ;;
  8252. a) dump=1;;
  8253. h) print -rP -- $__p9k_p10k_display_usage; return 0;;
  8254. ?) print -rP -- $__p9k_p10k_display_usage >&2; return 1;;
  8255. esac
  8256. done
  8257. if (( dump )); then
  8258. reply=()
  8259. shift $((OPTIND-1))
  8260. (( ARGC )) || set -- '*'
  8261. for opt; do
  8262. for k in ${(u@)_p9k_display_k[(I)$opt]:/(#m)*/$_p9k_display_k[$MATCH]}; do
  8263. reply+=($_p9k__display_v[k,k+1])
  8264. done
  8265. done
  8266. if (( __p9k_reset_state == -1 )); then
  8267. _p9k_reset_prompt
  8268. fi
  8269. return 0
  8270. fi
  8271. local REPLY
  8272. local -a reply
  8273. for opt in "${@:$OPTIND}"; do
  8274. pair=(${(s:=:)opt})
  8275. list=(${(s:,:)${pair[2]}})
  8276. if [[ ${(b)pair[1]} == $pair[1] ]]; then # this branch is purely for optimization
  8277. local ks=($_p9k_display_k[$pair[1]])
  8278. else
  8279. local ks=(${(u@)_p9k_display_k[(I)$pair[1]]:/(#m)*/$_p9k_display_k[$MATCH]})
  8280. fi
  8281. for k in $ks; do
  8282. if (( $#list == 1 )); then # this branch is purely for optimization
  8283. [[ $_p9k__display_v[k+1] == $list[1] ]] && continue
  8284. new=$list[1]
  8285. else
  8286. new=${list[list[(I)$_p9k__display_v[k+1]]+1]:-$list[1]}
  8287. [[ $_p9k__display_v[k+1] == $new ]] && continue
  8288. fi
  8289. _p9k__display_v[k+1]=$new
  8290. name=$_p9k__display_v[k]
  8291. if [[ $name == (empty_line|ruler) ]]; then
  8292. var=_p9k__${name}_i
  8293. [[ $new == show ]] && unset $var || typeset -gi $var=3
  8294. elif [[ $name == (#b)(<->)(*) ]]; then
  8295. var=_p9k__${match[1]}${${${${match[2]//\/}/#left/l}/#right/r}/#gap/g}
  8296. [[ $new == hide ]] && typeset -g $var= || unset $var
  8297. fi
  8298. if (( __p9k_reset_state > 0 )); then
  8299. __p9k_reset_state=2
  8300. else
  8301. __p9k_reset_state=-1
  8302. fi
  8303. done
  8304. done
  8305. if (( __p9k_reset_state == -1 )); then
  8306. _p9k_reset_prompt
  8307. fi
  8308. ;;
  8309. configure)
  8310. if (( ARGC > 1 )); then
  8311. print -rP -- $__p9k_p10k_configure_usage >&2
  8312. return 1
  8313. fi
  8314. local REPLY
  8315. local -a reply
  8316. p9k_configure "$@" || return
  8317. ;;
  8318. reload)
  8319. if (( ARGC > 1 )); then
  8320. print -rP -- $__p9k_p10k_reload_usage >&2
  8321. return 1
  8322. fi
  8323. (( $+_p9k__force_must_init )) || return 0
  8324. _p9k__force_must_init=1
  8325. ;;
  8326. help)
  8327. local var=__p9k_p10k_$2_usage
  8328. if (( $+parameters[$var] )); then
  8329. print -rP -- ${(P)var}
  8330. return 0
  8331. elif (( ARGC == 1 )); then
  8332. print -rP -- $__p9k_p10k_usage
  8333. return 0
  8334. else
  8335. print -rP -- $__p9k_p10k_usage >&2
  8336. return 1
  8337. fi
  8338. ;;
  8339. finalize)
  8340. print -rP -- $__p9k_p10k_finalize_usage >&2
  8341. return 1
  8342. ;;
  8343. clear-instant-prompt)
  8344. if (( $+__p9k_instant_prompt_active )); then
  8345. _p9k_clear_instant_prompt
  8346. unset __p9k_instant_prompt_active
  8347. fi
  8348. return 0
  8349. ;;
  8350. *)
  8351. print -rP -- $__p9k_p10k_usage >&2
  8352. return 1
  8353. ;;
  8354. esac
  8355. }
  8356. # Hook for zplugin.
  8357. powerlevel10k_plugin_unload() { prompt_powerlevel9k_teardown; }
  8358. function p10k-instant-prompt-finalize() {
  8359. unsetopt local_options
  8360. (( ${+__p9k_instant_prompt_active} )) && unsetopt prompt_cr prompt_sp || setopt prompt_cr prompt_sp
  8361. }
  8362. autoload -Uz add-zsh-hook
  8363. zmodload zsh/datetime
  8364. zmodload zsh/mathfunc
  8365. zmodload zsh/parameter 2>/dev/null # https://github.com/romkatv/gitstatus/issues/58#issuecomment-553407177
  8366. zmodload zsh/system
  8367. zmodload zsh/termcap
  8368. zmodload zsh/terminfo
  8369. zmodload zsh/zleparameter
  8370. zmodload -F zsh/stat b:zstat
  8371. zmodload -F zsh/net/socket b:zsocket
  8372. zmodload -F zsh/files b:zf_mv b:zf_rm
  8373. if [[ $__p9k_dump_file != $__p9k_instant_prompt_dump_file && -n $__p9k_instant_prompt_dump_file ]]; then
  8374. _p9k_delete_instant_prompt
  8375. zf_rm -f -- $__p9k_dump_file{,.zwc} 2>/dev/null
  8376. zf_rm -f -- $__p9k_instant_prompt_dump_file{,.zwc} 2>/dev/null
  8377. fi
  8378. _p9k_init_ssh
  8379. _p9k_init_toolbox
  8380. prompt_powerlevel9k_setup