From 88baa565a0be9f623d0aa52c0d3148c234b51516 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 18 Aug 2012 18:24:00 +0200 Subject: ponyquotes support --- ponysay.py | 30 +++++++++++++++++++++++++++++- ponysay.sh | 20 -------------------- pq4ps | 39 --------------------------------------- 3 files changed, 29 insertions(+), 60 deletions(-) delete mode 100755 pq4ps diff --git a/ponysay.py b/ponysay.py index a55fbca..632445d 100755 --- a/ponysay.py +++ b/ponysay.py @@ -71,7 +71,7 @@ for quotedir in _quotedirs: ''' 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('-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('-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('-q', '--quote', nargs = '*', dest = 'quote', help = 'select a pony which will quote herself') parser.add_argument('message', nargs = '?', help = 'message to ponysay') args = parser.parse_args() @@ -97,6 +98,7 @@ class ponysay(): elif args.linklist: self.linklist() elif args.quoters: self.quoters() elif args.onelist: self.onelist() + elif args.quote: self.quote(args) else: self.print_pony(args) @@ -332,6 +334,32 @@ class ponysay(): if linuxvt: print('\033[H\033[2J', end='') 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) diff --git a/ponysay.sh b/ponysay.sh index 27c0dce..e390f09 100755 --- a/ponysay.sh +++ b/ponysay.sh @@ -5,11 +5,7 @@ INSTALLDIR="$(dirname $( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))" # Subscripts -listcmd="$INSTALLDIR/lib/ponysay/list.pl" -linklistcmd="$INSTALLDIR/lib/ponysay/linklist.pl" truncatercmd="$INSTALLDIR/lib/ponysay/truncater" -quotecmd="$INSTALLDIR/lib/ponysay/pq4ps" -qlistcmd="$INSTALLDIR/lib/ponysay/pq4ps-list.pl" pony="*" # Selected pony wrap="" # Message wrap column @@ -44,13 +40,6 @@ else fi - -# Pony quotes -ponyquotes() { - [ "$TERM" = "-linux-" ] && TERM="linux" - "$0" ${wrap:+-W$wrap} $("$quotecmd" $@) -} - # Function for printing the ponies and the message say() { # 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 hash $cmd &>/dev/null; if [ $? -ne 0 ]; then diff --git a/pq4ps b/pq4ps deleted file mode 100755 index 576a4e8..0000000 --- a/pq4ps +++ /dev/null @@ -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 -- cgit