Skip to content

Revisit color support for poutput, perror, etc. #698

Closed
@tleonhardt

Description

@tleonhardt

Currently the signature for poutput and perror are as follows:

def poutput(self, msg: Any, end: str = '\n', color: str = '') -> None:
    pass

def perror(self, err: Union[str, Exception], traceback_war: bool = True, err_color: str = Fore.LIGHTRED_EX,
               war_color: str = Fore.LIGHTYELLOW_EX) -> None:
    pass

Where color, err_color and war_color are optional ANSI escape codes to style/format the output with and if present, we reset the foreground color after the output. However, we never considered that someone may set the background color or other effects and we do not reset those.

We should probably refactor these methods to take separate foreground and background styles such as:

def poutput(self, msg: Any, end: str = '\n', fg: str = '', bg: str = '') -> None:
    pass

And the foreground and background should be reset respectively at the end depending on what is provided.

We may also want to consider making these more user friendly where the user can pass in an English name for a color such as 'green' and we have two dictionaries maintained inside the cmd2.Cmd instance called something like self.fg_color and self.bg_color which map a string to an ANSI escape sequence. These dictionaries could be pre-populated with default styles for common colors as well as logical situations like warning, error, success, etc. But cmd2 application developers would be able to override these default styles for any given color.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions