aboutsummaryrefslogtreecommitdiff
path: root/ponysay.py
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2012-08-18 18:56:44 +0200
committerMattias Andrée <maandree@operamail.com>2012-08-18 18:56:44 +0200
commitde1f27a73accf24642399597749781d88a6a4ad6 (patch)
tree96f00b499acf2ddcd10fd420dc6c3295fc899f31 /ponysay.py
parent88baa565a0be9f623d0aa52c0d3148c234b51516 (diff)
downloadponysay-de1f27a73accf24642399597749781d88a6a4ad6.tar.gz
ponysay-de1f27a73accf24642399597749781d88a6a4ad6.tar.bz2
ponysay-de1f27a73accf24642399597749781d88a6a4ad6.zip
cowsay replacement env vars + can get terminal if stdin is piped in
Diffstat (limited to 'ponysay.py')
-rwxr-xr-xponysay.py38
1 files changed, 28 insertions, 10 deletions
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
@@ -318,6 +325,20 @@ class ponysay():
##
'''
+ 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
'''
def print_pony(self, args):
@@ -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('\'', '\'\\\'\'') + '\'')
'''