diff --git a/ponysay.py b/ponysay.py index ea9ecc05..800343ed 100755 --- a/ponysay.py +++ b/ponysay.py @@ -353,33 +353,63 @@ class ponysay(): pony = self.__getponypath(args.pony) (cowsay, customcowsay) = self.__getcowsay() - wrap_arg = ' -W ' + args.wrap if args.wrap is not None else '' - file_arg = ' -f ' + pony; - message_arg = ' \'' + msg.replace('\'', '\'\\\'\'') + '\'' # ' in message is replaces by '\'', this (combined by '..') ensures it will always be one argument - cowsay_args = wrap_arg + file_arg + message_arg + + cmd = [cowsay, '-f', pony] + if args.wrap is not None: + cmd += ['-W', args.wrap] + cmd.append(msg) if linuxvt: print('\033[H\033[2J', end='') - if customcowsay: - exit_value = os.system(cowsay + cowsay_args) - else: - exit_value = os.system(cowsay + cowsay_args) - ## TODO not implement, but it will be obsolete if we rewrite cowsay - ''' - pcmd='#!/usr/bin/perl\nuse utf8;' - ccmd=$(for c in $(echo $PATH":" | sed -e 's/:/\/'"$cmd"' /g'); do if [ -f $c ]; then echo $c; break; fi done) - - if [ ${0} == *ponythink ]; then - cat <(echo -e $pcmd) $ccmd > "/tmp/ponythink" - perl '/tmp/ponythink' "$@" - rm '/tmp/ponythink' - else - perl <(cat <(echo -e $pcmd) $ccmd) "$@" - fi - ''' + proc = Popen(cmd, stdout=PIPE, stdin=sys.stderr) + output = proc.communicate()[0].decode('utf8', 'replace') + if (len(output) > 0) and (output[-1] == '\n'): + output = output[:-1] + exit_value = proc.returncode + + + env_bottom = os.environ['PONYSAY_BOTTOM'] if 'PONYSAY_BOTTOM' in os.environ else None + if (env_bottom is None) or (env_bottom == ''): env_bottom = '' + + env_height = os.environ['PONYSAY_TRUNCATE_HEIGHT'] if 'PONYSAY_TRUNCATE_HEIGHT' in os.environ else None + if (env_height is None) or (env_height == ''): 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' + + lines = self.__gettermsize()[1] - int(env_lines) + + if not exit_value == 0: sys.stderr.write('Unable to successfully execute' + (' custom ' if customcowsay else ' ') + 'cowsay [' + cowsay + ']\n') + else: + if linuxvt or (env_height is ("yes", "y", "1")): + if env_bottom is ("yes", "y", "1"): + for line in output[: -lines]: + print(line) + else: + for line in output[: lines]: + print(line) + else: + print(output); + + + ## TODO not implement, but it will be obsolete if we rewrite cowsay + ''' + (if not customcowsay) + + pcmd='#!/usr/bin/perl\nuse utf8;' + ccmd=$(for c in $(echo $PATH":" | sed -e 's/:/\/'"$cmd"' /g'); do if [ -f $c ]; then echo $c; break; fi done) + + if [ ${0} == *ponythink ]; then + cat <(echo -e $pcmd) $ccmd > "/tmp/ponythink" + perl '/tmp/ponythink' "$@" + rm '/tmp/ponythink' + else + perl <(cat <(echo -e $pcmd) $ccmd) "$@" + fi + ''' ''' diff --git a/ponysay.sh b/ponysay.sh index ebd482df..033a642a 100755 --- a/ponysay.sh +++ b/ponysay.sh @@ -1,9 +1,6 @@ #!/usr/bin/env bash -scrw=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2` # Screen width -scrh=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 1` # Screen height - # KMS ponies extension kmscmd="" [ "$TERM" = "linux" ] && kmscmd=$(for c in $(echo $PATH":" | sed -e 's/:/\/ponysay2kmsponysay /g'); do if [ -f $c ]; then echo $c; break; fi done) @@ -12,19 +9,6 @@ kmscmd="" # Function for printing the ponies and the message say() { - # Set PONYSAY_SHELL_LINES to default if not specified - [ "$PONYSAY_SHELL_LINES" = "" ] && PONYSAY_SHELL_LINES=2 - - # Height trunction, show top - function htrunchead { - head --lines=$(( $scrh - $PONYSAY_SHELL_LINES )) - } - - # Height trunction, show bottom - function htrunctail { - tail --lines=$(( $scrh - $PONYSAY_SHELL_LINES )) - } - # KMS ponies support if [ "$kmscmd" = "" ]; then function runcmd { @@ -36,14 +20,4 @@ say() { } fi - # Print the pony and the message - if [ "$TERM" = "linux" ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = 'yes' ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = 'y' ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = '1' ]; then - if [ "$PONYSAY_BOTTOM" = 'yes' ] || [ "$PONYSAY_BOTTOM" = 'y' ] || [ "$PONYSAY_BOTTOM" = '1' ]; then - runcmd "${wrap:+-W$wrap}" | wtrunc | htrunctail - else - runcmd "${wrap:+-W$wrap}" | wtrunc | htrunchead - fi - else - runcmd "${wrap:+-W$wrap}" | wtrunc - fi }