From 424526ebee69cd6cb0752ebfe1d2fe71f1aa8817 Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Thu, 9 Jan 2020 07:13:31 -0500 Subject: [PATCH] Refactor: extract _zimfw_print function --- src/stage2/22_zimfw_print.zsh.erb | 5 +++ src/stage2/28_zimfw_mv.zsh.erb | 8 ++--- src/stage2/30_zimfw_build.zsh.erb | 5 +-- src/stage2/50_zimfw_clean_compiled.zsh.erb | 4 +-- src/stage2/50_zimfw_clean_dumpfile.zsh.erb | 4 +-- src/stage2/50_zimfw_uninstall.zsh.erb | 4 +-- src/stage2/50_zimfw_upgrade.zsh.erb | 5 +-- src/stage2/80_zimfw.zsh.erb | 4 +-- zimfw.zsh | 42 ++++++++-------------- 9 files changed, 28 insertions(+), 53 deletions(-) create mode 100644 src/stage2/22_zimfw_print.zsh.erb diff --git a/src/stage2/22_zimfw_print.zsh.erb b/src/stage2/22_zimfw_print.zsh.erb new file mode 100644 index 0000000..7cb43ca --- /dev/null +++ b/src/stage2/22_zimfw_print.zsh.erb @@ -0,0 +1,5 @@ +_zimfw_print() { + if (( ! _zquiet )); then + print "${@}" + fi +} diff --git a/src/stage2/28_zimfw_mv.zsh.erb b/src/stage2/28_zimfw_mv.zsh.erb index 8da341c..254d817 100644 --- a/src/stage2/28_zimfw_mv.zsh.erb +++ b/src/stage2/28_zimfw_mv.zsh.erb @@ -1,15 +1,11 @@ _zimfw_mv() { if command cmp -s ${2} ${1}; then - if (( ! _zquiet )); then - print -PR "<%= okay %>%B${2}:%b Already up to date" - fi + _zimfw_print -PR "<%= okay %>%B${2}:%b Already up to date" else if [[ -e ${2} ]]; then command mv -f ${2}{,.old} || return 1 fi command mv -f ${1} ${2} && \ - if (( ! _zquiet )); then - print -PR "<%= okay %>%B${2}:%b Updated. Restart your terminal for changes to take effect." - fi + _zimfw_print -PR "<%= okay %>%B${2}:%b Updated. Restart your terminal for changes to take effect." fi } diff --git a/src/stage2/30_zimfw_build.zsh.erb b/src/stage2/30_zimfw_build.zsh.erb index 8226784..73d2f78 100644 --- a/src/stage2/30_zimfw_build.zsh.erb +++ b/src/stage2/30_zimfw_build.zsh.erb @@ -1,6 +1,3 @@ _zimfw_build() { - _zimfw_build_init && _zimfw_build_login_init && \ - if (( ! _zquiet )); then - print -P '<%= done %>Done with build.' - fi + _zimfw_build_init && _zimfw_build_login_init && _zimfw_print -P '<%= done %>Done with build.' } diff --git a/src/stage2/50_zimfw_clean_compiled.zsh.erb b/src/stage2/50_zimfw_clean_compiled.zsh.erb index eae355e..3108796 100644 --- a/src/stage2/50_zimfw_clean_compiled.zsh.erb +++ b/src/stage2/50_zimfw_clean_compiled.zsh.erb @@ -3,7 +3,5 @@ _zimfw_clean_compiled() { (( ! _zquiet )) && zopt='-v' command find ${ZIM_HOME} \( -name '*.zwc' -o -name '*.zwc.old' \) -exec rm -f ${zopt} {} \; || return 1 command rm -f ${zopt} <%= home %>/<%= startup_files_glob %>.zwc(|.old)(N) || return 1 - if (( ! _zquiet )); then - print -P '<%= done %>Done with clean-compiled. Run %Bzimfw compile%b to re-compile.' - fi + _zimfw_print -P '<%= done %>Done with clean-compiled. Run %Bzimfw compile%b to re-compile.' } diff --git a/src/stage2/50_zimfw_clean_dumpfile.zsh.erb b/src/stage2/50_zimfw_clean_dumpfile.zsh.erb index 8cd2e0f..909628e 100644 --- a/src/stage2/50_zimfw_clean_dumpfile.zsh.erb +++ b/src/stage2/50_zimfw_clean_dumpfile.zsh.erb @@ -3,7 +3,5 @@ _zimfw_clean_dumpfile() { zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile=<%= home %>/.zcompdump (( ! _zquiet )) && zopt='-v' command rm -f ${zopt} ${zdumpfile}(|.zwc(|.old)) || return 1 - if (( ! _zquiet )); then - print -P '<%= done %>Done with clean-dumpfile. Restart your terminal to dump an updated configuration.' - fi + _zimfw_print -P '<%= done %>Done with clean-dumpfile. Restart your terminal to dump an updated configuration.' } diff --git a/src/stage2/50_zimfw_uninstall.zsh.erb b/src/stage2/50_zimfw_uninstall.zsh.erb index dcfe196..4c4aaf7 100644 --- a/src/stage2/50_zimfw_uninstall.zsh.erb +++ b/src/stage2/50_zimfw_uninstall.zsh.erb @@ -8,7 +8,5 @@ _zimfw_uninstall() { command rm -rf ${zopt} ${zdir} || return 1 fi done - if (( ! _zquiet )); then - print -P '<%= done %>Done with uninstall.' - fi + _zimfw_print -P '<%= done %>Done with uninstall.' } diff --git a/src/stage2/50_zimfw_upgrade.zsh.erb b/src/stage2/50_zimfw_upgrade.zsh.erb index 497c2b8..c352d68 100644 --- a/src/stage2/50_zimfw_upgrade.zsh.erb +++ b/src/stage2/50_zimfw_upgrade.zsh.erb @@ -7,10 +7,7 @@ _zimfw_upgrade() { else command curl -fsSL -o ${ztarget}.new ${zurl} || return 1 fi - _zimfw_mv ${ztarget}{.new,} && \ - if (( ! _zquiet )); then - print -P '<%= done %>Done with upgrade.' - fi + _zimfw_mv ${ztarget}{.new,} && _zimfw_print -P '<%= done %>Done with upgrade.' } always { command rm -f ${ztarget}.new } diff --git a/src/stage2/80_zimfw.zsh.erb b/src/stage2/80_zimfw.zsh.erb index fe40543..279b3c4 100644 --- a/src/stage2/80_zimfw.zsh.erb +++ b/src/stage2/80_zimfw.zsh.erb @@ -53,9 +53,7 @@ Options: install|update) _zimfw_source_zimrc 1 || return 1 print -Rn ${_zmodules_xargs} | xargs -0 -n6 -P10 zsh -c ${ztool} ${1} && \ - if (( ! _zquiet )); then - print -PR "<%= done %>Done with ${1}. Restart your terminal for any changes to take effect." - fi && \ + _zimfw_print -PR "<%= done %>Done with ${1}. Restart your terminal for any changes to take effect." && \ _zimfw_source_zimrc && _zimfw_build && _zimfw_compile ${2} ;; uninstall) _zimfw_source_zimrc && _zimfw_uninstall ;; diff --git a/zimfw.zsh b/zimfw.zsh index d2d2b20..2dedb88 100644 --- a/zimfw.zsh +++ b/zimfw.zsh @@ -32,19 +32,21 @@ fi # Define Zim location : ${ZIM_HOME=${0:A:h}} +_zimfw_print() { + if (( ! _zquiet )); then + print "${@}" + fi +} + _zimfw_mv() { if command cmp -s ${2} ${1}; then - if (( ! _zquiet )); then - print -PR "%F{green})%f %B${2}:%b Already up to date" - fi + _zimfw_print -PR "%F{green})%f %B${2}:%b Already up to date" else if [[ -e ${2} ]]; then command mv -f ${2}{,.old} || return 1 fi command mv -f ${1} ${2} && \ - if (( ! _zquiet )); then - print -PR "%F{green})%f %B${2}:%b Updated. Restart your terminal for changes to take effect." - fi + _zimfw_print -PR "%F{green})%f %B${2}:%b Updated. Restart your terminal for changes to take effect." fi } @@ -95,10 +97,7 @@ _zimfw_build_login_init() { } _zimfw_build() { - _zimfw_build_init && _zimfw_build_login_init && \ - if (( ! _zquiet )); then - print -P 'Done with build.' - fi + _zimfw_build_init && _zimfw_build_login_init && _zimfw_print -P 'Done with build.' } zmodule() { @@ -243,9 +242,7 @@ _zimfw_clean_compiled() { (( ! _zquiet )) && zopt='-v' command find ${ZIM_HOME} \( -name '*.zwc' -o -name '*.zwc.old' \) -exec rm -f ${zopt} {} \; || return 1 command rm -f ${zopt} ${ZDOTDIR:-${HOME}}/.z(shenv|profile|shrc|login|logout).zwc(|.old)(N) || return 1 - if (( ! _zquiet )); then - print -P 'Done with clean-compiled. Run %Bzimfw compile%b to re-compile.' - fi + _zimfw_print -P 'Done with clean-compiled. Run %Bzimfw compile%b to re-compile.' } _zimfw_clean_dumpfile() { @@ -253,9 +250,7 @@ _zimfw_clean_dumpfile() { zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile=${ZDOTDIR:-${HOME}}/.zcompdump (( ! _zquiet )) && zopt='-v' command rm -f ${zopt} ${zdumpfile}(|.zwc(|.old)) || return 1 - if (( ! _zquiet )); then - print -P 'Done with clean-dumpfile. Restart your terminal to dump an updated configuration.' - fi + _zimfw_print -P 'Done with clean-dumpfile. Restart your terminal to dump an updated configuration.' } _zimfw_compile() { @@ -263,7 +258,7 @@ _zimfw_compile() { } _zimfw_info() { - print 'Zim version: 1.0.1-SNAPSHOT (previous commit is 7374770)' + print 'Zim version: 1.0.1-SNAPSHOT (previous commit is 2cc69d5)' print -R 'ZIM_HOME: '${ZIM_HOME} print -R 'Zsh version: '${ZSH_VERSION} print -R 'System info: '$(command uname -a) @@ -279,9 +274,7 @@ _zimfw_uninstall() { command rm -rf ${zopt} ${zdir} || return 1 fi done - if (( ! _zquiet )); then - print -P 'Done with uninstall.' - fi + _zimfw_print -P 'Done with uninstall.' } _zimfw_upgrade() { @@ -293,10 +286,7 @@ _zimfw_upgrade() { else command curl -fsSL -o ${ztarget}.new ${zurl} || return 1 fi - _zimfw_mv ${ztarget}{.new,} && \ - if (( ! _zquiet )); then - print -P 'Done with upgrade.' - fi + _zimfw_mv ${ztarget}{.new,} && _zimfw_print -P 'Done with upgrade.' } always { command rm -f ${ztarget}.new } @@ -437,9 +427,7 @@ fi install|update) _zimfw_source_zimrc 1 || return 1 print -Rn ${_zmodules_xargs} | xargs -0 -n6 -P10 zsh -c ${ztool} ${1} && \ - if (( ! _zquiet )); then - print -PR "Done with ${1}. Restart your terminal for any changes to take effect." - fi && \ + _zimfw_print -PR "Done with ${1}. Restart your terminal for any changes to take effect." && \ _zimfw_source_zimrc && _zimfw_build && _zimfw_compile ${2} ;; uninstall) _zimfw_source_zimrc && _zimfw_uninstall ;;