diff options
Diffstat (limited to 'cmd2')
-rw-r--r-- | cmd2/ansi.py | 12 | ||||
-rw-r--r-- | cmd2/cmd2.py | 29 |
2 files changed, 37 insertions, 4 deletions
diff --git a/cmd2/ansi.py b/cmd2/ansi.py index b3dd7cc1..78e0df81 100644 --- a/cmd2/ansi.py +++ b/cmd2/ansi.py @@ -95,6 +95,7 @@ def style_aware_wcswidth(text: str) -> int: """ Wrap wcswidth to make it compatible with strings that contains ANSI style sequences :param text: the string being measured + :return: the width of the string when printed to the terminal """ # Strip ANSI style sequences since they cause wcswidth to return -1 return wcswidth(strip_style(text)) @@ -117,7 +118,7 @@ def fg_lookup(fg_name: str) -> str: Look up ANSI escape codes based on foreground color name. :param fg_name: foreground color name to look up ANSI escape code(s) for :return: ANSI escape code(s) associated with this color - :raises ValueError if the color cannot be found + :raises ValueError: if the color cannot be found """ try: ansi_escape = FG_COLORS[fg_name.lower()] @@ -131,7 +132,7 @@ def bg_lookup(bg_name: str) -> str: Look up ANSI escape codes based on background color name. :param bg_name: background color name to look up ANSI escape code(s) for :return: ANSI escape code(s) associated with this color - :raises ValueError if the color cannot be found + :raises ValueError: if the color cannot be found """ try: ansi_escape = BG_COLORS[bg_name.lower()] @@ -193,8 +194,13 @@ def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, # These can be altered to suit an application's needs and only need to be a # function with the following structure: func(str) -> str style_success = functools.partial(style, fg='green') +"""Partial function supplying arguments to style() to generate bold green text""" + style_warning = functools.partial(style, fg='bright_yellow') +"""Partial function supplying arguments to ansi.style() to generate yellow text""" + style_error = functools.partial(style, fg='bright_red') +"""Partial function supplying arguments to ansi.style() to generate bright red text""" def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_offset: int, alert_msg: str) -> str: @@ -255,6 +261,6 @@ def set_title_str(title: str) -> str: """Get the required string, including ANSI escape codes, for setting window title for the terminal. :param title: new title for the window - :return string to write to sys.stderr in order to set the window title to the desired test + :return: string to write to sys.stderr in order to set the window title to the desired test """ return colorama.ansi.set_title(title) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 434ade5f..ec8d67b2 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -133,6 +133,9 @@ class Cmd(cmd.Cmd): " This command is for internal use and is not intended to be called from the\n" " command line.") + debug = False + """Set to True to display a full stack trace when exceptions occur""" + # Sorting keys for strings ALPHABETICAL_SORT_KEY = utils.norm_fold NATURAL_SORT_KEY = utils.natural_keys @@ -222,6 +225,12 @@ class Cmd(cmd.Cmd): 'quiet': "Don't print nonessential feedback", 'timing': 'Report execution times' } + """This dictionary contains the name and description of all settings available to users. + + Users use the :ref:`features/builtin_commands:set` command to view and + modify settings. Settings are stored in instance attributes with the + same name as the setting. + """ # Commands to exclude from the help menu and tab completion self.hidden_commands = ['eof', '_relative_load', '_relative_run_script'] @@ -289,8 +298,8 @@ class Cmd(cmd.Cmd): # The error that prints when a non-existent command is run self.default_error = "{} is not a recognized command, alias, or macro" + """The error message displayed when a non-existent command is run.""" - # If this string is non-empty, then this warning message will print if a broken pipe error occurs while printing self.broken_pipe_warning = '' # Commands that will run at the beginning of the command loop @@ -413,6 +422,24 @@ class Cmd(cmd.Cmd): self.perror('Invalid value: {} (valid values: {}, {}, {})'.format(new_val, ansi.STYLE_TERMINAL, ansi.STYLE_ALWAYS, ansi.STYLE_NEVER)) + @property + def broken_pipe_warning(self) -> str: + """Message to display if a BrokenPipeError is raised while writing output. + + The following methods catch BrokenPipeError exceptions and output this message: + + - :meth:`~cmd2.cmd2.Cmd.poutput()` + - :meth:`~cmd2.cmd2.Cmd.ppaged()` + + The default value is an empty string meaning the BrokenPipeError is + silently swallowed. + """ + return self.broken_pipe_error + + @broken_pipe_warning.setter + def broken_pipe_warning(self, new_val: str) -> None: + self.broken_pipe_error = new_val + def _completion_supported(self) -> bool: """Return whether tab completion is supported""" return self.use_rawinput and self.completekey and rl_type != RlType.NONE |