From a9f208c8fc509b9c591169dd9758c48ad4325f76 Mon Sep 17 00:00:00 2001 From: Roman Perepelitsa Date: Mon, 15 Nov 2021 07:25:17 +0100 Subject: [PATCH] don't auto-start configuration wizard if it's likely that zshrc is broken This change is an attempt to automate this: https://github.com/romkatv/powerlevel10k#configuration-wizard-runs-automatically-every-time-zsh-is-started --- internal/configure.zsh | 5 +- internal/wizard.zsh | 114 ++++++++++++++++++++++++++--------------- 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/internal/configure.zsh b/internal/configure.zsh index 4fb7cb6e..3e880f23 100644 --- a/internal/configure.zsh +++ b/internal/configure.zsh @@ -59,7 +59,10 @@ function _p9k_can_configure() { $0_error "terminal size too small; must be at least $__p9k_wizard_columns columns by $__p9k_wizard_lines lines" return 1 } - [[ -t 0 && -t 1 ]] || { $0_error "no TTY"; return 2 } + [[ -t 0 && -t 1 ]] || { + $0_error "no TTY" + return 2 + } return 0 } always { unfunction $0_error diff --git a/internal/wizard.zsh b/internal/wizard.zsh index 9fcb36e3..0b81417a 100644 --- a/internal/wizard.zsh +++ b/internal/wizard.zsh @@ -14,22 +14,11 @@ if (( OPTIND <= ARGC )); then return 1 fi -if (( $+terminfo[smcup] && $+terminfo[rmcup] )) && echoti smcup 2>/dev/null; then - function restore_screen() { - echoti rmcup 2>/dev/null - function restore_screen() {} - } -else - function restore_screen() {} -fi - local -i in_z4h_wizard=0 [[ $force == 0 && $+functions[z4h] == 1 && -n $Z4H && -e $Z4H/welcome ]] && in_z4h_wizard=1 local -i success=0 -{ # always - local -ri force local -r font_base_url='https://github.com/romkatv/powerlevel10k-media/raw/master' @@ -1566,40 +1555,12 @@ function ask_zshrc_edit() { if (( $+functions[z4h] )); then zshrc_has_cfg=1 zshrc_has_instant_prompt=1 + return fi + check_zshrc_integration || quit -c [[ $instant_prompt == off ]] && zshrc_has_instant_prompt=1 - - if [[ -e $__p9k_zshrc ]]; then - zshrc_content="$(<$__p9k_zshrc)" || quit -c - local lines=(${(f)zshrc_content}) - local f0=$__p9k_cfg_path_o - local f1=${(q)f0} - local f2=${(q-)f0} - local f3=${(qq)f0} - local f4=${(qqq)f0} - local g1=${${(q)__p9k_cfg_path_o}/#(#b)${(q)HOME}\//'~/'} - local h0='${ZDOTDIR:-~}/.p10k.zsh' - local h1='${ZDOTDIR:-$HOME}/.p10k.zsh' - local h2='"${ZDOTDIR:-$HOME}/.p10k.zsh"' - local h3='"${ZDOTDIR:-$HOME}"/.p10k.zsh' - local h4='${ZDOTDIR}/.p10k.zsh' - local h5='"${ZDOTDIR}/.p10k.zsh"' - local h6='"${ZDOTDIR}"/.p10k.zsh' - local h7='$ZDOTDIR/.p10k.zsh' - local h8='"$ZDOTDIR/.p10k.zsh"' - local h9='"$ZDOTDIR"/.p10k.zsh' - local h10='$POWERLEVEL9K_CONFIG_FILE' - local h11='"$POWERLEVEL9K_CONFIG_FILE"' - if [[ -n ${(@M)lines:#(#b)[^#]#([^[:IDENT:]]|)source[[:space:]]##($f1|$f2|$f3|$f4|$g1|$h0|$h1|$h2|$h3|$h4|$h5|$h6|$h7|$h8|$h9|$h10|$h11)(|[[:space:]]*|'#'*)} ]]; then - zshrc_has_cfg=1 - fi - local pre='${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh' - if [[ -n ${(@M)lines:#(#b)[^#]#([^[:IDENT:]]|)source[[:space:]]##($pre|\"$pre\")(|[[:space:]]*|'#'*)} ]]; then - zshrc_has_instant_prompt=1 - fi - (( zshrc_has_cfg && zshrc_has_instant_prompt )) && return - fi + (( zshrc_has_cfg && zshrc_has_instant_prompt )) && return add_widget 0 flowing -c %BApply changes to "%b%2F${__p9k_zshrc_u//\\/\\\\}%f%B?%b" add_widget 0 print -P "" @@ -1951,6 +1912,75 @@ fi" || return return 0 } +function check_zshrc_integration() { + typeset -g zshrc_content= + typeset -gi zshrc_has_cfg=0 zshrc_has_instant_prompt=0 + [[ -e $__p9k_zshrc ]] || return 0 + zshrc_content="$(<$__p9k_zshrc)" || return + local lines=(${(f)zshrc_content}) + local f0=$__p9k_cfg_path_o + local f1=${(q)f0} + local f2=${(q-)f0} + local f3=${(qq)f0} + local f4=${(qqq)f0} + local g1=${${(q)__p9k_cfg_path_o}/#(#b)${(q)HOME}\//'~/'} + local h0='${ZDOTDIR:-~}/.p10k.zsh' + local h1='${ZDOTDIR:-$HOME}/.p10k.zsh' + local h2='"${ZDOTDIR:-$HOME}/.p10k.zsh"' + local h3='"${ZDOTDIR:-$HOME}"/.p10k.zsh' + local h4='${ZDOTDIR}/.p10k.zsh' + local h5='"${ZDOTDIR}/.p10k.zsh"' + local h6='"${ZDOTDIR}"/.p10k.zsh' + local h7='$ZDOTDIR/.p10k.zsh' + local h8='"$ZDOTDIR/.p10k.zsh"' + local h9='"$ZDOTDIR"/.p10k.zsh' + local h10='$POWERLEVEL9K_CONFIG_FILE' + local h11='"$POWERLEVEL9K_CONFIG_FILE"' + if [[ -n ${(@M)lines:#(#b)[^#]#([^[:IDENT:]]|)source[[:space:]]##($f1|$f2|$f3|$f4|$g1|$h0|$h1|$h2|$h3|$h4|$h5|$h6|$h7|$h8|$h9|$h10|$h11)(|[[:space:]]*|'#'*)} ]]; then + zshrc_has_cfg=1 + fi + local pre='${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh' + if [[ -n ${(@M)lines:#(#b)[^#]#([^[:IDENT:]]|)source[[:space:]]##($pre|\"$pre\")(|[[:space:]]*|'#'*)} ]]; then + zshrc_has_instant_prompt=1 + fi + return 0 +} + +() { + (( force )) && return + _p9k_can_configure -q || return 0 + local zshrc_content zshrc_has_cfg zshrc_has_instant_prompt + check_zshrc_integration 2>/dev/null || return 0 + (( zshrc_has_cfg )) || return 0 + [[ -s $__p9k_cfg_path ]] || return 0 + print -P "" + flowing \ + Powerlevel10k configuration file "($__p9k_cfg_path_u)" was not sourced. This \ + might have been caused by errors in zsh startup files, most likely in \ + $__p9k_zshrc_u. See above for any indication of such errors and fix them. If \ + there are no errors, try running Powerlevel10k configuration wizard: + print -P '' + print -P ' %2Fp10k%f %Bconfigure%b' + print -P '' + flowing \ + If you do nothing, you will see this message again when you start zsh. You can \ + suppress it by defining %BPOWERLEVEL9K_DISABLE_CONFIGURATION_WIZARD=true%b in \ + $__p9k_zshrc_u. + print -P '' + return 1 +} || return + +if (( $+terminfo[smcup] && $+terminfo[rmcup] )) && echoti smcup 2>/dev/null; then + function restore_screen() { + echoti rmcup 2>/dev/null + function restore_screen() {} + } +else + function restore_screen() {} +fi + +{ # always + if (( force )); then _p9k_can_configure || return else