mirror of
https://github.com/erkin/ponysay.git
synced 2025-03-29 14:57:43 +01:00
split out logic for compressing message
Signed-off-by: Mattias Andrée <maandree@operamail.com>
This commit is contained in:
parent
3bee0c375e
commit
5e15110754
1 changed files with 30 additions and 20 deletions
|
@ -872,12 +872,39 @@ class Ponysay():
|
||||||
print('%s %s' % ('ponysay', VERSION))
|
print('%s %s' % ('ponysay', VERSION))
|
||||||
|
|
||||||
|
|
||||||
|
def __compressMessage(self, msg):
|
||||||
|
'''
|
||||||
|
This algorithm should give some result as cowsay's
|
||||||
|
|
||||||
|
@param msg:str The message
|
||||||
|
@return :str The message compressed
|
||||||
|
'''
|
||||||
|
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
|
||||||
|
return buf.replace('\n', '\n\n')
|
||||||
|
|
||||||
|
|
||||||
def __useImage(self, pony):
|
def __useImage(self, pony):
|
||||||
'''
|
'''
|
||||||
Convert image to the ponysay format if it is a regular image
|
Convert image to the ponysay format if it is a regular image
|
||||||
|
|
||||||
@param pony:str The pony file
|
@param pony:str The pony file
|
||||||
@param pony:str The new pony file, or the old if it was already in the ponysay format
|
@return :str The new pony file, or the old if it was already in the ponysay format
|
||||||
'''
|
'''
|
||||||
if endswith(pony.lower(), '.png'):
|
if endswith(pony.lower(), '.png'):
|
||||||
pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
|
pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
|
||||||
|
@ -926,24 +953,7 @@ class Ponysay():
|
||||||
|
|
||||||
## This algorithm should give some result as cowsay's (according to tests)
|
## This algorithm should give some result as cowsay's (according to tests)
|
||||||
if args.opts['-c'] is not None:
|
if args.opts['-c'] is not None:
|
||||||
buf = ''
|
msg = self.__compressMessage(msg)
|
||||||
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')
|
|
||||||
|
|
||||||
## Print info
|
## Print info
|
||||||
printinfo('pony file: ' + pony)
|
printinfo('pony file: ' + pony)
|
||||||
|
|
Loading…
Add table
Reference in a new issue