ponyquotes support

This commit is contained in:
Mattias Andrée 2012-08-18 18:24:00 +02:00
parent 57ceac45a4
commit 88baa565a0
3 changed files with 29 additions and 60 deletions

View file

@ -71,7 +71,7 @@ for quotedir in _quotedirs:
''' '''
Argument parsing Argument parsing
''' '''
parser = argparse.ArgumentParser(description = 'Ponysay, like cowsay with ponies') parser = argparse.ArgumentParser(prog = 'ponysay', description = 'Like cowsay with ponies.')
parser.add_argument('-v', '--version', action = 'version', version = '%s %s' % ('ponysay', VERSION)) parser.add_argument('-v', '--version', action = 'version', version = '%s %s' % ('ponysay', VERSION))
parser.add_argument('-l', '--list', action = 'store_true', dest = 'list', help = 'list pony files') parser.add_argument('-l', '--list', action = 'store_true', dest = 'list', help = 'list pony files')
@ -80,6 +80,7 @@ parser.add_argument( '--quoters', action = 'store_true', dest = 'quoters',
parser.add_argument( '--onelist', action = 'store_true', dest = 'onelist', help = 'list pony files in one columns') # for shell completions parser.add_argument( '--onelist', action = 'store_true', dest = 'onelist', help = 'list pony files in one columns') # for shell completions
parser.add_argument('-W', '--wrap', action = 'store', dest = 'wrap', help = 'specify the column when the message should be wrapped') parser.add_argument('-W', '--wrap', action = 'store', dest = 'wrap', help = 'specify the column when the message should be wrapped')
parser.add_argument('-f', '--pony', action = 'append', dest = 'pony', help = 'select a pony (either a file name or a pony name)') parser.add_argument('-f', '--pony', action = 'append', dest = 'pony', help = 'select a pony (either a file name or a pony name)')
parser.add_argument('-q', '--quote', nargs = '*', dest = 'quote', help = 'select a pony which will quote herself')
parser.add_argument('message', nargs = '?', help = 'message to ponysay') parser.add_argument('message', nargs = '?', help = 'message to ponysay')
args = parser.parse_args() args = parser.parse_args()
@ -97,6 +98,7 @@ class ponysay():
elif args.linklist: self.linklist() elif args.linklist: self.linklist()
elif args.quoters: self.quoters() elif args.quoters: self.quoters()
elif args.onelist: self.onelist() elif args.onelist: self.onelist()
elif args.quote: self.quote(args)
else: self.print_pony(args) else: self.print_pony(args)
@ -334,6 +336,32 @@ class ponysay():
os.system(cmd + (' -W ' + args.wrap if args.wrap is not None else '') + ' -f ' + pony + ' \'' + msg.replace('\'', '\'\\\'\'') + '\'') os.system(cmd + (' -W ' + args.wrap if args.wrap is not None else '') + ' -f ' + pony + ' \'' + msg.replace('\'', '\'\\\'\'') + '\'')
'''
Print the pony with a speech or though bubble and a self quote
'''
def quote(self, args):
pairs = self.__quotes()
if len(args.quote) > 0:
ponyset = set(args.quote)
alts = []
for pair in pairs:
if pair[0] in ponyset:
alts.append(pair)
pairs = alts
pair = pairs[random.randrange(0, len(pairs))]
qfile = None
try:
qfile = open(pair[1], 'r')
args.message = '\n'.join(qfile.readlines())
finally:
if qfile is not None:
qfile.close()
args.pony = [pair[0]]
self.print_pony(args)
''' '''

View file

@ -5,11 +5,7 @@
INSTALLDIR="$(dirname $( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))" INSTALLDIR="$(dirname $( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))"
# Subscripts # Subscripts
listcmd="$INSTALLDIR/lib/ponysay/list.pl"
linklistcmd="$INSTALLDIR/lib/ponysay/linklist.pl"
truncatercmd="$INSTALLDIR/lib/ponysay/truncater" truncatercmd="$INSTALLDIR/lib/ponysay/truncater"
quotecmd="$INSTALLDIR/lib/ponysay/pq4ps"
qlistcmd="$INSTALLDIR/lib/ponysay/pq4ps-list.pl"
pony="*" # Selected pony pony="*" # Selected pony
wrap="" # Message wrap column wrap="" # Message wrap column
@ -44,13 +40,6 @@ else
fi fi
# Pony quotes
ponyquotes() {
[ "$TERM" = "-linux-" ] && TERM="linux"
"$0" ${wrap:+-W$wrap} $("$quotecmd" $@)
}
# Function for printing the ponies and the message # Function for printing the ponies and the message
say() { say() {
# Set PONYSAY_SHELL_LINES to default if not specified # Set PONYSAY_SHELL_LINES to default if not specified
@ -119,15 +108,6 @@ fi
# Parse options
while getopts "f:W:Llhvq" OPT; do
case ${OPT} in
q) shift $((OPTIND - 1)); ponyquotes "$*"; exit ;;
\?) usage >&2; exit 1 ;;
esac
done
shift $((OPTIND - 1))
# Check for cowsay # Check for cowsay
hash $cmd &>/dev/null; if [ $? -ne 0 ]; then hash $cmd &>/dev/null; if [ $? -ne 0 ]; then

39
pq4ps
View file

@ -1,39 +0,0 @@
#!/usr/bin/env bash
INSTALLDIR="$(dirname "$(dirname "$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )")")" # Get main bash script directory's parent
if [ $# == 1 ] && ([ "$1" == '-l' ] || [ "$1" == '--list' ]); then
perl "$0.pl" "$INSTALLDIR" | cut -d @ -f 1 | uniq
else
_ponies="$(perl "$0.pl" "$INSTALLDIR")"
ponies=()
if (( $# > 0 )); then
p=()
for arg in "$@"; do
p+="$(echo "$_ponies" | grep "^$arg@") "
done
_ponies=$p
fi
ponies=( $_ponies )
if (( ${#ponies[@]} == 0 )); then
ponies=()
for arg in "$@"; do
ponies+=( "$arg" )
done
p="${ponies[$RANDOM%${#ponies[@]}]}"
q='I am totally speechless'
echo "-f" $p $q
else
pony="${ponies[$RANDOM%${#ponies[@]}]}"
p="$(echo $pony | cut -d '@' -f 1)"
f="$(echo $pony | cut -d '@' -f 2)"
q="$(cat "$INSTALLDIR/share/ponysay/quotes/$f")"
echo "-f" $p $q
fi
fi