Unverified Commit cbb53426 authored by Marc Cornellà's avatar Marc Cornellà
Browse files

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

Fixes #9087
parent 8dedf262
...@@ -45,9 +45,9 @@ function _omz { ...@@ -45,9 +45,9 @@ function _omz {
'load:Load plugin(s)' 'load:Load plugin(s)'
) )
_describe 'command' subcmds ;; _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 ;; _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 ;; _describe 'command' subcmds ;;
esac esac
elif (( CURRENT == 4 )); then elif (( CURRENT == 4 )); then
...@@ -68,7 +68,7 @@ function _omz { ...@@ -68,7 +68,7 @@ function _omz {
plugin::info) plugin::info)
local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t)) local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
_describe 'plugin' plugins ;; _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"/:::)) 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 ;; _describe 'theme' themes ;;
esac esac
...@@ -266,9 +266,9 @@ multi == 1 && length(\$0) > 0 { ...@@ -266,9 +266,9 @@ multi == 1 && length(\$0) > 0 {
{ print \$0 } { print \$0 }
" "
awk "$awk_script" ~/.zshrc > ~/.zshrc.disabled \ awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
&& mv ~/.zshrc ~/.zshrc.swp \ && command mv -f ~/.zshrc ~/.zshrc.bck \
&& mv ~/.zshrc.disabled ~/.zshrc && command mv -f ~/.zshrc.new ~/.zshrc
# Exit if the new .zshrc file wasn't created correctly # Exit if the new .zshrc file wasn't created correctly
[[ $? -eq 0 ]] || { [[ $? -eq 0 ]] || {
...@@ -280,8 +280,8 @@ multi == 1 && length(\$0) > 0 { ...@@ -280,8 +280,8 @@ multi == 1 && length(\$0) > 0 {
# Exit if the new .zshrc file has syntax errors # Exit if the new .zshrc file has syntax errors
if ! zsh -n ~/.zshrc; then if ! zsh -n ~/.zshrc; then
_omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
command mv -f ~/.zshrc ~/.zshrc.disabled command mv -f ~/.zshrc ~/.zshrc.new
command mv -f ~/.zshrc.swp ~/.zshrc command mv -f ~/.zshrc.bck ~/.zshrc
return 1 return 1
fi fi
...@@ -341,9 +341,9 @@ multi == 1 && /^[^#]*\)/ { ...@@ -341,9 +341,9 @@ multi == 1 && /^[^#]*\)/ {
{ print \$0 } { print \$0 }
" "
awk "$awk_script" ~/.zshrc > ~/.zshrc.enabled \ awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
&& command mv -f ~/.zshrc ~/.zshrc.swp \ && command mv -f ~/.zshrc ~/.zshrc.bck \
&& command mv -f ~/.zshrc.enabled ~/.zshrc && command mv -f ~/.zshrc.new ~/.zshrc
# Exit if the new .zshrc file wasn't created correctly # Exit if the new .zshrc file wasn't created correctly
[[ $? -eq 0 ]] || { [[ $? -eq 0 ]] || {
...@@ -355,8 +355,8 @@ multi == 1 && /^[^#]*\)/ { ...@@ -355,8 +355,8 @@ multi == 1 && /^[^#]*\)/ {
# Exit if the new .zshrc file has syntax errors # Exit if the new .zshrc file has syntax errors
if ! zsh -n ~/.zshrc; then if ! zsh -n ~/.zshrc; then
_omz::log error "broken syntax in ~/.zshrc. Rolling back changes..." _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
command mv -f ~/.zshrc ~/.zshrc.enabled command mv -f ~/.zshrc ~/.zshrc.new
command mv -f ~/.zshrc.swp ~/.zshrc command mv -f ~/.zshrc.bck ~/.zshrc
return 1 return 1
fi fi
...@@ -606,7 +606,8 @@ Usage: omz theme <command> [options] ...@@ -606,7 +606,8 @@ Usage: omz theme <command> [options]
Available commands: Available commands:
list List all available Oh My Zsh themes list List all available Oh My Zsh themes
use <theme> Load an Oh My Zsh theme set <theme> Set a theme in your .zshrc file
use <theme> Load a theme
EOF EOF
return 1 return 1
...@@ -642,6 +643,73 @@ function _omz::theme::list { ...@@ -642,6 +643,73 @@ function _omz::theme::list {
fi fi
} }
function _omz::theme::set {
if [[ -z "$1" ]]; then
echo >&2 "Usage: omz theme set <theme>"
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 <<EOF
ZSH_THEME="$1" # set by \`omz\`
EOF
cat ~/.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 ]] || {
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 { function _omz::theme::use {
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
echo >&2 "Usage: omz theme use <theme>" echo >&2 "Usage: omz theme use <theme>"
...@@ -656,7 +724,7 @@ function _omz::theme::use { ...@@ -656,7 +724,7 @@ function _omz::theme::use {
elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
source "$ZSH/themes/$1.zsh-theme" source "$ZSH/themes/$1.zsh-theme"
else else
_omz::log error "theme '$1' not found" _omz::log error "%B$1%b theme not found"
return 1 return 1
fi fi
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment