From 53c9661b003b91e8af356c3ecbb95a025ffcd412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 5 Oct 2021 16:45:49 +0200 Subject: [PATCH] refactor(chucknorris): refresh plugin code --- plugins/chucknorris/README.md | 4 +-- plugins/chucknorris/chucknorris.plugin.zsh | 38 ++++++++++------------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/plugins/chucknorris/README.md b/plugins/chucknorris/README.md index 35f989180..b51875de9 100644 --- a/plugins/chucknorris/README.md +++ b/plugins/chucknorris/README.md @@ -1,8 +1,6 @@ # chucknorris -Chuck Norris fortunes plugin for oh-my-zsh. Perfectly suitable as MOTD. - -**Maintainers**: [apjanke](https://github.com/apjanke) [maff](https://github.com/maff) +Chuck Norris fortunes plugin for Oh My Zsh. Perfectly suitable as MOTD. To use it add `chucknorris` to the plugins array in you zshrc file. diff --git a/plugins/chucknorris/chucknorris.plugin.zsh b/plugins/chucknorris/chucknorris.plugin.zsh index 1dbb04ef0..b90abe320 100644 --- a/plugins/chucknorris/chucknorris.plugin.zsh +++ b/plugins/chucknorris/chucknorris.plugin.zsh @@ -1,28 +1,24 @@ -# chucknorris: Chuck Norris fortunes - -# Automatically generate or update Chuck's compiled fortune data file -# $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 - # 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 + # %x: name of file containing code being executed + local fortunes_dir="${${(%):-%x}:h}/fortunes" + + # Aliases + alias chuck="fortune -a $fortunes_dir" + alias chuck_cow="chuck | cowthink" + + # Automatically generate or update Chuck's compiled fortune data file + if [[ "$fortunes_dir/chucknorris" -ot "$fortunes_dir/chucknorris.dat" ]]; then + return fi - if which $strfile &> /dev/null; then - $strfile $DIR/chucknorris $DIR/chucknorris.dat >/dev/null - else + + # For some reason, Cygwin puts strfile in /usr/sbin, which is not on the path by default + local strfile="${commands[strfile]:-/usr/sbin/strfile}" + if [[ ! -x "$strfile" ]]; then 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 + return fi -fi -# Aliases -alias chuck="fortune -a $DIR" -alias chuck_cow="chuck | cowthink" + # Generate the compiled fortune data file + $strfile "$fortunes_dir/chucknorris" "$fortunes_dir/chucknorris.dat" >/dev/null } - -unset CHUCKNORRIS_PLUGIN_DIR