From 6767e271cd8e34cf3fa69fff79fb8c87138bf8a0 Mon Sep 17 00:00:00 2001 From: romkatv Date: Wed, 6 Nov 2019 11:54:14 +0100 Subject: [PATCH] wizard: add transient prompt (optional); disable hot reload --- config/p10k-classic.zsh | 15 +++++++++ config/p10k-lean.zsh | 15 +++++++++ config/p10k-pure.zsh | 15 +++++++++ config/p10k-rainbow.zsh | 15 +++++++++ internal/wizard.zsh | 73 ++++++++++++++++++++++++++++------------- 5 files changed, 111 insertions(+), 22 deletions(-) diff --git a/config/p10k-classic.zsh b/config/p10k-classic.zsh index 9113fd2..fd8a446 100644 --- a/config/p10k-classic.zsh +++ b/config/p10k-classic.zsh @@ -857,6 +857,15 @@ # typeset -g POWERLEVEL9K_EXAMPLE_FOREGROUND=208 # typeset -g POWERLEVEL9K_EXAMPLE_VISUAL_IDENTIFIER_EXPANSION='⭐' + # Transient prompt works similarly to the builtin transient_rprompt option. It trims down prompt + # when accepting a command line. Supported values: + # + # - off: Don't change prompt when accepting a command line. + # - always: Trim down prompt when accepting a command line. + # - same-dir: Trim down prompt when accepting a command line unless this is the first command + # typed after changing current working directory. + typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=off + # Instant prompt mode. # # - off: Disable instant prompt. Choose this if you've tried instant prompt and found @@ -868,6 +877,12 @@ # zsh initialization. Choose this if you've never tried instant prompt, haven't # seen the warning, or if you are unsure what this all means. typeset -g POWERLEVEL9K_INSTANT_PROMPT=verbose + + # Hot reload allows you to change POWERLEVEL9K options after Powerlevel10k has been initialized. + # For example, you can type POWERLEVEL9K_BACKGROUND=red and see your prompt turn red. Hot reload + # can slow down prompt by 1-2 milliseconds, so it's better to keep it turned off unless you + # really need it. + typeset -g POWERLEVEL9K_DISABLE_HOT_RELOAD=true } (( ${#p10k_config_opts} )) && setopt ${p10k_config_opts[@]} diff --git a/config/p10k-lean.zsh b/config/p10k-lean.zsh index e06eb55..150e3c7 100644 --- a/config/p10k-lean.zsh +++ b/config/p10k-lean.zsh @@ -819,6 +819,15 @@ # typeset -g POWERLEVEL9K_EXAMPLE_FOREGROUND=208 # typeset -g POWERLEVEL9K_EXAMPLE_VISUAL_IDENTIFIER_EXPANSION='⭐' + # Transient prompt works similarly to the builtin transient_rprompt option. It trims down prompt + # when accepting a command line. Supported values: + # + # - off: Don't change prompt when accepting a command line. + # - always: Trim down prompt when accepting a command line. + # - same-dir: Trim down prompt when accepting a command line unless this is the first command + # typed after changing current working directory. + typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=off + # Instant prompt mode. # # - off: Disable instant prompt. Choose this if you've tried instant prompt and found @@ -830,6 +839,12 @@ # zsh initialization. Choose this if you've never tried instant prompt, haven't # seen the warning, or if you are unsure what this all means. typeset -g POWERLEVEL9K_INSTANT_PROMPT=verbose + + # Hot reload allows you to change POWERLEVEL9K options after Powerlevel10k has been initialized. + # For example, you can type POWERLEVEL9K_BACKGROUND=red and see your prompt turn red. Hot reload + # can slow down prompt by 1-2 milliseconds, so it's better to keep it turned off unless you + # really need it. + typeset -g POWERLEVEL9K_DISABLE_HOT_RELOAD=true } (( ${#p10k_config_opts} )) && setopt ${p10k_config_opts[@]} diff --git a/config/p10k-pure.zsh b/config/p10k-pure.zsh index c91886a..6c717a4 100644 --- a/config/p10k-pure.zsh +++ b/config/p10k-pure.zsh @@ -125,6 +125,15 @@ # Remove space between '⇣' and '⇡'. typeset -g POWERLEVEL9K_VCS_CONTENT_EXPANSION='${P9K_CONTENT/⇣* ⇡/⇣⇡}' + # Transient prompt works similarly to the builtin transient_rprompt option. It trims down prompt + # when accepting a command line. Supported values: + # + # - off: Don't change prompt when accepting a command line. + # - always: Trim down prompt when accepting a command line. + # - same-dir: Trim down prompt when accepting a command line unless this is the first command + # typed after changing current working directory. + typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=off + # Instant prompt mode. # # - off: Disable instant prompt. Choose this if you've tried instant prompt and found @@ -136,6 +145,12 @@ # zsh initialization. Choose this if you've never tried instant prompt, haven't # seen the warning, or if you are unsure what this all means. typeset -g POWERLEVEL9K_INSTANT_PROMPT=verbose + + # Hot reload allows you to change POWERLEVEL9K options after Powerlevel10k has been initialized. + # For example, you can type POWERLEVEL9K_BACKGROUND=red and see your prompt turn red. Hot reload + # can slow down prompt by 1-2 milliseconds, so it's better to keep it turned off unless you + # really need it. + typeset -g POWERLEVEL9K_DISABLE_HOT_RELOAD=true } (( ${#p10k_config_opts} )) && setopt ${p10k_config_opts[@]} diff --git a/config/p10k-rainbow.zsh b/config/p10k-rainbow.zsh index 1aadec9..f64c363 100644 --- a/config/p10k-rainbow.zsh +++ b/config/p10k-rainbow.zsh @@ -884,6 +884,15 @@ # typeset -g POWERLEVEL9K_EXAMPLE_FOREGROUND=3 # typeset -g POWERLEVEL9K_EXAMPLE_VISUAL_IDENTIFIER_EXPANSION='⭐' + # Transient prompt works similarly to the builtin transient_rprompt option. It trims down prompt + # when accepting a command line. Supported values: + # + # - off: Don't change prompt when accepting a command line. + # - always: Trim down prompt when accepting a command line. + # - same-dir: Trim down prompt when accepting a command line unless this is the first command + # typed after changing current working directory. + typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=off + # Instant prompt mode. # # - off: Disable instant prompt. Choose this if you've tried instant prompt and found @@ -895,6 +904,12 @@ # zsh initialization. Choose this if you've never tried instant prompt, haven't # seen the warning, or if you are unsure what this all means. typeset -g POWERLEVEL9K_INSTANT_PROMPT=verbose + + # Hot reload allows you to change POWERLEVEL9K options after Powerlevel10k has been initialized. + # For example, you can type POWERLEVEL9K_BACKGROUND=red and see your prompt turn red. Hot reload + # can slow down prompt by 1-2 milliseconds, so it's better to keep it turned off unless you + # really need it. + typeset -g POWERLEVEL9K_DISABLE_HOT_RELOAD=true } (( ${#p10k_config_opts} )) && setopt ${p10k_config_opts[@]} diff --git a/internal/wizard.zsh b/internal/wizard.zsh index f2604ae..305fe66 100755 --- a/internal/wizard.zsh +++ b/internal/wizard.zsh @@ -64,7 +64,7 @@ typeset -r slanted_bar='\uE0BD' typeset -ra lean_left=( '' '${extra_icons[1]:+$extra_icons[1] }%31F$extra_icons[2]%B%39F~%b%31F/%B%39Fsrc%b%f $prefixes[1]%76F$extra_icons[3]master%f ' - '' '%76F❯%f █' + '' '%76F❯%f ${buffer:-█}' ) typeset -ra lean_right=( @@ -74,7 +74,7 @@ typeset -ra lean_right=( typeset -ra classic_left=( '%$frame_color[$color]F╭─' '%F{$bg_color[$color]}$left_tail%K{$bg_color[$color]} ${extra_icons[1]:+$extra_icons[1]%K{$bg_color[$color]\} %$sep_color[$color]F$left_subsep%f }%31F$extra_icons[2]%B%39F~%b%K{$bg_color[$color]}%31F/%B%39Fsrc%b%K{$bg_color[$color]} %$sep_color[$color]F$left_subsep%f %$prefix_color[$color]F$prefixes[1]%76F$extra_icons[3]master %k%$bg_color[$color]F$left_head%f' - '%$frame_color[$color]F╰─' '%f █' + '%$frame_color[$color]F╰─' '%f ${buffer:-█}' ) typeset -ra classic_right=( @@ -84,7 +84,7 @@ typeset -ra classic_right=( typeset -ra pure_left=( '' '%4F~/src%f %242Fmaster%f %3F5s%f' - '' '%5F❯%f █' + '' '%5F❯%f ${buffer:-█}' ) typeset -ra pure_right=( @@ -94,7 +94,7 @@ typeset -ra pure_right=( typeset -ra rainbow_left=( '%$frame_color[$color]F╭─' '%F{${${extra_icons[1]:+0}:-4}}$left_tail${extra_icons[1]:+%K{0\} $extra_icons[1] %K{4\}%0F$left_sep}%K{4}%254F $extra_icons[2]%B%255F~%b%K{4}%254F/%B%255Fsrc%b%K{4} %K{2}%4F$left_sep %0F$prefixes[1]$extra_icons[3]master %k%2F$left_head%f' - '%$frame_color[$color]F╰─' '%f █' + '%$frame_color[$color]F╰─' '%f ${buffer:-█}' ) typeset -ra rainbow_right=( @@ -116,7 +116,7 @@ function prompt_length() { typeset ${${(%):-$1%$m(l.x.y)}[-1]}=$m done fi - print $x + REPLY=$x } function print_prompt() { @@ -124,17 +124,19 @@ function print_prompt() { local right=${style}_right left=("${(@P)left}") right=("${(@P)right}") + (( disable_rprompt )) && right=() eval "left=(${(@)left:/(#b)(*)/\"$match[1]\"})" eval "right=(${(@)right:/(#b)(*)/\"$match[1]\"})" if (( num_lines == 1)); then left=($left[2] $left[4]) right=($right[1] $right[3]) else - (( left_frame )) || left=('' $left[2] '' '%76F❯%f █') + (( left_frame )) || left=('' $left[2] '' "%76F❯%f ${buffer:-█}") (( right_frame )) || right=($right[1] '' '' '') fi local -i right_indent=prompt_indent - local -i width=$(prompt_length ${(g::):-$left[1]$left[2]$right[1]$right[2]}) + prompt_length ${(g::):-$left[1]$left[2]$right[1]$right[2]} + local -i width=REPLY while (( wizard_columns - width <= prompt_indent + right_indent )); do (( --right_indent )) done @@ -142,7 +144,8 @@ function print_prompt() { for ((i = 1; i < $#left; i+=2)); do local l=${(g::):-$left[i]$left[i+1]} local r=${(g::):-$right[i]$right[i+1]} - local -i gap=$((wizard_columns - prompt_indent - right_indent - $(prompt_length $l$r))) + prompt_length $l$r + local -i gap=$((wizard_columns - prompt_indent - right_indent - REPLY)) (( num_lines == 2 && i == 1 )) && local fill=$gap_char || local fill=' ' print -n -- ${(pl:$prompt_indent:: :)} print -nP -- $l @@ -169,8 +172,8 @@ function flowing() { shift $((OPTIND-1)) local line word lines=() for word in "$@"; do - local n=$(prompt_length ${(g::):-"$line $word"}) - if (( n > wizard_columns )); then + prompt_length ${(g::):-"$line $word"} + if (( REPLY > wizard_columns )); then [[ -z $line ]] || lines+=$line line= fi @@ -183,8 +186,8 @@ function flowing() { done [[ -z $line ]] || lines+=$line for line in $lines; do - local n=$(prompt_length ${(g::)line}) - (( centered && n < wizard_columns )) && print -n -- ${(pl:$(((wizard_columns - n) / 2)):: :)} + prompt_length ${(g::)line} + (( centered && REPLY < wizard_columns )) && print -n -- ${(pl:$(((wizard_columns - REPLY) / 2)):: :)} print -P -- $line done } @@ -1260,27 +1263,51 @@ function ask_instant_prompt() { done } -function ask_confirm() { +function ask_transient_prompt() { + local disable_rprompt=$((num_lines == 1)) while true; do clear - flowing -c "%BLooks good?%b" - print -P "" - print_prompt - (( empty_line )) && print -P "" - print_prompt + flowing -c "%BEnable Transient Prompt?%b" print -P "" print -P "%B(y) Yes.%b" + if (( LINES >= 25 || num_lines == 1 )); then + print -P "" + print -P "${(pl:$prompt_indent:: :)}%76F❯%f %2Fgit%f pull" + elif (( LINES < 23 )); then + print -P "" + else + print -P "${(pl:$prompt_indent:: :)}%76F❯%f %2Fgit%f pull" + fi + print -P "${(pl:$prompt_indent:: :)}%76F❯%f %2Fgit%f branch x" + (( empty_line )) && echo + buffer="%2Fgit%f checkout x█" print_prompt + print -P "" + print -P "%B(n) No.%b" + if (( LINES >= 25 || num_lines == 1 )); then + print -P "" + buffer="%2Fgit%f pull" print_prompt + (( empty_line )) && echo + elif (( LINES < 23 )); then + print -P "" + else + buffer="%2Fgit%f pull" print_prompt + (( empty_line )) && echo + fi + buffer="%2Fgit%f branch x" print_prompt + (( empty_line )) && echo + buffer="%2Fgit%f checkout x█" print_prompt print -P "" print -P "(r) Restart from the beginning." print -P "(q) Quit and do nothing." print -P "" local key= - read -k key${(%):-"?%BChoice [yrq]: %b"} || quit -c + read -k key${(%):-"?%BChoice [ynrq]: %b"} || quit -c case $key in q) quit;; r) return 1;; - y) break;; + y) transient_prompt=1; break;; + n) transient_prompt=0; break;; esac done } @@ -1558,6 +1585,8 @@ function generate_config() { sub INSTANT_PROMPT $instant_prompt + (( transient_prompt )) && sub TRANSIENT_PROMPT same-dir + local header=${(%):-"# Generated by Powerlevel10k configuration wizard on %D{%Y-%m-%d at %H:%M %Z}."}$'\n' header+="# Based on romkatv/powerlevel10k/config/p10k-$style.zsh" if [[ $commands[sum] == ('/bin'|'/usr/bin'|'/usr/local/bin')'/sum' ]]; then @@ -1640,7 +1669,7 @@ while true; do local -i zshrc_has_cfg=0 zshrc_has_instant_prompt=0 write_zshrc=0 local POWERLEVEL9K_MODE= style= config_backup= config_backup_u= gap_char=' ' local left_subsep= right_subsep= left_tail= right_tail= left_head= right_head= show_time= - local -i num_lines=0 empty_line=0 color=2 left_frame=1 right_frame=1 + local -i num_lines=0 empty_line=0 color=2 left_frame=1 right_frame=1 transient_prompt=0 local -i cap_diamond=0 cap_python=0 cap_debian=0 cap_narrow_icons=0 cap_lock=0 local -a extra_icons=('' '' '') local -a prefixes=('' '') @@ -1716,7 +1745,7 @@ while true; do ask_extra_icons || continue ask_prefixes || continue fi - ask_confirm || continue + ask_transient_prompt || continue ask_instant_prompt || continue ask_config_overwrite || continue ask_zshrc_edit || continue