summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/cmd2.py35
-rwxr-xr-xexamples/pirate.py16
-rw-r--r--tests/test_cmd2.py8
3 files changed, 22 insertions, 37 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index eb745da5..c9fa28c3 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -587,8 +587,8 @@ class Cmd(cmd.Cmd):
msg = utils.strip_ansi(msg)
fileobj.write(msg)
- def poutput(self, msg: Any, end: str = '\n', fg: str = '', bg: str = '') -> None:
- """Smarter self.stdout.write(); color aware and adds newline by default
+ def poutput(self, msg: Any, *, end: str = '\n') -> None:
+ """Print message to self.stdout and appends a newline by default
Also handles BrokenPipeError exceptions for when a commands's output has
been piped to another process and that process terminates before the
@@ -596,21 +596,17 @@ class Cmd(cmd.Cmd):
:param msg: message to print (anything convertible to a str with '{}'.format() is OK)
:param end: (optional) string appended after the end of the message, default a newline
- :param fg: (optional) Foreground color. Accepts color names like 'red' or 'blue'
- :param bg: (optional) Background color. Accepts color names like 'red' or 'blue'
"""
- if msg is not None and msg != '':
- try:
- final_msg = utils.style_message(msg, fg=fg, bg=bg) + end
- self._decolorized_write(self.stdout, final_msg)
- except BrokenPipeError:
- # This occurs if a command's output is being piped to another
- # process and that process closes before the command is
- # finished. If you would like your application to print a
- # warning message, then set the broken_pipe_warning attribute
- # to the message you want printed.
- if self.broken_pipe_warning:
- sys.stderr.write(self.broken_pipe_warning)
+ try:
+ self._decolorized_write(self.stdout, "{}{}".format(msg, end))
+ except BrokenPipeError:
+ # This occurs if a command's output is being piped to another
+ # process and that process closes before the command is
+ # finished. If you would like your application to print a
+ # warning message, then set the broken_pipe_warning attribute
+ # to the message you want printed.
+ if self.broken_pipe_warning:
+ sys.stderr.write(self.broken_pipe_warning)
def perror(self, msg: Any, end: str = '\n', fg: str = 'lightred', bg: str = '') -> None:
"""Smarter sys.stderr.write(); color aware and adds newline by default
@@ -3742,12 +3738,12 @@ class Cmd(cmd.Cmd):
verinfo = ".".join(map(str, sys.version_info[:3]))
num_transcripts = len(transcripts_expanded)
plural = '' if len(transcripts_expanded) == 1 else 's'
- self.poutput(Style.BRIGHT + utils.center_text('cmd2 transcript test', pad='='))
+ self.poutput(Style.BRIGHT + utils.center_text('cmd2 transcript test', pad='=') + Style.NORMAL)
self.poutput('platform {} -- Python {}, cmd2-{}, readline-{}'.format(sys.platform, verinfo, cmd2.__version__,
rl_type))
self.poutput('cwd: {}'.format(os.getcwd()))
self.poutput('cmd2 app: {}'.format(sys.argv[0]))
- self.poutput(Style.BRIGHT + 'collected {} transcript{}'.format(num_transcripts, plural))
+ self.poutput(Style.BRIGHT + 'collected {} transcript{}'.format(num_transcripts, plural) + Style.NORMAL)
self.__class__.testfiles = transcripts_expanded
sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
@@ -3760,7 +3756,8 @@ class Cmd(cmd.Cmd):
if test_results.wasSuccessful():
self._decolorized_write(sys.stderr, stream.read())
finish_msg = '{0} transcript{1} passed in {2:.3f} seconds'.format(num_transcripts, plural, execution_time)
- self.poutput(Style.BRIGHT + utils.center_text(finish_msg, pad='='), fg="green")
+ finish_msg = utils.style_message(utils.center_text(finish_msg, pad='='), fg="green")
+ self.poutput(Style.BRIGHT + finish_msg + Style.NORMAL)
else:
# Strip off the initial traceback which isn't particularly useful for end users
error_str = stream.read()
diff --git a/examples/pirate.py b/examples/pirate.py
index 57ac0b53..6802edb6 100755
--- a/examples/pirate.py
+++ b/examples/pirate.py
@@ -8,22 +8,10 @@ It demonstrates many features of cmd2.
"""
import argparse
-from colorama import Fore
-
import cmd2
+from cmd2 import utils
from cmd2.constants import MULTILINE_TERMINATOR
-COLORS = {
- 'black': Fore.BLACK,
- 'red': Fore.RED,
- 'green': Fore.GREEN,
- 'yellow': Fore.YELLOW,
- 'blue': Fore.BLUE,
- 'magenta': Fore.MAGENTA,
- 'cyan': Fore.CYAN,
- 'white': Fore.WHITE,
-}
-
class Pirate(cmd2.Cmd):
"""A piratical example cmd2 application involving looting and drinking."""
@@ -82,7 +70,7 @@ class Pirate(cmd2.Cmd):
def do_sing(self, arg):
"""Sing a colorful song."""
- self.poutput(arg, fg=self.songcolor)
+ self.poutput(utils.style_message(arg, fg=self.songcolor))
yo_parser = argparse.ArgumentParser()
yo_parser.add_argument('--ho', type=int, default=2, help="How often to chant 'ho'")
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index f4c8d3e7..2c6a4eb0 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1447,20 +1447,20 @@ def test_poutput_empty_string(outsim_app):
msg = ''
outsim_app.poutput(msg)
out = outsim_app.stdout.getvalue()
- expected = msg
+ expected = '\n'
assert out == expected
def test_poutput_none(outsim_app):
msg = None
outsim_app.poutput(msg)
out = outsim_app.stdout.getvalue()
- expected = ''
+ expected = 'None\n'
assert out == expected
def test_poutput_color_always(outsim_app):
msg = 'Hello World'
outsim_app.colors = 'Always'
- outsim_app.poutput(msg, fg='cyan')
+ outsim_app.poutput(utils.style_message(msg, fg='cyan'))
out = outsim_app.stdout.getvalue()
expected = Fore.CYAN + msg + Fore.RESET + '\n'
assert out == expected
@@ -1468,7 +1468,7 @@ def test_poutput_color_always(outsim_app):
def test_poutput_color_never(outsim_app):
msg = 'Hello World'
outsim_app.colors = 'Never'
- outsim_app.poutput(msg, fg='cyan')
+ outsim_app.poutput(utils.style_message(msg, fg='cyan'))
out = outsim_app.stdout.getvalue()
expected = msg + '\n'
assert out == expected