From 6adad5c300a6bfde33b593489cc1c3b645b721e8 Mon Sep 17 00:00:00 2001 From: Willy Weiskopf Date: Wed, 16 Jul 2014 22:21:09 -0600 Subject: [PATCH 1/4] Move random theme functionality into "random" theme The statements for selecting a random theme in oh-my-zsh.sh and the themes plugin are duplicate. Most people eventually settle on a theme, making those lines in oh-my-zsh.sh superfluous. To address those, it may makes sense to put the random theme functionality into a theme of its own (since themes are just zsh scripts. --- oh-my-zsh.sh | 25 ++++++------------------- plugins/themes/themes.plugin.zsh | 24 +++++++++++------------- themes/random.zsh-theme | 10 ++++++++++ 3 files changed, 27 insertions(+), 32 deletions(-) create mode 100644 themes/random.zsh-theme diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index c3fae6efb..30259372c 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -97,25 +97,12 @@ done unset config_file # Load the theme -if [[ "$ZSH_THEME" == "random" ]]; then - if [[ "${(t)ZSH_THEME_RANDOM_CANDIDATES}" = "array" ]] && [[ "${#ZSH_THEME_RANDOM_CANDIDATES[@]}" -gt 0 ]]; then - themes=($ZSH/themes/${^ZSH_THEME_RANDOM_CANDIDATES}.zsh-theme) +if [ ! "$ZSH_THEME" = "" ]; then + if [ -f "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" ]; then + source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" + elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then + source "$ZSH_CUSTOM/themes/$ZSH_THEME.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 - source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" - elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then - source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" - else - source "$ZSH/themes/$ZSH_THEME.zsh-theme" - fi + source "$ZSH/themes/$ZSH_THEME.zsh-theme" fi fi diff --git a/plugins/themes/themes.plugin.zsh b/plugins/themes/themes.plugin.zsh index 2cd0ee327..ac4ccc980 100644 --- a/plugins/themes/themes.plugin.zsh +++ b/plugins/themes/themes.plugin.zsh @@ -1,19 +1,17 @@ function theme { - if [ -z "$1" ] || [ "$1" = "random" ]; then - themes=($ZSH/themes/*zsh-theme) - N=${#themes[@]} - ((N=(RANDOM%N)+1)) - RANDOM_THEME=${themes[$N]} - source "$RANDOM_THEME" - echo "[oh-my-zsh] Random theme '$RANDOM_THEME' loaded..." + if [ -z "$1" ]; then + 1="random" + fi + + if [ -f "$ZSH_CUSTOM/$1.zsh-theme" ] + then + source "$ZSH_CUSTOM/$1.zsh-theme" + elif [ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ] + then + source "$ZSH_CUSTOM/themes/$1.zsh-theme" else - if [ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ] - then - source "$ZSH_CUSTOM/themes/$1.zsh-theme" - else - source "$ZSH/themes/$1.zsh-theme" - fi + source "$ZSH/themes/$1.zsh-theme" fi } diff --git a/themes/random.zsh-theme b/themes/random.zsh-theme new file mode 100644 index 000000000..739567662 --- /dev/null +++ b/themes/random.zsh-theme @@ -0,0 +1,10 @@ +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..." From b297bf92964e04e24f960f4e38acdb9b740d2d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Wed, 19 Feb 2020 18:42:05 +0100 Subject: [PATCH 2/4] 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 --- themes/random.zsh-theme | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/themes/random.zsh-theme b/themes/random.zsh-theme index 739567662..92d2a6847 100644 --- a/themes/random.zsh-theme +++ b/themes/random.zsh-theme @@ -1,10 +1,34 @@ -if [[ "${(t)ZSH_THEME_RANDOM_CANDIDATES}" = "array" ]] && [[ "${#ZSH_THEME_RANDOM_CANDIDATES[@]}" -gt 0 ]]; then - themes=($ZSH/themes/${^ZSH_THEME_RANDOM_CANDIDATES}.zsh-theme) +# 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 - themes=($ZSH/themes/*zsh-theme) + # Look for themes in $ZSH_CUSTOM and $ZSH and add only the theme name (:t) + themes=( + "$ZSH_CUSTOM"/*.zsh-theme(N:t:r) + "$ZSH_CUSTOM"/themes/*.zsh-theme(N:t:r) + "$ZSH"/themes/*.zsh-theme(N:t:r) + ) fi + +# Choose a theme out of the pool of candidates N=${#themes[@]} -((N=(RANDOM%N)+1)) -RANDOM_THEME=${themes[$N]} -source "$RANDOM_THEME" -echo "[oh-my-zsh] Random theme '$RANDOM_THEME' loaded..." +(( N = (RANDOM%N) + 1 )) +RANDOM_THEME="${themes[$N]}" +unset N themes + +# 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" From f4b4a446aca37987bc2612d6115d156417628364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Wed, 19 Feb 2020 19:32:28 +0100 Subject: [PATCH 3/4] Polish themes plugin and error out if theme not found --- plugins/themes/_theme | 3 --- plugins/themes/themes.plugin.zsh | 29 ++++++++++++++++------------- 2 files changed, 16 insertions(+), 16 deletions(-) delete mode 100644 plugins/themes/_theme diff --git a/plugins/themes/_theme b/plugins/themes/_theme deleted file mode 100644 index 8214ddb0d..000000000 --- a/plugins/themes/_theme +++ /dev/null @@ -1,3 +0,0 @@ -#compdef theme - -_arguments "1: :($(lstheme | tr "\n" " "))" diff --git a/plugins/themes/themes.plugin.zsh b/plugins/themes/themes.plugin.zsh index ac4ccc980..1fbde5b64 100644 --- a/plugins/themes/themes.plugin.zsh +++ b/plugins/themes/themes.plugin.zsh @@ -1,24 +1,27 @@ -function theme -{ - if [ -z "$1" ]; then - 1="random" - fi +function theme { + : ${1:=random} # Use random theme if none provided - if [ -f "$ZSH_CUSTOM/$1.zsh-theme" ] - then + if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then source "$ZSH_CUSTOM/$1.zsh-theme" - elif [ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ] - then + elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then source "$ZSH_CUSTOM/themes/$1.zsh-theme" - else + elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then source "$ZSH/themes/$1.zsh-theme" + else + echo "$0: Theme '$1' not found" + return 1 fi } -function lstheme -{ +function _theme { + _arguments "1: :($(lstheme))" +} + +compdef _theme theme + +function lstheme { # Resources: # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Modifiers # 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) } From 3d4890dcc07478e7129de1e79afedafd3f08ffbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Wed, 19 Feb 2020 19:53:37 +0100 Subject: [PATCH 4/4] Add blacklist variable for random theme Co-authored-by: Fran Garcia --- themes/random.zsh-theme | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/themes/random.zsh-theme b/themes/random.zsh-theme index 92d2a6847..43f6cbb60 100644 --- a/themes/random.zsh-theme +++ b/themes/random.zsh-theme @@ -5,19 +5,23 @@ if [[ "${(t)ZSH_THEME_RANDOM_CANDIDATES}" = array && ${#ZSH_THEME_RANDOM_CANDIDA # 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 (:t) + # 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 +unset N themes theme # Source theme if [[ -f "$ZSH_CUSTOM/$RANDOM_THEME.zsh-theme" ]]; then