diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-06-26 10:36:24 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-06-26 10:36:24 -0400 |
commit | 72044030810dd293699a6bae8853e1cfd0b4887b (patch) | |
tree | f389fd97625c55e161cf04ac7e879422a2b19e9a /cmd2/ansi.py | |
parent | e34bba44ef53228aeba613ac6e928b2c315111cf (diff) | |
download | cmd2-git-72044030810dd293699a6bae8853e1cfd0b4887b.tar.gz |
Added TextStyle class and default implementations for various message types like Warning, Error, and Succes
Diffstat (limited to 'cmd2/ansi.py')
-rw-r--r-- | cmd2/ansi.py | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/cmd2/ansi.py b/cmd2/ansi.py index 889f1cb3..667a279d 100644 --- a/cmd2/ansi.py +++ b/cmd2/ansi.py @@ -72,46 +72,66 @@ def ansi_safe_wcswidth(text: str) -> int: return wcswidth(strip_ansi(text)) -def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underline: bool = False) -> str: +class TextStyle: + """Style settings for text""" + + def __init__(self, *, fg: str = '', bg: str = '', bold: bool = False, underline: bool = False): + """ + Initializer + :param fg: foreground color. Expects color names in FG_COLORS (e.g. 'lightred'). Defaults to blank. + :param bg: background color. Expects color names in BG_COLORS (e.g. 'black'). Defaults to blank. + :param bold: apply the bold style if True. Defaults to False. + :param underline: apply the underline style if True. Defaults to False. + """ + self.fg = fg + self.bg = bg + self.bold = bold + self.underline = underline + + +# Default styles. These can be altered to suit an application's needs. +SuccessStyle = TextStyle(fg='green') +WarningStyle = TextStyle(fg='lightyellow') +ErrorStyle = TextStyle(fg='lightred') + + +def style(text: Any, text_style: TextStyle) -> str: """ - Applies styles to text + Applies a style to text :param text: Any object compatible with str.format() - :param fg: foreground color. Accepts color names like 'red' or 'blue' - :param bg: background color. Accepts color names like 'red' or 'blue' - :param bold: apply the bold style if True. Defaults to False. - :param underline: apply the underline style if True. Defaults to False. + :param text_style: the style to be applied """ values = [] text = "{}".format(text) # Add styles - if fg: + if text_style.fg: try: - values.append(FG_COLORS[fg.lower()]) + values.append(FG_COLORS[text_style.fg.lower()]) except KeyError: - raise ValueError('Color {} does not exist.'.format(fg)) - if bg: + raise ValueError('Color {} does not exist.'.format(text_style.fg)) + if text_style.bg: try: - values.append(BG_COLORS[bg.lower()]) + values.append(BG_COLORS[text_style.bg.lower()]) except KeyError: - raise ValueError('Color {} does not exist.'.format(bg)) - if bold: + raise ValueError('Color {} does not exist.'.format(text_style.bg)) + if text_style.bold: values.append(Style.BRIGHT) - if underline: + if text_style.underline: underline_enable = colorama.ansi.code_to_chars(4) values.append(underline_enable) values.append(text) # Remove styles - if fg: + if text_style.fg: values.append(FG_COLORS['reset']) - if bg: + if text_style.bg: values.append(BG_COLORS['reset']) - if bold: + if text_style.bold: values.append(Style.NORMAL) - if underline: + if text_style.underline: underline_disable = colorama.ansi.code_to_chars(24) values.append(underline_disable) |