From de1f27a73accf24642399597749781d88a6a4ad6 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 18 Aug 2012 18:56:44 +0200 Subject: cowsay replacement env vars + can get terminal if stdin is piped in --- ponysay.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'ponysay.py') diff --git a/ponysay.py b/ponysay.py index 632445d..1f52558 100755 --- a/ponysay.py +++ b/ponysay.py @@ -86,6 +86,7 @@ parser.add_argument('message', nargs = '?', help = 'message to ponysay') args = parser.parse_args() + ''' This is the mane class of ponysay ''' @@ -178,6 +179,16 @@ class ponysay(): return rc + ''' + Gets the size of the terminal in (rows, columns) + ''' + def __gettermsize(self): + termsize = Popen(['stty', 'size'], stdout=PIPE, stdin=sys.stderr).communicate()[0] + termsize = termsize.decode('utf8', 'replace')[:-1].split(' ') # [:-1] removes a \n + termsize = [int(item) for item in termsize] + return termsize + + ## ## Listing methods ## @@ -186,9 +197,7 @@ class ponysay(): Lists the available ponies ''' def list(self): - termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(' ') - termsize = [int(item) for item in termsize] - + termsize = self.__gettermsize() quoters = self.__quoters() for ponydir in ponydirs: # Loop ponydirs @@ -216,9 +225,7 @@ class ponysay(): Lists the available ponies with alternatives inside brackets ''' def linklist(self): - termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(' ') - termsize = [int(item) for item in termsize] - + termsize = self.__gettermsize() quoters = self.__quoters() for ponydir in ponydirs: # Loop ponydirs @@ -317,6 +324,20 @@ class ponysay(): ## Displaying methods ## + ''' + Returns the cowsay command + ''' + def __getcowsay(self): + isthink = 'think.py' in __file__ + + if isthink: + cowthink = os.environ['PONYSAY_COWTHINK'] if 'PONYSAY_COWTHINK' in os.environ else None + return 'cowthink' if (cowthink is None) or (cowthink == "") else cowthink + + cowsay = os.environ['PONYSAY_COWSAY'] if 'PONYSAY_COWSAY' in os.environ else None + return 'cowsay' if (cowsay is None) or (cowsay == "") else cowsay + + ''' Print the pony with a speech or though bubble ''' @@ -328,12 +349,9 @@ class ponysay(): pony = self.__getponypath(args.pony) - if 'think.py' in __file__: cmd = 'cowthink' - else: cmd = 'cowsay' - 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('\'', '\'\\\'\'') + '\'') + os.system(self.__getcowsay() + (' -W ' + args.wrap if args.wrap is not None else '') + ' -f ' + pony + ' \'' + msg.replace('\'', '\'\\\'\'') + '\'') ''' -- cgit