summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-09-24 11:13:42 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-09-24 11:13:42 -0400
commita2ad70bd79a78123d2bb67b1ff1ed0b815dd721d (patch)
tree22059fc991a5e3ff3b0cc4b4cfc2015efd9dcf41 /examples
parent806fe461acee59bcda791a9cb9ebf396af80126e (diff)
downloadcmd2-git-a2ad70bd79a78123d2bb67b1ff1ed0b815dd721d.tar.gz
Added optional color argument to poutput() for providing a color escape code to colorize the output with
Also: - Added optional err_color and war_color arguments to perror() for providing color escape codes - Removed usage of deprecated colorize() method within the examples
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/pirate.py20
-rwxr-xr-xexamples/python_scripting.py4
2 files changed, 20 insertions, 4 deletions
diff --git a/examples/pirate.py b/examples/pirate.py
index 34906a9f..22274dbf 100755
--- a/examples/pirate.py
+++ b/examples/pirate.py
@@ -8,8 +8,21 @@ It demonstrates many features of cmd2.
"""
import argparse
+from colorama import Fore
+
import cmd2
+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."""
@@ -17,10 +30,10 @@ class Pirate(cmd2.Cmd):
self.default_to_shell = True
self.multiline_commands = ['sing']
self.terminators = self.terminators + ['...']
- self.songcolor = 'blue'
+ self.songcolor = Fore.BLUE
# Add stuff to settable and/or shortcuts before calling base class initializer
- self.settable['songcolor'] = 'Color to ``sing`` in (red/blue/green/cyan/magenta, bold, underline)'
+ self.settable['songcolor'] = 'Color to ``sing`` in (black/red/green/yellow/blue/magenta/cyan/white)'
self.shortcuts.update({'~': 'sing'})
"""Initialize the base class as well as this one"""
@@ -68,7 +81,8 @@ class Pirate(cmd2.Cmd):
def do_sing(self, arg):
"""Sing a colorful song."""
- self.poutput(self.colorize(arg, self.songcolor))
+ color_escape = COLORS.get(self.songcolor, default=Fore.RESET)
+ self.poutput(arg, color=color_escape)
yo_parser = argparse.ArgumentParser()
yo_parser.add_argument('--ho', type=int, default=2, help="How often to chant 'ho'")
diff --git a/examples/python_scripting.py b/examples/python_scripting.py
index ab5ecc2b..b6700cb7 100755
--- a/examples/python_scripting.py
+++ b/examples/python_scripting.py
@@ -17,6 +17,8 @@ This application and the "scripts/conditional.py" script serve as an example for
import argparse
import os
+from colorama import Fore
+
import cmd2
@@ -33,7 +35,7 @@ class CmdLineApp(cmd2.Cmd):
def _set_prompt(self):
"""Set prompt so it displays the current working directory."""
self.cwd = os.getcwd()
- self.prompt = self.colorize('{!r} $ '.format(self.cwd), 'cyan')
+ self.prompt = Fore.CYAN + '{!r} $ '.format(self.cwd) + Fore.RESET
def postcmd(self, stop: bool, line: str) -> bool:
"""Hook method executed just after a command dispatch is finished.