Unverified Commit bc9fe742 authored by Marc Cornellà's avatar Marc Cornellà Committed by GitHub
Browse files

Merge pull request #8651 from mcornella/random-theme-refactor

Add random theme and consolidate logic from init and themes plugin
parents d76258ff 3d4890dc
...@@ -97,19 +97,7 @@ done ...@@ -97,19 +97,7 @@ done
unset config_file unset config_file
# Load the theme # Load the theme
if [[ "$ZSH_THEME" == "random" ]]; then if [ ! "$ZSH_THEME" = "" ]; then
if [[ "${(t)ZSH_THEME_RANDOM_CANDIDATES}" = "array" ]] && [[ "${#ZSH_THEME_RANDOM_CANDIDATES[@]}" -gt 0 ]]; then
themes=($ZSH/themes/${^ZSH_THEME_RANDOM_CANDIDATES}.zsh-theme)
else
themes=($ZSH/themes/*zsh-theme)
fi
N=${#themes[@]}
((N=(RANDOM%N)+1))
RANDOM_THEME=${themes[$N]}
source "$RANDOM_THEME"
echo "[oh-my-zsh] Random theme '$RANDOM_THEME' loaded..."
else
if [ ! "$ZSH_THEME" = "" ]; then
if [ -f "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" ]; then if [ -f "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" ]; then
source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then
...@@ -117,5 +105,4 @@ else ...@@ -117,5 +105,4 @@ else
else else
source "$ZSH/themes/$ZSH_THEME.zsh-theme" source "$ZSH/themes/$ZSH_THEME.zsh-theme"
fi fi
fi
fi fi
#compdef theme
_arguments "1: :($(lstheme | tr "\n" " "))"
function theme function theme {
{ : ${1:=random} # Use random theme if none provided
if [ -z "$1" ] || [ "$1" = "random" ]; then
themes=($ZSH/themes/*zsh-theme) if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
N=${#themes[@]} source "$ZSH_CUSTOM/$1.zsh-theme"
((N=(RANDOM%N)+1)) elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
RANDOM_THEME=${themes[$N]}
source "$RANDOM_THEME"
echo "[oh-my-zsh] Random theme '$RANDOM_THEME' loaded..."
else
if [ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]
then
source "$ZSH_CUSTOM/themes/$1.zsh-theme" source "$ZSH_CUSTOM/themes/$1.zsh-theme"
else elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
source "$ZSH/themes/$1.zsh-theme" source "$ZSH/themes/$1.zsh-theme"
else
echo "$0: Theme '$1' not found"
return 1
fi fi
fi
} }
function lstheme function _theme {
{ _arguments "1: :($(lstheme))"
}
compdef _theme theme
function lstheme {
# Resources: # Resources:
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Modifiers # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Modifiers
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Glob-Qualifiers # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Glob-Qualifiers
print -l {$ZSH,$ZSH_CUSTOM}/themes/*.zsh-theme(N:t:r) print "$ZSH_CUSTOM"/*.zsh-theme(N:t:r) {"$ZSH_CUSTOM","$ZSH"}/themes/*.zsh-theme(N:t:r)
} }
# Make themes a unique array
typeset -Ua themes
if [[ "${(t)ZSH_THEME_RANDOM_CANDIDATES}" = array && ${#ZSH_THEME_RANDOM_CANDIDATES[@]} -gt 0 ]]; then
# Use ZSH_THEME_RANDOM_CANDIDATES if properly defined
themes=($ZSH_THEME_RANDOM_CANDIDATES)
else
# Look for themes in $ZSH_CUSTOM and $ZSH and add only the theme name
themes=(
"$ZSH_CUSTOM"/*.zsh-theme(N:t:r)
"$ZSH_CUSTOM"/themes/*.zsh-theme(N:t:r)
"$ZSH"/themes/*.zsh-theme(N:t:r)
)
# Remove blacklisted themes from the list
for theme in ${ZSH_THEME_RANDOM_BLACKLIST[@]}; do
themes=("${(@)themes:#$theme}")
done
fi
# Choose a theme out of the pool of candidates
N=${#themes[@]}
(( N = (RANDOM%N) + 1 ))
RANDOM_THEME="${themes[$N]}"
unset N themes theme
# Source theme
if [[ -f "$ZSH_CUSTOM/$RANDOM_THEME.zsh-theme" ]]; then
source "$ZSH_CUSTOM/$RANDOM_THEME.zsh-theme"
elif [[ -f "$ZSH_CUSTOM/themes/$RANDOM_THEME.zsh-theme" ]]; then
source "$ZSH_CUSTOM/themes/$RANDOM_THEME.zsh-theme"
elif [[ -f "$ZSH/themes/$RANDOM_THEME.zsh-theme" ]]; then
source "$ZSH/themes/$RANDOM_THEME.zsh-theme"
else
echo "[oh-my-zsh] Random theme '${RANDOM_THEME}' not found"
return 1
fi
echo "[oh-my-zsh] Random theme '${RANDOM_THEME}' loaded"
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