From 4455c13e063cf81ba427f98e1dce2024ecd50762 Mon Sep 17 00:00:00 2001 From: pollyduan Date: Tue, 17 Aug 2021 18:10:54 +0800 Subject: feat(cli): add subcommands for plugin `enable` and `disable` (#9869) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Cornellà --- lib/cli.zsh | 218 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 207 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/cli.zsh b/lib/cli.zsh index 7e3e37be8..1289df730 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -37,7 +37,13 @@ function _omz { changelog) local -a refs refs=("${(@f)$(command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}") _describe 'command' refs ;; - plugin) subcmds=('info:Get plugin information' 'list:List plugins' 'load:Load plugin(s)') + plugin) subcmds=( + 'disable:Disable plugin(s)' + 'enable:Enable plugin(s)' + 'info:Get plugin information' + 'list:List plugins' + 'load:Load plugin(s)' + ) _describe 'command' subcmds ;; pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches') _describe 'command' subcmds ;; @@ -45,8 +51,21 @@ function _omz { _describe 'command' subcmds ;; esac elif (( CURRENT == 4 )); then - case "$words[2]::$words[3]" in - plugin::(info|load)) + case "${words[2]}::${words[3]}" in + plugin::(disable|enable|load)) + local -aU valid_plugins + + if [[ "${words[3]}" = disable ]]; then + # if command is "disable", only offer already enabled plugins + valid_plugins=($plugins) + else + valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t)) + # if command is "enable", remove already enabled plugins + [[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins}) + fi + + _describe 'plugin' valid_plugins ;; + plugin::info) local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t)) _describe 'plugin' plugins ;; theme::use) @@ -54,18 +73,27 @@ function _omz { _describe 'theme' themes ;; esac elif (( CURRENT > 4 )); then - case "$words[2]::$words[3]" in - plugin::load) - local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t)) + case "${words[2]}::${words[3]}" in + plugin::(enable|disable|load)) + local -aU valid_plugins + + if [[ "${words[3]}" = disable ]]; then + # if command is "disable", only offer already enabled plugins + valid_plugins=($plugins) + else + valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t)) + # if command is "enable", remove already enabled plugins + [[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins}) + fi # Remove plugins already passed as arguments # NOTE: $(( CURRENT - 1 )) is the last plugin argument completely passed, i.e. that which # has a space after them. This is to avoid removing plugins partially passed, which makes # the completion not add a space after the completed plugin. local -a args=(${words[4,$(( CURRENT - 1))]}) - plugins=(${plugins:|args}) + valid_plugins=(${valid_plugins:|args}) - _describe 'plugin' plugins ;; + _describe 'plugin' valid_plugins ;; esac fi @@ -161,9 +189,11 @@ Usage: omz plugin [options] Available commands: - info Get information of a plugin - list List all available Oh My Zsh plugins - load Load plugin(s) + disable Disable plugin(s) + enable Enable plugin(s) + info Get information of a plugin + list List all available Oh My Zsh plugins + load Load plugin(s) EOF return 1 @@ -175,6 +205,172 @@ EOF _omz::plugin::$command "$@" } +function _omz::plugin::disable { + if [[ -z "$1" ]]; then + echo >&2 "Usage: omz plugin disable [...]" + return 1 + fi + + # Check that plugin is in $plugins + local -a dis_plugins=() + for plugin in "$@"; do + if [[ ${plugins[(Ie)$plugin]} -eq 0 ]]; then + _omz::log warn "plugin '$plugin' is not enabled." + continue + fi + dis_plugins+=("$plugin") + done + + # Exit if there are no enabled plugins to disable + if [[ ${#dis_plugins} -eq 0 ]]; then + return 1 + fi + + # Disable plugins awk script + local awk_script=" +# if plugins=() is in oneline form, substitute disabled plugins and go to next line +/^\s*plugins=\([^#]+\).*\$/ { + sub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before + sub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after + sub(/\((${(j:|:)dis_plugins})\)/, \"\") # without spaces (only plugin) + print \$0 + next +} + +# if plugins=() is in multiline form, enable multi flag and disable plugins if they're there +/^\s*plugins=\(/ { + multi=1 + sub(/\s+(${(j:|:)dis_plugins})/, \"\") + sub(/(${(j:|:)dis_plugins})\s+/, \"\") + sub(/\((${(j:|:)dis_plugins})\)/, \"\") + print \$0 + next +} + +# if multi flag is enabled and we find a valid closing parenthesis, +# add new plugins and disable multi flag +multi == 1 && /^[^#]*\)/ { + multi=0 + sub(/\s+(${(j:|:)dis_plugins})/, \"\") + sub(/(${(j:|:)dis_plugins})\s+/, \"\") + sub(/\((${(j:|:)dis_plugins})\)/, \"\") + print \$0 + next +} + +multi == 1 { + sub(/\s+(${(j:|:)dis_plugins})/, \"\") + sub(/(${(j:|:)dis_plugins})\s+/, \"\") + sub(/\((${(j:|:)dis_plugins})\)/, \"\") + print \$0 + next +} + +{ print \$0 } +" + + awk "$awk_script" ~/.zshrc > ~/.zshrc.disabled \ + && mv ~/.zshrc ~/.zshrc.swp \ + && mv ~/.zshrc.disabled ~/.zshrc + + # Exit if the new .zshrc file wasn't created correctly + [[ $? -eq 0 ]] || { + local ret=$? + _omz::log error "error disabling plugins." + return $ret + } + + # Exit if the new .zshrc file has syntax errors + if ! zsh -n ~/.zshrc; then + _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." + mv ~/.zshrc ~/.zshrc.disabled + mv ~/.zshrc.swp ~/.zshrc + return 1 + fi + + # Restart the zsh session if there were no errors + _omz::log info "" + + # Old zsh versions don't have ZSH_ARGZERO + local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}" + # Check whether to run a login shell + [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh" +} + +function _omz::plugin::enable { + if [[ -z "$1" ]]; then + echo >&2 "Usage: omz plugin enable [...]" + return 1 + fi + + # Check that plugin is not in $plugins + local -a add_plugins=() + for plugin in "$@"; do + if [[ ${plugins[(Ie)$plugin]} -ne 0 ]]; then + _omz::log warn "plugin '$plugin' is already enabled." + continue + fi + add_plugins+=("$plugin") + done + + # Exit if there are no plugins to enable + if [[ ${#add_plugins} -eq 0 ]]; then + return 1 + fi + + # Enable plugins awk script + local awk_script=" +# if plugins=() is in oneline form, substitute ) with new plugins and go to the next line +/^\s*plugins=\([^#]+\).*\$/ { + sub(/\)/, \" $add_plugins&\") + print \$0 + next +} + +# if plugins=() is in multiline form, enable multi flag +/^\s*plugins=\(/ { + multi=1 +} + +# if multi flag is enabled and we find a valid closing parenthesis, +# add new plugins and disable multi flag +multi == 1 && /^[^#]*\)/ { + multi=0 + sub(/\)/, \" $add_plugins&\") + print \$0 + next +} + +{ print \$0 } +" + + awk "$awk_script" ~/.zshrc > ~/.zshrc.disabled \ + && mv ~/.zshrc ~/.zshrc.swp \ + && mv ~/.zshrc.disabled ~/.zshrc + + # Exit if the new .zshrc file wasn't created correctly + [[ $? -eq 0 ]] || { + local ret=$? + _omz::log error "error disabling plugins." + return $ret + } + + # Exit if the new .zshrc file has syntax errors + if ! zsh -n ~/.zshrc; then + _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." + mv ~/.zshrc ~/.zshrc.disabled + mv ~/.zshrc.swp ~/.zshrc + return 1 + fi + + # Restart the zsh session if there were no errors + + # Old zsh versions don't have ZSH_ARGZERO + local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}" + # Check whether to run a login shell + [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh" +} + function _omz::plugin::info { if [[ -z "$1" ]]; then echo >&2 "Usage: omz plugin info " -- cgit v1.2.3-70-g09d2 From 708bbe12c5817e380c83d0c49bf684b0fc5d0024 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 17 Aug 2021 12:31:37 +0200 Subject: fix(cli): fix multiple errors in `plugin disable/enable` --- lib/cli.zsh | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/lib/cli.zsh b/lib/cli.zsh index 1289df730..e490149e0 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -230,9 +230,9 @@ function _omz::plugin::disable { local awk_script=" # if plugins=() is in oneline form, substitute disabled plugins and go to next line /^\s*plugins=\([^#]+\).*\$/ { - sub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before - sub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after - sub(/\((${(j:|:)dis_plugins})\)/, \"\") # without spaces (only plugin) + gsub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before + gsub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after + gsub(/\((${(j:|:)dis_plugins})\)/, \"\") # without spaces (only plugin) print \$0 next } @@ -240,9 +240,9 @@ function _omz::plugin::disable { # if plugins=() is in multiline form, enable multi flag and disable plugins if they're there /^\s*plugins=\(/ { multi=1 - sub(/\s+(${(j:|:)dis_plugins})/, \"\") - sub(/(${(j:|:)dis_plugins})\s+/, \"\") - sub(/\((${(j:|:)dis_plugins})\)/, \"\") + gsub(/\s+(${(j:|:)dis_plugins})/, \"\") + gsub(/(${(j:|:)dis_plugins})\s+/, \"\") + gsub(/\((${(j:|:)dis_plugins})\)/, \"\") print \$0 next } @@ -251,17 +251,17 @@ function _omz::plugin::disable { # add new plugins and disable multi flag multi == 1 && /^[^#]*\)/ { multi=0 - sub(/\s+(${(j:|:)dis_plugins})/, \"\") - sub(/(${(j:|:)dis_plugins})\s+/, \"\") - sub(/\((${(j:|:)dis_plugins})\)/, \"\") + gsub(/\s+(${(j:|:)dis_plugins})/, \"\") + gsub(/(${(j:|:)dis_plugins})\s+/, \"\") + gsub(/\((${(j:|:)dis_plugins})\)/, \"\") print \$0 next } multi == 1 { - sub(/\s+(${(j:|:)dis_plugins})/, \"\") - sub(/(${(j:|:)dis_plugins})\s+/, \"\") - sub(/\((${(j:|:)dis_plugins})\)/, \"\") + gsub(/\s+(${(j:|:)dis_plugins})/, \"\") + gsub(/(${(j:|:)dis_plugins})\s+/, \"\") + gsub(/\((${(j:|:)dis_plugins})\)/, \"\") print \$0 next } @@ -283,13 +283,13 @@ multi == 1 { # Exit if the new .zshrc file has syntax errors if ! zsh -n ~/.zshrc; then _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." - mv ~/.zshrc ~/.zshrc.disabled - mv ~/.zshrc.swp ~/.zshrc + command mv -f ~/.zshrc ~/.zshrc.disabled + command mv -f ~/.zshrc.swp ~/.zshrc return 1 fi # Restart the zsh session if there were no errors - _omz::log info "" + _omz::log info "plugins disabled: ${(j:, :)dis_plugins}." # Old zsh versions don't have ZSH_ARGZERO local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}" @@ -344,26 +344,27 @@ multi == 1 && /^[^#]*\)/ { { print \$0 } " - awk "$awk_script" ~/.zshrc > ~/.zshrc.disabled \ - && mv ~/.zshrc ~/.zshrc.swp \ - && mv ~/.zshrc.disabled ~/.zshrc + awk "$awk_script" ~/.zshrc > ~/.zshrc.enabled \ + && command mv -f ~/.zshrc ~/.zshrc.swp \ + && command mv -f ~/.zshrc.enabled ~/.zshrc # Exit if the new .zshrc file wasn't created correctly [[ $? -eq 0 ]] || { local ret=$? - _omz::log error "error disabling plugins." + _omz::log error "error enabling plugins." return $ret } # Exit if the new .zshrc file has syntax errors if ! zsh -n ~/.zshrc; then _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." - mv ~/.zshrc ~/.zshrc.disabled - mv ~/.zshrc.swp ~/.zshrc + command mv -f ~/.zshrc ~/.zshrc.enabled + command mv -f ~/.zshrc.swp ~/.zshrc return 1 fi # Restart the zsh session if there were no errors + _omz::log info "plugins enabled: ${(j:, :)add_plugins}." # Old zsh versions don't have ZSH_ARGZERO local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}" -- cgit v1.2.3-70-g09d2 From bf888680ea5b7161cf1d06011df33bfbeda8b255 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 17 Aug 2021 12:38:48 +0200 Subject: refactor(cli): extract substitution awk script in `plugin disable` --- lib/cli.zsh | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/lib/cli.zsh b/lib/cli.zsh index e490149e0..a10b0e147 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -226,13 +226,17 @@ function _omz::plugin::disable { return 1 fi + # Remove plugins substitution awk script + local awk_subst_plugins="\ + gsub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before + gsub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after + gsub(/\((${(j:|:)dis_plugins})\)/, \"\") # without spaces (only plugin) +" # Disable plugins awk script local awk_script=" # if plugins=() is in oneline form, substitute disabled plugins and go to next line /^\s*plugins=\([^#]+\).*\$/ { - gsub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before - gsub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after - gsub(/\((${(j:|:)dis_plugins})\)/, \"\") # without spaces (only plugin) + $awk_subst_plugins print \$0 next } @@ -240,29 +244,22 @@ function _omz::plugin::disable { # if plugins=() is in multiline form, enable multi flag and disable plugins if they're there /^\s*plugins=\(/ { multi=1 - gsub(/\s+(${(j:|:)dis_plugins})/, \"\") - gsub(/(${(j:|:)dis_plugins})\s+/, \"\") - gsub(/\((${(j:|:)dis_plugins})\)/, \"\") + $awk_subst_plugins print \$0 next } -# if multi flag is enabled and we find a valid closing parenthesis, -# add new plugins and disable multi flag +# if multi flag is enabled and we find a valid closing parenthesis, remove plugins and disable multi flag multi == 1 && /^[^#]*\)/ { multi=0 - gsub(/\s+(${(j:|:)dis_plugins})/, \"\") - gsub(/(${(j:|:)dis_plugins})\s+/, \"\") - gsub(/\((${(j:|:)dis_plugins})\)/, \"\") + $awk_subst_plugins print \$0 next } -multi == 1 { - gsub(/\s+(${(j:|:)dis_plugins})/, \"\") - gsub(/(${(j:|:)dis_plugins})\s+/, \"\") - gsub(/\((${(j:|:)dis_plugins})\)/, \"\") - print \$0 +multi == 1 && length(\$0) > 0 { + $awk_subst_plugins + if (length(\$0) > 0) print \$0 next } -- cgit v1.2.3-70-g09d2 From bc7ce982ddcabdb7a6d446207ef4a0b78920da8b Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 17 Aug 2021 12:53:09 +0200 Subject: style(cli): fill rows in column output in theme and plugin `list` commands --- lib/cli.zsh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/cli.zsh b/lib/cli.zsh index a10b0e147..6bdac6cf1 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -405,14 +405,14 @@ function _omz::plugin::list { if (( ${#custom_plugins} )); then print -P "%U%BCustom plugins%b%u:" - print -l ${(q-)custom_plugins} | column + print -l ${(q-)custom_plugins} | column -x fi if (( ${#builtin_plugins} )); then (( ${#custom_plugins} )) && echo # add a line of separation print -P "%U%BBuilt-in plugins%b%u:" - print -l ${(q-)builtin_plugins} | column + print -l ${(q-)builtin_plugins} | column -x fi } @@ -631,14 +631,14 @@ function _omz::theme::list { if (( ${#custom_themes} )); then print -P "%U%BCustom themes%b%u:" - print -l ${(q-)custom_themes} | column + print -l ${(q-)custom_themes} | column -x fi if (( ${#builtin_themes} )); then (( ${#custom_themes} )) && echo # add a line of separation print -P "%U%BBuilt-in themes%b%u:" - print -l ${(q-)builtin_themes} | column + print -l ${(q-)builtin_themes} | column -x fi } -- cgit v1.2.3-70-g09d2 From 7a4f4ad91e1f937b36a54703984b958abe9da4b8 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 17 Aug 2021 17:38:31 +0200 Subject: fix(lib): fix clipboard copy on Termux --- lib/clipboard.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 122145f15..4e3ba0a45 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -76,7 +76,7 @@ function detect-clipboard() { function clipcopy() { win32yank -i < "${1:-/dev/stdin}"; } function clippaste() { win32yank -o; } elif [[ $OSTYPE == linux-android* ]] && (( $+commands[termux-clipboard-set] )); then - function clipcopy() { termux-clipboard-set "${1:-/dev/stdin}"; } + function clipcopy() { termux-clipboard-set < "${1:-/dev/stdin}"; } function clippaste() { termux-clipboard-get; } elif [ -n "${TMUX:-}" ] && (( ${+commands[tmux]} )); then function clipcopy() { tmux load-buffer "${1:--}"; } -- cgit v1.2.3-70-g09d2 From 8dedf26294d4236ffe84260d30d686a705b0d2ca Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Wed, 18 Aug 2021 12:50:22 +0200 Subject: style(cli): print usage messages to stderr --- lib/cli.zsh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/cli.zsh b/lib/cli.zsh index 6bdac6cf1..b49643339 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -150,7 +150,7 @@ function _omz::log { ## User-facing commands function _omz::help { - cat <&2 < [options] Available commands: @@ -171,7 +171,7 @@ function _omz::changelog { if ! command git -C "$ZSH" show-ref --verify refs/heads/$version &>/dev/null && \ ! command git -C "$ZSH" show-ref --verify refs/tags/$version &>/dev/null && \ ! command git -C "$ZSH" rev-parse --verify "${version}^{commit}" &>/dev/null; then - cat <&2 < must be a valid branch, tag or commit. @@ -184,7 +184,7 @@ EOF function _omz::plugin { (( $# > 0 && $+functions[_omz::plugin::$1] )) || { - cat <&2 < [options] Available commands: @@ -468,7 +468,7 @@ function _omz::plugin::load { function _omz::pr { (( $# > 0 && $+functions[_omz::pr::$1] )) || { - cat <&2 < [options] Available commands: @@ -600,7 +600,7 @@ function _omz::pr::test { function _omz::theme { (( $# > 0 && $+functions[_omz::theme::$1] )) || { - cat <&2 < [options] Available commands: -- cgit v1.2.3-70-g09d2 From cbb534267aca09fd123635fc39a7d00c0e21a5f7 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Wed, 18 Aug 2021 16:57:56 +0200 Subject: feat(cli): add `theme set` subcommand to change theme in .zshrc Fixes #9087 --- lib/cli.zsh | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/cli.zsh b/lib/cli.zsh index b49643339..2189e24ca 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -45,9 +45,9 @@ function _omz { 'load:Load plugin(s)' ) _describe 'command' subcmds ;; - pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches') + pr) subcmds=('clean:Delete all Pull Request branches' 'test:Test a Pull Request') _describe 'command' subcmds ;; - theme) subcmds=('use:Load a theme' 'list:List themes') + theme) subcmds=('list:List themes' 'set:Set a theme in your .zshrc file' 'use:Load a theme') _describe 'command' subcmds ;; esac elif (( CURRENT == 4 )); then @@ -68,7 +68,7 @@ function _omz { plugin::info) local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t)) _describe 'plugin' plugins ;; - theme::use) + theme::(set|use)) local -aU themes=("$ZSH"/themes/*.zsh-theme(.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::)) _describe 'theme' themes ;; esac @@ -266,9 +266,9 @@ multi == 1 && length(\$0) > 0 { { print \$0 } " - awk "$awk_script" ~/.zshrc > ~/.zshrc.disabled \ - && mv ~/.zshrc ~/.zshrc.swp \ - && mv ~/.zshrc.disabled ~/.zshrc + awk "$awk_script" ~/.zshrc > ~/.zshrc.new \ + && command mv -f ~/.zshrc ~/.zshrc.bck \ + && command mv -f ~/.zshrc.new ~/.zshrc # Exit if the new .zshrc file wasn't created correctly [[ $? -eq 0 ]] || { @@ -280,8 +280,8 @@ multi == 1 && length(\$0) > 0 { # Exit if the new .zshrc file has syntax errors if ! zsh -n ~/.zshrc; then _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." - command mv -f ~/.zshrc ~/.zshrc.disabled - command mv -f ~/.zshrc.swp ~/.zshrc + command mv -f ~/.zshrc ~/.zshrc.new + command mv -f ~/.zshrc.bck ~/.zshrc return 1 fi @@ -341,9 +341,9 @@ multi == 1 && /^[^#]*\)/ { { print \$0 } " - awk "$awk_script" ~/.zshrc > ~/.zshrc.enabled \ - && command mv -f ~/.zshrc ~/.zshrc.swp \ - && command mv -f ~/.zshrc.enabled ~/.zshrc + awk "$awk_script" ~/.zshrc > ~/.zshrc.new \ + && command mv -f ~/.zshrc ~/.zshrc.bck \ + && command mv -f ~/.zshrc.new ~/.zshrc # Exit if the new .zshrc file wasn't created correctly [[ $? -eq 0 ]] || { @@ -355,8 +355,8 @@ multi == 1 && /^[^#]*\)/ { # Exit if the new .zshrc file has syntax errors if ! zsh -n ~/.zshrc; then _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." - command mv -f ~/.zshrc ~/.zshrc.enabled - command mv -f ~/.zshrc.swp ~/.zshrc + command mv -f ~/.zshrc ~/.zshrc.new + command mv -f ~/.zshrc.bck ~/.zshrc return 1 fi @@ -606,7 +606,8 @@ Usage: omz theme [options] Available commands: list List all available Oh My Zsh themes - use Load an Oh My Zsh theme + set Set a theme in your .zshrc file + use Load a theme EOF return 1 @@ -642,6 +643,73 @@ function _omz::theme::list { fi } +function _omz::theme::set { + if [[ -z "$1" ]]; then + echo >&2 "Usage: omz theme set " + return 1 + fi + + # Check that theme exists + if [[ ! -f "$ZSH_CUSTOM/$1.zsh-theme" ]] \ + && [[ ! -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]] \ + && [[ ! -f "$ZSH/themes/$1.zsh-theme" ]]; then + _omz::log error "%B$1%b theme not found" + return 1 + fi + + # Enable theme in .zshrc + local awk_script=' +!set && /^\s*ZSH_THEME=[^#]+.*$/ { + set=1 + sub(/^\s*ZSH_THEME=[^#]+.*$/, "ZSH_THEME=\"'$1'\" # set by `omz`") + print $0 + next +} + +{ print $0 } + +END { + # If no ZSH_THEME= line was found, return an error + if (!set) exit 1 +} +' + + awk "$awk_script" ~/.zshrc > ~/.zshrc.new \ + || { + # Prepend ZSH_THEME= line to .zshrc if it doesn't exist + cat < ~/.zshrc.new \ + && command mv -f ~/.zshrc ~/.zshrc.bck \ + && command mv -f ~/.zshrc.new ~/.zshrc + + # Exit if the new .zshrc file wasn't created correctly + [[ $? -eq 0 ]] || { + local ret=$? + _omz::log error "error setting theme." + return $ret + } + + # Exit if the new .zshrc file has syntax errors + if ! zsh -n ~/.zshrc; then + _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." + command mv -f ~/.zshrc ~/.zshrc.new + command mv -f ~/.zshrc.bck ~/.zshrc + return 1 + fi + + # Restart the zsh session if there were no errors + _omz::log info "'$1' theme set correctly." + + # Old zsh versions don't have ZSH_ARGZERO + local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}" + # Check whether to run a login shell + [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh" +} + function _omz::theme::use { if [[ -z "$1" ]]; then echo >&2 "Usage: omz theme use " @@ -656,7 +724,7 @@ function _omz::theme::use { elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then source "$ZSH/themes/$1.zsh-theme" else - _omz::log error "theme '$1' not found" + _omz::log error "%B$1%b theme not found" return 1 fi } -- cgit v1.2.3-70-g09d2