From 801bab847341fb9a35d10f1d0b4a629a4fc8f14c Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 19 Dec 2019 16:04:06 -0500 Subject: Changed allow_ansi to allow_style for accuracy in what types of ANSI escape sequences are handled --- examples/colors.py | 14 +++++++------- examples/plumbum_colors.py | 14 +++++++------- examples/transcripts/exampleSession.txt | 2 +- examples/transcripts/transcript_regex.txt | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) (limited to 'examples') diff --git a/examples/colors.py b/examples/colors.py index 8c54dfa4..ea8be479 100755 --- a/examples/colors.py +++ b/examples/colors.py @@ -6,21 +6,21 @@ A sample application for cmd2. Demonstrating colorized output. Experiment with the command line options on the `speak` command to see how different output colors ca -The allow_ansi setting has three possible values: +The allow_style setting has three possible values: Never - poutput(), pfeedback(), and ppaged() strip all ANSI escape sequences + poutput(), pfeedback(), and ppaged() strip all ANSI style sequences which instruct the terminal to colorize output Terminal (the default value) poutput(), pfeedback(), and ppaged() do not strip any - ANSI escape sequences when the output is a terminal, but if the output is - a pipe or a file the escape sequences are stripped. If you want colorized - output you must add ANSI escape sequences using either cmd2's internal ansi + ANSI style sequences when the output is a terminal, but if the output is + a pipe or a file the style sequences are stripped. If you want colorized + output you must add ANSI style sequences using either cmd2's internal ansi module or another color library such as `plumbum.colors` or `colorama`. Always - poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences, + poutput(), pfeedback(), and ppaged() never strip ANSI style sequences, regardless of the output destination """ import argparse @@ -40,7 +40,7 @@ class CmdLineApp(cmd2.Cmd): self.settable['maxrepeats'] = 'max repetitions for speak command' # Should ANSI color output be allowed - self.allow_ansi = ansi.ANSI_TERMINAL + self.allow_style = ansi.STYLE_TERMINAL speak_parser = argparse.ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') diff --git a/examples/plumbum_colors.py b/examples/plumbum_colors.py index a969c4de..8eaf3fe8 100755 --- a/examples/plumbum_colors.py +++ b/examples/plumbum_colors.py @@ -6,21 +6,21 @@ A sample application for cmd2. Demonstrating colorized output using the plumbum Experiment with the command line options on the `speak` command to see how different output colors ca -The allow_ansi setting has three possible values: +The allow_style setting has three possible values: Never - poutput(), pfeedback(), and ppaged() strip all ANSI escape sequences + poutput(), pfeedback(), and ppaged() strip all ANSI style sequences which instruct the terminal to colorize output Terminal (the default value) poutput(), pfeedback(), and ppaged() do not strip any - ANSI escape sequences when the output is a terminal, but if the output is - a pipe or a file the escape sequences are stripped. If you want colorized - output you must add ANSI escape sequences using either cmd2's internal ansi + ANSI style sequences when the output is a terminal, but if the output is + a pipe or a file the style sequences are stripped. If you want colorized + output you must add ANSI style sequences using either cmd2's internal ansi module or another color library such as `plumbum.colors` or `colorama`. Always - poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences, + poutput(), pfeedback(), and ppaged() never strip ANSI style sequences, regardless of the output destination WARNING: This example requires the plumbum package, which isn't normally required by cmd2. @@ -78,7 +78,7 @@ class CmdLineApp(cmd2.Cmd): self.settable['maxrepeats'] = 'max repetitions for speak command' # Should ANSI color output be allowed - self.allow_ansi = ansi.ANSI_TERMINAL + self.allow_style = ansi.STYLE_TERMINAL speak_parser = argparse.ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') diff --git a/examples/transcripts/exampleSession.txt b/examples/transcripts/exampleSession.txt index 3e579c28..5a75235b 100644 --- a/examples/transcripts/exampleSession.txt +++ b/examples/transcripts/exampleSession.txt @@ -3,7 +3,7 @@ # The regex for editor will match whatever program you use. # regexes on prompts just make the trailing space obvious (Cmd) set -allow_ansi: /(Terminal|Always|Never)/ +allow_style: /(Terminal|Always|Never)/ continuation_prompt: >/ / debug: False echo: False diff --git a/examples/transcripts/transcript_regex.txt b/examples/transcripts/transcript_regex.txt index d94c442b..276f7d22 100644 --- a/examples/transcripts/transcript_regex.txt +++ b/examples/transcripts/transcript_regex.txt @@ -3,7 +3,7 @@ # The regex for editor will match whatever program you use. # regexes on prompts just make the trailing space obvious (Cmd) set -allow_ansi: /(Terminal|Always|Never)/ +allow_style: /(Terminal|Always|Never)/ continuation_prompt: >/ / debug: False echo: False -- cgit v1.2.1 From 6b6809a394b1e0baffad46d7b91f61d77ece128d Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 6 Jan 2020 11:52:27 -0500 Subject: Updated dynamic commands example to add help category --- examples/dynamic_commands.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/dynamic_commands.py b/examples/dynamic_commands.py index 69816d40..620acb7f 100755 --- a/examples/dynamic_commands.py +++ b/examples/dynamic_commands.py @@ -3,13 +3,32 @@ """A simple example demonstrating how do_* commands can be created in a loop. """ import functools + import cmd2 -COMMAND_LIST = ['foo', 'bar', 'baz'] +from cmd2.constants import COMMAND_FUNC_PREFIX, HELP_FUNC_PREFIX + +COMMAND_LIST = ['foo', 'bar'] +CATEGORY = 'Dynamic Commands' class CommandsInLoop(cmd2.Cmd): """Example of dynamically adding do_* commands.""" def __init__(self): + # Add dynamic commands before calling cmd2.Cmd's init since it validates command names + for command in COMMAND_LIST: + # Create command function and add help category to it + cmd_func = functools.partial(self.send_text, text=command) + cmd2.categorize(cmd_func, CATEGORY) + + # Add command function to CLI object + cmd_func_name = COMMAND_FUNC_PREFIX + command + setattr(self, cmd_func_name, cmd_func) + + # Add help function to CLI object + help_func = functools.partial(self.text_help, text=command) + help_func_name = HELP_FUNC_PREFIX + command + setattr(self, help_func_name, help_func) + super().__init__(use_ipython=True) def send_text(self, args: cmd2.Statement, *, text: str): @@ -21,11 +40,6 @@ class CommandsInLoop(cmd2.Cmd): self.poutput("Simulate sending {!r} to a server and printing the response".format(text)) -for command in COMMAND_LIST: - setattr(CommandsInLoop, 'do_{}'.format(command), functools.partialmethod(CommandsInLoop.send_text, text=command)) - setattr(CommandsInLoop, 'help_{}'.format(command), functools.partialmethod(CommandsInLoop.text_help, text=command)) - - if __name__ == '__main__': app = CommandsInLoop() app.cmdloop() -- cgit v1.2.1