diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-02-07 14:20:23 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-02-07 14:20:23 -0500 |
commit | 5096a2b0c8fbe98b4f366a94552f883797d04083 (patch) | |
tree | f627b15dbb24064b9cf74d2a6c8fac8c070dca3d /cmd2/ansi.py | |
parent | 0d7d533f9077fcd422d8192dc675f4a2f8f32cbb (diff) | |
download | cmd2-git-5096a2b0c8fbe98b4f366a94552f883797d04083.tar.gz |
Updated plumbum example
Diffstat (limited to 'cmd2/ansi.py')
-rw-r--r-- | cmd2/ansi.py | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/cmd2/ansi.py b/cmd2/ansi.py index 3a106776..f54abf0a 100644 --- a/cmd2/ansi.py +++ b/cmd2/ansi.py @@ -3,9 +3,9 @@ Support for ANSI escape sequences which are used for things like applying style to text, setting the window title, and asynchronous alerts. """ -from enum import Enum, unique import functools import re +from enum import Enum from typing import Any, IO, List, Union import colorama @@ -29,8 +29,10 @@ ANSI_STYLE_RE = re.compile(r'\x1b\[[^m]*m') class ColorBase(Enum): """ - Base class for fg and bg classes - This expects the child classes to define enums of: color name -> ANSI color sequence + Base class used for defining color enums. See fg and bg classes for examples. + This expects the child classes to define enums of the follow structure + key: color name (e.g. black) + value: anything that when cast to a string returns an ANSI sequence """ def __str__(self) -> str: """ @@ -38,7 +40,7 @@ class ColorBase(Enum): This is helpful when using a ColorBase in an f-string or format() call e.g. my_str = "{}hello{}".format(fg.blue, fg.reset) """ - return self.value + return str(self.value) def __add__(self, other: Any) -> str: """ @@ -57,12 +59,12 @@ class ColorBase(Enum): @classmethod def colors(cls) -> List[str]: """Return a list of color names.""" - return [color.name for color in cls] + # Use __members__ to ensure we get all key names, including those which are aliased + return [color for color in cls.__members__] # Foreground colors -# noinspection PyPep8Naming,DuplicatedCode -@unique +# noinspection PyPep8Naming class fg(ColorBase): """Enum class for foreground colors""" black = Fore.BLACK @@ -85,8 +87,7 @@ class fg(ColorBase): # Background colors -# noinspection PyPep8Naming,DuplicatedCode -@unique +# noinspection PyPep8Naming class bg(ColorBase): """Enum class for background colors""" black = Back.BLACK |