From 5f907817836ea1186120b9165556d7894f2c8481 Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Thu, 19 Feb 2015 23:53:48 -0500 Subject: [PATCH 1/3] plugins/chucknorris: Add automatic updating of the compiled fortune data file. Use local variable for readability, and anonymous function to provide a scope for the local variable. --- plugins/chucknorris/chucknorris.plugin.zsh | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugins/chucknorris/chucknorris.plugin.zsh b/plugins/chucknorris/chucknorris.plugin.zsh index 096364633..fd52f2b78 100644 --- a/plugins/chucknorris/chucknorris.plugin.zsh +++ b/plugins/chucknorris/chucknorris.plugin.zsh @@ -1,6 +1,12 @@ -if [ ! -f $ZSH/plugins/chucknorris/fortunes/chucknorris.dat ]; then - strfile $ZSH/plugins/chucknorris/fortunes/chucknorris $ZSH/plugins/chucknorris/fortunes/chucknorris.dat -fi +() { + # Automatically generate or update Chuck's compiled fortune data file + local fdir=$ZSH/plugins/chucknorris/fortunes + if [[ ! -f $fdir/chucknorris.dat ]] || [[ $fdir/chucknorris.dat -ot $fdir/chucknorris ]]; then + strfile $fdir/chucknorris $fdir/chucknorris.dat + fi + + # Aliases + alias chuck="fortune -a $fdir" + alias chuck_cow="chuck | cowthink" +} -alias chuck="fortune -a $ZSH/plugins/chucknorris/fortunes" -alias chuck_cow="chuck | cowthink" From cb0718c14785de7cf808ef4e2d7ddf5ab3eda8e0 Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Sat, 21 Feb 2015 18:24:07 -0500 Subject: [PATCH 2/3] chucknorris: locate plugin dir using $0 instead of hardcoded $ZSH --- plugins/chucknorris/chucknorris.plugin.zsh | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/plugins/chucknorris/chucknorris.plugin.zsh b/plugins/chucknorris/chucknorris.plugin.zsh index fd52f2b78..1cdc945d1 100644 --- a/plugins/chucknorris/chucknorris.plugin.zsh +++ b/plugins/chucknorris/chucknorris.plugin.zsh @@ -1,12 +1,11 @@ -() { - # Automatically generate or update Chuck's compiled fortune data file - local fdir=$ZSH/plugins/chucknorris/fortunes - if [[ ! -f $fdir/chucknorris.dat ]] || [[ $fdir/chucknorris.dat -ot $fdir/chucknorris ]]; then - strfile $fdir/chucknorris $fdir/chucknorris.dat - fi +# Automatically generate or update Chuck's compiled fortune data file +DIR=${0:h}/fortunes +if [[ ! -f $DIR/chucknorris.dat ]] || [[ $DIR/chucknorris.dat -ot $DIR/chucknorris ]]; then + strfile $DIR/chucknorris $DIR/chucknorris.dat +fi - # Aliases - alias chuck="fortune -a $fdir" - alias chuck_cow="chuck | cowthink" -} +# Aliases +alias chuck="fortune -a $DIR" +alias chuck_cow="chuck | cowthink" +unset DIR \ No newline at end of file From a2e01e9a87c0d120482cc7bbe21ccab21bf71fb1 Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Thu, 2 Jul 2015 20:27:48 -0400 Subject: [PATCH 3/3] chucknorris: check for strfile dependency Gives a better error message than "command not found: strfile", because it's not obvious to many users that strfile is part of fortune. Also handles the weird /usr/sbin install location for strfile on Cygwin. --- plugins/chucknorris/chucknorris.plugin.zsh | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/plugins/chucknorris/chucknorris.plugin.zsh b/plugins/chucknorris/chucknorris.plugin.zsh index 1cdc945d1..1dbb04ef0 100644 --- a/plugins/chucknorris/chucknorris.plugin.zsh +++ b/plugins/chucknorris/chucknorris.plugin.zsh @@ -1,11 +1,28 @@ +# chucknorris: Chuck Norris fortunes + # Automatically generate or update Chuck's compiled fortune data file -DIR=${0:h}/fortunes +# $0 must be used outside a local function. This variable name is unlikly to collide. +CHUCKNORRIS_PLUGIN_DIR=${0:h} + +() { +local DIR=$CHUCKNORRIS_PLUGIN_DIR/fortunes if [[ ! -f $DIR/chucknorris.dat ]] || [[ $DIR/chucknorris.dat -ot $DIR/chucknorris ]]; then - strfile $DIR/chucknorris $DIR/chucknorris.dat + # For some reason, Cygwin puts strfile in /usr/sbin, which is not on the path by default + local strfile=strfile + if ! which strfile &>/dev/null && [[ -f /usr/sbin/strfile ]]; then + strfile=/usr/sbin/strfile + fi + if which $strfile &> /dev/null; then + $strfile $DIR/chucknorris $DIR/chucknorris.dat >/dev/null + else + echo "[oh-my-zsh] chucknorris depends on strfile, which is not installed" >&2 + echo "[oh-my-zsh] strfile is often provided as part of the 'fortune' package" >&2 + fi fi # Aliases alias chuck="fortune -a $DIR" alias chuck_cow="chuck | cowthink" +} -unset DIR \ No newline at end of file +unset CHUCKNORRIS_PLUGIN_DIR