Skip to content

Commit 862d511

Browse files
committed
Suppressed bogus PyCharm warnings and improved error message for invalid colors passed to style
1 parent e66dc6f commit 862d511

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

cmd2/ansi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
# Regular expression to match ANSI style sequences (including 8-bit and 24-bit colors)
2727
ANSI_STYLE_RE = re.compile(r'\x1b\[[^m]*m')
2828

29+
2930
# Foreground colors
31+
# noinspection PyPep8Naming,DuplicatedCode
3032
@unique
3133
class fg(Enum):
3234
"""Enum class for foreground colors (to support IDE autocompletion)."""
@@ -64,6 +66,7 @@ def get_value(color: str) -> str:
6466

6567

6668
# Background colors
69+
# noinspection PyPep8Naming,DuplicatedCode
6770
@unique
6871
class bg(Enum):
6972
"""Enum class for background colors (to support IDE autocompletion)."""
@@ -161,7 +164,7 @@ def fg_lookup(fg_name: Union[str, fg]) -> str:
161164
try:
162165
ansi_escape = fg.get_value(fg_name.lower())
163166
except KeyError:
164-
raise ValueError('Foreground color {!r} does not exist.'.format(fg_name))
167+
raise ValueError('Foreground color {!r} does not exist; must be one of: {}'.format(fg_name, fg.colors()))
165168
return ansi_escape
166169

167170

@@ -179,10 +182,11 @@ def bg_lookup(bg_name: Union[str, bg]) -> str:
179182
try:
180183
ansi_escape = bg.get_value(bg_name.lower())
181184
except KeyError:
182-
raise ValueError('Background color {!r} does not exist.'.format(bg_name))
185+
raise ValueError('Background color {!r} does not exist; must be one of: {}'.format(bg_name, bg.colors()))
183186
return ansi_escape
184187

185188

189+
# noinspection PyShadowingNames
186190
def style(text: Any, *, fg: Union[str, fg] = '', bg: Union[str, bg] = '', bold: bool = False,
187191
dim: bool = False, underline: bool = False) -> str:
188192
"""

0 commit comments

Comments
 (0)