ponysay/ponysay.sh

82 lines
2.2 KiB
Bash
Raw Normal View History

2012-07-21 15:43:21 +02:00
#!/usr/bin/env bash
2012-02-26 19:52:17 +01:00
2012-08-01 03:33:30 +02:00
scrw=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2` # Screen width
scrh=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 1` # Screen height
# KMS ponies extension
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-07-21 18:16:18 +02:00
2012-05-07 16:07:39 +02:00
2012-08-01 03:33:30 +02:00
# Function for printing the ponies and the message
say() {
2012-08-01 03:33:30 +02:00
# Set PONYSAY_SHELL_LINES to default if not specified
[ "$PONYSAY_SHELL_LINES" = "" ] && PONYSAY_SHELL_LINES=2
2012-08-18 17:57:50 +02:00
2012-08-01 03:33:30 +02:00
# Height trunction, show top
function htrunchead {
2012-08-01 03:33:30 +02:00
head --lines=$(( $scrh - $PONYSAY_SHELL_LINES ))
}
2012-08-18 17:57:50 +02:00
2012-08-01 03:33:30 +02:00
# Height trunction, show bottom
function htrunctail {
2012-08-01 03:33:30 +02:00
tail --lines=$(( $scrh - $PONYSAY_SHELL_LINES ))
}
2012-08-18 17:57:50 +02:00
2012-08-01 03:33:30 +02:00
# Simplification of customisation of cowsay
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
cat <(echo -e $pcmd) $ccmd > "/tmp/ponythink"
perl '/tmp/ponythink' "$@"
rm '/tmp/ponythink'
else
perl <(cat <(echo -e $pcmd) $ccmd) "$@"
fi
}
else
function cowcmd {
$cmd "$@"
}
fi
2012-08-01 03:33:30 +02:00
# KMS ponies support
if [ "$kmscmd" = "" ]; then
2012-07-21 18:16:18 +02:00
function runcmd {
cowcmd -f "$pony" "$@"
}
else
function runcmd {
cowcmd -f <($kmscmd "$pony") "$@"
}
fi
2012-08-01 03:33:30 +02:00
# Print the pony and the message
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
else
2012-07-21 18:16:18 +02:00
runcmd "${wrap:+-W$wrap}" | wtrunc | htrunchead
fi
else
2012-07-21 18:16:18 +02:00
runcmd "${wrap:+-W$wrap}" | wtrunc
fi
}
2012-08-01 03:33:30 +02:00
# Check for cowsay
hash $cmd &>/dev/null; if [ $? -ne 0 ]; then
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
exit 1
2012-03-12 02:34:00 +01:00
fi