From da29e0013d92c0d4833b8b8d12287ab2f53baed9 Mon Sep 17 00:00:00 2001 From: MenkeTechnologies Date: Wed, 1 Apr 2026 15:26:01 -0400 Subject: [PATCH] add zsh completion for the p10k command Provides tab completion for p10k subcommands (configure, finalize, reload, display, segment, help) and dynamic completion of prompt segments via p10k display -a for the display subcommand. --- _p10k | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100755 _p10k diff --git a/_p10k b/_p10k new file mode 100755 index 00000000..78d0076a --- /dev/null +++ b/_p10k @@ -0,0 +1,229 @@ +#compdef p10k + +__p10k_getpart(){ + + local -aU keys lines sides segments __p10k_prompt_selectors + local -A reply + lines=() + sides=() + segments=() + equals="$1" + __p10k_prompt_selectors=("*$equals" "*/*$equals" "*/*/*$equals") + + p10k display -a '*' + keys=( ${(uk)reply} ) + + for k in ${keys[@]};do + + resparts=( ${(s:/:)k} ) + resline=${resparts[1]} + resside=${resparts[2]} + reselem=${resparts[3]} + + resCnt=$#resparts + + if (( resCnt == 1 )); then + __p10k_prompt_selectors+=("$resline$equals") + lines+=("$resline") + elif (( resCnt == 2 )); then + __p10k_prompt_selectors+=("$resline/$resside$equals") + sides+=("$resside") + else + segments+=("$segments") + __p10k_prompt_selectors+=("$resline/$resside/$reselem$equals") + fi + + done + + for line in ${lines}; do + p10k display -a "$line/*" + keys=( ${(uk)reply} ) + + hadkkSecond=false + hadThird=false + for k in ${keys[@]};do + + resparts=( ${(s:/:)k} ) + resline=${resparts[1]} + resside=${resparts[2]} + reselem=${resparts[3]} + + resCnt=$#resparts + + if (( resCnt == 2 )); then + hadSecond=true + elif (( resCnt == 3 )); then + __p10k_prompt_selectors+=("$resline/*/$reselem$equals") + hadThird=true + fi + done + + if [[ $hadSecond == true ]]; then + __p10k_prompt_selectors+=("$line/*=") + fi + + if [[ $hadThird == true ]]; then + __p10k_prompt_selectors+=("$line/*/*$equals") + fi + done + + for side in ${sides}; do + p10k display -a "*/$side/*" + keys=( ${(uk)reply} ) + + __p10k_prompt_selectors+=("*/$side$equals") + hadThird=false + for k in ${keys[@]};do + + resparts=( ${(s:/:)k} ) + resline=${resparts[1]} + resside=${resparts[2]} + reselem=${resparts[3]} + resCnt=$#resparts + + if (( resCnt == 3 )); then + __p10k_prompt_selectors+=("*/$side/$reselem$equals") + hadThird=true + fi + done + if [[ $hadThird == true ]]; then + __p10k_prompt_selectors+=("*/$side/*$equals") + fi + done + + if [[ $equals != '' ]]; then + declare -gA __p10k_prompt_selectors_state=("${__p10k_prompt_selectors[@]}") + else + declare -gA __p10k_prompt_selectors_nostate=("${__p10k_prompt_selectors[@]}") + fi + +} + +__p10k_partpattern(){ + if ! (( $+__p10k_prompt_selectors_nostate )) && ! _retrieve_cache p10k_segment_cache; then + __p10k_getpart "" + _store_cache p10k_segment_cache __p10k_prompt_selectors_nostate + fi + + _multi_parts / __p10k_prompt_selectors_nostate + +} + +__p10k_partpattern_state(){ + + local -aU state=( + 'show:the part is displayed' + 'hide:the part is not displayed' + 'print:the part is printed in precmd' + ) + + if compset -P '*='; then + _describe -t p10k-state 'p10k-state' state + else + if ! (( $+__p10k_prompt_selectors_state )) && ! _retrieve_cache p10k_segment_cache_state; then + __p10k_getpart '=' + _store_cache p10k_segment_cache_state __p10k_prompt_selectors_state + fi + _multi_parts -qS '=' / __p10k_prompt_selectors_state + fi + +} + +__p10k_display(){ + + local arguments + + arguments=( + '(-)-a[Populate array `reply` with states of prompt parts]:part-pattern:__p10k_partpattern' + '(- * :)-r[Redisplay prompt]' + '(-)*:part-pattern:__p10k_partpattern_state' + ) + + _arguments -S -s $arguments && return 0 + +} + +__p10k_segment(){ + + arguments=( + '-t[segment main content; will undergo prompt expansion]:text' + '-i[segment icon; default is empty]:icon' + '-r[icon is a symbolic reference that needs to be resolved]' + '+r[icon is already resolved and should be printed literally; for example,⭐]' + '-b[background color; for example, empty value means]:bg' + '-f[foreground color; for example, empty value means]:fg' + '-s:segment state for the purpose of applying styling options]:state' + '-c[if empty after parameter expansion and process substitution]:condition' + '-e[segment main content will undergo parameter expansion and process]' + '+e[segment main content should not undergo parameter expansion and process]' + '(- * :)-h[print this help message]' + ) + + _arguments -S -s $arguments && return 0 + +} + +function _p10k(){ + + local arguments verb + local curcontext=$curcontext state line ret=0 __p10k_commands + + __p10k_commands=( + 'configure:run interactive configuration wizard' + 'reload:reload configuration' + 'segment:print a user-defined prompt segment' + 'display:show, hide or toggle prompt parts' + 'help:print this help message' + ) + + arguments=( + '1:p10k subcommand:->verb' + '*:: :->args' + ) + + _arguments -S -s $arguments && return 0 + + case $state in + verb) + _describe -t p10k-commands "p10k verbs" __p10k_commands && ret=0 + ;; + args) + verb=$words[1] + curcontext="${curcontext%:*:*}:p10k-$verb:" + case $verb in + configure) + _message 'no more arguments' + ret=1 + ;; + reload) + _message 'no more arguments' + ret=1 + ;; + segment) + __p10k_segment + ret=$? + ;; + display) + __p10k_display + ret=$? + ;; + help) + if (( $#words > 2)); then + _message 'no more arguments' + ret=1 + else + _describe -t p10k-commands "p10k verbs" __p10k_commands && ret=0 + fi + ;; + *) + _message 'unknown p10k command' + ret=1 + ;; + esac + ;; + esac + + return $ret +} + +_p10k "$@"