76bfa7dd2a5422569c48ceb5429fb31adf1f23c2

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

rand-quote: refactor rand-quote function

- Make function variables local
- Use prompt color sequences
- Use guard clause to check for curl
- Improved syntax

Diff

 1diff --git a/plugins/rand-quote/rand-quote.plugin.zsh b/plugins/rand-quote/rand-quote.plugin.zsh
 2index a64e9b4edc106126d165bf38e1c538b71cf81eea..371b997d389ebca156b857d65d1e02b0fb9d1ea7 100644
 3--- a/plugins/rand-quote/rand-quote.plugin.zsh
 4+++ b/plugins/rand-quote/rand-quote.plugin.zsh
 5@@ -1,26 +1,14 @@
 6-# Get a random quote fron the site http://www.quotationspage.com/random.php
 7-# Created by Eduardo San Martin Morote aka Posva
 8-# http://posva.github.io
 9-# Sun Jun 09 10:59:36 CEST 2013 
10-# Don't remove this header, thank you
11-# Usage: quote
12-
13-WHO_COLOR="\e[0;33m"
14-TEXT_COLOR="\e[0;35m"
15-COLON_COLOR="\e[0;35m"
16-END_COLOR="\e[m"
17-
18-if [[ -x `which curl` ]]; then
19-    function quote()
20-    {
21-        Q=$(curl -s --connect-timeout 2 "http://www.quotationspage.com/random.php" | iconv -c -f ISO-8859-1 -t UTF-8 | grep -m 1 "dt ")
22-        TXT=$(echo "$Q" | sed -e 's/<\/dt>.*//g' -e 's/.*html//g' -e 's/^[^a-zA-Z]*//' -e 's/<\/a..*$//g')
23-        W=$(echo "$Q" | sed -e 's/.*\/quotes\///g' -e 's/<.*//g' -e 's/.*">//g')
24-        if [ "$W" -a "$TXT" ]; then
25-          echo "${WHO_COLOR}${W}${COLON_COLOR}: ${TEXT_COLOR}“${TXT}”${END_COLOR}"
26-        fi
27-    }
28-    #quote
29-else
30+if ! (( $+commands[curl] )); then
31     echo "rand-quote plugin needs curl to work" >&2
32+    return
33 fi
34+
35+function quote {
36+    emulate -L zsh
37+    Q=$(curl -s --connect-timeout 2 "http://www.quotationspage.com/random.php" | iconv -c -f ISO-8859-1 -t UTF-8 | grep -m 1 "dt ")
38+
39+    TXT=$(echo "$Q" | sed -e 's/<\/dt>.*//g' -e 's/.*html//g' -e 's/^[^a-zA-Z]*//' -e 's/<\/a..*$//g')
40+    WHO=$(echo "$Q" | sed -e 's/.*\/quotes\///g' -e 's/<.*//g' -e 's/.*">//g')
41+
42+    [[ -n "$WHO" && -n "$TXT" ]] && print -P "%F{3}${WHO}%f: “%F{5}${TXT}%f”"
43+}