2012-07-21 15:43:21 +02:00
|
|
|
#!/usr/bin/env bash
|
2012-02-26 19:52:17 +01:00
|
|
|
|
2012-07-21 16:30:37 +02:00
|
|
|
version=1.1
|
2012-07-19 20:58:58 +02:00
|
|
|
SCRIPTDIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # Get bash script directory
|
2012-02-26 20:21:18 +01:00
|
|
|
pony="*"
|
2012-07-11 17:36:18 +02:00
|
|
|
wrap=""
|
2012-02-26 18:28:03 +01:00
|
|
|
|
2012-07-24 16:34:44 +02:00
|
|
|
SYSTEMPONIES="$(dirname $SCRIPTDIR)/share/ponysay"
|
|
|
|
HOMEPONIES="${HOME}/.local/share/ponysay"
|
|
|
|
|
2012-07-21 18:16:18 +02:00
|
|
|
kmscmd=""
|
|
|
|
[[ "$TERM" = "linux" ]] && kmscmd=$(for c in $(echo $PATH":" | sed -e 's/:/\/ponysay2kmsponysay /g'); do if [[ -f $c ]]; then echo $c; break; fi done)
|
|
|
|
[[ ! "$kmscmd" = "" ]] && TERM="-linux-"
|
|
|
|
|
2012-05-07 16:07:39 +02:00
|
|
|
if [ "$TERM" = "linux" ]; then
|
2012-07-24 16:34:44 +02:00
|
|
|
SYSTEMPONIES="$SYSTEMPONIES/ttyponies"
|
|
|
|
HOMEPONIES="$HOMEPONIES/ttyponies"
|
|
|
|
else
|
|
|
|
SYSTEMPONIES="$SYSTEMPONIES/ponies"
|
|
|
|
HOMEPONIES="$HOMEPONIES/ponies"
|
2012-05-07 16:07:39 +02:00
|
|
|
fi
|
|
|
|
|
2012-07-20 05:10:41 +02:00
|
|
|
if [[ "$PONYSAY_COWSAY" = "" ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
cmd=cowsay
|
|
|
|
customcmd=0
|
2012-07-20 05:10:41 +02:00
|
|
|
else
|
2012-07-20 18:07:59 +02:00
|
|
|
cmd="$PONYSAY_COWSAY"
|
|
|
|
customcmd=1
|
2012-07-20 05:10:41 +02:00
|
|
|
fi
|
2012-07-20 18:07:59 +02:00
|
|
|
|
2012-07-20 05:46:58 +02:00
|
|
|
[[ ${0} == *ponythink ]] &&
|
2012-07-20 18:07:59 +02:00
|
|
|
if [[ "$PONYSAY_COWTHINK" = "" ]]; then
|
2012-07-20 05:46:58 +02:00
|
|
|
cmd=cowthink
|
|
|
|
customcmd=0
|
2012-07-20 18:07:59 +02:00
|
|
|
else
|
2012-07-20 05:46:58 +02:00
|
|
|
cmd="$PONYSAY_COWTHINK"
|
|
|
|
customcmd=1
|
2012-07-20 18:07:59 +02:00
|
|
|
fi
|
2012-02-26 16:47:05 +01:00
|
|
|
|
2012-02-26 20:01:22 +01:00
|
|
|
version() {
|
2012-07-20 18:07:59 +02:00
|
|
|
echo "ponysay v$version"
|
2012-02-26 20:01:22 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 23:55:00 +02:00
|
|
|
scrw=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2`
|
2012-07-19 20:58:58 +02:00
|
|
|
listcmd="${SCRIPTDIR}/../lib/ponysay/list.pl"
|
2012-07-24 14:57:44 +02:00
|
|
|
linklistcmd="${SCRIPTDIR}/../lib/ponysay/linklist.pl"
|
2012-07-19 20:58:58 +02:00
|
|
|
truncatercmd="${SCRIPTDIR}/../lib/ponysay/truncater"
|
2012-07-24 14:57:44 +02:00
|
|
|
[[ -f "./ponysaylist.pl" ]] && listcmd="./ponysaylist.pl"
|
|
|
|
[[ -f "./ponysaylinklist.pl" ]] && linklistcmd="./ponysaylinklist.pl"
|
|
|
|
[[ -f "./ponysaytruncater" ]] && truncatercmd="./ponysaytruncater"
|
2012-07-18 23:55:00 +02:00
|
|
|
|
2012-07-19 03:02:42 +02:00
|
|
|
haspq=1
|
|
|
|
if [[ -f './pq4ps-list' ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
function qlist {
|
|
|
|
./pq4ps-list
|
|
|
|
}
|
2012-07-19 03:02:42 +02:00
|
|
|
elif [[ -f '/usr/bin/pq4ps-list' ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
function qlist {
|
|
|
|
/usr/bin/pq4ps-list
|
|
|
|
}
|
2012-07-19 03:02:42 +02:00
|
|
|
else
|
2012-07-20 18:07:59 +02:00
|
|
|
haspq=0
|
|
|
|
function qlist {
|
|
|
|
cat
|
|
|
|
}
|
2012-07-19 03:02:42 +02:00
|
|
|
fi
|
|
|
|
|
2012-04-05 17:17:13 +02:00
|
|
|
list() {
|
2012-07-20 18:07:59 +02:00
|
|
|
echo -e "\\e[01mponyfiles located in $SYSTEMPONIES:\\e[21m"
|
|
|
|
perl $listcmd $scrw $(ls --color=no $SYSTEMPONIES | sed -e 's/\.pony$//' | sort) | qlist
|
|
|
|
|
|
|
|
if [[ -d $HOMEPONIES ]]; then
|
|
|
|
echo -e "\\e[01mponyfiles located in $HOMEPONIES:\\e[21m"
|
|
|
|
perl $listcmd $scrw $(ls --color=no $HOMEPONIES | sed -e 's/\.pony$//' | sort) | qlist
|
|
|
|
fi
|
2012-07-18 23:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_linklist() {
|
2012-07-20 18:07:59 +02:00
|
|
|
echo -e "\\e[01mponyfiles located in $1:\\e[21m"
|
|
|
|
files=$(ls --color=no $1 | sed -e 's/\.pony$//' | sort)
|
|
|
|
|
|
|
|
args=""
|
|
|
|
|
|
|
|
for file in $files; do
|
|
|
|
target=$(readlink $1"/"$file".pony")
|
|
|
|
|
|
|
|
if [[ $target = "" ]]; then
|
|
|
|
target=$file
|
|
|
|
else
|
|
|
|
target=$(echo $target | sed -e 's/^\.\///g' -e 's/\.pony$//g')
|
|
|
|
fi
|
|
|
|
|
|
|
|
args=$(echo $args $file $target)
|
|
|
|
done
|
|
|
|
|
|
|
|
scrw=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2`
|
|
|
|
|
2012-07-24 14:57:44 +02:00
|
|
|
perl $listcmd $scrw $(perl $linklistcmd $(echo $args) | sed -e 's/ /_/g') | sed -e 's/_/ /g' | qlist
|
2012-07-18 23:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
linklist() {
|
2012-07-20 18:07:59 +02:00
|
|
|
_linklist $SYSTEMPONIES
|
|
|
|
|
|
|
|
if [[ -d $HOMEPONIES ]]; then
|
|
|
|
_linklist $HOMEPONIES
|
|
|
|
fi
|
2012-04-05 17:17:13 +02:00
|
|
|
}
|
|
|
|
|
2012-02-26 20:01:22 +01:00
|
|
|
usage() {
|
2012-07-20 18:07:59 +02:00
|
|
|
version
|
|
|
|
cat <<EOF
|
2012-03-12 02:34:00 +01:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
${0##*/} [options] [message]
|
|
|
|
|
2012-07-04 10:32:12 +02:00
|
|
|
If [message] is not provided, reads the message from STDIN.
|
2012-03-12 02:34:00 +01:00
|
|
|
|
|
|
|
Options:
|
2012-07-04 10:32:12 +02:00
|
|
|
-v Show version and exit.
|
|
|
|
-h Show this help and exit.
|
|
|
|
-l List pony files.
|
2012-07-18 23:55:00 +02:00
|
|
|
-L List pony files with synonyms inside brackets.
|
2012-07-19 03:02:42 +02:00
|
|
|
EOF
|
2012-07-20 18:07:59 +02:00
|
|
|
|
|
|
|
if [[ $haspq = 1 ]]; then
|
|
|
|
cat <<EOF
|
2012-07-19 03:02:42 +02:00
|
|
|
-q Use the pony quote feature.
|
|
|
|
EOF
|
2012-07-20 18:07:59 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
cat <<EOF
|
2012-07-04 10:32:12 +02:00
|
|
|
-f[name] Select a pony (either a file name or a pony name.)
|
|
|
|
-W[column] The screen column where the message should be wrapped.
|
|
|
|
|
2012-07-13 02:18:53 +02:00
|
|
|
See man ponysay(6) for more information.
|
2012-03-12 02:34:00 +01:00
|
|
|
EOF
|
2012-02-26 20:01:22 +01:00
|
|
|
}
|
2012-02-26 16:47:05 +01:00
|
|
|
|
2012-05-21 21:55:31 +02:00
|
|
|
# if no stdin and no arguments then print usage and exit
|
2012-05-08 10:04:26 +02:00
|
|
|
if [[ -t 0 && $# == 0 ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
usage
|
|
|
|
exit
|
2012-05-08 10:04:26 +02:00
|
|
|
fi
|
|
|
|
|
2012-02-26 20:30:25 +01:00
|
|
|
say() {
|
2012-07-20 18:07:59 +02:00
|
|
|
# Ponies use UTF-8 drawing characters. Prevent a Perl warning.
|
|
|
|
export PERL_UNICODE=S
|
|
|
|
|
2012-07-24 18:03:57 +02:00
|
|
|
if [ "$TERM" = "linux" ] || [ "$TERM" = "-linux-" ]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
echo -ne '\e[H\e[2J'
|
2012-07-05 09:14:14 +02:00
|
|
|
fi
|
2012-07-20 18:07:59 +02:00
|
|
|
|
|
|
|
function wtrunc {
|
|
|
|
if [ "$PONYSAY_FULL_WIDTH" = 'yes' ] || [ "$PONYSAY_FULL_WIDTH" = 'y' ] || [ "$PONYSAY_FULL_WIDTH" = '1' ]; then
|
|
|
|
cat
|
|
|
|
else
|
|
|
|
WIDTH=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2`
|
|
|
|
$truncatercmd $WIDTH 2>/dev/null || cat
|
|
|
|
fi
|
2012-07-20 05:10:41 +02:00
|
|
|
}
|
2012-07-20 18:07:59 +02:00
|
|
|
|
|
|
|
if [ "$PONYSAY_SHELL_LINES" = "" ]; then
|
|
|
|
PONYSAY_SHELL_LINES=2
|
|
|
|
fi
|
|
|
|
|
|
|
|
function htrunchead {
|
|
|
|
head --lines=$(( `stty size <&2 | cut -d ' ' -f 1` - $PONYSAY_SHELL_LINES ))
|
|
|
|
}
|
|
|
|
|
|
|
|
function htrunctail {
|
|
|
|
tail --lines=$(( `stty size <&2 | cut -d ' ' -f 1` - $PONYSAY_SHELL_LINES ))
|
2012-07-20 05:10:41 +02:00
|
|
|
}
|
2012-07-20 18:07:59 +02:00
|
|
|
|
|
|
|
if [[ $customcmd = 0 ]]; then
|
|
|
|
function cowcmd {
|
|
|
|
pcmd='#!/usr/bin/perl\nuse utf8;'
|
|
|
|
ccmd=$(for c in $(echo $PATH":" | sed -e 's/:/\/'"$cmd"' /g'); do if [[ -f $c ]]; then echo $c; break; fi done)
|
|
|
|
|
|
|
|
if [[ ${0} == *ponythink ]]; then
|
2012-07-24 16:16:59 +02:00
|
|
|
cat <(echo -e $pcmd) $ccmd > "/tmp/ponythink"
|
|
|
|
perl '/tmp/ponythink' "$@"
|
|
|
|
rm '/tmp/ponythink'
|
2012-07-20 18:07:59 +02:00
|
|
|
else
|
|
|
|
perl <(cat <(echo -e $pcmd) $ccmd) "$@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
else
|
|
|
|
function cowcmd {
|
|
|
|
echo "custom $cmd"
|
|
|
|
$cmd "$@"
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
2012-07-21 18:16:18 +02:00
|
|
|
if [[ "$kmscmd" = "" ]]; then
|
|
|
|
function runcmd {
|
|
|
|
cowcmd -f "$pony" "$@"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
function runcmd {
|
|
|
|
cowcmd -f <($kmscmd "$pony") "$@"
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
2012-07-20 18:07:59 +02:00
|
|
|
if [ "$TERM" = "linux" ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = 'yes' ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = 'y' ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = '1' ]; then
|
|
|
|
if [ "$PONYSAY_BOTTOM" = 'yes' ] || [ "$PONYSAY_BOTTOM" = 'y' ] || [ "$PONYSAY_BOTTOM" = '1' ]; then
|
2012-07-21 18:16:18 +02:00
|
|
|
runcmd "${wrap:+-W$wrap}" | wtrunc | htrunctail
|
2012-07-20 18:07:59 +02:00
|
|
|
else
|
2012-07-21 18:16:18 +02:00
|
|
|
runcmd "${wrap:+-W$wrap}" | wtrunc | htrunchead
|
2012-07-20 18:07:59 +02:00
|
|
|
fi
|
2012-07-03 09:57:53 +02:00
|
|
|
else
|
2012-07-21 18:16:18 +02:00
|
|
|
runcmd "${wrap:+-W$wrap}" | wtrunc
|
2012-07-03 09:57:53 +02:00
|
|
|
fi
|
2012-02-26 20:30:25 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 16:47:57 +02:00
|
|
|
ponies=()
|
|
|
|
|
2012-07-19 03:02:42 +02:00
|
|
|
opts="f:W:Llhv"
|
|
|
|
if [[ $haspq ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
opts=$opts"q"
|
2012-07-19 03:02:42 +02:00
|
|
|
fi
|
2012-07-20 18:07:59 +02:00
|
|
|
|
2012-07-19 03:02:42 +02:00
|
|
|
usepq=0
|
2012-07-20 18:07:59 +02:00
|
|
|
while getopts $opts OPT; do
|
|
|
|
case ${OPT} in
|
|
|
|
v) version; exit ;;
|
|
|
|
h) usage; exit ;;
|
2012-07-21 12:54:34 +02:00
|
|
|
f) ponies+=($(echo $OPTARG | sed -e 's/ //g')) ;;
|
2012-07-20 18:07:59 +02:00
|
|
|
l) list; exit ;;
|
|
|
|
L) linklist; exit ;;
|
|
|
|
W) wrap="$OPTARG" ;;
|
|
|
|
q) usepq=1 ;;
|
|
|
|
\?) usage >&2; exit 1 ;;
|
|
|
|
esac
|
2012-02-26 16:47:05 +01:00
|
|
|
done
|
2012-02-26 20:30:25 +01:00
|
|
|
shift $((OPTIND - 1))
|
2012-02-26 19:52:17 +01:00
|
|
|
|
2012-07-19 03:02:42 +02:00
|
|
|
if [[ $usepq = 1 ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
if [[ -f './pq4ps' ]]; then
|
|
|
|
function q {
|
|
|
|
./pq4ps $@
|
|
|
|
}
|
|
|
|
elif [[ -f '/usr/bin/pq4ps' ]]; then
|
|
|
|
function q {
|
|
|
|
/usr/bin/pq4ps $@
|
|
|
|
}
|
|
|
|
fi
|
2012-07-24 18:03:57 +02:00
|
|
|
[[ "$TERM" = "-linux-" ]] && TERM="linux"
|
2012-07-20 18:07:59 +02:00
|
|
|
$0 ${wrap:+-W$wrap} $(q "$*")
|
|
|
|
exit
|
2012-07-19 03:02:42 +02:00
|
|
|
fi
|
|
|
|
|
2012-07-11 17:36:18 +02:00
|
|
|
hash $cmd &>/dev/null; if [ $? -ne 0 ]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
cat >&2 <<EOF
|
2012-03-12 02:34:00 +01:00
|
|
|
You don't seem to have the $cmd program.
|
|
|
|
Please install it in order to use this wrapper.
|
|
|
|
|
|
|
|
Alternatively, symlink it to '$cmd' in anywhere in \$PATH
|
|
|
|
if it actually exists under a different filename.
|
|
|
|
EOF
|
2012-07-20 18:07:59 +02:00
|
|
|
exit 1
|
2012-03-12 02:34:00 +01:00
|
|
|
fi
|
|
|
|
|
2012-07-18 16:47:57 +02:00
|
|
|
if [[ ! ${#ponies[@]} == 0 ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
pony="${ponies[$RANDOM%${#ponies[@]}]}"
|
2012-07-18 16:47:57 +02:00
|
|
|
fi
|
|
|
|
|
2012-02-26 19:52:17 +01:00
|
|
|
if [[ ! -f $pony ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
# Pony not a file? Search for it
|
|
|
|
|
|
|
|
ponies=()
|
|
|
|
[[ -d $SYSTEMPONIES ]] && ponies+=( "$SYSTEMPONIES"/$pony.pony )
|
|
|
|
[[ -d $HOMEPONIES ]] && ponies+=( "$HOMEPONIES"/$pony.pony )
|
|
|
|
|
|
|
|
if (( ${#ponies} < 1 )); then
|
|
|
|
echo >&2 "All the ponies are missing! Call the Princess!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Choose a random pony
|
|
|
|
pony="${ponies[$RANDOM%${#ponies[@]}]}"
|
2012-02-26 19:52:17 +01:00
|
|
|
fi
|
|
|
|
|
2012-02-26 20:30:25 +01:00
|
|
|
if [[ -n "$*" ]]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
# Handle a message given via arguments
|
|
|
|
say <<<"$*"
|
2012-02-26 20:30:25 +01:00
|
|
|
else
|
2012-07-20 18:07:59 +02:00
|
|
|
say
|
2012-02-26 20:30:25 +01:00
|
|
|
fi
|
2012-07-20 18:07:59 +02:00
|
|
|
|