Skip to content

Commit 09aeb21

Browse files
committed
Removed unnecessary methods from fg and bg color enums
1 parent 4307a20 commit 09aeb21

File tree

2 files changed

+8
-32
lines changed

2 files changed

+8
-32
lines changed

cmd2/ansi.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,11 @@ class fg(Enum):
5050
bright_white = Fore.LIGHTWHITE_EX
5151
reset = Fore.RESET
5252

53-
def __str__(self) -> str:
54-
"""Make the value the string representation instead of the enum name."""
55-
return self.value
56-
5753
@staticmethod
5854
def colors() -> List[str]:
5955
"""Return a list of color names."""
6056
return [color.name for color in fg]
6157

62-
@staticmethod
63-
def get_value(name: str) -> str:
64-
"""Retrieve color code by name string."""
65-
return fg[name].value
66-
6758

6859
# Background colors
6960
# noinspection PyPep8Naming,DuplicatedCode
@@ -88,20 +79,11 @@ class bg(Enum):
8879
bright_white = Back.LIGHTWHITE_EX
8980
reset = Back.RESET
9081

91-
def __str__(self) -> str:
92-
"""Make the value the string representation instead of the enum name."""
93-
return self.value
94-
9582
@staticmethod
9683
def colors() -> List[str]:
9784
"""Return a list of color names."""
9885
return [color.name for color in bg]
9986

100-
@staticmethod
101-
def get_value(name: str) -> str:
102-
"""Retrieve color code by name string."""
103-
return bg[name].value
104-
10587

10688
FG_RESET = fg.reset.value
10789
BG_RESET = bg.reset.value
@@ -162,7 +144,7 @@ def fg_lookup(fg_name: Union[str, fg]) -> str:
162144
return fg_name.value
163145

164146
try:
165-
ansi_escape = fg.get_value(fg_name.lower())
147+
ansi_escape = fg[fg_name.lower()].value
166148
except KeyError:
167149
raise ValueError('Foreground color {!r} does not exist; must be one of: {}'.format(fg_name, fg.colors()))
168150
return ansi_escape
@@ -180,7 +162,7 @@ def bg_lookup(bg_name: Union[str, bg]) -> str:
180162
return bg_name.value
181163

182164
try:
183-
ansi_escape = bg.get_value(bg_name.lower())
165+
ansi_escape = bg[bg_name.lower()].value
184166
except KeyError:
185167
raise ValueError('Background color {!r} does not exist; must be one of: {}'.format(bg_name, bg.colors()))
186168
return ansi_escape

tests/test_ansi.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ def test_style_none():
3232
def test_style_fg():
3333
base_str = HELLO_WORLD
3434
fg_color = 'blue'
35-
ansi_str = ansi.fg.get_value(fg_color) + base_str + ansi.FG_RESET
35+
ansi_str = ansi.fg[fg_color].value + base_str + ansi.FG_RESET
3636
assert ansi.style(base_str, fg=fg_color) == ansi_str
3737

3838

3939
def test_style_bg():
4040
base_str = HELLO_WORLD
4141
bg_color = 'green'
42-
ansi_str = ansi.bg.get_value(bg_color) + base_str + ansi.BG_RESET
42+
ansi_str = ansi.bg[bg_color].value + base_str + ansi.BG_RESET
4343
assert ansi.style(base_str, bg=bg_color) == ansi_str
4444

4545

@@ -65,7 +65,7 @@ def test_style_multi():
6565
base_str = HELLO_WORLD
6666
fg_color = 'blue'
6767
bg_color = 'green'
68-
ansi_str = (ansi.fg.get_value(fg_color) + ansi.bg.get_value(bg_color) +
68+
ansi_str = (ansi.fg[fg_color].value + ansi.bg[bg_color].value +
6969
ansi.INTENSITY_BRIGHT + ansi.INTENSITY_DIM + ansi.UNDERLINE_ENABLE +
7070
base_str +
7171
ansi.FG_RESET + ansi.BG_RESET +
@@ -85,7 +85,7 @@ def test_style_color_not_exist():
8585

8686
def test_fg_lookup_exist():
8787
fg_color = 'green'
88-
assert ansi.fg_lookup(fg_color) == ansi.fg.get_value(fg_color)
88+
assert ansi.fg_lookup(fg_color) == ansi.fg_lookup(ansi.fg.green)
8989

9090

9191
def test_fg_lookup_nonexist():
@@ -94,8 +94,8 @@ def test_fg_lookup_nonexist():
9494

9595

9696
def test_bg_lookup_exist():
97-
bg_color = 'green'
98-
assert ansi.bg_lookup(bg_color) == ansi.bg.get_value(bg_color)
97+
bg_color = 'red'
98+
assert ansi.bg_lookup(bg_color) == ansi.bg_lookup(ansi.bg.red)
9999

100100

101101
def test_bg_lookup_nonexist():
@@ -124,15 +124,9 @@ def test_async_alert_str(cols, prompt, line, cursor, msg, expected):
124124
def test_fg_enum():
125125
assert ansi.fg_lookup('bright_red') == ansi.fg_lookup(ansi.fg.bright_red)
126126

127-
def test_fg_enum_to_str():
128-
assert str(ansi.fg.black) == ansi.fg_lookup('black')
129-
130127
def test_bg_enum():
131128
assert ansi.bg_lookup('green') == ansi.bg_lookup(ansi.bg.green)
132129

133-
def test_bg_enum_to_str():
134-
assert str(ansi.bg.blue) == ansi.bg_lookup('blue')
135-
136130
def test_fg_colors():
137131
assert list(ansi.fg.__members__.keys()) == ansi.fg.colors()
138132

0 commit comments

Comments
 (0)