diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-02-07 06:12:14 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-02-07 06:12:14 -0500 |
commit | 09aeb21dbdbc7b6016daa3d9ee277d7050abb7d4 (patch) | |
tree | f4c98d9873e9885098bb3bb0d6ead7f70e5043c6 | |
parent | 4307a20ea2e6d3df8f3ca628aced7fdaaf3c89c1 (diff) | |
download | cmd2-git-09aeb21dbdbc7b6016daa3d9ee277d7050abb7d4.tar.gz |
Removed unnecessary methods from fg and bg color enums
-rw-r--r-- | cmd2/ansi.py | 22 | ||||
-rw-r--r-- | tests/test_ansi.py | 18 |
2 files changed, 8 insertions, 32 deletions
diff --git a/cmd2/ansi.py b/cmd2/ansi.py index 013aa107..98b98726 100644 --- a/cmd2/ansi.py +++ b/cmd2/ansi.py @@ -50,20 +50,11 @@ class fg(Enum): bright_white = Fore.LIGHTWHITE_EX reset = Fore.RESET - def __str__(self) -> str: - """Make the value the string representation instead of the enum name.""" - return self.value - @staticmethod def colors() -> List[str]: """Return a list of color names.""" return [color.name for color in fg] - @staticmethod - def get_value(name: str) -> str: - """Retrieve color code by name string.""" - return fg[name].value - # Background colors # noinspection PyPep8Naming,DuplicatedCode @@ -88,20 +79,11 @@ class bg(Enum): bright_white = Back.LIGHTWHITE_EX reset = Back.RESET - def __str__(self) -> str: - """Make the value the string representation instead of the enum name.""" - return self.value - @staticmethod def colors() -> List[str]: """Return a list of color names.""" return [color.name for color in bg] - @staticmethod - def get_value(name: str) -> str: - """Retrieve color code by name string.""" - return bg[name].value - FG_RESET = fg.reset.value BG_RESET = bg.reset.value @@ -162,7 +144,7 @@ def fg_lookup(fg_name: Union[str, fg]) -> str: return fg_name.value try: - ansi_escape = fg.get_value(fg_name.lower()) + ansi_escape = fg[fg_name.lower()].value except KeyError: raise ValueError('Foreground color {!r} does not exist; must be one of: {}'.format(fg_name, fg.colors())) return ansi_escape @@ -180,7 +162,7 @@ def bg_lookup(bg_name: Union[str, bg]) -> str: return bg_name.value try: - ansi_escape = bg.get_value(bg_name.lower()) + ansi_escape = bg[bg_name.lower()].value except KeyError: raise ValueError('Background color {!r} does not exist; must be one of: {}'.format(bg_name, bg.colors())) return ansi_escape diff --git a/tests/test_ansi.py b/tests/test_ansi.py index 51c3b50f..526a06e4 100644 --- a/tests/test_ansi.py +++ b/tests/test_ansi.py @@ -32,14 +32,14 @@ def test_style_none(): def test_style_fg(): base_str = HELLO_WORLD fg_color = 'blue' - ansi_str = ansi.fg.get_value(fg_color) + base_str + ansi.FG_RESET + ansi_str = ansi.fg[fg_color].value + base_str + ansi.FG_RESET assert ansi.style(base_str, fg=fg_color) == ansi_str def test_style_bg(): base_str = HELLO_WORLD bg_color = 'green' - ansi_str = ansi.bg.get_value(bg_color) + base_str + ansi.BG_RESET + ansi_str = ansi.bg[bg_color].value + base_str + ansi.BG_RESET assert ansi.style(base_str, bg=bg_color) == ansi_str @@ -65,7 +65,7 @@ def test_style_multi(): base_str = HELLO_WORLD fg_color = 'blue' bg_color = 'green' - ansi_str = (ansi.fg.get_value(fg_color) + ansi.bg.get_value(bg_color) + + ansi_str = (ansi.fg[fg_color].value + ansi.bg[bg_color].value + ansi.INTENSITY_BRIGHT + ansi.INTENSITY_DIM + ansi.UNDERLINE_ENABLE + base_str + ansi.FG_RESET + ansi.BG_RESET + @@ -85,7 +85,7 @@ def test_style_color_not_exist(): def test_fg_lookup_exist(): fg_color = 'green' - assert ansi.fg_lookup(fg_color) == ansi.fg.get_value(fg_color) + assert ansi.fg_lookup(fg_color) == ansi.fg_lookup(ansi.fg.green) def test_fg_lookup_nonexist(): @@ -94,8 +94,8 @@ def test_fg_lookup_nonexist(): def test_bg_lookup_exist(): - bg_color = 'green' - assert ansi.bg_lookup(bg_color) == ansi.bg.get_value(bg_color) + bg_color = 'red' + assert ansi.bg_lookup(bg_color) == ansi.bg_lookup(ansi.bg.red) def test_bg_lookup_nonexist(): @@ -124,15 +124,9 @@ def test_async_alert_str(cols, prompt, line, cursor, msg, expected): def test_fg_enum(): assert ansi.fg_lookup('bright_red') == ansi.fg_lookup(ansi.fg.bright_red) -def test_fg_enum_to_str(): - assert str(ansi.fg.black) == ansi.fg_lookup('black') - def test_bg_enum(): assert ansi.bg_lookup('green') == ansi.bg_lookup(ansi.bg.green) -def test_bg_enum_to_str(): - assert str(ansi.bg.blue) == ansi.bg_lookup('blue') - def test_fg_colors(): assert list(ansi.fg.__members__.keys()) == ansi.fg.colors() |