2012-09-29 22:04:37 +02:00
|
|
|
|
#!/usr/bin/env python3
|
2012-08-20 02:22:12 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2012-08-18 15:25:20 +02:00
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
2012-10-30 01:31:17 +01:00
|
|
|
|
ponysay - Ponysay, cowsay reimplementation for ponies
|
2012-08-25 04:36:47 +02:00
|
|
|
|
Copyright (C) 2012 Erkin Batu Altunbaş et al.
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-08-25 04:36:47 +02:00
|
|
|
|
This program is free software. It comes without any warranty, to
|
|
|
|
|
the extent permitted by applicable law. You can redistribute it
|
|
|
|
|
and/or modify it under the terms of the Do What The Fuck You Want
|
|
|
|
|
To Public License, Version 2, as published by Sam Hocevar. See
|
|
|
|
|
http://sam.zoy.org/wtfpl/COPYING for more details.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Authors of ponysay.py:
|
|
|
|
|
|
|
|
|
|
Erkin Batu Altunbaş: Project leader, helped write the first implementation
|
2012-08-20 02:22:12 +02:00
|
|
|
|
Mattias "maandree" Andrée: Major contributor of both implementions
|
|
|
|
|
Elis "etu" Axelsson: Major contributor of current implemention and patcher of the first implementation
|
|
|
|
|
Sven-Hendrik "svenstaro" Haase: Major contributor of the first implementation
|
|
|
|
|
Jan Alexander "heftig" Steffens: Major contributor of the first implementation
|
|
|
|
|
Kyah "L-four" Rindlisbacher: Patched the first implementation
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
import os
|
2012-08-21 19:37:51 +02:00
|
|
|
|
import shutil
|
2012-08-20 02:22:12 +02:00
|
|
|
|
import sys
|
|
|
|
|
import random
|
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
The version of ponysay
|
|
|
|
|
'''
|
2012-08-26 06:17:13 +02:00
|
|
|
|
VERSION = 'dev' # this line should not be edited, it is fixed by the build system
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-27 02:31:14 +02:00
|
|
|
|
|
|
|
|
|
'''
|
2012-10-12 12:11:24 +02:00
|
|
|
|
Hack to enforce UTF-8 in output (in the future, if you see anypony not using utf-8 in
|
|
|
|
|
programs by default, report them to Princess Celestia so she can banish them to the moon)
|
|
|
|
|
|
|
|
|
|
@param text:str The text to print (empty string is default)
|
|
|
|
|
@param end:str The appendix to the text to print (line breaking is default)
|
2012-08-27 02:31:14 +02:00
|
|
|
|
'''
|
2012-08-27 02:32:47 +02:00
|
|
|
|
def print(text = '', end = '\n'):
|
2012-08-27 02:40:56 +02:00
|
|
|
|
sys.stdout.buffer.write((str(text) + end).encode('utf-8'))
|
2012-08-27 02:31:14 +02:00
|
|
|
|
|
2012-10-22 14:40:20 +02:00
|
|
|
|
'''
|
|
|
|
|
stderr equivalent to print()
|
|
|
|
|
|
|
|
|
|
@param text:str The text to print (empty string is default)
|
|
|
|
|
@param end:str The appendix to the text to print (line breaking is default)
|
|
|
|
|
'''
|
|
|
|
|
def printerr(text = '', end = '\n'):
|
|
|
|
|
sys.stderr.buffer.write((str(text) + end).encode('utf-8'))
|
2012-08-27 02:31:14 +02:00
|
|
|
|
|
2012-10-23 23:10:35 +02:00
|
|
|
|
fd3 = None
|
|
|
|
|
'''
|
|
|
|
|
/proc/self/fd/3 equivalent to print()
|
|
|
|
|
|
|
|
|
|
@param text:str The text to print (empty string is default)
|
|
|
|
|
@param end:str The appendix to the text to print (line breaking is default)
|
|
|
|
|
'''
|
|
|
|
|
def printinfo(text = '', end = '\n'):
|
|
|
|
|
global fd3
|
|
|
|
|
if os.path.exists('/proc/self/fd/3'):
|
|
|
|
|
if fd3 is None:
|
|
|
|
|
fd3 = os.fdopen(3, 'w')
|
|
|
|
|
fd3.write(str(text) + end)
|
|
|
|
|
|
|
|
|
|
|
2012-09-30 02:55:48 +02:00
|
|
|
|
'''
|
|
|
|
|
Checks whether a text ends with a specific text, but has more
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param text The text to test
|
|
|
|
|
@param ending The desired end of the text
|
|
|
|
|
@return :bool The result of the test
|
2012-09-30 02:55:48 +02:00
|
|
|
|
'''
|
|
|
|
|
def endswith(text, ending):
|
2012-10-25 00:06:30 +02:00
|
|
|
|
return text.endswith(ending) and not (text == ending)
|
2012-09-30 02:55:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
This is the mane class of ponysay
|
|
|
|
|
'''
|
|
|
|
|
class Ponysay():
|
2012-10-30 01:31:17 +01:00
|
|
|
|
'''
|
|
|
|
|
Constructor
|
|
|
|
|
'''
|
|
|
|
|
def __init__(self):
|
|
|
|
|
'''
|
|
|
|
|
The user's home directory
|
|
|
|
|
'''
|
|
|
|
|
self.HOME = os.environ['HOME'] if 'HOME' in os.environ else os.path.expanduser('~')
|
|
|
|
|
|
|
|
|
|
## Change system enviroments with ~/.ponysayrc
|
|
|
|
|
if os.path.exists(self.HOME + '/.ponysayrc'):
|
|
|
|
|
with open(self.HOME + '/.ponysayrc', 'rb') as ponysayrc:
|
|
|
|
|
code = ponysayrc.read().decode('utf8', 'replace') + '\n'
|
|
|
|
|
env = os.environ
|
|
|
|
|
code = compile(code, self.HOME + '/.ponysayrc', 'exec')
|
|
|
|
|
exec(code)
|
|
|
|
|
|
|
|
|
|
self.HOME = os.environ['HOME'] if 'HOME' in os.environ else os.path.expanduser('~') # in case ~/.ponysayrc changes it
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Whether any unrecognised options was parsed, this should be set by the invoker before run()
|
|
|
|
|
'''
|
|
|
|
|
self.unrecognised = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Whether the program is execute in Linux VT (TTY)
|
|
|
|
|
'''
|
|
|
|
|
self.linuxvt = ('TERM' in os.environ) and (os.environ['TERM'] == 'linux')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Whether the script is executed as ponythink
|
|
|
|
|
'''
|
|
|
|
|
self.isthink = (len(__file__) >= len('think')) and (__file__.endswith('think'))
|
|
|
|
|
self.isthink = ((len(__file__) >= len('think.py')) and (__file__.endswith('think.py'))) or self.isthink
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Whether stdin is piped
|
|
|
|
|
'''
|
|
|
|
|
self.pipelinein = not sys.stdin.isatty()
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Whether stdout is piped
|
|
|
|
|
'''
|
|
|
|
|
self.pipelineout = not sys.stdout.isatty()
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Whether stderr is piped
|
|
|
|
|
'''
|
|
|
|
|
self.pipelineerr = not sys.stderr.isatty()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Whether KMS is used
|
|
|
|
|
'''
|
|
|
|
|
self.usekms = self.isUsingKMS()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Mode string that modifies or adds $ variables in the pony image
|
|
|
|
|
'''
|
|
|
|
|
self.mode = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
The directories where pony files are stored, ttyponies/ are used if the terminal is Linux VT (also known as TTY) and not with KMS
|
|
|
|
|
'''
|
|
|
|
|
appendset = set()
|
|
|
|
|
self.xponydirs = []
|
|
|
|
|
_ponydirs = [self.HOME + '/.local/share/ponysay/ponies/', '/usr/share/ponysay/ponies/']
|
|
|
|
|
for ponydir in _ponydirs:
|
|
|
|
|
if os.path.isdir(ponydir) and (ponydir not in appendset):
|
|
|
|
|
self.xponydirs.append(ponydir)
|
|
|
|
|
appendset.add(ponydir)
|
|
|
|
|
appendset = set()
|
|
|
|
|
self.vtponydirs = []
|
|
|
|
|
_ponydirs = [self.HOME + '/.local/share/ponysay/ttyponies/', '/usr/share/ponysay/ttyponies/']
|
|
|
|
|
for ponydir in _ponydirs:
|
|
|
|
|
if os.path.isdir(ponydir) and (ponydir not in appendset):
|
|
|
|
|
self.vtponydirs.append(ponydir)
|
|
|
|
|
appendset.add(ponydir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
The directories where pony files are stored, extrattyponies/ are used if the terminal is Linux VT (also known as TTY) and not with KMS
|
|
|
|
|
'''
|
|
|
|
|
appendset = set()
|
|
|
|
|
self.extraxponydirs = []
|
|
|
|
|
_extraponydirs = [self.HOME + '/.local/share/ponysay/extraponies/', '/usr/share/ponysay/extraponies/']
|
|
|
|
|
for extraponydir in _extraponydirs:
|
|
|
|
|
if os.path.isdir(extraponydir) and (extraponydir not in appendset):
|
|
|
|
|
self.extraxponydirs.append(extraponydir)
|
|
|
|
|
appendset.add(extraponydir)
|
|
|
|
|
appendset = set()
|
|
|
|
|
self.extravtponydirs = []
|
|
|
|
|
_extraponydirs = [self.HOME + '/.local/share/ponysay/extrattyponies/', '/usr/share/ponysay/extrattyponies/']
|
|
|
|
|
for extraponydir in _extraponydirs:
|
|
|
|
|
if os.path.isdir(extraponydir) and (extraponydir not in appendset):
|
|
|
|
|
self.extravtponydirs.append(extraponydir)
|
|
|
|
|
appendset.add(extraponydir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
The directories where quotes files are stored
|
|
|
|
|
'''
|
|
|
|
|
appendset = set()
|
|
|
|
|
self.quotedirs = []
|
|
|
|
|
_quotedirs = [self.HOME + '/.local/share/ponysay/quotes/', '/usr/share/ponysay/quotes/']
|
|
|
|
|
for quotedir in _quotedirs:
|
|
|
|
|
if os.path.isdir(quotedir) and (quotedir not in appendset):
|
|
|
|
|
self.quotedirs.append(quotedir)
|
|
|
|
|
appendset.add(quotedir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
The directories where balloon style files are stored
|
|
|
|
|
'''
|
|
|
|
|
appendset = set()
|
|
|
|
|
self.balloondirs = []
|
|
|
|
|
_balloondirs = [self.HOME + '/.local/share/ponysay/balloons/', '/usr/share/ponysay/balloons/']
|
|
|
|
|
for balloondir in _balloondirs:
|
|
|
|
|
if os.path.isdir(balloondir) and (balloondir not in appendset):
|
|
|
|
|
self.balloondirs.append(balloondir)
|
|
|
|
|
appendset.add(balloondir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
ucsmap files
|
|
|
|
|
'''
|
|
|
|
|
appendset = set()
|
|
|
|
|
self.ucsmaps = []
|
|
|
|
|
_ucsmaps = [self.HOME + '/.local/share/ponysay/ucsmap', '/usr/share/ponysay/ucsmap']
|
|
|
|
|
for ucsmap in _ucsmaps:
|
|
|
|
|
if os.path.isdir(ucsmap) and (ucsmap not in appendset):
|
|
|
|
|
self.ucsmaps.append(ucsmap)
|
|
|
|
|
appendset.add(ucsmap)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
Starts the part of the program the arguments indicate
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param args:ArgParser Parsed command line arguments
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
2012-10-30 01:31:17 +01:00
|
|
|
|
def run(self, args):
|
|
|
|
|
if (args.argcount == 0) and not self.pipelinein:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
args.help()
|
2012-10-22 15:04:49 +02:00
|
|
|
|
exit(254)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
return
|
|
|
|
|
|
2012-10-11 23:24:40 +02:00
|
|
|
|
## Emulate termial capabilities
|
2012-10-30 01:31:17 +01:00
|
|
|
|
if args.opts['-X'] is not None: (self.linuxvt, self.usekms) = (False, False)
|
|
|
|
|
elif args.opts['-V'] is not None: (self.linuxvt, self.usekms) = (True, False)
|
|
|
|
|
elif args.opts['-K'] is not None: (self.linuxvt, self.usekms) = (True, True)
|
|
|
|
|
self.ponydirs = self.vtponydirs if self.linuxvt and not self.usekms else self.xponydirs
|
|
|
|
|
self.extraponydirs = self.extravtponydirs if self.linuxvt and not self.usekms else self.extraxponydirs
|
2012-10-11 23:24:40 +02:00
|
|
|
|
|
2012-10-28 03:33:14 +01:00
|
|
|
|
## Variadic variants of -f, +f and -q
|
2012-10-22 14:40:20 +02:00
|
|
|
|
if args.opts['--f'] is not None:
|
|
|
|
|
if args.opts['-f'] is not None: args.opts['-f'] += args.opts['--f']
|
|
|
|
|
else: args.opts['-f'] = args.opts['--f']
|
2012-10-28 03:33:14 +01:00
|
|
|
|
if args.opts['++f'] is not None:
|
|
|
|
|
if args.opts['+f'] is not None: args.opts['+f'] += args.opts['++f']
|
|
|
|
|
else: args.opts['+f'] = args.opts['++F']
|
2012-10-22 14:40:20 +02:00
|
|
|
|
if args.opts['--q'] is not None:
|
|
|
|
|
if args.opts['-q'] is not None: args.opts['-q'] += args.opts['--q']
|
|
|
|
|
else: args.opts['-q'] = args.opts['--q']
|
|
|
|
|
|
2012-10-11 23:24:40 +02:00
|
|
|
|
## Run modes
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if args.opts['-h'] is not None: args.help()
|
|
|
|
|
elif args.opts['--quoters'] is not None: self.quoters()
|
|
|
|
|
elif args.opts['--onelist'] is not None: self.onelist()
|
|
|
|
|
elif args.opts['-v'] is not None: self.version()
|
|
|
|
|
elif args.opts['-l'] is not None: self.list()
|
|
|
|
|
elif args.opts['-L'] is not None: self.linklist()
|
2012-08-21 17:26:21 +02:00
|
|
|
|
elif args.opts['-B'] is not None: self.balloonlist()
|
2012-08-22 16:01:37 +02:00
|
|
|
|
elif args.opts['++onelist'] is not None: self.__extraponies(); self.onelist()
|
|
|
|
|
elif args.opts['+l'] is not None: self.__extraponies(); self.list()
|
|
|
|
|
elif args.opts['+L'] is not None: self.__extraponies(); self.linklist()
|
2012-09-14 20:58:43 +02:00
|
|
|
|
elif args.opts['-A'] is not None: self.list(); self.__extraponies(); self.list()
|
|
|
|
|
elif args.opts['+A'] is not None: self.linklist(); self.__extraponies(); self.linklist()
|
2012-08-21 21:29:24 +02:00
|
|
|
|
else:
|
2012-10-12 01:56:08 +02:00
|
|
|
|
## Colouring features
|
|
|
|
|
if args.opts['--colour-pony'] is not None:
|
2012-10-30 01:31:17 +01:00
|
|
|
|
self.mode += '\033[' + ';'.join(args.opts['--colour-pony']) + 'm'
|
2012-10-12 01:56:08 +02:00
|
|
|
|
else:
|
2012-10-30 01:31:17 +01:00
|
|
|
|
self.mode += '\033[0m'
|
2012-10-12 01:56:08 +02:00
|
|
|
|
if args.opts['+c'] is not None:
|
|
|
|
|
if args.opts['--colour-msg'] is None: args.opts['--colour-msg'] = args.opts['+c']
|
|
|
|
|
if args.opts['--colour-link'] is None: args.opts['--colour-link'] = args.opts['+c']
|
|
|
|
|
if args.opts['--colour-bubble'] is None: args.opts['--colour-bubble'] = args.opts['+c']
|
|
|
|
|
|
2012-10-11 22:29:26 +02:00
|
|
|
|
## Other extra features
|
2012-08-22 15:56:48 +02:00
|
|
|
|
self.__extraponies(args)
|
2012-08-22 00:09:07 +02:00
|
|
|
|
self.__bestpony(args)
|
|
|
|
|
self.__ucsremap(args)
|
2012-10-11 23:09:14 +02:00
|
|
|
|
if args.opts['-o'] is not None:
|
2012-10-30 01:31:17 +01:00
|
|
|
|
self.mode += '$/= $$\\= $'
|
2012-10-23 20:02:22 +02:00
|
|
|
|
args.message = ''
|
2012-10-11 22:29:26 +02:00
|
|
|
|
|
|
|
|
|
## The stuff
|
2012-10-22 14:40:20 +02:00
|
|
|
|
if args.opts['-q'] is not None:
|
2012-10-28 03:33:14 +01:00
|
|
|
|
warn = (args.opts['-f'] is not None) or (args.opts['+f'] is not None)
|
|
|
|
|
if (len(args.opts['-q']) == 1) and ((args.opts['-q'][0] == '-f') or (args.opts['-q'][0] == '+f')):
|
2012-10-22 14:40:20 +02:00
|
|
|
|
warn = True
|
|
|
|
|
if args.opts['-q'][0] == '-f':
|
|
|
|
|
args.opts['-q'] = args.files
|
|
|
|
|
if args.opts['-f'] is not None:
|
|
|
|
|
args.opts['-q'] += args.opts['-f']
|
|
|
|
|
self.quote(args)
|
|
|
|
|
if warn:
|
2012-10-28 03:33:14 +01:00
|
|
|
|
printerr('-q cannot be used at the same time as -f or +f.')
|
2012-10-30 01:31:17 +01:00
|
|
|
|
elif not self.unrecognised:
|
2012-10-22 14:40:20 +02:00
|
|
|
|
self.print_pony(args)
|
2012-10-22 14:58:55 +02:00
|
|
|
|
else:
|
|
|
|
|
args.help()
|
2012-10-22 15:04:49 +02:00
|
|
|
|
exit(255)
|
|
|
|
|
return
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
2012-10-12 12:11:24 +02:00
|
|
|
|
##############################################
|
|
|
|
|
## Methods that run before the mane methods ##
|
|
|
|
|
##############################################
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-08-22 15:50:42 +02:00
|
|
|
|
'''
|
2012-08-22 15:56:48 +02:00
|
|
|
|
Use extra ponies
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param args:ArgParser Parsed command line arguments, may be `None`
|
2012-08-22 15:50:42 +02:00
|
|
|
|
'''
|
2012-08-22 16:01:37 +02:00
|
|
|
|
def __extraponies(self, args = None):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## If extraponies are used, change ponydir to extraponydir
|
2012-08-22 16:01:37 +02:00
|
|
|
|
if args is None:
|
2012-10-30 01:31:17 +01:00
|
|
|
|
self.ponydirs[:] = self.extraponydirs
|
2012-10-28 03:33:14 +01:00
|
|
|
|
elif args.opts['+f'] is not None:
|
|
|
|
|
args.opts['-f'] = args.opts['+f']
|
2012-10-30 01:31:17 +01:00
|
|
|
|
self.ponydirs[:] = self.extraponydirs
|
2012-08-22 15:50:42 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-22 00:09:07 +02:00
|
|
|
|
'''
|
|
|
|
|
Use best.pony if nothing else is set
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param args:ArgParser Parsed command line arguments
|
2012-08-22 00:09:07 +02:00
|
|
|
|
'''
|
|
|
|
|
def __bestpony(self, args):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Set best.pony as the pony to display if none is selected
|
2012-08-22 00:09:07 +02:00
|
|
|
|
if (args.opts['-f'] is None) or (args.opts['-q'] is None) or (len(args.opts['-q']) == 0):
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ponydir in self.ponydirs:
|
2012-08-22 00:09:07 +02:00
|
|
|
|
if os.path.isfile(ponydir + 'best.pony') or os.path.islink(ponydir + 'best.pony'):
|
|
|
|
|
pony = os.path.realpath(ponydir + 'best.pony') # Canonical path
|
2012-08-23 00:56:06 +02:00
|
|
|
|
args.opts['-f' if args.opts['-q'] is None else '-q'] = [pony]
|
2012-08-22 00:09:07 +02:00
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Apply pony name remapping to args according to UCS settings
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param args:ArgParser Parsed command line arguments
|
2012-08-22 00:09:07 +02:00
|
|
|
|
'''
|
|
|
|
|
def __ucsremap(self, args):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Read UCS configurations
|
2012-08-22 00:09:07 +02:00
|
|
|
|
env_ucs = os.environ['PONYSAY_UCS_ME'] if 'PONYSAY_UCS_ME' in os.environ else ''
|
|
|
|
|
ucs_conf = 0
|
|
|
|
|
if env_ucs in ('yes', 'y', '1'): ucs_conf = 1
|
|
|
|
|
elif env_ucs in ('harder', 'h', '2'): ucs_conf = 2
|
|
|
|
|
|
2012-10-22 12:26:17 +02:00
|
|
|
|
## Stop UCS is not used
|
2012-08-22 00:09:07 +02:00
|
|
|
|
if ucs_conf == 0:
|
|
|
|
|
return
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Read all lines in all UCS → ASCII map files
|
2012-08-22 00:09:07 +02:00
|
|
|
|
maplines = []
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ucsmap in self.ucsmaps:
|
2012-08-26 06:17:13 +02:00
|
|
|
|
if os.path.isfile(ucsmap):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
with open(ucsmap, 'rb') as mapfile:
|
2012-08-27 02:31:14 +02:00
|
|
|
|
maplines += [line.replace('\n', '') for line in mapfile.read().decode('utf8', 'replace').split('\n')]
|
2012-08-22 00:09:07 +02:00
|
|
|
|
|
2012-10-22 12:26:17 +02:00
|
|
|
|
## Create UCS → ASCII mapping from read lines
|
2012-08-22 00:09:07 +02:00
|
|
|
|
map = {}
|
|
|
|
|
stripset = ' \t' # must be string, wtf! and way doesn't python's doc say so
|
|
|
|
|
for line in maplines:
|
|
|
|
|
if (len(line) > 0) and not (line[0] == '#'):
|
|
|
|
|
s = line.index('→')
|
|
|
|
|
ucs = line[:s] .strip(stripset)
|
|
|
|
|
ascii = line[s + 1:].strip(stripset)
|
|
|
|
|
map[ucs] = ascii
|
|
|
|
|
|
2012-10-22 12:26:17 +02:00
|
|
|
|
## Apply UCS → ASCII mapping to -f and -q arguments
|
2012-08-22 00:09:07 +02:00
|
|
|
|
for flag in ('-f', '-q'):
|
|
|
|
|
if args.opts[flag] is not None:
|
|
|
|
|
for i in range(0, len(args.opts[flag])):
|
|
|
|
|
if args.opts[flag][i] in map:
|
|
|
|
|
args.opts[flag][i] = map[args.opts[flag][i]]
|
|
|
|
|
|
|
|
|
|
|
2012-10-12 12:11:24 +02:00
|
|
|
|
#######################
|
|
|
|
|
## Auxiliary methods ##
|
|
|
|
|
#######################
|
2012-08-22 15:50:42 +02:00
|
|
|
|
|
2012-08-22 00:48:18 +02:00
|
|
|
|
'''
|
2012-10-22 12:26:17 +02:00
|
|
|
|
Apply UCS:ise pony names according to UCS settings
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param ponies:list<str> List of all ponies (of interrest)
|
|
|
|
|
@param links:map<str> Map to fill with simulated symlink ponies, may be `None`
|
2012-08-22 00:48:18 +02:00
|
|
|
|
'''
|
|
|
|
|
def __ucsise(self, ponies, links = None):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Read UCS configurations
|
2012-08-22 00:48:18 +02:00
|
|
|
|
env_ucs = os.environ['PONYSAY_UCS_ME'] if 'PONYSAY_UCS_ME' in os.environ else ''
|
|
|
|
|
ucs_conf = 0
|
|
|
|
|
if env_ucs in ('yes', 'y', '1'): ucs_conf = 1
|
|
|
|
|
elif env_ucs in ('harder', 'h', '2'): ucs_conf = 2
|
|
|
|
|
|
2012-10-22 12:26:17 +02:00
|
|
|
|
## Stop UCS is not used
|
2012-08-22 00:48:18 +02:00
|
|
|
|
if ucs_conf == 0:
|
|
|
|
|
return
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Read all lines in all UCS → ASCII map files
|
2012-08-22 00:48:18 +02:00
|
|
|
|
maplines = []
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ucsmap in self.ucsmaps:
|
2012-08-26 06:17:13 +02:00
|
|
|
|
if os.path.isfile(ucsmap):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
with open(ucsmap, 'rb') as mapfile:
|
2012-08-27 02:31:14 +02:00
|
|
|
|
maplines += [line.replace('\n', '') for line in mapfile.read().decode('utf8', 'replace').split('\n')]
|
2012-08-22 00:48:18 +02:00
|
|
|
|
|
2012-10-22 12:26:17 +02:00
|
|
|
|
## Create UCS → ASCII mapping from read lines
|
2012-08-22 00:48:18 +02:00
|
|
|
|
map = {}
|
|
|
|
|
stripset = ' \t' # must be string, wtf! and way doesn't python's doc say so
|
|
|
|
|
for line in maplines:
|
2012-09-30 02:55:48 +02:00
|
|
|
|
if not line.startswith('#'):
|
2012-08-22 00:48:18 +02:00
|
|
|
|
s = line.index('→')
|
|
|
|
|
ucs = line[:s] .strip(stripset)
|
|
|
|
|
ascii = line[s + 1:].strip(stripset)
|
|
|
|
|
map[ascii] = ucs
|
|
|
|
|
|
2012-10-22 12:26:17 +02:00
|
|
|
|
## Apply UCS → ACII mapping to ponies, by alias if weak settings
|
2012-08-22 00:48:18 +02:00
|
|
|
|
if ucs_conf == 1:
|
|
|
|
|
for pony in ponies:
|
|
|
|
|
if pony in map:
|
|
|
|
|
ponies.append(map[pony])
|
2012-08-22 03:23:11 +02:00
|
|
|
|
if links is not None:
|
2012-08-22 03:48:25 +02:00
|
|
|
|
links[map[pony]] = pony
|
2012-08-22 00:48:18 +02:00
|
|
|
|
else:
|
|
|
|
|
for j in range(0, len(ponies)):
|
|
|
|
|
if ponies[j] in map:
|
|
|
|
|
ponies[j] = map[ponies[j]]
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
2012-08-21 17:26:21 +02:00
|
|
|
|
Returns one file with full path, names is filter for names, also accepts filepaths
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param names:list<str> Ponies to choose from, may be `None`
|
2012-10-22 13:17:42 +02:00
|
|
|
|
@param alt:bool For method internal use...
|
2012-10-12 12:11:24 +02:00
|
|
|
|
@return :str The file name of a pony
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
2012-10-22 13:17:42 +02:00
|
|
|
|
def __getponypath(self, names = None, alt = False):
|
2012-08-20 02:22:12 +02:00
|
|
|
|
ponies = {}
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## List all pony files, without the .pony ending
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ponydir in self.ponydirs:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for ponyfile in os.listdir(ponydir):
|
2012-10-22 13:17:42 +02:00
|
|
|
|
if endswith(ponyfile, ".pony"):
|
|
|
|
|
pony = ponyfile[:-5]
|
|
|
|
|
if pony not in ponies:
|
|
|
|
|
ponies[pony] = ponydir + ponyfile
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 01:13:21 +02:00
|
|
|
|
## Support for explicit pony file names
|
2012-10-25 02:36:35 +02:00
|
|
|
|
if names is not None:
|
2012-08-21 16:20:29 +02:00
|
|
|
|
for name in names:
|
2012-08-22 01:59:06 +02:00
|
|
|
|
if os.path.exists(name):
|
2012-08-21 16:20:29 +02:00
|
|
|
|
ponies[name] = name
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## If there is not select ponies, choose all of them
|
2012-10-25 02:36:35 +02:00
|
|
|
|
if (names is None) or (len(names) == 0):
|
2012-08-20 02:22:12 +02:00
|
|
|
|
names = list(ponies.keys())
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Select a random pony of the choosen onles
|
2012-08-20 02:22:12 +02:00
|
|
|
|
pony = names[random.randrange(0, len(names))]
|
|
|
|
|
if pony not in ponies:
|
2012-10-22 13:17:42 +02:00
|
|
|
|
if not alt:
|
2012-10-30 01:31:17 +01:00
|
|
|
|
autocorrect = SpelloCorrecter(self.ponydirs, '.pony')
|
2012-10-22 13:17:42 +02:00
|
|
|
|
(alternatives, dist) = autocorrect.correct(pony)
|
2012-10-28 04:09:53 +01:00
|
|
|
|
limit = os.environ['PONYSAY_TYPO_LIMIT'] if 'PONYSAY_TYPO_LIMIT' in os.environ else ''
|
|
|
|
|
limit = 5 if len(limit) == 0 else int(dist)
|
|
|
|
|
if (len(alternatives) > 0) and (dist <= limit):
|
2012-10-22 13:17:42 +02:00
|
|
|
|
return self.__getponypath(alternatives, True)
|
2012-09-02 16:25:25 +02:00
|
|
|
|
sys.stderr.write('I have never heard of anypony named %s\n' % (pony));
|
2012-08-20 02:22:12 +02:00
|
|
|
|
exit(1)
|
|
|
|
|
else:
|
|
|
|
|
return ponies[pony]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Returns a set with all ponies that have quotes and are displayable
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@return :set<str> All ponies that have quotes and are displayable
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def __quoters(self):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## List all unique quote files
|
2012-08-20 02:22:12 +02:00
|
|
|
|
quotes = []
|
|
|
|
|
quoteshash = set()
|
|
|
|
|
_quotes = []
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for quotedir in self.quotedirs:
|
2012-08-27 01:40:43 +02:00
|
|
|
|
_quotes += [item[:item.index('.')] for item in os.listdir(quotedir)]
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for quote in _quotes:
|
|
|
|
|
if not quote == '':
|
|
|
|
|
if not quote in quoteshash:
|
|
|
|
|
quoteshash.add(quote)
|
|
|
|
|
quotes.append(quote)
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Create a set of all ponyes that have quotes
|
2012-08-20 02:22:12 +02:00
|
|
|
|
ponies = set()
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ponydir in self.ponydirs:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for pony in os.listdir(ponydir):
|
|
|
|
|
if not pony[0] == '.':
|
|
|
|
|
p = pony[:-5] # remove .pony
|
|
|
|
|
for quote in quotes:
|
|
|
|
|
if ('+' + p + '+') in ('+' + quote + '+'):
|
|
|
|
|
if not p in ponies:
|
|
|
|
|
ponies.add(p)
|
|
|
|
|
|
|
|
|
|
return ponies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Returns a list with all (pony, quote file) pairs
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@return (pony, quote):(str, str) All ponies–quote file-pairs
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def __quotes(self):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Get all ponyquote files
|
2012-08-20 02:22:12 +02:00
|
|
|
|
quotes = []
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for quotedir in self.quotedirs:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
quotes += [quotedir + item for item in os.listdir(quotedir)]
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Create list of all pony–quote file-pairs
|
|
|
|
|
rc = []
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ponydir in self.ponydirs:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for pony in os.listdir(ponydir):
|
|
|
|
|
if not pony[0] == '.':
|
|
|
|
|
p = pony[:-5] # remove .pony
|
|
|
|
|
for quote in quotes:
|
|
|
|
|
q = quote[quote.rindex('/') + 1:]
|
|
|
|
|
q = q[:q.rindex('.')]
|
|
|
|
|
if ('+' + p + '+') in ('+' + q + '+'):
|
|
|
|
|
rc.append((p, quote))
|
|
|
|
|
|
|
|
|
|
return rc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Gets the size of the terminal in (rows, columns)
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@return (rows, columns):(int, int) The number or lines and the number of columns in the terminal's display area
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def __gettermsize(self):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Call `stty` to determine the size of the terminal, this way is better then using python's ncurses
|
2012-10-25 00:06:30 +02:00
|
|
|
|
for channel in (sys.stderr, sys.stdout, sys.stdin):
|
|
|
|
|
termsize = Popen(['stty', 'size'], stdout=PIPE, stdin=channel, stderr=PIPE).communicate()[0]
|
|
|
|
|
if len(termsize) > 0:
|
|
|
|
|
termsize = termsize.decode('utf8', 'replace')[:-1].split(' ') # [:-1] removes a \n
|
|
|
|
|
termsize = [int(item) for item in termsize]
|
|
|
|
|
return termsize
|
|
|
|
|
return (24, 80) # fall back to minimal sane size
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-22 03:48:25 +02:00
|
|
|
|
|
2012-10-12 12:11:24 +02:00
|
|
|
|
#####################
|
|
|
|
|
## Listing methods ##
|
|
|
|
|
#####################
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-08-23 00:56:06 +02:00
|
|
|
|
'''
|
|
|
|
|
Columnise a list and prints it
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param ponies:list<(str, str)> All items to list, each item should have to elements: unformated name, formated name
|
2012-08-23 00:56:06 +02:00
|
|
|
|
'''
|
|
|
|
|
def __columnise(self, ponies):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Get terminal width, and a 2 which is the space between columns
|
2012-08-23 02:27:36 +02:00
|
|
|
|
termwidth = self.__gettermsize()[1] + 2
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Sort the ponies, and get the cells' widths, and the largest width + 2
|
2012-08-23 00:56:06 +02:00
|
|
|
|
ponies.sort(key = lambda pony : pony[0])
|
|
|
|
|
widths = [UCS.dispLen(pony[0]) for pony in ponies]
|
|
|
|
|
width = max(widths) + 2 # longest pony file name + space between columns
|
2012-08-23 02:27:36 +02:00
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Calculate the number of rows and columns, can create a list of empty columns
|
|
|
|
|
cols = termwidth // width # do not believe electricians, this means ⌊termwidth / width⌋
|
2012-08-23 02:27:36 +02:00
|
|
|
|
rows = (len(ponies) + cols - 1) // cols
|
2012-08-23 03:01:15 +02:00
|
|
|
|
columns = []
|
|
|
|
|
for c in range(0, cols): columns.append([])
|
2012-08-23 02:27:36 +02:00
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Fill the columns with cells of ponies
|
2012-08-23 02:27:36 +02:00
|
|
|
|
(y, x) = (0, 0)
|
2012-08-23 00:56:06 +02:00
|
|
|
|
for j in range(0, len(ponies)):
|
2012-08-23 02:27:36 +02:00
|
|
|
|
cell = ponies[j][1] + ' ' * (width - widths[j]);
|
2012-08-23 03:01:15 +02:00
|
|
|
|
columns[x].append(cell)
|
2012-08-23 02:27:36 +02:00
|
|
|
|
y += 1
|
|
|
|
|
if y == rows:
|
|
|
|
|
x += 1
|
|
|
|
|
y = 0
|
2012-08-23 00:56:06 +02:00
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Make the columnisation nicer by letting the last row be partially empty rather than the last column
|
2012-08-23 03:01:15 +02:00
|
|
|
|
diff = rows * cols - len(ponies)
|
|
|
|
|
if diff > 2:
|
|
|
|
|
c = cols - 1
|
|
|
|
|
diff -= 1
|
|
|
|
|
while diff > 0:
|
|
|
|
|
columns[c] = columns[c - 1][-diff:] + columns[c]
|
|
|
|
|
c -= 1
|
|
|
|
|
columns[c] = columns[c][:-diff]
|
|
|
|
|
diff -= 1
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Create rows from columns
|
2012-08-23 03:01:15 +02:00
|
|
|
|
lines = []
|
|
|
|
|
for r in range(0, rows):
|
|
|
|
|
lines.append([])
|
|
|
|
|
for c in range(0, cols):
|
|
|
|
|
if r < len(columns[c]):
|
|
|
|
|
line = lines[r].append(columns[c][r])
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Print the matrix, with one extra blank row
|
|
|
|
|
print('\n'.join([''.join(line)[:-2] for line in lines]))
|
2012-08-23 02:27:36 +02:00
|
|
|
|
print()
|
2012-08-23 00:56:06 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
Lists the available ponies
|
|
|
|
|
'''
|
|
|
|
|
def list(self):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Get all quoters
|
2012-08-20 02:22:12 +02:00
|
|
|
|
quoters = self.__quoters()
|
|
|
|
|
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ponydir in self.ponydirs: # Loop ponydirs
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Get all ponies in the directory
|
2012-08-21 17:26:21 +02:00
|
|
|
|
_ponies = os.listdir(ponydir)
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
|
|
|
|
## Remove .pony from all files and skip those that does not have .pony
|
2012-08-21 17:26:21 +02:00
|
|
|
|
ponies = []
|
|
|
|
|
for pony in _ponies:
|
2012-09-30 02:55:48 +02:00
|
|
|
|
if endswith(pony, '.pony'):
|
2012-08-21 17:26:21 +02:00
|
|
|
|
ponies.append(pony[:-5])
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
|
|
|
|
## UCS:ise pony names, they are already sorted
|
2012-08-22 00:48:18 +02:00
|
|
|
|
self.__ucsise(ponies)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## If ther directory is not empty print its name and all ponies, columnised
|
2012-08-22 16:36:47 +02:00
|
|
|
|
if len(ponies) == 0:
|
|
|
|
|
continue
|
|
|
|
|
print('\033[1mponies located in ' + ponydir + '\033[21m')
|
2012-08-23 00:56:06 +02:00
|
|
|
|
self.__columnise([(pony, '\033[1m' + pony + '\033[21m' if pony in quoters else pony) for pony in ponies])
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Lists the available ponies with alternatives inside brackets
|
|
|
|
|
'''
|
|
|
|
|
def linklist(self):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Get the size of the terminal and all ponies with quotes
|
2012-08-20 02:22:12 +02:00
|
|
|
|
termsize = self.__gettermsize()
|
|
|
|
|
quoters = self.__quoters()
|
|
|
|
|
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ponydir in self.ponydirs: # Loop ponydirs
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Get all pony files in the directory
|
2012-08-21 17:26:21 +02:00
|
|
|
|
_ponies = os.listdir(ponydir)
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
|
|
|
|
## Remove .pony from all files and skip those that does not have .pony
|
2012-08-21 17:26:21 +02:00
|
|
|
|
ponies = []
|
|
|
|
|
for pony in _ponies:
|
2012-09-30 02:55:48 +02:00
|
|
|
|
if endswith(pony, '.pony'):
|
2012-08-21 17:26:21 +02:00
|
|
|
|
ponies.append(pony[:-5])
|
2012-08-22 16:36:47 +02:00
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## If there are no ponies in the directory skip to next directory, otherwise, print the directories name
|
2012-08-22 16:36:47 +02:00
|
|
|
|
if len(ponies) == 0:
|
|
|
|
|
continue
|
|
|
|
|
print('\033[1mponies located in ' + ponydir + '\033[21m')
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## UCS:ise pony names
|
2012-08-22 03:23:11 +02:00
|
|
|
|
pseudolinkmap = {}
|
2012-08-23 00:56:06 +02:00
|
|
|
|
self.__ucsise(ponies, pseudolinkmap)
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
|
|
|
|
## Create target–link-pair, with `None` as link if the file is not a symlink or in `pseudolinkmap`
|
2012-08-22 03:48:25 +02:00
|
|
|
|
pairs = []
|
|
|
|
|
for pony in ponies:
|
|
|
|
|
if pony in pseudolinkmap:
|
|
|
|
|
pairs.append((pony, pseudolinkmap[pony] + '.pony'));
|
|
|
|
|
else:
|
|
|
|
|
pairs.append((pony, os.path.realpath(ponydir + pony + '.pony') if os.path.islink(ponydir + pony + '.pony') else None))
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Create map from source pony to alias ponies for each pony
|
2012-08-20 02:22:12 +02:00
|
|
|
|
ponymap = {}
|
|
|
|
|
for pair in pairs:
|
2012-08-22 03:23:11 +02:00
|
|
|
|
if (pair[1] is None) or (pair[1] == ''):
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if pair[0] not in ponymap:
|
|
|
|
|
ponymap[pair[0]] = []
|
|
|
|
|
else:
|
|
|
|
|
target = pair[1][:-5]
|
|
|
|
|
if '/' in target:
|
|
|
|
|
target = target[target.rindex('/') + 1:]
|
|
|
|
|
if target in ponymap:
|
|
|
|
|
ponymap[target].append(pair[0])
|
|
|
|
|
else:
|
|
|
|
|
ponymap[target] = [pair[0]]
|
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Create list of source ponies concatenated with alias ponies in brackets
|
2012-08-22 03:23:11 +02:00
|
|
|
|
ponies = {}
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for pony in ponymap:
|
2012-08-22 01:34:06 +02:00
|
|
|
|
w = UCS.dispLen(pony)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
item = '\033[1m' + pony + '\033[21m' if (pony in quoters) else pony
|
|
|
|
|
syms = ponymap[pony]
|
2012-08-22 03:28:21 +02:00
|
|
|
|
syms.sort()
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if len(syms) > 0:
|
|
|
|
|
w += 2 + len(syms)
|
|
|
|
|
item += ' ('
|
|
|
|
|
first = True
|
|
|
|
|
for sym in syms:
|
2012-08-22 01:34:06 +02:00
|
|
|
|
w += UCS.dispLen(sym)
|
2012-08-23 00:56:06 +02:00
|
|
|
|
if first: first = False
|
|
|
|
|
else: item += ' '
|
2012-08-20 02:22:12 +02:00
|
|
|
|
item += '\033[1m' + sym + '\033[21m' if (sym in quoters) else sym
|
|
|
|
|
item += ')'
|
2012-08-23 00:56:06 +02:00
|
|
|
|
ponies[(item.replace('\033[1m', '').replace('\033[21m', ''), item)] = w
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Print the ponies, columnised
|
2012-08-23 00:56:06 +02:00
|
|
|
|
self.__columnise(list(ponies))
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
2012-09-30 00:53:16 +02:00
|
|
|
|
Lists with all ponies that have quotes and are displayable, on one column without anything bold or otherwise formated
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def quoters(self):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Get all quoters
|
|
|
|
|
ponies = self.__quoters()
|
|
|
|
|
|
2012-10-22 12:26:17 +02:00
|
|
|
|
## UCS:ise and sort
|
2012-08-22 00:48:18 +02:00
|
|
|
|
self.__ucsise(ponies)
|
2012-10-29 05:10:03 +01:00
|
|
|
|
ponies = list(ponies)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
ponies.sort()
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
|
|
|
|
## Print each one on a seperate line, but skip duplicates
|
|
|
|
|
last = ''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for pony in ponies:
|
|
|
|
|
if not pony == last:
|
|
|
|
|
last = pony
|
|
|
|
|
print(pony)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
2012-09-30 00:53:16 +02:00
|
|
|
|
Lists the available ponies on one column without anything bold or otherwise formated
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def onelist(self):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
## Get all pony files
|
2012-08-21 17:26:21 +02:00
|
|
|
|
_ponies = []
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for ponydir in self.ponydirs: # Loop ponydirs
|
2012-08-21 17:26:21 +02:00
|
|
|
|
_ponies += os.listdir(ponydir)
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
|
|
|
|
## Remove .pony from all files and skip those that does not have .pony
|
2012-08-21 17:26:21 +02:00
|
|
|
|
ponies = []
|
|
|
|
|
for pony in _ponies:
|
2012-09-30 02:55:48 +02:00
|
|
|
|
if endswith(pony, '.pony'):
|
2012-08-21 17:26:21 +02:00
|
|
|
|
ponies.append(pony[:-5])
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
2012-10-22 12:26:17 +02:00
|
|
|
|
## UCS:ise and sort
|
2012-08-22 00:48:18 +02:00
|
|
|
|
self.__ucsise(ponies)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
ponies.sort()
|
2012-09-30 00:53:16 +02:00
|
|
|
|
|
|
|
|
|
## Print each one on a seperate line, but skip duplicates
|
|
|
|
|
last = ''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for pony in ponies:
|
|
|
|
|
if not pony == last:
|
|
|
|
|
last = pony
|
|
|
|
|
print(pony)
|
|
|
|
|
|
|
|
|
|
|
2012-10-12 12:11:24 +02:00
|
|
|
|
#####################
|
|
|
|
|
## Balloon methods ##
|
|
|
|
|
#####################
|
2012-08-21 15:57:48 +02:00
|
|
|
|
|
2012-08-21 16:20:29 +02:00
|
|
|
|
'''
|
2012-08-21 17:26:21 +02:00
|
|
|
|
Prints a list of all balloons
|
|
|
|
|
'''
|
|
|
|
|
def balloonlist(self):
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Get the size of the terminal
|
2012-08-21 17:26:21 +02:00
|
|
|
|
termsize = self.__gettermsize()
|
|
|
|
|
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Get all balloons
|
|
|
|
|
balloonset = set()
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for balloondir in self.balloondirs:
|
2012-08-21 17:26:21 +02:00
|
|
|
|
for balloon in os.listdir(balloondir):
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Use .think if running ponythink, otherwise .say
|
2012-10-30 01:31:17 +01:00
|
|
|
|
if self.isthink and endswith(balloon, '.think'):
|
2012-08-21 17:26:21 +02:00
|
|
|
|
balloon = balloon[:-6]
|
2012-10-30 01:31:17 +01:00
|
|
|
|
elif (not self.isthink) and endswith(balloon, '.say'):
|
2012-08-21 17:26:21 +02:00
|
|
|
|
balloon = balloon[:-4]
|
|
|
|
|
else:
|
|
|
|
|
continue
|
2012-09-30 01:11:50 +02:00
|
|
|
|
|
|
|
|
|
## Add the balloon if there is none with the same name
|
2012-08-21 17:26:21 +02:00
|
|
|
|
if balloon not in balloonset:
|
|
|
|
|
balloonset.add(balloon)
|
|
|
|
|
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Print all balloos, columnised
|
2012-08-23 00:56:06 +02:00
|
|
|
|
self.__columnise([(balloon, balloon) for balloon in list(balloonset)])
|
2012-08-21 17:26:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Returns one file with full path, names is filter for style names, also accepts filepaths
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param names:list<str> Balloons to choose from, may be `None`
|
2012-10-22 13:17:42 +02:00
|
|
|
|
@param alt:bool For method internal use
|
2012-10-12 12:11:24 +02:00
|
|
|
|
@param :str The file name of the balloon, will be `None` iff `names` is `None`
|
2012-08-21 16:20:29 +02:00
|
|
|
|
'''
|
2012-10-22 13:17:42 +02:00
|
|
|
|
def __getballoonpath(self, names, alt = False):
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Stop if their is no choosen balloon
|
2012-08-21 16:20:29 +02:00
|
|
|
|
if names is None:
|
|
|
|
|
return None
|
|
|
|
|
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Get all balloons
|
|
|
|
|
balloons = {}
|
2012-10-30 01:31:17 +01:00
|
|
|
|
for balloondir in self.balloondirs:
|
2012-08-21 17:26:21 +02:00
|
|
|
|
for balloon in os.listdir(balloondir):
|
|
|
|
|
balloonfile = balloon
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Use .think if running ponythink, otherwise .say
|
2012-10-30 01:31:17 +01:00
|
|
|
|
if self.isthink and endswith(balloon, '.think'):
|
2012-08-21 17:26:21 +02:00
|
|
|
|
balloon = balloon[:-6]
|
2012-10-30 01:31:17 +01:00
|
|
|
|
elif (not self.isthink) and endswith(balloon, '.say'):
|
2012-08-21 17:26:21 +02:00
|
|
|
|
balloon = balloon[:-4]
|
|
|
|
|
else:
|
|
|
|
|
continue
|
2012-09-30 01:11:50 +02:00
|
|
|
|
|
|
|
|
|
## Add the balloon if there is none with the same name
|
2012-08-21 16:20:29 +02:00
|
|
|
|
if balloon not in balloons:
|
|
|
|
|
balloons[balloon] = balloondir + balloonfile
|
|
|
|
|
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Support for explicit balloon file names
|
2012-08-21 16:20:29 +02:00
|
|
|
|
for name in names:
|
2012-08-22 01:59:06 +02:00
|
|
|
|
if os.path.exists(name):
|
2012-08-21 16:20:29 +02:00
|
|
|
|
balloons[name] = name
|
|
|
|
|
|
2012-09-30 01:11:50 +02:00
|
|
|
|
## Select a random balloon of the choosen ones
|
2012-08-21 16:20:29 +02:00
|
|
|
|
balloon = names[random.randrange(0, len(names))]
|
|
|
|
|
if balloon not in balloons:
|
2012-10-22 13:17:42 +02:00
|
|
|
|
if not alt:
|
2012-10-30 01:31:17 +01:00
|
|
|
|
autocorrect = SpelloCorrecter(self.balloondirs, '.think' if self.isthink else '.say')
|
2012-10-23 22:09:59 +02:00
|
|
|
|
(alternatives, dist) = autocorrect.correct(balloon)
|
2012-10-28 04:09:53 +01:00
|
|
|
|
limit = os.environ['PONYSAY_TYPO_LIMIT'] if 'PONYSAY_TYPO_LIMIT' in os.environ else ''
|
|
|
|
|
limit = 5 if len(limit) == 0 else int(dist)
|
|
|
|
|
if (len(alternatives) > 0) and (dist <= limit):
|
2012-10-23 23:10:35 +02:00
|
|
|
|
return self.__getballoonpath(alternatives, True)
|
2012-08-21 16:20:29 +02:00
|
|
|
|
sys.stderr.write('That balloon style %s does not exist\n' % (balloon));
|
|
|
|
|
exit(1)
|
|
|
|
|
else:
|
|
|
|
|
return balloons[balloon]
|
|
|
|
|
|
|
|
|
|
|
2012-08-21 15:57:48 +02:00
|
|
|
|
'''
|
|
|
|
|
Creates the balloon style object
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param balloonfile:str The file with the balloon style, may be `None`
|
|
|
|
|
@return :Balloon Instance describing the balloon's style
|
2012-08-21 15:57:48 +02:00
|
|
|
|
'''
|
|
|
|
|
def __getballoon(self, balloonfile):
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Use default balloon if none is specified
|
2012-08-21 15:57:48 +02:00
|
|
|
|
if balloonfile is None:
|
2012-10-30 01:31:17 +01:00
|
|
|
|
if self.isthink:
|
2012-10-25 00:06:30 +02:00
|
|
|
|
return Balloon('o', 'o', '( ', ' )', [' _'], ['_'], ['_'], ['_'], ['_ '], ' )', ' )', ' )', ['- '], ['-'], ['-'], ['-'], [' -'], '( ', '( ', '( ')
|
|
|
|
|
return Balloon('\\', '/', '< ', ' >', [' _'], ['_'], ['_'], ['_'], ['_ '], ' \\', ' |', ' /', ['- '], ['-'], ['-'], ['-'], [' -'], '\\ ', '| ', '/ ')
|
2012-08-21 15:57:48 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Initialise map for balloon parts
|
2012-08-21 15:57:48 +02:00
|
|
|
|
map = {}
|
|
|
|
|
for elem in ('\\', '/', 'ww', 'ee', 'nw', 'nnw', 'n', 'nne', 'ne', 'nee', 'e', 'see', 'se', 'sse', 's', 'ssw', 'sw', 'sww', 'w', 'nww'):
|
|
|
|
|
map[elem] = []
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Read all lines in the balloon file
|
2012-09-30 00:53:16 +02:00
|
|
|
|
with open(balloonfile, 'rb') as balloonstream:
|
2012-10-25 00:06:30 +02:00
|
|
|
|
data = balloonstream.read().decode('utf8', 'replace')
|
|
|
|
|
data = [line.replace('\n', '') for line in data.split('\n')]
|
2012-08-21 15:57:48 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Parse the balloon file, and fill the map
|
2012-08-21 15:57:48 +02:00
|
|
|
|
last = None
|
|
|
|
|
for line in data:
|
|
|
|
|
if len(line) > 0:
|
|
|
|
|
if line[0] == ':':
|
|
|
|
|
map[last].append(line[1:])
|
|
|
|
|
else:
|
|
|
|
|
last = line[:line.index(':')]
|
|
|
|
|
value = line[len(last) + 1:]
|
|
|
|
|
map[last].append(value)
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Return the balloon
|
2012-08-21 15:57:48 +02:00
|
|
|
|
return Balloon(map['\\'][0], map['/'][0], map['ww'][0], map['ee'][0], map['nw'], map['nnw'], map['n'],
|
|
|
|
|
map['nne'], map['ne'], map['nee'][0], map['e'][0], map['see'][0], map['se'], map['sse'],
|
|
|
|
|
map['s'], map['ssw'], map['sw'], map['sww'][0], map['w'][0], map['nww'][0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-12 12:11:24 +02:00
|
|
|
|
########################
|
|
|
|
|
## Displaying methods ##
|
|
|
|
|
########################
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Prints the name of the program and the version of the program
|
|
|
|
|
'''
|
|
|
|
|
def version(self):
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Prints the "ponysay $VERSION", if this is modified, ./dev/dist.sh must be modified accordingly
|
2012-08-20 02:22:12 +02:00
|
|
|
|
print('%s %s' % ('ponysay', VERSION))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Print the pony with a speech or though bubble. message, pony and wrap from args are used.
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param args:ArgParser Parsed command line arguments
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def print_pony(self, args):
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Get message and remove tailing whitespace from stdin (but not for each line)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if args.message == None:
|
|
|
|
|
msg = ''.join(sys.stdin.readlines()).rstrip()
|
|
|
|
|
else:
|
|
|
|
|
msg = args.message
|
2012-10-12 01:56:08 +02:00
|
|
|
|
if args.opts['--colour-msg'] is not None:
|
|
|
|
|
msg = '\033[' + ';'.join(args.opts['--colour-msg']) + 'm' + msg
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## This algorithm should give some result as cowsay's (according to tests)
|
|
|
|
|
if args.opts['-c'] is not None:
|
2012-08-22 04:35:07 +02:00
|
|
|
|
buf = ''
|
|
|
|
|
last = ' '
|
|
|
|
|
CHARS = '\t \n'
|
|
|
|
|
for c in msg:
|
|
|
|
|
if (c in CHARS) and (last in CHARS):
|
|
|
|
|
if last == '\n':
|
|
|
|
|
buf += last
|
|
|
|
|
last = c
|
|
|
|
|
else:
|
|
|
|
|
buf += c
|
|
|
|
|
last = c
|
|
|
|
|
msg = buf.strip(CHARS)
|
|
|
|
|
buf = ''
|
|
|
|
|
for c in msg:
|
|
|
|
|
if (c != '\n') or (last != '\n'):
|
|
|
|
|
buf += c
|
|
|
|
|
last = c
|
|
|
|
|
msg = buf.replace('\n', '\n\n')
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Get the pony
|
2012-08-20 02:22:12 +02:00
|
|
|
|
pony = self.__getponypath(args.opts['-f'])
|
2012-10-23 23:10:35 +02:00
|
|
|
|
printinfo('pony file: ' + pony)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Use PNG file as pony file
|
2012-09-30 02:55:48 +02:00
|
|
|
|
if endswith(pony.lower(), '.png'):
|
2012-08-20 02:22:12 +02:00
|
|
|
|
pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
|
2012-10-30 01:31:17 +01:00
|
|
|
|
pngcmd = ('img2ponysay -p -- ' if self.linuxvt else 'img2ponysay -- ') + pony
|
2012-08-20 02:22:12 +02:00
|
|
|
|
pngpipe = os.pipe()
|
|
|
|
|
Popen(pngcmd, stdout=os.fdopen(pngpipe[1], 'w'), shell=True).wait()
|
|
|
|
|
pony = '/proc/' + str(os.getpid()) + '/fd/' + str(pngpipe[0])
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## If KMS is utilies, select a KMS pony file and create it if necessary
|
2012-08-20 13:27:41 +02:00
|
|
|
|
pony = self.__kms(pony)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## If in Linux VT clean the terminal (See info/pdf-manual [Printing in TTY with KMS])
|
2012-10-30 01:31:17 +01:00
|
|
|
|
if self.linuxvt:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
print('\033[H\033[2J', end='')
|
|
|
|
|
|
2012-10-27 22:50:17 +02:00
|
|
|
|
## Get width truncation and wrapping
|
2012-08-20 02:22:12 +02:00
|
|
|
|
env_width = os.environ['PONYSAY_FULL_WIDTH'] if 'PONYSAY_FULL_WIDTH' in os.environ else None
|
|
|
|
|
if env_width is None: env_width = ''
|
|
|
|
|
widthtruncation = self.__gettermsize()[1] if env_width not in ('yes', 'y', '1') else None
|
2012-10-27 22:50:17 +02:00
|
|
|
|
messagewrap = 40
|
|
|
|
|
if (args.opts['-W'] is not None) and (len(args.opts['-W'][0]) > 0):
|
|
|
|
|
messagewrap = args.opts['-W'][0]
|
|
|
|
|
if messagewrap[0] in 'nmsNMS': # m is left to n on QWERTY and s is left to n on Dvorak
|
|
|
|
|
messagewrap = None
|
|
|
|
|
elif messagewrap[0] in 'iouIOU': # o is left to i on QWERTY and u is right to i on Dvorak
|
|
|
|
|
messagewrap = self.__gettermsize()[1]
|
|
|
|
|
else:
|
|
|
|
|
messagewrap = int(args.opts['-W'][0])
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Get balloon object
|
2012-10-23 23:10:35 +02:00
|
|
|
|
balloonfile = self.__getballoonpath(args.opts['-b'])
|
|
|
|
|
printinfo('balloon style file: ' + str(balloonfile))
|
|
|
|
|
balloon = self.__getballoon(balloonfile) if args.opts['-o'] is None else None
|
2012-08-21 17:26:21 +02:00
|
|
|
|
|
2012-10-12 01:56:08 +02:00
|
|
|
|
## Get hyphen style
|
2012-10-28 16:25:11 +01:00
|
|
|
|
hyphen = os.environ['PONYSAY_WRAP_HYPHEN'] if 'PONYSAY_WRAP_HYPHEN' in os.environ else None
|
2012-10-28 04:09:53 +01:00
|
|
|
|
if (hyphen is None) or (len(hyphen) == 0):
|
|
|
|
|
hyphen = '-'
|
2012-10-12 01:56:08 +02:00
|
|
|
|
hyphencolour = ''
|
|
|
|
|
if args.opts['--colour-wrap'] is not None:
|
|
|
|
|
hyphencolour = '\033[' + ';'.join(args.opts['--colour-wrap']) + 'm'
|
2012-10-28 04:09:53 +01:00
|
|
|
|
hyphen = '\033[31m' + hyphencolour + hyphen
|
2012-10-12 01:56:08 +02:00
|
|
|
|
|
|
|
|
|
## Link and balloon colouring
|
|
|
|
|
linkcolour = ''
|
|
|
|
|
if args.opts['--colour-link'] is not None:
|
|
|
|
|
linkcolour = '\033[' + ';'.join(args.opts['--colour-link']) + 'm'
|
|
|
|
|
ballooncolour = ''
|
|
|
|
|
if args.opts['--colour-bubble'] is not None:
|
|
|
|
|
ballooncolour = '\033[' + ';'.join(args.opts['--colour-bubble']) + 'm'
|
|
|
|
|
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Run cowsay replacement
|
2012-10-30 01:31:17 +01:00
|
|
|
|
backend = Backend(message = msg, ponyfile = pony, wrapcolumn = messagewrap, width = widthtruncation, balloon = balloon,
|
|
|
|
|
hyphen = hyphen, linkcolour = linkcolour, ballooncolour = ballooncolour, mode = self.mode)
|
2012-08-20 23:45:39 +02:00
|
|
|
|
backend.parse()
|
|
|
|
|
output = backend.output
|
2012-09-30 02:55:48 +02:00
|
|
|
|
if output.endswith('\n'):
|
2012-08-20 02:22:12 +02:00
|
|
|
|
output = output[:-1]
|
|
|
|
|
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Load height trunction settings
|
2012-08-20 02:22:12 +02:00
|
|
|
|
env_bottom = os.environ['PONYSAY_BOTTOM'] if 'PONYSAY_BOTTOM' in os.environ else None
|
|
|
|
|
if env_bottom is None: env_bottom = ''
|
|
|
|
|
|
|
|
|
|
env_height = os.environ['PONYSAY_TRUNCATE_HEIGHT'] if 'PONYSAY_TRUNCATE_HEIGHT' in os.environ else None
|
|
|
|
|
if env_height is None: env_height = ''
|
|
|
|
|
|
|
|
|
|
env_lines = os.environ['PONYSAY_SHELL_LINES'] if 'PONYSAY_SHELL_LINES' in os.environ else None
|
|
|
|
|
if (env_lines is None) or (env_lines == ''): env_lines = '2'
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Print the output, truncated on height is so set
|
2012-08-20 02:22:12 +02:00
|
|
|
|
lines = self.__gettermsize()[0] - int(env_lines)
|
2012-10-30 01:31:17 +01:00
|
|
|
|
if self.linuxvt or (env_height is ('yes', 'y', '1')):
|
2012-08-20 23:45:39 +02:00
|
|
|
|
if env_bottom is ('yes', 'y', '1'):
|
|
|
|
|
for line in output.split('\n')[: -lines]:
|
|
|
|
|
print(line)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
else:
|
2012-08-20 23:45:39 +02:00
|
|
|
|
for line in output.split('\n')[: lines]:
|
|
|
|
|
print(line)
|
|
|
|
|
else:
|
2012-08-27 02:31:14 +02:00
|
|
|
|
print(output)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Print the pony with a speech or though bubble and a self quote
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param args:ArgParser Parsed command line arguments
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def quote(self, args):
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Get all quotes, and if any pony is choosen just keep them
|
2012-08-20 02:22:12 +02:00
|
|
|
|
pairs = self.__quotes()
|
|
|
|
|
if len(args.opts['-q']) > 0:
|
2012-08-21 21:29:24 +02:00
|
|
|
|
ponyset = {}
|
|
|
|
|
for pony in args.opts['-q']:
|
2012-09-30 02:55:48 +02:00
|
|
|
|
if endswith(pony, '.pony'):
|
2012-08-21 21:29:24 +02:00
|
|
|
|
ponyname = pony[:-5]
|
|
|
|
|
if '/' in ponyname:
|
|
|
|
|
ponyname = ponyname[ponyname.rindex('/') + 1:]
|
|
|
|
|
ponyset[ponyname] = pony
|
|
|
|
|
else:
|
|
|
|
|
ponyset[pony] = pony
|
2012-08-20 02:22:12 +02:00
|
|
|
|
alts = []
|
|
|
|
|
for pair in pairs:
|
|
|
|
|
if pair[0] in ponyset:
|
2012-08-21 21:29:24 +02:00
|
|
|
|
alts.append((ponyset[pair[0]], pair[1]))
|
2012-08-20 02:22:12 +02:00
|
|
|
|
pairs = alts
|
2012-09-30 02:33:58 +02:00
|
|
|
|
|
|
|
|
|
## Select a random pony–quote-pair, load it and print it
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if not len(pairs) == 0:
|
|
|
|
|
pair = pairs[random.randrange(0, len(pairs))]
|
2012-10-23 23:42:28 +02:00
|
|
|
|
printinfo('quote file: ' + pair[1])
|
2012-09-30 00:53:16 +02:00
|
|
|
|
with open(pair[1], 'rb') as qfile:
|
2012-08-27 02:31:14 +02:00
|
|
|
|
args.message = qfile.read().decode('utf8', 'replace').strip()
|
2012-08-20 02:22:12 +02:00
|
|
|
|
args.opts['-f'] = [pair[0]]
|
|
|
|
|
elif len(args.opts['-q']) == 0:
|
2012-08-21 21:29:24 +02:00
|
|
|
|
sys.stderr.write('Princess Celestia! All the ponies are mute!\n')
|
2012-08-20 02:22:12 +02:00
|
|
|
|
exit(1)
|
|
|
|
|
else:
|
|
|
|
|
args.opts['-f'] = [args.opts['-q'][random.randrange(0, len(args.opts['-q']))]]
|
2012-08-21 21:29:24 +02:00
|
|
|
|
args.message = 'Zecora! Help me, I am mute!'
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
self.print_pony(args)
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:02:08 +02:00
|
|
|
|
'''
|
2012-09-30 02:33:58 +02:00
|
|
|
|
Identifies whether KMS support is utilised
|
2012-08-20 12:02:08 +02:00
|
|
|
|
'''
|
2012-10-30 01:31:17 +01:00
|
|
|
|
def isUsingKMS(self):
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## KMS is not utilised if Linux VT is not used
|
2012-10-30 01:31:17 +01:00
|
|
|
|
if not self.linuxvt:
|
2012-08-20 12:07:21 +02:00
|
|
|
|
return False
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Read the PONYSAY_KMS_PALETTE environment variable
|
2012-08-20 12:02:08 +02:00
|
|
|
|
env_kms = os.environ['PONYSAY_KMS_PALETTE'] if 'PONYSAY_KMS_PALETTE' in os.environ else None
|
|
|
|
|
if env_kms is None: env_kms = ''
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Read the PONYSAY_KMS_PALETTE_CMD environment variable, and run it
|
2012-08-20 12:02:08 +02:00
|
|
|
|
env_kms_cmd = os.environ['PONYSAY_KMS_PALETTE_CMD'] if 'PONYSAY_KMS_PALETTE_CMD' in os.environ else None
|
|
|
|
|
if (env_kms_cmd is not None) and (not env_kms_cmd == ''):
|
|
|
|
|
env_kms = Popen(shlex.split(env_kms_cmd), stdout=PIPE, stdin=sys.stderr).communicate()[0].decode('utf8', 'replace')
|
|
|
|
|
if env_kms[-1] == '\n':
|
|
|
|
|
env_kms = env_kms[:-1]
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## If the palette string is empty KMS is not utilised
|
2012-08-20 12:02:08 +02:00
|
|
|
|
return env_kms != ''
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
Returns the file name of the input pony converted to a KMS pony, or if KMS is not used, the input pony itself
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param pony:str Choosen pony file
|
|
|
|
|
@return :str Pony file to display
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def __kms(self, pony):
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## If not in Linux VT, return the pony as is
|
2012-10-30 01:31:17 +01:00
|
|
|
|
if not self.linuxvt:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
return pony
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## KMS support version constant
|
2012-08-21 19:35:55 +02:00
|
|
|
|
KMS_VERSION = '1'
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Read the PONYSAY_KMS_PALETTE environment variable
|
2012-08-20 02:22:12 +02:00
|
|
|
|
env_kms = os.environ['PONYSAY_KMS_PALETTE'] if 'PONYSAY_KMS_PALETTE' in os.environ else None
|
|
|
|
|
if env_kms is None: env_kms = ''
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Read the PONYSAY_KMS_PALETTE_CMD environment variable, and run it
|
2012-08-20 02:22:12 +02:00
|
|
|
|
env_kms_cmd = os.environ['PONYSAY_KMS_PALETTE_CMD'] if 'PONYSAY_KMS_PALETTE_CMD' in os.environ else None
|
|
|
|
|
if (env_kms_cmd is not None) and (not env_kms_cmd == ''):
|
|
|
|
|
env_kms = Popen(shlex.split(env_kms_cmd), stdout=PIPE, stdin=sys.stderr).communicate()[0].decode('utf8', 'replace')
|
|
|
|
|
if env_kms[-1] == '\n':
|
|
|
|
|
env_kms = env_kms[:-1]
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## If not using KMS, return the pony as is
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if env_kms == '':
|
|
|
|
|
return pony
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Store palette string and a clong with just the essentials
|
2012-08-20 02:22:12 +02:00
|
|
|
|
palette = env_kms
|
|
|
|
|
palettefile = env_kms.replace('\033]P', '')
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Get and in necessary make cache directory
|
2012-08-20 18:53:00 +02:00
|
|
|
|
cachedir = '/var/cache/ponysay'
|
2012-10-08 18:58:24 +02:00
|
|
|
|
shared = True
|
2012-08-20 18:53:00 +02:00
|
|
|
|
if not os.path.isdir(cachedir):
|
2012-10-30 01:31:17 +01:00
|
|
|
|
cachedir = self.HOME + '/.cache/ponysay'
|
2012-10-08 18:58:24 +02:00
|
|
|
|
shared = False
|
2012-08-20 18:53:00 +02:00
|
|
|
|
if not os.path.isdir(cachedir):
|
|
|
|
|
os.makedirs(cachedir)
|
2012-10-08 18:58:24 +02:00
|
|
|
|
_cachedir = '\'' + cachedir.replace('\'', '\'\\\'\'') + '\''
|
2012-08-20 18:53:00 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## KMS support version control, clean everything if not matching
|
2012-08-21 19:35:55 +02:00
|
|
|
|
newversion = False
|
|
|
|
|
if not os.path.isfile(cachedir + '/.version'):
|
|
|
|
|
newversion = True
|
|
|
|
|
else:
|
2012-09-30 00:53:16 +02:00
|
|
|
|
with open(cachedir + '/.version', 'rb') as cachev:
|
2012-10-08 18:58:24 +02:00
|
|
|
|
if cachev.read().decode('utf8', 'replace').replace('\n', '') != KMS_VERSION:
|
2012-08-21 19:35:55 +02:00
|
|
|
|
newversion = True
|
|
|
|
|
if newversion:
|
2012-08-21 19:45:35 +02:00
|
|
|
|
for cached in os.listdir(cachedir):
|
|
|
|
|
cached = cachedir + '/' + cached
|
|
|
|
|
if os.path.isdir(cached) and not os.path.islink(cached):
|
|
|
|
|
shutil.rmtree(cached, False)
|
|
|
|
|
else:
|
|
|
|
|
os.remove(cached)
|
2012-09-30 00:53:16 +02:00
|
|
|
|
with open(cachedir + '/.version', 'w+') as cachev:
|
2012-08-21 19:35:55 +02:00
|
|
|
|
cachev.write(KMS_VERSION)
|
2012-10-08 18:58:24 +02:00
|
|
|
|
if shared:
|
|
|
|
|
Popen('chmod 666 -- ' + _cachedir + '/.version', shell=True).wait()
|
2012-08-21 19:35:55 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Get kmspony directory and kmspony file
|
2012-08-20 18:53:00 +02:00
|
|
|
|
kmsponies = cachedir + '/kmsponies/' + palettefile
|
2012-08-20 02:22:12 +02:00
|
|
|
|
kmspony = (kmsponies + pony).replace('//', '/')
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## If the kmspony is missing, create it
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if not os.path.isfile(kmspony):
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Protokmsponies are uncolourful ttyponies
|
2012-08-20 18:53:00 +02:00
|
|
|
|
protokmsponies = cachedir + '/protokmsponies/'
|
2012-08-20 02:22:12 +02:00
|
|
|
|
protokmspony = (protokmsponies + pony).replace('//', '/')
|
|
|
|
|
protokmsponydir = protokmspony[:protokmspony.rindex('/')]
|
|
|
|
|
kmsponydir = kmspony[: kmspony.rindex('/')]
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Change file names to be shell friendly
|
2012-08-20 02:22:12 +02:00
|
|
|
|
_protokmspony = '\'' + protokmspony.replace('\'', '\'\\\'\'') + '\''
|
|
|
|
|
_kmspony = '\'' + kmspony.replace('\'', '\'\\\'\'') + '\''
|
|
|
|
|
_pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
|
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Create protokmspony is missing
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if not os.path.isfile(protokmspony):
|
|
|
|
|
if not os.path.isdir(protokmsponydir):
|
|
|
|
|
os.makedirs(protokmsponydir)
|
2012-10-08 18:58:24 +02:00
|
|
|
|
if shared:
|
|
|
|
|
Popen('chmod -R 6777 -- ' + _cachedir, shell=True).wait()
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if not os.system('ponysay2ttyponysay < ' + _pony + ' > ' + _protokmspony) == 0:
|
|
|
|
|
sys.stderr.write('Unable to run ponysay2ttyponysay successfully, you need util-say for KMS support\n')
|
|
|
|
|
exit(1)
|
2012-10-08 18:58:24 +02:00
|
|
|
|
if shared:
|
|
|
|
|
Popen('chmod 666 -- ' + _protokmspony, shell=True).wait()
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-09-30 02:33:58 +02:00
|
|
|
|
## Create kmspony
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if not os.path.isdir(kmsponydir):
|
|
|
|
|
os.makedirs(kmsponydir)
|
2012-10-08 18:58:24 +02:00
|
|
|
|
if shared:
|
|
|
|
|
Popen('chmod -R 6777 -- ' + _cachedir, shell=True).wait()
|
2012-08-20 12:02:08 +02:00
|
|
|
|
if not os.system('tty2colourfultty -p ' + palette + ' < ' + _protokmspony + ' > ' + _kmspony) == 0:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
sys.stderr.write('Unable to run tty2colourfultty successfully, you need util-say for KMS support\n')
|
|
|
|
|
exit(1)
|
2012-10-08 18:58:24 +02:00
|
|
|
|
if shared:
|
|
|
|
|
Popen('chmod 666 -- ' + _kmspony, shell=True).wait()
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
return kmspony
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-12 12:11:24 +02:00
|
|
|
|
'''
|
|
|
|
|
Option takes no arguments
|
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
ARGUMENTLESS = 0
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Option takes one argument per instance
|
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
ARGUMENTED = 1
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Option consumes all following arguments
|
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
VARIADIC = 2
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
Simple argument parser
|
|
|
|
|
'''
|
|
|
|
|
class ArgParser():
|
|
|
|
|
'''
|
|
|
|
|
Constructor.
|
|
|
|
|
The short description is printed on same line as the program name
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param program:str The name of the program
|
|
|
|
|
@param description:str Short, single-line, description of the program
|
|
|
|
|
@param usage:str Formated, multi-line, usage text
|
|
|
|
|
@param longdescription:str Long, multi-line, description of the program, may be `None`
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def __init__(self, program, description, usage, longdescription = None):
|
2012-10-30 01:31:17 +01:00
|
|
|
|
self.linuxvt = ('TERM' in os.environ) and (os.environ['TERM'] == 'linux')
|
2012-08-20 02:22:12 +02:00
|
|
|
|
self.__program = program
|
|
|
|
|
self.__description = description
|
|
|
|
|
self.__usage = usage
|
|
|
|
|
self.__longdescription = longdescription
|
|
|
|
|
self.__arguments = []
|
|
|
|
|
self.opts = {}
|
|
|
|
|
self.optmap = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Add option that takes no arguments
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param alternatives:list<str> Option names
|
|
|
|
|
@param help:str Short description, use `None` to hide the option
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def add_argumentless(self, alternatives, help = None):
|
|
|
|
|
ARGUMENTLESS
|
|
|
|
|
self.__arguments.append((ARGUMENTLESS, alternatives, None, help))
|
|
|
|
|
stdalt = alternatives[0]
|
|
|
|
|
self.opts[stdalt] = None
|
|
|
|
|
for alt in alternatives:
|
|
|
|
|
self.optmap[alt] = (stdalt, ARGUMENTLESS)
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Add option that takes one argument
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param alternatives:list<str> Option names
|
|
|
|
|
@param arg:str The name of the takes argument, one word
|
|
|
|
|
@param help:str Short description, use `None` to hide the option
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def add_argumented(self, alternatives, arg, help = None):
|
|
|
|
|
self.__arguments.append((ARGUMENTED, alternatives, arg, help))
|
|
|
|
|
stdalt = alternatives[0]
|
|
|
|
|
self.opts[stdalt] = None
|
|
|
|
|
for alt in alternatives:
|
|
|
|
|
self.optmap[alt] = (stdalt, ARGUMENTED)
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Add option that takes all following argument
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param alternatives:list<str> Option names
|
|
|
|
|
@param arg:str The name of the takes arguments, one word
|
|
|
|
|
@param help:str Short description, use `None` to hide the option
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def add_variadic(self, alternatives, arg, help = None):
|
|
|
|
|
self.__arguments.append((VARIADIC, alternatives, arg, help))
|
|
|
|
|
stdalt = alternatives[0]
|
|
|
|
|
self.opts[stdalt] = None
|
|
|
|
|
for alt in alternatives:
|
|
|
|
|
self.optmap[alt] = (stdalt, VARIADIC)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Parse arguments
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
2012-10-22 14:58:55 +02:00
|
|
|
|
@param args:list<str> The command line arguments, should include the execute file at index 0, `sys.argv` is default
|
|
|
|
|
@return :bool Whether no unrecognised option is used
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
def parse(self, argv = sys.argv):
|
|
|
|
|
self.argcount = len(argv) - 1
|
|
|
|
|
self.files = []
|
|
|
|
|
|
|
|
|
|
argqueue = []
|
|
|
|
|
optqueue = []
|
|
|
|
|
deque = []
|
|
|
|
|
for arg in argv[1:]:
|
|
|
|
|
deque.append(arg)
|
|
|
|
|
|
|
|
|
|
dashed = False
|
|
|
|
|
tmpdashed = False
|
|
|
|
|
get = 0
|
|
|
|
|
dontget = 0
|
2012-10-22 14:58:55 +02:00
|
|
|
|
self.rc = True
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
def unrecognised(arg):
|
|
|
|
|
sys.stderr.write('%s: warning: unrecognised option %s\n' % (self.__program, arg))
|
2012-10-22 14:58:55 +02:00
|
|
|
|
self.rc = False
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
while len(deque) != 0:
|
|
|
|
|
arg = deque[0]
|
|
|
|
|
deque = deque[1:]
|
|
|
|
|
if (get > 0) and (dontget == 0):
|
|
|
|
|
get -= 1
|
|
|
|
|
argqueue.append(arg)
|
|
|
|
|
elif tmpdashed:
|
|
|
|
|
self.files.append(arg)
|
|
|
|
|
tmpdashed = False
|
|
|
|
|
elif dashed: self.files.append(arg)
|
|
|
|
|
elif arg == '++': tmpdashed = True
|
|
|
|
|
elif arg == '--': dashed = True
|
2012-08-23 18:22:54 +02:00
|
|
|
|
elif (len(arg) > 1) and (arg[0] in ('-', '+')):
|
|
|
|
|
if (len(arg) > 2) and (arg[:2] in ('--', '++')):
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if dontget > 0:
|
|
|
|
|
dontget -= 1
|
|
|
|
|
elif (arg in self.optmap) and (self.optmap[arg][1] == ARGUMENTLESS):
|
|
|
|
|
optqueue.append(arg)
|
|
|
|
|
argqueue.append(None)
|
|
|
|
|
elif '=' in arg:
|
|
|
|
|
arg_opt = arg[:arg.index('=')]
|
|
|
|
|
if (arg_opt in self.optmap) and (self.optmap[arg_opt][1] >= ARGUMENTED):
|
|
|
|
|
optqueue.append(arg_opt)
|
|
|
|
|
argqueue.append(arg[arg.index('=') + 1:])
|
|
|
|
|
if self.optmap[arg_opt][1] == VARIADIC:
|
|
|
|
|
dashed = True
|
|
|
|
|
else:
|
|
|
|
|
unrecognised(arg)
|
|
|
|
|
elif (arg in self.optmap) and (self.optmap[arg][1] == ARGUMENTED):
|
|
|
|
|
optqueue.append(arg)
|
|
|
|
|
get += 1
|
|
|
|
|
elif (arg in self.optmap) and (self.optmap[arg][1] == VARIADIC):
|
|
|
|
|
optqueue.append(arg)
|
|
|
|
|
argqueue.append(None)
|
|
|
|
|
dashed = True
|
|
|
|
|
else:
|
|
|
|
|
unrecognised(arg)
|
|
|
|
|
else:
|
|
|
|
|
sign = arg[0]
|
|
|
|
|
i = 1
|
|
|
|
|
n = len(arg)
|
|
|
|
|
while i < n:
|
|
|
|
|
narg = sign + arg[i]
|
|
|
|
|
i += 1
|
|
|
|
|
if (narg in self.optmap):
|
|
|
|
|
if self.optmap[narg][1] == ARGUMENTLESS:
|
|
|
|
|
optqueue.append(narg)
|
|
|
|
|
argqueue.append(None)
|
|
|
|
|
elif self.optmap[narg][1] == ARGUMENTED:
|
|
|
|
|
optqueue.append(narg)
|
|
|
|
|
nargarg = arg[i:]
|
|
|
|
|
if len(nargarg) == 0:
|
|
|
|
|
get += 1
|
|
|
|
|
else:
|
|
|
|
|
argqueue.append(nargarg)
|
|
|
|
|
break
|
|
|
|
|
elif self.optmap[narg][1] == VARIADIC:
|
|
|
|
|
optqueue.append(narg)
|
|
|
|
|
nargarg = arg[i:]
|
|
|
|
|
argqueue.append(nargarg if len(nargarg) > 0 else None)
|
|
|
|
|
dashed = True
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
unrecognised(arg)
|
|
|
|
|
else:
|
|
|
|
|
self.files.append(arg)
|
|
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
n = len(optqueue)
|
|
|
|
|
while i < n:
|
|
|
|
|
opt = optqueue[i]
|
2012-10-29 20:29:25 +01:00
|
|
|
|
arg = argqueue[i] if len(argqueue) > i else None
|
2012-08-20 02:22:12 +02:00
|
|
|
|
i += 1
|
|
|
|
|
opt = self.optmap[opt][0]
|
|
|
|
|
if (opt not in self.opts) or (self.opts[opt] is None):
|
|
|
|
|
self.opts[opt] = []
|
2012-10-29 20:29:25 +01:00
|
|
|
|
if len(argqueue) >= i:
|
|
|
|
|
self.opts[opt].append(arg)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
for arg in self.__arguments:
|
2012-10-29 20:29:25 +01:00
|
|
|
|
if arg[0] == VARIADIC:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
varopt = self.opts[arg[1][0]]
|
|
|
|
|
if varopt is not None:
|
|
|
|
|
additional = ','.join(self.files).split(',') if len(self.files) > 0 else []
|
|
|
|
|
if varopt[0] is None:
|
|
|
|
|
self.opts[arg[1][0]] = additional
|
|
|
|
|
else:
|
|
|
|
|
self.opts[arg[1][0]] = varopt[0].split(',') + additional
|
|
|
|
|
self.files = []
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
self.message = ' '.join(self.files) if len(self.files) > 0 else None
|
2012-10-22 14:58:55 +02:00
|
|
|
|
|
|
|
|
|
return self.rc
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Prints a colourful help message
|
|
|
|
|
'''
|
|
|
|
|
def help(self):
|
2012-10-30 01:31:17 +01:00
|
|
|
|
print('\033[1m%s\033[21m %s %s' % (self.__program, '-' if self.linuxvt else '—', self.__description))
|
2012-08-20 02:22:12 +02:00
|
|
|
|
print()
|
|
|
|
|
if self.__longdescription is not None:
|
|
|
|
|
print(self.__longdescription)
|
|
|
|
|
print()
|
|
|
|
|
|
|
|
|
|
print('\033[1mUSAGE:\033[21m', end='')
|
|
|
|
|
first = True
|
|
|
|
|
for line in self.__usage.split('\n'):
|
|
|
|
|
if first:
|
|
|
|
|
first = False
|
|
|
|
|
else:
|
2012-08-21 15:57:48 +02:00
|
|
|
|
print(' or', end='')
|
2012-08-20 02:22:12 +02:00
|
|
|
|
print('\t%s' % (line))
|
|
|
|
|
print()
|
|
|
|
|
|
2012-10-30 01:31:17 +01:00
|
|
|
|
maxfirstlen = []
|
|
|
|
|
for opt in self.__arguments:
|
|
|
|
|
opt_alts = opt[1]
|
|
|
|
|
opt_help = opt[3]
|
|
|
|
|
if opt_help is None:
|
|
|
|
|
continue
|
|
|
|
|
first = opt_alts[0]
|
|
|
|
|
last = opt_alts[-1]
|
|
|
|
|
if first is not last:
|
|
|
|
|
maxfirstlen.append(first)
|
|
|
|
|
maxfirstlen = len(max(maxfirstlen, key = len))
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
print('\033[1mSYNOPSIS:\033[21m')
|
2012-08-26 18:57:10 +02:00
|
|
|
|
(lines, lens) = ([], [])
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for opt in self.__arguments:
|
|
|
|
|
opt_type = opt[0]
|
|
|
|
|
opt_alts = opt[1]
|
|
|
|
|
opt_arg = opt[2]
|
|
|
|
|
opt_help = opt[3]
|
|
|
|
|
if opt_help is None:
|
|
|
|
|
continue
|
2012-08-26 18:57:10 +02:00
|
|
|
|
(line, l) = ('', 0)
|
|
|
|
|
first = opt_alts[0]
|
|
|
|
|
last = opt_alts[-1]
|
2012-10-30 01:31:17 +01:00
|
|
|
|
alts = ['', last] if first is last else [first, last]
|
|
|
|
|
alts[0] += ' ' * (maxfirstlen - len(alts[0]))
|
2012-08-26 18:57:10 +02:00
|
|
|
|
for opt_alt in alts:
|
|
|
|
|
if opt_alt is alts[-1]:
|
|
|
|
|
line += '%colour%' + opt_alt
|
|
|
|
|
l += len(opt_alt)
|
|
|
|
|
if opt_type == ARGUMENTED: line += ' \033[4m%s\033[24m' % (opt_arg); l += len(opt_arg) + 1
|
|
|
|
|
elif opt_type == VARIADIC: line += ' [\033[4m%s\033[24m...]' % (opt_arg); l += len(opt_arg) + 6
|
2012-08-20 02:22:12 +02:00
|
|
|
|
else:
|
2012-08-26 18:57:10 +02:00
|
|
|
|
line += ' \033[2m%s\033[22m ' % (opt_alt)
|
|
|
|
|
l += len(opt_alt) + 6
|
|
|
|
|
lines.append(line)
|
|
|
|
|
lens.append(l)
|
|
|
|
|
|
|
|
|
|
col = max(lens)
|
|
|
|
|
col += 8 - ((col - 4) & 7)
|
|
|
|
|
index = 0
|
|
|
|
|
for opt in self.__arguments:
|
|
|
|
|
opt_help = opt[3]
|
|
|
|
|
if opt_help is None:
|
|
|
|
|
continue
|
2012-08-20 02:22:12 +02:00
|
|
|
|
first = True
|
2012-08-26 18:57:10 +02:00
|
|
|
|
colour = '36' if (index & 1) == 0 else '34'
|
|
|
|
|
print(lines[index].replace('%colour%', '\033[%s;1m' % (colour)), end=' ' * (col - lens[index]))
|
2012-08-20 02:22:12 +02:00
|
|
|
|
for line in opt_help.split('\n'):
|
|
|
|
|
if first:
|
|
|
|
|
first = False
|
2012-08-26 18:57:10 +02:00
|
|
|
|
print('%s' % (line), end='\033[21;39m\n')
|
2012-08-20 02:22:12 +02:00
|
|
|
|
else:
|
2012-08-26 18:57:10 +02:00
|
|
|
|
print('%s\033[%sm%s\033[39m' % (' ' * col, colour, line))
|
|
|
|
|
index += 1
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
print()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-21 14:36:01 +02:00
|
|
|
|
'''
|
|
|
|
|
Balloon format class
|
|
|
|
|
'''
|
|
|
|
|
class Balloon():
|
|
|
|
|
'''
|
|
|
|
|
Constructor
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param link:str The \-directional balloon line character
|
|
|
|
|
@param linkmirror:str The /-directional balloon line character
|
|
|
|
|
@param ww:str See the info manual
|
|
|
|
|
@param ee:str See the info manual
|
|
|
|
|
@param nw:list<str> See the info manual
|
|
|
|
|
@param nnw:list<str> See the info manual
|
|
|
|
|
@param n:list<str> See the info manual
|
|
|
|
|
@param nne:list<str> See the info manual
|
|
|
|
|
@param ne:list<str> See the info manual
|
|
|
|
|
@param nee:str See the info manual
|
|
|
|
|
@param e:str See the info manual
|
|
|
|
|
@param see:str See the info manual
|
|
|
|
|
@param se:list<str> See the info manual
|
|
|
|
|
@param sse:list<str> See the info manual
|
|
|
|
|
@param s:list<str> See the info manual
|
|
|
|
|
@param ssw:list<str> See the info manual
|
|
|
|
|
@param sw:list<str> See the info manual
|
|
|
|
|
@param sww:str See the info manual
|
|
|
|
|
@param w:str See the info manual
|
|
|
|
|
@param nww:str See the info manual
|
2012-08-21 14:36:01 +02:00
|
|
|
|
'''
|
|
|
|
|
def __init__(self, link, linkmirror, ww, ee, nw, nnw, n, nne, ne, nee, e, see, se, sse, s, ssw, sw, sww, w, nww):
|
|
|
|
|
(self.link, self.linkmirror) = (link, linkmirror)
|
|
|
|
|
(self.ww, self.ee) = (ww, ee)
|
|
|
|
|
(self.nw, self.ne, self.se, self.sw) = (nw, ne, se, sw)
|
|
|
|
|
(self.nnw, self.n, self.nne) = (nnw, n, nne)
|
|
|
|
|
(self.nee, self.e, self.see) = (nee, e, see)
|
|
|
|
|
(self.sse, self.s, self.ssw) = (sse, s, ssw)
|
|
|
|
|
(self.sww, self.w, self.nww) = (sww, w, nww)
|
|
|
|
|
|
2012-08-22 01:34:06 +02:00
|
|
|
|
_ne = max(ne, key = UCS.dispLen)
|
|
|
|
|
_nw = max(nw, key = UCS.dispLen)
|
|
|
|
|
_se = max(se, key = UCS.dispLen)
|
|
|
|
|
_sw = max(sw, key = UCS.dispLen)
|
2012-08-21 14:55:09 +02:00
|
|
|
|
|
2012-08-22 01:34:06 +02:00
|
|
|
|
minE = UCS.dispLen(max([_ne, nee, e, see, _se, ee], key = UCS.dispLen))
|
|
|
|
|
minW = UCS.dispLen(max([_nw, nww, e, sww, _sw, ww], key = UCS.dispLen))
|
2012-08-21 14:36:01 +02:00
|
|
|
|
minN = len(max([ne, nne, n, nnw, nw], key = len))
|
|
|
|
|
minS = len(max([se, sse, s, ssw, sw], key = len))
|
|
|
|
|
|
2012-10-24 19:40:18 +02:00
|
|
|
|
self.minwidth = minE + minE
|
2012-08-21 14:36:01 +02:00
|
|
|
|
self.minheight = minN + minS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Generates a balloon with a message
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param minw:int The minimum number of columns of the balloon
|
|
|
|
|
@param minh:int The minimum number of lines of the balloon
|
|
|
|
|
@param lines:list<str> The text lines to display
|
|
|
|
|
@param lencalc:int(str) Function used to compute the length of a text line
|
|
|
|
|
@return :str The balloon as a formated string
|
2012-08-21 14:36:01 +02:00
|
|
|
|
'''
|
|
|
|
|
def get(self, minw, minh, lines, lencalc):
|
2012-08-21 14:55:09 +02:00
|
|
|
|
h = self.minheight + len(lines)
|
|
|
|
|
w = self.minwidth + lencalc(max(lines, key = lencalc))
|
2012-08-21 14:36:01 +02:00
|
|
|
|
if w < minw: w = minw
|
|
|
|
|
if h < minh: h = minh
|
|
|
|
|
|
2012-08-21 14:55:09 +02:00
|
|
|
|
if len(lines) > 1:
|
|
|
|
|
(ws, es) = ({0 : self.nww, len(lines) - 1 : self.sww}, {0 : self.nee, len(lines) - 1 : self.see})
|
|
|
|
|
for j in range(1, len(lines) - 1):
|
|
|
|
|
ws[j] = self.w
|
|
|
|
|
es[j] = self.e
|
|
|
|
|
else:
|
|
|
|
|
(ws, es) = ({0 : self.ww}, {0 : self.ee})
|
|
|
|
|
|
2012-08-21 15:03:34 +02:00
|
|
|
|
rc = []
|
|
|
|
|
|
|
|
|
|
for j in range(0, len(self.n)):
|
2012-08-22 01:55:50 +02:00
|
|
|
|
outer = UCS.dispLen(self.nw[j]) + UCS.dispLen(self.ne[j])
|
|
|
|
|
inner = UCS.dispLen(self.nnw[j]) + UCS.dispLen(self.nne[j])
|
2012-10-25 00:06:30 +02:00
|
|
|
|
if outer + inner <= w:
|
2012-08-21 15:03:34 +02:00
|
|
|
|
rc.append(self.nw[j] + self.nnw[j] + self.n[j] * (w - outer - inner) + self.nne[j] + self.ne[j])
|
|
|
|
|
else:
|
|
|
|
|
rc.append(self.nw[j] + self.n[j] * (w - outer) + self.ne[j])
|
|
|
|
|
|
2012-08-21 14:55:09 +02:00
|
|
|
|
for j in range(0, len(lines)):
|
2012-08-22 01:55:50 +02:00
|
|
|
|
rc.append(ws[j] + lines[j] + ' ' * (w - lencalc(lines[j]) - UCS.dispLen(self.w) - UCS.dispLen(self.e)) + es[j])
|
2012-08-21 14:55:09 +02:00
|
|
|
|
|
|
|
|
|
for j in range(0, len(self.s)):
|
2012-08-22 01:55:50 +02:00
|
|
|
|
outer = UCS.dispLen(self.sw[j]) + UCS.dispLen(self.se[j])
|
|
|
|
|
inner = UCS.dispLen(self.ssw[j]) + UCS.dispLen(self.sse[j])
|
2012-10-25 00:06:30 +02:00
|
|
|
|
if outer + inner <= w:
|
2012-08-21 15:03:34 +02:00
|
|
|
|
rc.append(self.sw[j] + self.ssw[j] + self.s[j] * (w - outer - inner) + self.sse[j] + self.se[j])
|
|
|
|
|
else:
|
|
|
|
|
rc.append(self.sw[j] + self.s[j] * (w - outer) + self.se[j])
|
2012-08-21 14:55:09 +02:00
|
|
|
|
|
|
|
|
|
return '\n'.join(rc)
|
2012-08-21 14:36:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
2012-10-12 18:47:19 +02:00
|
|
|
|
Super-ultra-extreme-awesomazing replacement for cowsay
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
|
|
|
|
class Backend():
|
|
|
|
|
'''
|
|
|
|
|
Constructor
|
2012-10-12 16:46:27 +02:00
|
|
|
|
|
|
|
|
|
@param message:str The message spoken by the pony
|
|
|
|
|
@param ponyfile:str The pony file
|
|
|
|
|
@param wrapcolumn:int The column at where to wrap the message, `None` for no wrapping
|
|
|
|
|
@param width:int The width of the screen, `None` if truncation should not be applied
|
|
|
|
|
@param balloon:Balloon The balloon style object, `None` if only the pony should be printed
|
|
|
|
|
@param hyphen:str How hyphens added by the wordwrapper should be printed
|
|
|
|
|
@param linkcolour:str How to colour the link character, empty string if none
|
|
|
|
|
@param ballooncolour:str How to colour the balloon, empty string if none
|
2012-10-30 01:31:17 +01:00
|
|
|
|
@param mode:str Mode string for the pony
|
2012-08-20 02:22:12 +02:00
|
|
|
|
'''
|
2012-10-30 01:31:17 +01:00
|
|
|
|
def __init__(self, message, ponyfile, wrapcolumn, width, balloon, hyphen, linkcolour, ballooncolour, mode):
|
2012-08-20 02:22:12 +02:00
|
|
|
|
self.message = message
|
|
|
|
|
self.ponyfile = ponyfile
|
2012-10-28 15:41:42 +01:00
|
|
|
|
self.wrapcolumn = None if wrapcolumn is None else wrapcolumn - (0 if balloon is None else balloon.minwidth)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
self.width = width
|
2012-08-21 15:57:48 +02:00
|
|
|
|
self.balloon = balloon
|
2012-10-12 01:56:08 +02:00
|
|
|
|
self.hyphen = hyphen
|
|
|
|
|
self.ballooncolour = ballooncolour
|
2012-10-30 01:31:17 +01:00
|
|
|
|
self.mode = mode
|
2012-08-21 14:36:01 +02:00
|
|
|
|
|
2012-10-11 23:35:51 +02:00
|
|
|
|
if self.balloon is not None:
|
2012-10-12 01:56:08 +02:00
|
|
|
|
self.link = {'\\' : linkcolour + self.balloon.link,
|
|
|
|
|
'/' : linkcolour + self.balloon.linkmirror}
|
2012-10-11 23:35:51 +02:00
|
|
|
|
else:
|
|
|
|
|
self.link = {}
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-08-20 13:17:17 +02:00
|
|
|
|
self.output = ''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
self.pony = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Process all data
|
|
|
|
|
'''
|
|
|
|
|
def parse(self):
|
|
|
|
|
self.__expandMessage()
|
2012-10-25 00:06:30 +02:00
|
|
|
|
self.__unpadMessage()
|
2012-08-20 02:22:12 +02:00
|
|
|
|
self.__loadFile()
|
2012-10-25 00:06:30 +02:00
|
|
|
|
|
2012-10-23 23:10:35 +02:00
|
|
|
|
if self.pony.startswith('$$$\n'):
|
|
|
|
|
self.pony = self.pony[4:]
|
|
|
|
|
infoend = self.pony.index('\n$$$\n')
|
|
|
|
|
printinfo(self.pony[:infoend])
|
|
|
|
|
self.pony = self.pony[infoend + 5:]
|
2012-10-30 01:31:17 +01:00
|
|
|
|
self.pony = self.mode + self.pony
|
2012-10-25 00:06:30 +02:00
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
self.__processPony()
|
|
|
|
|
self.__truncate()
|
|
|
|
|
|
|
|
|
|
|
2012-10-25 00:06:30 +02:00
|
|
|
|
'''
|
|
|
|
|
Remove padding spaces fortune cookies are padded with whitespace (damn featherbrains)
|
|
|
|
|
'''
|
|
|
|
|
def __unpadMessage(self):
|
|
|
|
|
lines = self.message.split('\n')
|
|
|
|
|
for spaces in (8, 4, 2, 1):
|
|
|
|
|
padded = True
|
|
|
|
|
for line in lines:
|
|
|
|
|
if not line.startswith(' ' * spaces):
|
|
|
|
|
padded = False
|
|
|
|
|
break
|
|
|
|
|
if padded:
|
|
|
|
|
for i in range(0, len(lines)):
|
|
|
|
|
line = lines[i]
|
|
|
|
|
while line.startswith(' ' * spaces):
|
|
|
|
|
line = line[spaces:]
|
|
|
|
|
lines[i] = line
|
|
|
|
|
lines = [line.rstrip(' ') for line in lines]
|
|
|
|
|
self.message = '\n'.join(lines)
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
|
|
|
|
Converts all tabs in the message to spaces by expanding
|
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
def __expandMessage(self):
|
|
|
|
|
lines = self.message.split('\n')
|
|
|
|
|
buf = ''
|
|
|
|
|
for line in lines:
|
|
|
|
|
(i, n, x) = (0, len(line), 0)
|
|
|
|
|
while i < n:
|
|
|
|
|
c = line[i]
|
|
|
|
|
i += 1
|
|
|
|
|
if c == '\033':
|
2012-10-30 02:17:00 +01:00
|
|
|
|
colour = Backend.getcolour(line, i - 1)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
i += len(colour) - 1
|
|
|
|
|
buf += colour
|
|
|
|
|
elif c == '\t':
|
|
|
|
|
nx = 8 - (x & 7)
|
|
|
|
|
buf += ' ' * nx
|
|
|
|
|
x += nx
|
|
|
|
|
else:
|
|
|
|
|
buf += c
|
2012-08-22 01:55:50 +02:00
|
|
|
|
if not UCS.isCombining(c):
|
|
|
|
|
x += 1
|
2012-08-20 02:22:12 +02:00
|
|
|
|
buf += '\n'
|
|
|
|
|
self.message = buf[:-1]
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
|
|
|
|
Loads the pony file
|
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
def __loadFile(self):
|
2012-09-30 00:53:16 +02:00
|
|
|
|
with open(self.ponyfile, 'rb') as ponystream:
|
2012-08-27 02:31:14 +02:00
|
|
|
|
self.pony = ponystream.read().decode('utf8', 'replace')
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
|
|
|
|
Truncate output to the width of the screen
|
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
def __truncate(self):
|
|
|
|
|
if self.width is None:
|
|
|
|
|
return
|
|
|
|
|
lines = self.output.split('\n')
|
|
|
|
|
self.output = ''
|
|
|
|
|
for line in lines:
|
|
|
|
|
(i, n, x) = (0, len(line), 0)
|
|
|
|
|
while i < n:
|
|
|
|
|
c = line[i]
|
|
|
|
|
i += 1
|
|
|
|
|
if c == '\033':
|
2012-10-30 02:17:00 +01:00
|
|
|
|
colour = Backend.getcolour(line, i - 1)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
i += len(colour) - 1
|
|
|
|
|
self.output += colour
|
|
|
|
|
else:
|
|
|
|
|
if x < self.width:
|
|
|
|
|
self.output += c
|
2012-08-22 01:55:50 +02:00
|
|
|
|
if not UCS.isCombining(c):
|
|
|
|
|
x += 1
|
2012-08-20 02:22:12 +02:00
|
|
|
|
self.output += '\n'
|
|
|
|
|
self.output = self.output[:-1]
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
|
|
|
|
Process the pony file and generate output to self.output
|
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
def __processPony(self):
|
|
|
|
|
self.output = ''
|
|
|
|
|
|
2012-08-21 02:25:24 +02:00
|
|
|
|
AUTO_PUSH = '\033[01010~'
|
|
|
|
|
AUTO_POP = '\033[10101~'
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
variables = {'' : '$'}
|
|
|
|
|
for key in self.link:
|
2012-08-21 02:25:24 +02:00
|
|
|
|
variables[key] = AUTO_PUSH + self.link[key] + AUTO_POP
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
indent = 0
|
|
|
|
|
dollar = None
|
2012-08-21 01:46:24 +02:00
|
|
|
|
balloonLines = None
|
2012-08-21 02:25:24 +02:00
|
|
|
|
colourstack = ColourStack(AUTO_PUSH, AUTO_POP)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-08-21 01:46:24 +02:00
|
|
|
|
(i, n, lineindex, skip, nonskip) = (0, len(self.pony), 0, 0, 0)
|
2012-08-20 02:22:12 +02:00
|
|
|
|
while i < n:
|
|
|
|
|
c = self.pony[i]
|
|
|
|
|
if c == '\t':
|
2012-10-23 22:27:20 +02:00
|
|
|
|
n += 7 - (indent & 7)
|
|
|
|
|
ed = ' ' * (8 - (indent & 7))
|
2012-08-20 02:22:12 +02:00
|
|
|
|
c = ' '
|
2012-10-23 22:27:20 +02:00
|
|
|
|
self.pony = self.pony[:i] + ed + self.pony[i + 1:]
|
2012-08-20 02:22:12 +02:00
|
|
|
|
i += 1
|
|
|
|
|
if c == '$':
|
|
|
|
|
if dollar is not None:
|
|
|
|
|
if '=' in dollar:
|
2012-10-10 20:51:48 +02:00
|
|
|
|
name = dollar[:dollar.find('=')]
|
|
|
|
|
value = dollar[dollar.find('=') + 1:]
|
2012-08-20 02:22:12 +02:00
|
|
|
|
variables[name] = value
|
2012-09-30 02:55:48 +02:00
|
|
|
|
elif not dollar.startswith('balloon'):
|
2012-08-21 01:46:24 +02:00
|
|
|
|
data = variables[dollar].replace('$', '$$')
|
|
|
|
|
if data == '$$': # if not handled specially we will get an infinity loop
|
|
|
|
|
if (skip == 0) or (nonskip > 0):
|
|
|
|
|
if nonskip > 0:
|
|
|
|
|
nonskip -= 1
|
|
|
|
|
self.output += '$'
|
|
|
|
|
indent += 1
|
2012-08-20 02:22:12 +02:00
|
|
|
|
else:
|
2012-08-21 01:46:24 +02:00
|
|
|
|
skip -= 1
|
|
|
|
|
else:
|
|
|
|
|
n += len(data)
|
|
|
|
|
self.pony = self.pony[:i] + data + self.pony[i:]
|
2012-10-11 23:35:51 +02:00
|
|
|
|
elif self.balloon is not None:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
(w, h) = (0, 0)
|
|
|
|
|
props = dollar[7:]
|
2012-08-20 03:34:54 +02:00
|
|
|
|
if len(props) > 0:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
if ',' in props:
|
|
|
|
|
if props[0] is not ',':
|
|
|
|
|
w = int(props[:props.index(',')])
|
|
|
|
|
h = int(props[props.index(',') + 1:])
|
|
|
|
|
else:
|
|
|
|
|
w = int(props)
|
2012-09-01 06:52:42 +02:00
|
|
|
|
balloon = self.__getballoon(w, h, indent)
|
|
|
|
|
balloon = balloon.split('\n')
|
2012-10-12 01:56:08 +02:00
|
|
|
|
balloon = [AUTO_PUSH + self.ballooncolour + item + AUTO_POP for item in balloon]
|
2012-08-21 02:25:24 +02:00
|
|
|
|
for b in balloon[0]:
|
|
|
|
|
self.output += b + colourstack.feed(b)
|
2012-08-21 01:46:24 +02:00
|
|
|
|
if lineindex == 0:
|
|
|
|
|
balloonpre = '\n' + (' ' * indent)
|
|
|
|
|
for line in balloon[1:]:
|
|
|
|
|
self.output += balloonpre;
|
2012-08-21 02:25:24 +02:00
|
|
|
|
for b in line:
|
|
|
|
|
self.output += b + colourstack.feed(b);
|
2012-08-21 01:46:24 +02:00
|
|
|
|
indent = 0
|
|
|
|
|
elif len(balloon) > 1:
|
|
|
|
|
balloonLines = balloon
|
|
|
|
|
balloonLine = 0
|
|
|
|
|
balloonIndent = indent
|
2012-10-30 02:17:00 +01:00
|
|
|
|
indent += Backend.len(balloonLines[0])
|
2012-08-21 01:46:24 +02:00
|
|
|
|
balloonLines[0] = None
|
2012-08-20 02:22:12 +02:00
|
|
|
|
dollar = None
|
|
|
|
|
else:
|
|
|
|
|
dollar = ''
|
|
|
|
|
elif dollar is not None:
|
|
|
|
|
if c == '\033':
|
|
|
|
|
c = self.pony[i]
|
|
|
|
|
i += 1
|
|
|
|
|
dollar += c
|
|
|
|
|
elif c == '\033':
|
2012-10-30 02:17:00 +01:00
|
|
|
|
colour = Backend.getcolour(self.pony, i - 1)
|
2012-08-21 02:25:24 +02:00
|
|
|
|
for b in colour:
|
|
|
|
|
self.output += b + colourstack.feed(b);
|
2012-08-20 02:22:12 +02:00
|
|
|
|
i += len(colour) - 1
|
|
|
|
|
elif c == '\n':
|
|
|
|
|
self.output += c
|
|
|
|
|
indent = 0
|
2012-08-21 01:46:24 +02:00
|
|
|
|
(skip, nonskip) = (0, 0)
|
|
|
|
|
lineindex += 1
|
|
|
|
|
if balloonLines is not None:
|
|
|
|
|
balloonLine += 1
|
|
|
|
|
if balloonLine == len(balloonLines):
|
|
|
|
|
balloonLines = None
|
2012-08-20 02:22:12 +02:00
|
|
|
|
else:
|
2012-08-21 01:46:24 +02:00
|
|
|
|
if (balloonLines is not None) and (balloonLines[balloonLine] is not None) and (balloonIndent == indent):
|
|
|
|
|
data = balloonLines[balloonLine]
|
2012-10-30 02:17:00 +01:00
|
|
|
|
datalen = Backend.len(data)
|
2012-08-21 01:46:24 +02:00
|
|
|
|
skip += datalen
|
|
|
|
|
nonskip += datalen
|
2012-08-21 15:57:48 +02:00
|
|
|
|
data = data.replace('$', '$$')
|
2012-08-21 01:46:24 +02:00
|
|
|
|
n += len(data)
|
|
|
|
|
self.pony = self.pony[:i] + data + self.pony[i:]
|
|
|
|
|
balloonLines[balloonLine] = None
|
|
|
|
|
else:
|
|
|
|
|
if (skip == 0) or (nonskip > 0):
|
|
|
|
|
if nonskip > 0:
|
|
|
|
|
nonskip -= 1
|
2012-08-21 02:25:24 +02:00
|
|
|
|
self.output += c + colourstack.feed(c);
|
2012-08-22 01:55:50 +02:00
|
|
|
|
if not UCS.isCombining(c):
|
|
|
|
|
indent += 1
|
2012-08-21 01:46:24 +02:00
|
|
|
|
else:
|
|
|
|
|
skip -= 1
|
|
|
|
|
|
|
|
|
|
if balloonLines is not None:
|
|
|
|
|
for line in balloonLines[balloonLine:]:
|
2012-08-21 02:25:24 +02:00
|
|
|
|
data = ' ' * (balloonIndent - indent) + line + '\n'
|
|
|
|
|
for b in data:
|
|
|
|
|
self.output += b + colourstack.feed(b);
|
2012-08-21 01:46:24 +02:00
|
|
|
|
indent = 0
|
2012-08-21 02:25:24 +02:00
|
|
|
|
|
|
|
|
|
self.output = self.output.replace(AUTO_PUSH, '').replace(AUTO_POP, '')
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
2012-08-20 13:17:17 +02:00
|
|
|
|
Gets colour code att the currect offset in a buffer
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param input:str The input buffer
|
|
|
|
|
@param offset:int The offset at where to start reading, a escape must begin here
|
|
|
|
|
@return :str The escape sequence
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
2012-10-30 02:17:00 +01:00
|
|
|
|
@staticmethod
|
|
|
|
|
def getcolour(input, offset):
|
2012-08-20 02:22:12 +02:00
|
|
|
|
(i, n) = (offset, len(input))
|
|
|
|
|
rc = input[i]
|
|
|
|
|
i += 1
|
|
|
|
|
if i == n: return rc
|
|
|
|
|
c = input[i]
|
|
|
|
|
i += 1
|
|
|
|
|
rc += c
|
|
|
|
|
|
|
|
|
|
if c == ']':
|
|
|
|
|
if i == n: return rc
|
|
|
|
|
c = input[i]
|
|
|
|
|
i += 1
|
|
|
|
|
rc += c
|
|
|
|
|
if c == 'P':
|
|
|
|
|
di = 0
|
|
|
|
|
while (di < 7) and (i < n):
|
|
|
|
|
c = input[i]
|
|
|
|
|
i += 1
|
|
|
|
|
di += 1
|
|
|
|
|
rc += c
|
|
|
|
|
elif c == '[':
|
|
|
|
|
while i < n:
|
|
|
|
|
c = input[i]
|
|
|
|
|
i += 1
|
|
|
|
|
rc += c
|
|
|
|
|
if (c == '~') or (('a' <= c) and (c <= 'z')) or (('A' <= c) and (c <= 'Z')):
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
return rc
|
|
|
|
|
|
|
|
|
|
|
2012-08-21 01:46:24 +02:00
|
|
|
|
'''
|
|
|
|
|
Calculates the number of visible characters in a text
|
2012-10-12 12:11:24 +02:00
|
|
|
|
|
|
|
|
|
@param input:str The input buffer
|
|
|
|
|
@return :int The number of visible characters
|
2012-08-21 01:46:24 +02:00
|
|
|
|
'''
|
2012-10-30 02:17:00 +01:00
|
|
|
|
@staticmethod
|
|
|
|
|
def len(input):
|
2012-08-21 01:46:24 +02:00
|
|
|
|
(rc, i, n) = (0, 0, len(input))
|
|
|
|
|
while i < n:
|
|
|
|
|
c = input[i]
|
|
|
|
|
if c == '\033':
|
2012-10-30 02:17:00 +01:00
|
|
|
|
i += len(Backend.getcolour(input, i))
|
2012-08-21 01:46:24 +02:00
|
|
|
|
else:
|
|
|
|
|
i += 1
|
2012-08-22 01:34:06 +02:00
|
|
|
|
if not UCS.isCombining(c):
|
|
|
|
|
rc += 1
|
2012-08-21 01:46:24 +02:00
|
|
|
|
return rc
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
|
|
|
|
Generates a balloon with the message
|
2012-10-12 16:46:27 +02:00
|
|
|
|
|
|
|
|
|
@param width:int The minimum width of the balloon
|
|
|
|
|
@param height:int The minimum height of the balloon
|
|
|
|
|
@param left:int The column where the balloon starts
|
|
|
|
|
@return :str The balloon the the message as a string
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
def __getballoon(self, width, height, left):
|
|
|
|
|
wrap = None
|
|
|
|
|
if self.wrapcolumn is not None:
|
|
|
|
|
wrap = self.wrapcolumn - left
|
2012-08-21 04:45:05 +02:00
|
|
|
|
if wrap < 8:
|
|
|
|
|
wrap = 8
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
msg = self.message
|
|
|
|
|
if wrap is not None:
|
|
|
|
|
msg = self.__wrapMessage(msg, wrap)
|
|
|
|
|
|
2012-10-12 02:59:43 +02:00
|
|
|
|
msg = msg.replace('\n', '\033[0m%s\n' % (self.ballooncolour)) + '\033[0m' + self.ballooncolour
|
2012-08-27 01:26:03 +02:00
|
|
|
|
|
2012-10-30 02:17:00 +01:00
|
|
|
|
return self.balloon.get(width, height, msg.split('\n'), Backend.len);
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
|
|
|
|
Wraps the message
|
2012-10-12 16:46:27 +02:00
|
|
|
|
|
|
|
|
|
@param message:str The message to wrap
|
|
|
|
|
@param wrap:int The width at where to force wrapping
|
|
|
|
|
@return :str The message wrapped
|
2012-08-20 12:07:21 +02:00
|
|
|
|
'''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
def __wrapMessage(self, message, wrap):
|
2012-10-28 04:46:51 +01:00
|
|
|
|
wraplimit = os.environ['PONYSAY_WRAP_LIMIT'] if 'PONYSAY_WRAP_LIMIT' in os.environ else ''
|
|
|
|
|
wraplimit = 8 if len(wraplimit) == 0 else int(wraplimit)
|
|
|
|
|
|
|
|
|
|
wrapexceed = os.environ['PONYSAY_WRAP_EXCEED'] if 'PONYSAY_WRAP_EXCEED' in os.environ else ''
|
|
|
|
|
wrapexceed = 5 if len(wrapexceed) == 0 else int(wrapexceed)
|
|
|
|
|
|
2012-10-12 02:59:43 +02:00
|
|
|
|
buf = ''
|
2012-10-25 00:06:30 +02:00
|
|
|
|
try:
|
|
|
|
|
AUTO_PUSH = '\033[01010~'
|
|
|
|
|
AUTO_POP = '\033[10101~'
|
|
|
|
|
msg = message.replace('\n', AUTO_PUSH + '\n' + AUTO_POP);
|
|
|
|
|
cstack = ColourStack(AUTO_PUSH, AUTO_POP)
|
|
|
|
|
for c in msg:
|
|
|
|
|
buf += c + cstack.feed(c)
|
|
|
|
|
lines = buf.replace(AUTO_PUSH, '').replace(AUTO_POP, '').split('\n')
|
|
|
|
|
buf = ''
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-10-25 00:06:30 +02:00
|
|
|
|
for line in lines:
|
|
|
|
|
b = [None] * len(line)
|
|
|
|
|
map = {0 : 0}
|
|
|
|
|
(bi, cols, w) = (0, 0, wrap)
|
|
|
|
|
(indent, indentc) = (-1, 0)
|
|
|
|
|
|
|
|
|
|
(i, n) = (0, len(line))
|
|
|
|
|
while i <= n:
|
|
|
|
|
d = None
|
|
|
|
|
if i < n:
|
|
|
|
|
d = line[i]
|
2012-08-20 02:22:12 +02:00
|
|
|
|
i += 1
|
2012-10-30 02:17:00 +01:00
|
|
|
|
if d == '\033': # TODO this should use Backend.getcolour()
|
2012-10-25 00:06:30 +02:00
|
|
|
|
## Invisible stuff
|
|
|
|
|
b[bi] = d
|
|
|
|
|
bi += 1
|
2012-08-20 02:22:12 +02:00
|
|
|
|
b[bi] = line[i]
|
|
|
|
|
d = line[i]
|
|
|
|
|
bi += 1
|
|
|
|
|
i += 1
|
2012-10-25 00:06:30 +02:00
|
|
|
|
if d == '[':
|
|
|
|
|
while True:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
b[bi] = line[i]
|
2012-10-25 00:06:30 +02:00
|
|
|
|
d = line[i]
|
2012-08-20 02:22:12 +02:00
|
|
|
|
bi += 1
|
|
|
|
|
i += 1
|
2012-10-25 00:06:30 +02:00
|
|
|
|
if (('a' <= d) and (d <= 'z')) or (('A' <= d) and (d <= 'Z')) or (d == '~'):
|
|
|
|
|
break
|
|
|
|
|
elif d == ']':
|
|
|
|
|
b[bi] = line[i]
|
|
|
|
|
d = line[i]
|
|
|
|
|
bi += 1
|
|
|
|
|
i += 1
|
|
|
|
|
if d == 'P':
|
|
|
|
|
for j in range(0, 7):
|
|
|
|
|
b[bi] = line[i]
|
|
|
|
|
bi += 1
|
|
|
|
|
i += 1
|
|
|
|
|
elif (d is not None) and (d != ' '):
|
|
|
|
|
## Fetch word
|
|
|
|
|
if indent == -1:
|
|
|
|
|
indent = i - 1
|
|
|
|
|
for j in range(0, indent):
|
|
|
|
|
if line[j] == ' ':
|
|
|
|
|
indentc += 1
|
|
|
|
|
b[bi] = d
|
|
|
|
|
bi += 1
|
|
|
|
|
if (not UCS.isCombining(d)) and (d != ''):
|
|
|
|
|
cols += 1
|
|
|
|
|
map[cols] = bi
|
|
|
|
|
else:
|
|
|
|
|
## Wrap?
|
|
|
|
|
mm = 0
|
|
|
|
|
bisub = 0
|
|
|
|
|
iwrap = wrap - (0 if indent == 1 else indentc)
|
2012-10-28 04:46:51 +01:00
|
|
|
|
|
|
|
|
|
while ((w > wraplimit) and (cols > w + wrapexceed)) or (cols > iwrap):
|
2012-10-25 00:06:30 +02:00
|
|
|
|
## wrap
|
|
|
|
|
x = w;
|
2012-10-28 04:46:51 +01:00
|
|
|
|
if mm + x not in map: # Too much whitespace?
|
2012-10-25 00:06:30 +02:00
|
|
|
|
cols = 0
|
|
|
|
|
break
|
|
|
|
|
nbsp = b[map[mm + x]] == ' '
|
2012-10-04 04:46:55 +02:00
|
|
|
|
m = map[mm + x]
|
2012-10-25 00:06:30 +02:00
|
|
|
|
|
|
|
|
|
if ('' in b[bisub : m]) and not nbsp:
|
|
|
|
|
hyphen = m - 1
|
|
|
|
|
while b[hyphen] != '':
|
|
|
|
|
hyphen -= 1
|
|
|
|
|
while map[mm + x] > hyphen: ## Only looking backward, if foreward is required the word is probabily not hyphenated correctly
|
|
|
|
|
x -= 1
|
|
|
|
|
x += 1
|
|
|
|
|
m = map[mm + x]
|
|
|
|
|
|
|
|
|
|
mm += x - (0 if nbsp else 1) ## − 1 so we have space for a hythen
|
|
|
|
|
|
|
|
|
|
for bb in b[bisub : m]:
|
|
|
|
|
buf += bb
|
|
|
|
|
buf += '\n' if nbsp else '\0\n'
|
|
|
|
|
cols -= x - (0 if nbsp else 1)
|
|
|
|
|
bisub = m
|
|
|
|
|
|
|
|
|
|
w = iwrap
|
|
|
|
|
if indent != -1:
|
|
|
|
|
buf += line[:indent]
|
2012-10-04 04:46:55 +02:00
|
|
|
|
|
2012-10-25 00:06:30 +02:00
|
|
|
|
for j in range(bisub, bi):
|
|
|
|
|
b[j - bisub] = b[j]
|
|
|
|
|
bi -= bisub
|
2012-10-04 04:46:55 +02:00
|
|
|
|
|
2012-10-25 00:06:30 +02:00
|
|
|
|
if cols > w:
|
2012-08-20 02:22:12 +02:00
|
|
|
|
buf += '\n'
|
|
|
|
|
w = wrap
|
|
|
|
|
if indent != -1:
|
2012-10-25 00:06:30 +02:00
|
|
|
|
buf += line[:indent]
|
2012-08-20 02:22:12 +02:00
|
|
|
|
w -= indentc
|
2012-10-25 00:06:30 +02:00
|
|
|
|
for bb in b[:bi]:
|
2012-10-28 04:46:51 +01:00
|
|
|
|
if bb is not None:
|
|
|
|
|
buf += bb
|
2012-10-25 00:06:30 +02:00
|
|
|
|
w -= cols
|
|
|
|
|
cols = 0
|
|
|
|
|
bi = 0
|
|
|
|
|
if d is None:
|
|
|
|
|
i += 1
|
|
|
|
|
else:
|
|
|
|
|
if w > 0:
|
|
|
|
|
buf += ' '
|
|
|
|
|
w -= 1
|
|
|
|
|
else:
|
|
|
|
|
buf += '\n'
|
|
|
|
|
w = wrap
|
|
|
|
|
if indent != -1:
|
|
|
|
|
buf + line[:indent]
|
|
|
|
|
w -= indentc
|
|
|
|
|
buf += '\n'
|
|
|
|
|
|
|
|
|
|
rc = '\n'.join(line.rstrip() for line in buf[:-1].split('\n'));
|
|
|
|
|
rc = rc.replace('', ''); # remove soft hyphens
|
|
|
|
|
rc = rc.replace('\0', '%s%s%s' % (AUTO_PUSH, self.hyphen, AUTO_POP))
|
|
|
|
|
return rc
|
|
|
|
|
except Exception as err:
|
|
|
|
|
import traceback
|
|
|
|
|
errormessage = ''.join(traceback.format_exception(type(err), err, None))
|
|
|
|
|
rc = '\n'.join(line.rstrip() for line in buf.split('\n'));
|
|
|
|
|
rc = rc.replace('\0', '%s%s%s' % (AUTO_PUSH, self.hyphen, AUTO_POP))
|
|
|
|
|
errormessage += '\n---- WRAPPING BUFFER ----\n\n' + rc
|
|
|
|
|
try:
|
|
|
|
|
if os.readlink('/proc/self/fd/2') != os.readlink('/proc/self/fd/1'):
|
|
|
|
|
printerr(errormessage, end='')
|
|
|
|
|
return message
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
return message + '\n\n\033[0;1;31m---- EXCEPTION IN PONYSAY WHILE WRAPPING ----\033[0m\n\n' + errormessage
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-21 02:25:24 +02:00
|
|
|
|
'''
|
|
|
|
|
ANSI colour stack
|
2012-10-12 16:46:27 +02:00
|
|
|
|
|
|
|
|
|
This is used to make layers with independent coloursations
|
2012-08-21 02:25:24 +02:00
|
|
|
|
'''
|
|
|
|
|
class ColourStack():
|
|
|
|
|
'''
|
|
|
|
|
Constructor
|
2012-10-12 16:46:27 +02:00
|
|
|
|
|
|
|
|
|
@param autopush:str String that, when used, will create a new independently colourised layer
|
|
|
|
|
@param autopop:str String that, when used, will end the current layer and continue of the previous layer
|
2012-08-21 02:25:24 +02:00
|
|
|
|
'''
|
|
|
|
|
def __init__(self, autopush, autopop):
|
2012-08-21 02:53:01 +02:00
|
|
|
|
self.autopush = autopush
|
2012-09-01 06:52:42 +02:00
|
|
|
|
self.autopop = autopop
|
|
|
|
|
self.lenpush = len(autopush)
|
|
|
|
|
self.lenpop = len(autopop)
|
2012-08-21 02:53:01 +02:00
|
|
|
|
self.bufproto = ' ' * (self.lenpush if self.lenpush > self.lenpop else self.lenpop)
|
2012-09-01 06:52:42 +02:00
|
|
|
|
self.stack = []
|
2012-08-21 02:53:01 +02:00
|
|
|
|
self.push()
|
2012-09-01 06:52:42 +02:00
|
|
|
|
self.seq = None
|
2012-08-21 02:53:01 +02:00
|
|
|
|
|
2012-08-21 02:25:24 +02:00
|
|
|
|
|
2012-10-12 16:46:27 +02:00
|
|
|
|
'''
|
|
|
|
|
Create a new independently colourised layer
|
|
|
|
|
|
|
|
|
|
@return :str String that should be inserted into your buffer
|
|
|
|
|
'''
|
2012-08-21 02:25:24 +02:00
|
|
|
|
def push(self):
|
2012-10-25 00:06:30 +02:00
|
|
|
|
self.stack.insert(0, [self.bufproto, None, None, [False] * 9])
|
2012-08-21 02:53:01 +02:00
|
|
|
|
if len(self.stack) == 1:
|
2012-08-21 03:51:34 +02:00
|
|
|
|
return None
|
2012-09-01 06:52:42 +02:00
|
|
|
|
return '\033[0m'
|
2012-08-21 02:25:24 +02:00
|
|
|
|
|
2012-08-21 02:53:01 +02:00
|
|
|
|
|
2012-10-12 16:46:27 +02:00
|
|
|
|
'''
|
|
|
|
|
End the current layer and continue of the previous layer
|
|
|
|
|
|
|
|
|
|
@return :str String that should be inserted into your buffer
|
|
|
|
|
'''
|
2012-08-21 02:25:24 +02:00
|
|
|
|
def pop(self):
|
2012-10-25 00:06:30 +02:00
|
|
|
|
old = self.stack.pop(0)
|
2012-09-01 06:52:42 +02:00
|
|
|
|
rc = '\033[0;'
|
2012-10-25 00:06:30 +02:00
|
|
|
|
if len(self.stack) == 0: # last resort in case something made it pop too mush
|
|
|
|
|
push()
|
2012-08-21 03:51:34 +02:00
|
|
|
|
new = self.stack[0]
|
|
|
|
|
if new[1] is not None: rc += new[1] + ';'
|
|
|
|
|
if new[2] is not None: rc += new[2] + ';'
|
|
|
|
|
for i in range(0, 9):
|
|
|
|
|
if new[3][i]:
|
|
|
|
|
rc += str(i + 1) + ';'
|
2012-09-01 06:52:42 +02:00
|
|
|
|
return rc[:-1] + 'm'
|
2012-08-21 02:25:24 +02:00
|
|
|
|
|
2012-08-21 02:53:01 +02:00
|
|
|
|
|
2012-10-12 16:46:27 +02:00
|
|
|
|
'''
|
|
|
|
|
Use this, in sequence, for which character in your buffer that contains yor autopush and autopop
|
|
|
|
|
string, the automatically get push and pop string to insert after each character
|
|
|
|
|
|
|
|
|
|
@param :chr One character in your buffer
|
|
|
|
|
@return :str The text to insert after the input character
|
|
|
|
|
'''
|
2012-08-21 02:25:24 +02:00
|
|
|
|
def feed(self, char):
|
2012-08-21 03:51:34 +02:00
|
|
|
|
if self.seq is not None:
|
|
|
|
|
self.seq += char
|
|
|
|
|
if (char == '~') or (('a' <= char) and (char <= 'z')) or (('A' <= char) and (char <= 'Z')):
|
|
|
|
|
if (self.seq[0] == '[') and (self.seq[-1] == 'm'):
|
|
|
|
|
self.seq = self.seq[1:-1].split(';')
|
|
|
|
|
(i, n) = (0, len(self.seq))
|
|
|
|
|
while i < n:
|
|
|
|
|
part = self.seq[i]
|
|
|
|
|
p = 0 if part == '' else int(part)
|
|
|
|
|
i += 1
|
2012-08-21 17:26:21 +02:00
|
|
|
|
if p == 0: self.stack[0][1:] = [None, None, [False] * 9]
|
2012-08-21 03:51:34 +02:00
|
|
|
|
elif (1 <= p) and (p <= 9): self.stack[0][3][p - 1] = True
|
|
|
|
|
elif (21 <= p) and (p <= 29): self.stack[0][3][p - 21] = False
|
|
|
|
|
elif p == 39: self.stack[0][1] = None
|
2012-08-21 19:48:24 +02:00
|
|
|
|
elif p == 49: self.stack[0][2] = None
|
2012-08-21 03:51:34 +02:00
|
|
|
|
elif (30 <= p) and (p <= 37): self.stack[0][1] = part
|
|
|
|
|
elif (90 <= p) and (p <= 97): self.stack[0][1] = part
|
|
|
|
|
elif (40 <= p) and (p <= 47): self.stack[0][2] = part
|
|
|
|
|
elif (100 <= p) and (p <= 107): self.stack[0][2] = part
|
|
|
|
|
elif p == 38:
|
|
|
|
|
self.stack[0][1] = '%s;%s;%s' % (part, self.seq[i], self.seq[i + 1])
|
|
|
|
|
i += 2
|
|
|
|
|
elif p == 48:
|
|
|
|
|
self.stack[0][2] = '%s;%s;%s' % (part, self.seq[i], self.seq[i + 1])
|
|
|
|
|
i += 2
|
|
|
|
|
self.seq = None
|
|
|
|
|
elif char == '\033':
|
|
|
|
|
self.seq = ''
|
2012-08-21 02:53:01 +02:00
|
|
|
|
buf = self.stack[0][0]
|
2012-08-21 03:51:34 +02:00
|
|
|
|
buf = buf[1:] + char
|
2012-08-21 02:53:01 +02:00
|
|
|
|
rc = ''
|
2012-09-01 06:52:42 +02:00
|
|
|
|
if buf[-self.lenpush:] == self.autopush: rc = self.push()
|
|
|
|
|
elif buf[-self.lenpop:] == self.autopop: rc = self.pop()
|
2012-08-21 03:51:34 +02:00
|
|
|
|
self.stack[0][0] = buf
|
2012-08-21 02:53:01 +02:00
|
|
|
|
return rc
|
2012-08-21 02:25:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-22 01:34:06 +02:00
|
|
|
|
'''
|
|
|
|
|
UCS utility class
|
|
|
|
|
'''
|
|
|
|
|
class UCS():
|
|
|
|
|
'''
|
|
|
|
|
Checks whether a character is a combining character
|
2012-10-12 16:46:27 +02:00
|
|
|
|
|
|
|
|
|
@param char:chr The character to test
|
|
|
|
|
@return :bool Whether the character is a combining character
|
2012-08-22 01:34:06 +02:00
|
|
|
|
'''
|
|
|
|
|
@staticmethod
|
|
|
|
|
def isCombining(char):
|
|
|
|
|
o = ord(char)
|
|
|
|
|
if (0x0300 <= o) and (o <= 0x036F): return True
|
|
|
|
|
if (0x20D0 <= o) and (o <= 0x20FF): return True
|
|
|
|
|
if (0x1DC0 <= o) and (o <= 0x1DFF): return True
|
|
|
|
|
if (0xFE20 <= o) and (o <= 0xFE2F): return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Gets the number of combining characters in a string
|
2012-10-12 16:46:27 +02:00
|
|
|
|
|
|
|
|
|
@param string:str A text to count combining characters in
|
|
|
|
|
@return :int The number of combining characters in the string
|
2012-08-22 01:34:06 +02:00
|
|
|
|
'''
|
|
|
|
|
@staticmethod
|
|
|
|
|
def countCombining(string):
|
|
|
|
|
rc = 0
|
|
|
|
|
for char in string:
|
|
|
|
|
if UCS.isCombining(char):
|
|
|
|
|
rc += 1
|
|
|
|
|
return rc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Gets length of a string not counting combining characters
|
2012-10-12 16:46:27 +02:00
|
|
|
|
|
|
|
|
|
@param string:str The text of which to determine the monospaced width
|
|
|
|
|
@return The determine the monospaced width of the text, provided it does not have escape sequnces
|
2012-08-22 01:34:06 +02:00
|
|
|
|
'''
|
|
|
|
|
@staticmethod
|
|
|
|
|
def dispLen(string):
|
|
|
|
|
return len(string) - UCS.countCombining(string)
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 02:22:12 +02:00
|
|
|
|
|
2012-10-04 22:11:33 +02:00
|
|
|
|
'''
|
|
|
|
|
Class used for correcting spellos and typos,
|
|
|
|
|
|
|
|
|
|
Note that this implementation will not find that correctly spelled word are correct faster than it corrects words.
|
|
|
|
|
It is also limited to words of size 0 to 127 (inclusive)
|
|
|
|
|
'''
|
2012-10-12 12:11:24 +02:00
|
|
|
|
class SpelloCorrecter(): # Naïvely and quickly proted and adapted from optimised Java, may not be the nicest, or even fast, Python code
|
2012-10-12 16:46:27 +02:00
|
|
|
|
'''
|
|
|
|
|
Constructor
|
|
|
|
|
|
|
|
|
|
@param directories:list<str> List of directories that contains the file names with the correct spelling
|
|
|
|
|
@param ending:str The file name ending of the correctly spelled file names, this is removed for the name
|
|
|
|
|
'''
|
2012-10-04 22:11:33 +02:00
|
|
|
|
def __init__(self, directories, ending):
|
|
|
|
|
self.weights = {'k' : {'c' : 0.25, 'g' : 0.75, 'q' : 0.125},
|
|
|
|
|
'c' : {'k' : 0.25, 'g' : 0.75, 's' : 0.5, 'z' : 0.5, 'q' : 0.125},
|
|
|
|
|
's' : {'z' : 0.25, 'c' : 0.5},
|
|
|
|
|
'z' : {'s' : 0.25, 'c' : 0.5},
|
|
|
|
|
'g' : {'k' : 0.75, 'c' : 0.75, 'q' : 0.9},
|
|
|
|
|
'o' : {'u' : 0.5},
|
|
|
|
|
'u' : {'o' : 0.5, 'v' : 0.75, 'w' : 0.5},
|
|
|
|
|
'b' : {'v' : 0.75},
|
|
|
|
|
'v' : {'b' : 0.75, 'w' : 0.5, 'u' : 0.7},
|
|
|
|
|
'w' : {'v' : 0.5, 'u' : 0.5},
|
|
|
|
|
'q' : {'c' : 0.125, 'k' : 0.125, 'g' : 0.9}}
|
|
|
|
|
|
|
|
|
|
self.corrections = None
|
|
|
|
|
self.dictionary = [None] * 513
|
|
|
|
|
self.reusable = [0] * 512
|
|
|
|
|
self.dictionaryEnd = 512
|
|
|
|
|
self.closestDistance = 0
|
|
|
|
|
|
|
|
|
|
self.M = [None] * 128
|
|
|
|
|
for y in range(0, 128):
|
|
|
|
|
self.M[y] = [0] * 128
|
|
|
|
|
self.M[y][0] = y
|
|
|
|
|
m0 = self.M[0]
|
|
|
|
|
x = 127
|
|
|
|
|
while x > -1:
|
|
|
|
|
m0[x] = x
|
|
|
|
|
x -= 1
|
|
|
|
|
|
2012-10-22 14:58:55 +02:00
|
|
|
|
previous = ''
|
2012-10-04 22:11:33 +02:00
|
|
|
|
self.dictionary[-1] = previous;
|
|
|
|
|
|
|
|
|
|
for directory in directories:
|
2012-10-08 18:22:13 +02:00
|
|
|
|
for filename in os.listdir(directory):
|
2012-10-04 22:11:33 +02:00
|
|
|
|
if (not endswith(filename, ending)) or (len(filename) - len(ending) > 127):
|
|
|
|
|
continue
|
|
|
|
|
proper = filename[:-len(ending)]
|
|
|
|
|
|
2012-10-22 13:17:42 +02:00
|
|
|
|
if self.dictionaryEnd == 0:
|
|
|
|
|
self.dictionaryEnd = len(self.dictionary)
|
|
|
|
|
self.reusable = [0] * self.dictionaryEnd + self.reusable
|
|
|
|
|
self.dictionary = [None] * self.dictionaryEnd + self.dictionary
|
|
|
|
|
|
|
|
|
|
self.dictionaryEnd -= 1
|
|
|
|
|
self.dictionary[self.dictionaryEnd] = proper
|
2012-10-04 22:11:33 +02:00
|
|
|
|
|
|
|
|
|
prevCommon = min(len(previous), len(proper))
|
|
|
|
|
for i in range(0, prevCommon):
|
|
|
|
|
if previous[i] != proper[i]:
|
|
|
|
|
prevCommon = i
|
|
|
|
|
break
|
2012-10-22 13:17:42 +02:00
|
|
|
|
previous = proper
|
|
|
|
|
self.reusable[self.dictionaryEnd] = prevCommon
|
|
|
|
|
#part = self.dictionary[self.dictionaryEnd : len(self.dictionary) - 1]
|
|
|
|
|
#part.sort()
|
|
|
|
|
#self.dictionary[self.dictionaryEnd : len(self.dictionary) - 1] = part
|
|
|
|
|
#
|
|
|
|
|
#index = len(self.dictionary) - 1
|
|
|
|
|
#while index >= self.dictionaryEnd:
|
|
|
|
|
# proper = self.dictionary[index]
|
|
|
|
|
# prevCommon = min(len(previous), len(proper))
|
|
|
|
|
# for i in range(0, prevCommon):
|
|
|
|
|
# if previous[i] != proper[i]:
|
|
|
|
|
# prevCommon = i
|
|
|
|
|
# break
|
|
|
|
|
# previous = proper
|
|
|
|
|
# self.reusable[self.dictionaryEnd] = prevCommon
|
2012-10-22 14:58:55 +02:00
|
|
|
|
# index -= 1;
|
2012-10-04 22:11:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
2012-10-12 16:46:27 +02:00
|
|
|
|
Finds the closests correct spelled word
|
|
|
|
|
|
|
|
|
|
@param used:str The word to correct
|
|
|
|
|
@return (words, distance):(list<string>, int) A list the closest spellings and the weighted distance
|
2012-10-04 22:11:33 +02:00
|
|
|
|
'''
|
|
|
|
|
def correct(self, used):
|
2012-10-22 13:17:42 +02:00
|
|
|
|
if len(used) > 127:
|
2012-10-04 22:11:33 +02:00
|
|
|
|
return ([used], 0)
|
|
|
|
|
|
2012-10-22 13:17:42 +02:00
|
|
|
|
self.__correct(used)
|
|
|
|
|
return (self.corrections, self.closestDistance)
|
2012-10-04 22:11:33 +02:00
|
|
|
|
|
|
|
|
|
|
2012-10-12 16:46:27 +02:00
|
|
|
|
'''
|
|
|
|
|
Finds the closests correct spelled word
|
|
|
|
|
|
|
|
|
|
@param used:str The word to correct, it must satisfy all restrictions
|
|
|
|
|
'''
|
2012-10-04 22:11:33 +02:00
|
|
|
|
def __correct(self, used):
|
|
|
|
|
self.closestDistance = 0x7FFFFFFF
|
|
|
|
|
previous = self.dictionary[-1]
|
|
|
|
|
prevLen = 0
|
|
|
|
|
usedLen = len(used)
|
|
|
|
|
|
|
|
|
|
proper = None
|
|
|
|
|
prevCommon = 0
|
|
|
|
|
|
2012-10-22 13:17:42 +02:00
|
|
|
|
d = len(self.dictionary) - 1
|
2012-10-04 22:11:33 +02:00
|
|
|
|
while d > self.dictionaryEnd:
|
|
|
|
|
d -= 1
|
|
|
|
|
proper = self.dictionary[d]
|
|
|
|
|
if abs(len(proper) - usedLen) <= self.closestDistance:
|
|
|
|
|
if previous == self.dictionary[d + 1]:
|
|
|
|
|
prevCommon = self.reusable[d];
|
|
|
|
|
else:
|
|
|
|
|
prevCommon = min(prevLen, len(proper))
|
|
|
|
|
for i in range(0, prevCommon):
|
|
|
|
|
if previous[i] != proper[i]:
|
|
|
|
|
prevCommon = i
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
skip = min(prevLen, len(proper))
|
|
|
|
|
i = prevCommon
|
2012-10-22 13:17:42 +02:00
|
|
|
|
while i < skip:
|
2012-10-04 22:11:33 +02:00
|
|
|
|
for u in range(0, usedLen):
|
|
|
|
|
if (used[u] == previous[i]) or (used[u] == proper[i]):
|
|
|
|
|
skip = i
|
|
|
|
|
break
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
common = min(skip, min(usedLen, len(proper)))
|
|
|
|
|
for i in range(0, common):
|
|
|
|
|
if used[i] != proper[i]:
|
|
|
|
|
common = i
|
|
|
|
|
break
|
|
|
|
|
|
2012-10-22 13:17:42 +02:00
|
|
|
|
distance = self.__distance(proper, skip, len(proper), used, common, usedLen)
|
2012-10-04 22:11:33 +02:00
|
|
|
|
|
|
|
|
|
if self.closestDistance > distance:
|
|
|
|
|
self.closestDistance = distance
|
2012-10-22 13:17:42 +02:00
|
|
|
|
self.corrections = [proper]
|
2012-10-04 22:11:33 +02:00
|
|
|
|
elif self.closestDistance == distance:
|
2012-10-22 13:17:42 +02:00
|
|
|
|
self.corrections.append(proper)
|
2012-10-04 22:11:33 +02:00
|
|
|
|
|
|
|
|
|
previous = proper;
|
|
|
|
|
if distance >= 0x7FFFFF00:
|
|
|
|
|
prevLen = distance & 255
|
|
|
|
|
else:
|
|
|
|
|
prevLen = len(proper)
|
|
|
|
|
|
|
|
|
|
|
2012-10-12 16:46:27 +02:00
|
|
|
|
'''
|
|
|
|
|
Calculate the distance between a correct word and a incorrect word
|
|
|
|
|
|
|
|
|
|
@param proper:str The correct word
|
|
|
|
|
@param y0:int The offset for `proper`
|
|
|
|
|
@param yn:int The length, before applying `y0`, of `proper`
|
|
|
|
|
@param used:str The incorrect word
|
|
|
|
|
@param x0:int The offset for `used`
|
|
|
|
|
@param xn:int The length, before applying `x0`, of `used`
|
|
|
|
|
@return :float The distance between the words
|
|
|
|
|
'''
|
2012-10-04 22:11:33 +02:00
|
|
|
|
def __distance(self, proper, y0, yn, used, x0, xn):
|
|
|
|
|
my = self.M[y0]
|
|
|
|
|
for y in range(y0, yn):
|
|
|
|
|
best = 0x7FFFFFFF
|
|
|
|
|
p = proper[y]
|
|
|
|
|
myy = self.M[y + 1] # only one array bound check, and at most one + ☺
|
|
|
|
|
x = x0
|
|
|
|
|
while x < xn:
|
|
|
|
|
change = my[x]
|
|
|
|
|
u = used[x]
|
|
|
|
|
if p == u:
|
|
|
|
|
# commence black magick … twilight would be so disappointed
|
|
|
|
|
x += 1
|
|
|
|
|
myy[x] = change
|
|
|
|
|
best = min(best, change)
|
|
|
|
|
remove = myy[x]
|
|
|
|
|
add = my[x + 1]
|
|
|
|
|
|
|
|
|
|
cw = 1
|
|
|
|
|
if my[x] in self.weights:
|
|
|
|
|
if p in self.weights[u]:
|
|
|
|
|
cw = self.weights[u][p]
|
2012-10-22 13:17:42 +02:00
|
|
|
|
x += 1
|
2012-10-04 22:11:33 +02:00
|
|
|
|
|
|
|
|
|
myy[x] = min(cw + change, 1 + min(remove, add))
|
|
|
|
|
if best > myy[x]:
|
|
|
|
|
best = myy[x]
|
|
|
|
|
|
|
|
|
|
if best > self.closestDistance:
|
2012-10-22 13:17:42 +02:00
|
|
|
|
return 0x7FFFFF00 | y
|
2012-10-04 22:11:33 +02:00
|
|
|
|
my = myy
|
|
|
|
|
return my[xn]
|
2012-10-28 15:41:42 +01:00
|
|
|
|
|
2012-10-04 22:11:33 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-20 12:02:08 +02:00
|
|
|
|
'''
|
2012-10-28 15:59:29 +01:00
|
|
|
|
Start the program from ponysay.__init__ if this is the executed file
|
2012-08-26 06:17:13 +02:00
|
|
|
|
'''
|
2012-10-28 15:59:29 +01:00
|
|
|
|
if __name__ == '__main__':
|
2012-10-30 19:19:37 +01:00
|
|
|
|
isthink = (len(__file__) >= len('think')) and (__file__.endswith('think'))
|
|
|
|
|
isthink = ((len(__file__) >= len('think.py')) and (__file__.endswith('think.py'))) or isthink
|
|
|
|
|
|
2012-10-28 15:59:29 +01:00
|
|
|
|
usage_saythink = '\033[34;1m(ponysay | ponythink)\033[21;39m'
|
2012-10-30 21:32:56 +01:00
|
|
|
|
usage_common = '[-c] [-W\033[33mCOLUMN\033[39m] [-b\033[33mSTYLE\033[39m]'
|
2012-10-29 21:12:40 +01:00
|
|
|
|
usage_listhelp = '(-l | -L | -B | +l | +L | -A | + A | -v | -h)'
|
2012-10-30 21:32:56 +01:00
|
|
|
|
usage_file = '[-f\033[33mPONY\033[39m]* [[--] \033[33mmessage\033[39m]'
|
|
|
|
|
usage_xfile = '(+f\033[33mPONY\033[39m)* [[--] \033[33mmessage\033[39m]'
|
|
|
|
|
usage_quote = '(-q\033[33mPONY\033[39m)*'
|
2012-10-28 15:59:29 +01:00
|
|
|
|
|
|
|
|
|
usage = '%s %s\n%s %s %s\n%s %s %s\n%s %s %s' % (usage_saythink, usage_listhelp,
|
|
|
|
|
usage_saythink, usage_common, usage_file,
|
|
|
|
|
usage_saythink, usage_common, usage_xfile,
|
|
|
|
|
usage_saythink, usage_common, usage_quote)
|
|
|
|
|
|
|
|
|
|
usage = usage.replace('\033[', '\0')
|
|
|
|
|
for sym in ('[', ']', '(', ')', '|', '...', '*'):
|
|
|
|
|
usage = usage.replace(sym, '\033[2m' + sym + '\033[22m')
|
|
|
|
|
usage = usage.replace('\0', '\033[')
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Argument parsing
|
|
|
|
|
'''
|
|
|
|
|
opts = ArgParser(program = 'ponythink' if isthink else 'ponysay',
|
|
|
|
|
description = 'cowsay reimplemention for ponies',
|
|
|
|
|
usage = usage,
|
|
|
|
|
longdescription =
|
2012-08-20 12:02:08 +02:00
|
|
|
|
'''Ponysay displays an image of a pony saying some text provided by the user.
|
|
|
|
|
If \033[4mmessage\033[24m is not provided, it accepts standard input. For an extensive
|
|
|
|
|
documentation run `info ponysay`, or for just a little more help than this
|
|
|
|
|
run `man ponysay`. Ponysay has so much more to offer than described here.''')
|
2012-10-28 15:59:29 +01:00
|
|
|
|
|
|
|
|
|
opts.add_argumentless(['--quoters'])
|
|
|
|
|
opts.add_argumentless(['--onelist'])
|
|
|
|
|
opts.add_argumentless(['++onelist'])
|
|
|
|
|
|
|
|
|
|
opts.add_argumentless(['-X', '--256-colours', '--256colours', '--x-colours'])
|
|
|
|
|
opts.add_argumentless(['-V', '--tty-colours', '--ttycolours', '--vt-colours'])
|
|
|
|
|
opts.add_argumentless(['-K', '--kms-colours', '--kmscolours'])
|
|
|
|
|
|
|
|
|
|
opts.add_argumented(['+c', '--colour'], arg = 'COLOUR')
|
|
|
|
|
opts.add_argumented(['--colour-bubble', '--colour-balloon'], arg = 'COLOUR')
|
|
|
|
|
opts.add_argumented(['--colour-link'], arg = 'COLOUR')
|
|
|
|
|
opts.add_argumented(['--colour-msg', '--colour-message'], arg = 'COLOUR')
|
|
|
|
|
opts.add_argumented(['--colour-pony'], arg = 'COLOUR')
|
|
|
|
|
opts.add_argumented(['--colour-wrap', '--colour-hyphen'], arg = 'COLOUR')
|
|
|
|
|
|
|
|
|
|
opts.add_argumentless(['-h', '--help'], help = 'Print this help message.')
|
|
|
|
|
opts.add_argumentless(['-v', '--version'], help = 'Print the version of the program.')
|
|
|
|
|
opts.add_argumentless(['-l', '--list'], help = 'List pony names.')
|
|
|
|
|
opts.add_argumentless(['-L', '--symlist', '--altlist'], help = 'List pony names with alternatives.')
|
|
|
|
|
opts.add_argumentless(['+l', '++list'], help = 'List non-MLP:FiM pony names.')
|
|
|
|
|
opts.add_argumentless(['+L', '++symlist', '++altlist'], help = 'List non-MLP:FiM pony names with alternatives.')
|
|
|
|
|
opts.add_argumentless(['-A', '--all'], help = 'List all pony names.')
|
|
|
|
|
opts.add_argumentless(['+A', '++all', '--symall', '--altall'], help = 'List all pony names with alternatives.')
|
|
|
|
|
opts.add_argumentless(['-B', '--bubblelist', '--balloonlist'], help = 'List balloon styles.')
|
2012-10-29 20:58:27 +01:00
|
|
|
|
opts.add_argumentless(['-c', '--compress', '--compact'], help = 'Compress messages.')
|
2012-10-28 15:59:29 +01:00
|
|
|
|
opts.add_argumentless(['-o', '--pony-only', '--ponyonly'], help = 'Print only the pony.')
|
|
|
|
|
opts.add_argumented( ['-W', '--wrap'], arg = 'COLUMN', help = 'Specify column where the message should be wrapped.')
|
|
|
|
|
opts.add_argumented( ['-b', '--bubble', '--balloon'], arg = 'STYLE', help = 'Select a balloon style.')
|
|
|
|
|
opts.add_argumented( ['-f', '--file', '--pony'], arg = 'PONY', help = 'Select a pony.\nEither a file name or a pony name.')
|
|
|
|
|
opts.add_argumented( ['+f', '++file', '++pony'], arg = 'PONY', help = 'Select a non-MLP:FiM pony.')
|
|
|
|
|
opts.add_argumented( ['-q', '--quote'], arg = 'PONY', help = 'Select a pony which will quote herself.')
|
|
|
|
|
opts.add_variadic( ['--f', '--files', '--ponies'], arg = 'PONY')
|
|
|
|
|
opts.add_variadic( ['++f', '++files', '++ponies'], arg = 'PONY')
|
|
|
|
|
opts.add_variadic( ['--q', '--quotes'], arg = 'PONY')
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Whether at least one unrecognised option was used
|
|
|
|
|
'''
|
|
|
|
|
unrecognised = not opts.parse()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Start
|
2012-10-30 01:31:17 +01:00
|
|
|
|
ponysay = Ponysay()
|
|
|
|
|
ponysay.unrecognised = unrecognised
|
|
|
|
|
ponysay.run(opts)
|