From fd6863c37a18c958a5d32d6f05e6302ebe22d456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Andr=C3=A9e?= Date: Sat, 17 Aug 2013 07:23:17 +0200 Subject: [PATCH] split out logic for height truncation and printing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/ponysay.py | 52 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/ponysay.py b/src/ponysay.py index 84bc04f3..355a515c 100755 --- a/src/ponysay.py +++ b/src/ponysay.py @@ -954,27 +954,8 @@ class Ponysay(): output = output[:-1] - ## Load height trunction settings - 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' - - ## Print the output, truncated on height is so set - lines = gettermsize()[0] - int(env_lines) - if self.linuxvt or (env_height in ('yes', 'y', '1')): - if env_bottom in ('yes', 'y', '1'): - for line in output.split('\n')[: -lines]: - print(line) - else: - for line in output.split('\n')[: lines]: - print(line) - else: - print(output) + ## Print the output, truncated on the height + self.__printOutput(output) def __getMessage(self, args, quote): @@ -1080,4 +1061,33 @@ class Ponysay(): else: messagewrap = int(args.opts['-W'][0]) return messagewrap + + + def __printOutput(self, output): + ''' + Print the output, but truncate it on the height + + @param output:str The output truncated on the width but not on the height + ''' + ## Load height trunction settings + 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' + + ## Print the output, truncated on height if so set + lines = gettermsize()[0] - int(env_lines) + if self.linuxvt or (env_height in ('yes', 'y', '1')): + if env_bottom in ('yes', 'y', '1'): + for line in output.split('\n')[: -lines]: + print(line) + else: + for line in output.split('\n')[: lines]: + print(line) + else: + print(output)