Add themes in $ZSH_CUSTOM to the pool of candidates
Also add comments and unset leftover variables, and print only the
name of the theme loaded.
When looking for $ZSH_CUSTOM themes, the chosen algorithm is to add
the theme names to the pool disregarding the path, and then source
whatever theme is selected with the same logic as the init script,
which is to source first custom themes even if there is another
default theme of the same name.
Co-authored-by: Mihai Serban <mihai.serban@gmail.com>
Diff
1diff --git a/themes/random.zsh-theme b/themes/random.zsh-theme
2index 7395676620a8781b51f2650a5fdc9d23b4c5603f..92d2a68474e52c488b768c9c7c486d4eece180ac 100644
3--- a/themes/random.zsh-theme
4+++ b/themes/random.zsh-theme
5@@ -1,10 +1,34 @@
6-if [[ "${(t)ZSH_THEME_RANDOM_CANDIDATES}" = "array" ]] && [[ "${#ZSH_THEME_RANDOM_CANDIDATES[@]}" -gt 0 ]]; then
7- themes=($ZSH/themes/${^ZSH_THEME_RANDOM_CANDIDATES}.zsh-theme)
8+# Make themes a unique array
9+typeset -Ua themes
10+
11+if [[ "${(t)ZSH_THEME_RANDOM_CANDIDATES}" = array && ${#ZSH_THEME_RANDOM_CANDIDATES[@]} -gt 0 ]]; then
12+ # Use ZSH_THEME_RANDOM_CANDIDATES if properly defined
13+ themes=($ZSH_THEME_RANDOM_CANDIDATES)
14 else
15- themes=($ZSH/themes/*zsh-theme)
16+ # Look for themes in $ZSH_CUSTOM and $ZSH and add only the theme name (:t)
17+ themes=(
18+ "$ZSH_CUSTOM"/*.zsh-theme(N:t:r)
19+ "$ZSH_CUSTOM"/themes/*.zsh-theme(N:t:r)
20+ "$ZSH"/themes/*.zsh-theme(N:t:r)
21+ )
22 fi
23+
24+# Choose a theme out of the pool of candidates
25 N=${#themes[@]}
26-((N=(RANDOM%N)+1))
27-RANDOM_THEME=${themes[$N]}
28-source "$RANDOM_THEME"
29-echo "[oh-my-zsh] Random theme '$RANDOM_THEME' loaded..."
30+(( N = (RANDOM%N) + 1 ))
31+RANDOM_THEME="${themes[$N]}"
32+unset N themes
33+
34+# Source theme
35+if [[ -f "$ZSH_CUSTOM/$RANDOM_THEME.zsh-theme" ]]; then
36+ source "$ZSH_CUSTOM/$RANDOM_THEME.zsh-theme"
37+elif [[ -f "$ZSH_CUSTOM/themes/$RANDOM_THEME.zsh-theme" ]]; then
38+ source "$ZSH_CUSTOM/themes/$RANDOM_THEME.zsh-theme"
39+elif [[ -f "$ZSH/themes/$RANDOM_THEME.zsh-theme" ]]; then
40+ source "$ZSH/themes/$RANDOM_THEME.zsh-theme"
41+else
42+ echo "[oh-my-zsh] Random theme '${RANDOM_THEME}' not found"
43+ return 1
44+fi
45+
46+echo "[oh-my-zsh] Random theme '${RANDOM_THEME}' loaded"