ponysay/ponysay

142 lines
3.3 KiB
Text
Raw Normal View History

2012-02-26 17:43:26 +01:00
#!/usr/bin/env bash
2012-02-26 19:52:17 +01:00
version=0.9
SYSTEMPONIES=/usr/share/ponysay/ponies
HOMEPONIES="${HOME}/.ponysay/ponies"
pony="*"
2012-02-26 20:12:59 +01:00
wrap=
2012-02-26 18:28:03 +01:00
2012-05-07 16:07:39 +02:00
if [ "$TERM" = "linux" ]; then
SYSTEMPONIES=/usr/share/ponysay/ttyponies
HOMEPONIES="${HOME}/.ponysay/ttyponies"
2012-05-07 16:07:39 +02:00
fi
2012-02-26 18:28:03 +01:00
cmd=cowsay
[[ ${0} == *ponythink ]] && cmd=cowthink
2012-02-26 16:47:05 +01:00
2012-02-26 20:01:22 +01:00
version() {
echo "ponysay v$version"
}
2012-04-05 17:17:13 +02:00
list() {
echo "ponyfiles located in $SYSTEMPONIES:"
ls -1 $SYSTEMPONIES | sed "s/.pony//"
if [[ -d $HOMEPONIES ]]; then
echo "ponyfiles located in $HOMEPONIES:"
ls -1 $HOMEPONIES | sed "s/.pony//"
fi
}
2012-02-26 20:01:22 +01:00
usage() {
version
2012-03-12 02:34:00 +01:00
cat <<EOF
Usage:
${0##*/} [options] [message]
If [message] is not provided, reads the message from STDIN.
2012-03-12 02:34:00 +01:00
Options:
-v Show version and exit.
-h Show this help and exit.
-l List pony files.
-f[name] Select a pony (either a file name or a pony name.)
-W[column] The screen column where the message should be wrapped.
See man ponysay(1) 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
usage
exit
fi
say() {
2012-02-26 20:33:18 +01:00
# Ponies use UTF-8 drawing characters. Prevent a Perl warning.
export PERL_UNICODE=S
2012-05-07 16:07:39 +02:00
if [ "$TERM" = "linux" ]; then
2012-05-08 06:17:09 +02:00
echo -ne '\ec'
2012-05-07 16:07:39 +02:00
fi
function wtrunc
{
if [ "$PONYSAY_FULL_WIDTH" = 'no' ] || [ "$PONYSAY_FULL_WIDTH" = 'n' ] || [ "$PONYSAY_FULL_WIDTH" = '0' ]; then
cat
else
WIDTH=$((stty size <&2 || echo 0 0) | cut -d ' ' -f 2)
ponysaytruncater $WIDTH 2>/dev/null ||
${HOME}/.local/bin/ponysaytruncater $WIDTH 2>/dev/null ||
./ponysaytruncater $WIDTH 2>/dev/null ||
cat
fi
}
function htrunc
{
if [ "$PONYSAY_SHELL_LINES" = "" ]; then
PONYSAY_SHELL_LINES=2
fi
head --lines=$(( $(stty size <&2 | cut -d ' ' -f 1) - $PONYSAY_SHELL_LINES ))
}
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
exec "$cmd" -f "$pony" "${wrap:+-W$wrap}" | wtrunc | tac | htrunc | tac
else
exec "$cmd" -f "$pony" "${wrap:+-W$wrap}" | wtrunc | htrunc
fi
else
exec "$cmd" -f "$pony" "${wrap:+-W$wrap}" | wtrunc
fi
}
2012-04-05 17:17:13 +02:00
while getopts f:W:lhv OPT
2012-02-26 16:47:05 +01:00
do
case ${OPT} in
2012-02-26 20:01:22 +01:00
v) version; exit ;;
h) usage; exit ;;
2012-02-26 20:12:59 +01:00
f) pony="$OPTARG" ;;
2012-04-05 17:17:13 +02:00
l) list; exit ;;
2012-02-26 20:12:59 +01:00
W) wrap="$OPTARG" ;;
2012-02-26 20:07:36 +01:00
\?) usage >&2; exit 1 ;;
2012-02-26 16:47:05 +01:00
esac
done
shift $((OPTIND - 1))
2012-02-26 19:52:17 +01:00
2012-03-12 02:34:00 +01:00
if ! hash $cmd &>/dev/null; then
cat >&2 <<EOF
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
exit 1
fi
2012-02-26 19:52:17 +01:00
if [[ ! -f $pony ]]; then
# Pony not a file? Search for it
2012-02-26 19:52:17 +01:00
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
2012-02-26 19:52:17 +01:00
fi
# Choose a random pony
pony="${ponies[$RANDOM%${#ponies[@]}]}"
fi
if [[ -n "$*" ]]; then
# Handle a message given via arguments
say <<<"$*"
else
say
fi