cbb534267aca09fd123635fc39a7d00c0e21a5f7

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(cli): add `theme set` subcommand to change theme in .zshrc

Fixes #9087

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/cli.zsh b/lib/cli.zsh
  2index b49643339a39cd4ff3b27a1454e88d0b24195633..2189e24ca13c18c39e5b0fc4ba795ae29c3526d4 100644
  3--- a/lib/cli.zsh
  4+++ b/lib/cli.zsh
  5@@ -45,9 +45,9 @@ function _omz {
  6         'load:Load plugin(s)'
  7       )
  8         _describe 'command' subcmds ;;
  9-      pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches')
 10+      pr) subcmds=('clean:Delete all Pull Request branches' 'test:Test a Pull Request')
 11         _describe 'command' subcmds ;;
 12-      theme) subcmds=('use:Load a theme' 'list:List themes')
 13+      theme) subcmds=('list:List themes' 'set:Set a theme in your .zshrc file' 'use:Load a theme')
 14         _describe 'command' subcmds ;;
 15     esac
 16   elif (( CURRENT == 4 )); then
 17@@ -68,7 +68,7 @@ function _omz {
 18       plugin::info)
 19         local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
 20         _describe 'plugin' plugins ;;
 21-      theme::use)
 22+      theme::(set|use))
 23         local -aU themes=("$ZSH"/themes/*.zsh-theme(.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
 24         _describe 'theme' themes ;;
 25     esac
 26@@ -266,9 +266,9 @@ multi == 1 && length(\$0) > 0 {
 27 { print \$0 }
 28 "
 29 
 30-  awk "$awk_script" ~/.zshrc > ~/.zshrc.disabled \
 31-  && mv ~/.zshrc ~/.zshrc.swp \
 32-  && mv ~/.zshrc.disabled ~/.zshrc
 33+  awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
 34+  && command mv -f ~/.zshrc ~/.zshrc.bck \
 35+  && command mv -f ~/.zshrc.new ~/.zshrc
 36 
 37   # Exit if the new .zshrc file wasn't created correctly
 38   [[ $? -eq 0 ]] || {
 39@@ -280,8 +280,8 @@ multi == 1 && length(\$0) > 0 {
 40   # Exit if the new .zshrc file has syntax errors
 41   if ! zsh -n ~/.zshrc; then
 42     _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
 43-    command mv -f ~/.zshrc ~/.zshrc.disabled
 44-    command mv -f ~/.zshrc.swp ~/.zshrc
 45+    command mv -f ~/.zshrc ~/.zshrc.new
 46+    command mv -f ~/.zshrc.bck ~/.zshrc
 47     return 1
 48   fi
 49 
 50@@ -341,9 +341,9 @@ multi == 1 && /^[^#]*\)/ {
 51 { print \$0 }
 52 "
 53 
 54-  awk "$awk_script" ~/.zshrc > ~/.zshrc.enabled \
 55-  && command mv -f ~/.zshrc ~/.zshrc.swp \
 56-  && command mv -f ~/.zshrc.enabled ~/.zshrc
 57+  awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
 58+  && command mv -f ~/.zshrc ~/.zshrc.bck \
 59+  && command mv -f ~/.zshrc.new ~/.zshrc
 60 
 61   # Exit if the new .zshrc file wasn't created correctly
 62   [[ $? -eq 0 ]] || {
 63@@ -355,8 +355,8 @@ multi == 1 && /^[^#]*\)/ {
 64   # Exit if the new .zshrc file has syntax errors
 65   if ! zsh -n ~/.zshrc; then
 66     _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
 67-    command mv -f ~/.zshrc ~/.zshrc.enabled
 68-    command mv -f ~/.zshrc.swp ~/.zshrc
 69+    command mv -f ~/.zshrc ~/.zshrc.new
 70+    command mv -f ~/.zshrc.bck ~/.zshrc
 71     return 1
 72   fi
 73 
 74@@ -606,7 +606,8 @@ Usage: omz theme <command> [options]
 75 Available commands:
 76 
 77   list            List all available Oh My Zsh themes
 78-  use <theme>     Load an Oh My Zsh theme
 79+  set <theme>     Set a theme in your .zshrc file
 80+  use <theme>     Load a theme
 81 
 82 EOF
 83     return 1
 84@@ -642,6 +643,73 @@ function _omz::theme::list {
 85   fi
 86 }
 87 
 88+function _omz::theme::set {
 89+  if [[ -z "$1" ]]; then
 90+    echo >&2 "Usage: omz theme set <theme>"
 91+    return 1
 92+  fi
 93+
 94+  # Check that theme exists
 95+  if [[ ! -f "$ZSH_CUSTOM/$1.zsh-theme" ]] \
 96+    && [[ ! -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]] \
 97+    && [[ ! -f "$ZSH/themes/$1.zsh-theme" ]]; then
 98+    _omz::log error "%B$1%b theme not found"
 99+    return 1
100+  fi
101+
102+  # Enable theme in .zshrc
103+  local awk_script='
104+!set && /^\s*ZSH_THEME=[^#]+.*$/ {