Browse code

fixed theme chooser + options + list available themes + show all themes

fox authored on 03/08/2011 at 07:42:16
Showing 1 changed files
... ...
@@ -1,4 +1,4 @@
1
-#!/bin/sh
1
+#!/bin/zsh
2 2
 
3 3
 # Zsh Theme Chooser by fox (fox91 at anche dot no)
4 4
 # This program is free software. It comes without any warranty, to
... ...
@@ -9,9 +9,10 @@
9 9
 
10 10
 THEMES_DIR="$ZSH/themes"
11 11
 FAVLIST="${HOME}/.zsh_favlist"
12
+source $ZSH/oh-my-zsh.sh
12 13
 
13 14
 function noyes() {
14
-    read -p "$1 [y/N] " a
15
+    read "a?$1 [y/N] "
15 16
     if [[ $a == "N" || $a == "n" || $a = "" ]]; then
16 17
         return 0
17 18
     fi
... ...
@@ -20,29 +21,76 @@ function noyes() {
20 20
 
21 21
 function theme_preview() {
22 22
     THEME=$1
23
-    THEME_NAME=`echo $THEME | sed s/\.zsh-theme//`
24
-    export ZDOTDIR="$(mktemp -d tmp.zshXXXX)"
25
-
26
-cat <<-EOF >"$ZDOTDIR/.zshrc"
27
-    ZSH_THEME="$THEME_NAME"
28
-    source $ZSH/oh-my-zsh.sh
29
-EOF
30
-    zsh
31
-    rm -rf "$ZDOTDIR"
23
+    THEME_NAME=`echo $THEME | sed s/\.zsh-theme$//`
24
+    print "$fg[blue]${(l.((${COLUMNS}-${#THEME_NAME}-5))..─.)}$reset_color $THEME_NAME $fg[blue]───$reset_color"
25
+    source "$THEMES_DIR/$THEME"
26
+    print -P $PROMPT
27
+}
32 28
 
29
+function banner() {
33 30
     echo
34
-    noyes "Do you want to add it to your favourite list ($FAVLIST)?" || \
35
-          echo $THEME_NAME >> $FAVLIST
31
+    echo "╺━┓┏━┓╻ ╻   ╺┳╸╻ ╻┏━╸┏┳┓┏━╸   ┏━╸╻ ╻┏━┓┏━┓┏━┓┏━╸┏━┓"
32
+    echo "┏━┛┗━┓┣━┫    ┃ ┣━┫┣╸ ┃┃┃┣╸    ┃  ┣━┫┃ ┃┃ ┃┗━┓┣╸ ┣┳┛"
33
+    echo "┗━╸┗━┛╹ ╹    ╹ ╹ ╹┗━╸╹ ╹┗━╸   ┗━╸╹ ╹┗━┛┗━┛┗━┛┗━╸╹┗╸"
36 34
     echo
37 35
 }
38 36
 
39
-echo
40
-echo "╺━┓┏━┓╻ ╻   ╺┳╸╻ ╻┏━╸┏┳┓┏━╸   ┏━╸╻ ╻┏━┓┏━┓┏━┓┏━╸┏━┓"
41
-echo "┏━┛┗━┓┣━┫    ┃ ┣━┫┣╸ ┃┃┃┣╸    ┃  ┣━┫┃ ┃┃ ┃┗━┓┣╸ ┣┳┛"
42
-echo "┗━╸┗━┛╹ ╹    ╹ ╹ ╹┗━╸╹ ╹┗━╸   ┗━╸╹ ╹┗━┛┗━┛┗━┛┗━╸╹┗╸"
43
-echo
37
+function usage() {
38
+    echo "Usage: $0 [options] [theme]"
39
+    echo
40
+    echo "Options"
41
+    echo "  -l   List available themes"
42
+    echo "  -s   Show all themes"
43
+    echo "  -h   Get this help message"
44
+    exit 1
45
+}
46
+
47
+function list_themes() {
48
+    for THEME in $(ls $THEMES_DIR); do
49
+        THEME_NAME=`echo $THEME | sed s/\.zsh-theme$//`
50
+        echo $THEME_NAME
51
+    done
52
+}
53
+
54
+function insert_favlist() {
55
+    if grep -q "$THEME_NAME" $FAVLIST 2> /dev/null ; then
56
+        echo "Already in favlist"
57
+    else
58
+        echo $THEME_NAME >> $FAVLIST
59
+        echo "Saved to favlist"
60
+    fi
61
+
62
+}
44 63
 
45
-for i in $(ls $THEMES_DIR); do
46
-    echo "Now showing theme $i"
47
-    theme_preview $i
64
+function theme_chooser() {
65
+    for THEME in $(ls $THEMES_DIR); do
66
+        echo
67
+        theme_preview $THEME
68
+        echo
69
+        if [[ -z $1 ]]; then
70
+            noyes "Do you want to add it to your favourite list ($FAVLIST)?" || \
71
+                  insert_favlist $THEME_NAME
72
+            echo
73
+        fi
74
+    done
75
+}
76
+
77
+while getopts ":lhs" Option
78
+do
79
+  case $Option in
80
+    l ) list_themes ;;
81
+    s ) theme_chooser 0 ;;
82
+    h ) usage ;;
83
+    * ) usage ;; # Default.
84
+  esac
48 85
 done
86
+
87
+if [[ -z $Option ]]; then
88
+    if [[ -z $1 ]]; then
89
+        banner
90
+        echo
91
+        theme_chooser
92
+    else
93
+        theme_preview $1".zsh-theme"
94
+    fi
95
+fi