plugins/themes:

Rewrite theme() and lstheme() to remove pwd and variable side effects and make behavior consistent.

Include custom themes in lstheme() output.
Add comments that lay down the function interface.
Look for custom themes in $ZSH_CUSTOM/themes as well as base $ZSH_CUSTOM.
Rewrite theme's random theme selection in terms of lstheme() so random themes are included, and to streamline code.
Add a failure indication to theme() with nonzero return status. When a bad theme name is requested, indicate so by issuing a warning and returning nonzero.
This commit is contained in:
Andrew Janke 2015-02-14 22:25:26 -05:00
commit 2012ae8126

View file

@ -1,24 +1,55 @@
function theme # Load a theme
{ #
if [ -z "$1" ] || [ "$1" = "random" ]; then # Usage:
themes=($ZSH/themes/*zsh-theme) # "theme <name>" loads the named theme
N=${#themes[@]} # "theme random" or "theme" with no argument loads a random theme
((N=(RANDOM%N)+1)) # Return value:
RANDOM_THEME=${themes[$N]} # 0 on success
source "$RANDOM_THEME" # Nonzero on failure, such as requested theme not being found
echo "[oh-my-zsh] Random theme '$RANDOM_THEME' loaded..." function theme() {
if [[ -z "$1" ]] || [[ "$1" = "random" ]]; then
# Select a random theme
local themes n random_theme
themes=($(lstheme))
n=${#themes[@]}
((n=(RANDOM%n)+1))
random_theme=${themes[$n]}
echo "[oh-my-zsh] Loading random theme '$random_theme'..."
theme $random_theme
else else
if [ -f "$ZSH_CUSTOM/$1.zsh-theme" ] # Main case: load named theme
then local name theme_dir found
source "$ZSH_CUSTOM/$1.zsh-theme" name=$1
else found=false
source "$ZSH/themes/$1.zsh-theme" for theme_dir ($ZSH_CUSTOM $ZSH_CUSTOM/themes $ZSH/themes); do
if [[ -f "$theme_dir/$name.zsh-theme" ]]; then
source "$theme_dir/$name.zsh-theme"
found=true
break
fi
done
if [[ $found == false ]]; then
echo "[oh-my-zsh] Theme not found: $name"
return 1
fi fi
fi fi
} }
function lstheme # List available themes by name
{ #
cd $ZSH/themes # The list will always be in sorted order, so you can index in to it.
ls *zsh-theme | sed 's,\.zsh-theme$,,' function lstheme(){
local themes x theme_dir
themes=()
for theme_dir ($ZSH_CUSTOM $ZSH_CUSTOM/themes $ZSH/themes); do
if [[ -d $theme_dir ]]; then
themes+=($(_omz_lstheme_dir $theme_dir))
fi
done
echo ${(F)themes} | sort | uniq
} }
# List themes defined in a given dir
function _omz_lstheme_dir() {
ls $1 | grep '.zsh-theme$' | sed 's,\.zsh-theme$,,'
}