rand-quote.plugin.zsh 699 B

123456789101112131415161718192021222324
  1. if ! (( $+commands[curl] )); then
  2. echo "rand-quote plugin needs curl to work" >&2
  3. return
  4. fi
  5. function quote {
  6. setopt localoptions nopromptsubst
  7. # Get random quote data
  8. local data
  9. data="$(command curl -s --connect-timeout 2 "http://www.quotationspage.com/random.php" \
  10. | iconv -c -f ISO-8859-1 -t UTF-8 \
  11. | command grep -a -m 1 'dt class="quote"')"
  12. # Exit if could not fetch random quote
  13. [[ -n "$data" ]] || return 0
  14. local quote author
  15. quote=$(sed -e 's|</dt>.*||g' -e 's|.*html||g' -e 's|^[^a-zA-Z]*||' -e 's|</a..*$||g' <<< "$data")
  16. author=$(sed -e 's|.*/quotes/||g' -e 's|<.*||g' -e 's|.*">||g' <<< "$data")
  17. print -P "%F{3}${author}%f: “%F{5}${quote}%f”"
  18. }