From 5e151107549886c84dd9d834e4a9e35599b73345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Andr=C3=A9e?= Date: Sat, 17 Aug 2013 06:18:18 +0200 Subject: [PATCH] split out logic for compressing message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/ponysay.py | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/ponysay.py b/src/ponysay.py index c828c3f6..cd7a8bfc 100755 --- a/src/ponysay.py +++ b/src/ponysay.py @@ -872,12 +872,39 @@ class Ponysay(): 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): ''' Convert image to the ponysay format if it is a regular image - @param pony:str The pony file - @param pony:str The new pony file, or the old if it was already in the ponysay format + @param pony:str The pony file + @return :str The new pony file, or the old if it was already in the ponysay format ''' if endswith(pony.lower(), '.png'): pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\'' @@ -926,24 +953,7 @@ class Ponysay(): ## This algorithm should give some result as cowsay's (according to tests) if args.opts['-c'] is not None: - 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') + msg = self.__compressMessage(msg) ## Print info printinfo('pony file: ' + pony)