From 4f482aa7e0ee3f208fa53cbb70c017ebba1dcc6e Mon Sep 17 00:00:00 2001 From: romkatv Date: Sun, 28 Jul 2019 17:29:56 +0200 Subject: [PATCH] better zshrc writing --- internal/configure.zsh | 22 +++++++++++++------- internal/wizard.zsh | 46 +++++++++++++++++++++++++++++------------- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/internal/configure.zsh b/internal/configure.zsh index 3de28df5..c75484a8 100644 --- a/internal/configure.zsh +++ b/internal/configure.zsh @@ -1,11 +1,13 @@ typeset -gr __p9k_wizard_columns=70 typeset -gr __p9k_wizard_lines=28 -typeset -gr __p9k_zd=${ZDOTDIR:-$HOME} -typeset -gr __p9k_zd_u=${__p9k_zd/#(#b)$HOME(|\/*)/'~'$match[1]} +typeset -gr __p9k_zd=${${ZDOTDIR:-$HOME}:A} +typeset -gr __p9k_zd_u=${${(q-)__p9k_zd}/#(#b)$HOME(|\/*)/'~'$match[1]} typeset -gr __p9k_cfg_basename=.p10k.zsh typeset -gr __p9k_cfg_path=$__p9k_zd/$__p9k_cfg_basename typeset -gr __p9k_cfg_path_u=$__p9k_zd_u/$__p9k_cfg_basename -typeset -gr __p9k_root_dir_u=${__p9k_root_dir/#(#b)$HOME(|\/*)/'~'$match[1]} +typeset -gr __p9k_zshrc=$__p9k_zd/.zshrc +typeset -gr __p9k_zshrc_u=$__p9k_zd_u/.zshrc +typeset -gr __p9k_root_dir_u=${${(q-)__p9k_root_dir}/#(#b)$HOME(|\/*)/'~'$match[1]} function _p9k_can_configure() { emulate -L zsh @@ -21,15 +23,21 @@ function _p9k_can_configure() { [[ "${#$(print -P '\u276F' 2>/dev/null)}" == 1 ]] || $0_error "shell doesn't support unicode" [[ -w $__p9k_zd ]] || $0_error "$__p9k_zd_u is not writable" [[ ! -d $__p9k_cfg_path ]] || $0_error "$__p9k_cfg_path_u is a directory" + [[ ! -d $__p9k_zshrc ]] || $0_error "$__p9k_zshrc_u is a directory" [[ ! -e $__p9k_cfg_path || -f $__p9k_cfg_path || -h $__p9k_cfg_path ]] || $0_error "$__p9k_cfg_path_u is a special file" - [[ -r $__p9k_root_dir/config/p10k-lean.zsh ]] || + [[ -r $__p9k_root_dir/config/p10k-lean.zsh ]] || $0_error "cannot read $__p9k_root_dir_u/config/p10k-lean.zsh" - [[ -r $__p9k_root_dir/config/p10k-classic.zsh ]] || + [[ -r $__p9k_root_dir/config/p10k-classic.zsh ]] || $0_error "cannot read $__p9k_root_dir_u/config/p10k-classic.zsh" - - (( LINES >= __p9k_wizard_lines && COLUMNS >= __p9k_wizard_columns )) || + [[ ! -e $__p9k_zshrc || -f $__p9k_zshrc || -h $__p9k_zshrc ]] || + $0_error "$__p9k_zshrc_u a special file" + [[ ! -e $__p9k_zshrc || -r $__p9k_zshrc ]] || + $0_error "$__p9k_zshrc_u is not readable" + [[ ! -e $__p9k_zshrc || -w $__p9k_zshrc ]] || + $0_error "$__p9k_zshrc_u is not writable" + (( LINES >= __p9k_wizard_lines && COLUMNS >= __p9k_wizard_columns )) || $0_error "terminal size too small" } always { unfunction $0_error diff --git a/internal/wizard.zsh b/internal/wizard.zsh index f9d9fb1e..fa58bd9c 100755 --- a/internal/wizard.zsh +++ b/internal/wizard.zsh @@ -103,7 +103,7 @@ function quit() { print -P "an option that does nothing except for disabling Powerlevel10k" print -P "configuration wizard, type the following command:" print -P "" - print -P " %2Fecho%f %3F'POWERLEVEL9K_MODE='%f %15F>>! $__p9k_zd_u/.zshrc%f" + print -P " %2Fecho%f %3F'POWERLEVEL9K_MODE='%f %15F>>! $__p9k_zshrc_u%f" print -P "" } @@ -488,6 +488,34 @@ function generate_config() { print -lr -- "$header" "$lines[@]" >$__p9k_cfg_path } +function write_zshrc() { + if [[ -e $__p9k_zshrc ]]; then + local lines=(${(f)"$(<$__p9k_zshrc)"}) + local f1=$__p9k_cfg_path + local f2=$__p9k_cfg_path_u + local f3=${__p9k_cfg_path_u/#\~\//\$HOME\/} + local f4=${__p9k_cfg_path_u/#\~\//\"\$HOME\"\/} + local f5="'$f1'" + local f6="\"$f1\"" + local f7="\"$f3\"" + if [[ -n ${(@M)lines:#(#b)source[[:space:]]##($f1|$f2|$f3|$f4|$f5|$f6|$f7)*} ]]; then + print -P "No changes have been made to %B%4F$__p9k_zshrc_u%f%b because it already sources %B%2F$__p9k_cfg_path_u%f%b." + return + fi + fi + + local comments=( + "# You can customize your prompt by editing $__p9k_cfg_path_u." + "# To run configuration wizard again, remove the next line." + ) + print -lr -- "" $comments "source $__p9k_cfg_path_u" >>$__p9k_zshrc + + print -P "" + print -P "The following lines have been appended to your %B%4F$__p9k_zshrc_u%f%b:" + print -P "" + print -lP -- ' %8F'${^comments}'%f' " %2Fsource%f %15F$__p9k_cfg_path_u%f" +} + _p9k_can_configure || return source $__p9k_root_dir/internal/icons.zsh || return @@ -524,25 +552,15 @@ done clear +print -P "Powerlevel10k configuration has been written to %B%2F$__p9k_cfg_path_u%f%b." if [[ -n $config_backup ]]; then - print -P "The previous version of your %B%2F$__p9k_cfg_path_u%f%b has been copied" - print -P "to %B%2F$config_backup%f%b." + print -P "The backup of the previuos version is at %B%3F$config_backup%f%b." fi if (( write_config )); then generate_config || return fi -local comments=( - "# You can customize your prompt by editing $__p9k_cfg_path_u." - "# To run configuration wizard again, remove the next line." -) - -print -lr -- "" $comments "source $__p9k_cfg_path_u" >>$__p9k_zd/.zshrc - -print -P "" -print -P "The following lines have been appended to your %B%2F$__p9k_zd_u/.zshrc%f%b:" -print -P "" -print -lP -- ' %8F'${^comments}'%f' " %2Fsource%f %15F$__p9k_cfg_path_u%f" +write_zshrc || return } "$@"