summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-26 01:20:48 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-26 01:20:48 -0400
commite6f65e607c6ad2350dfce05f3d5080d0ffbe35f1 (patch)
treed777863fc4f5143bb4e53b83ed27b18734945a46 /cmd2/utils.py
parentdb984de915944d6421e70b2064400fb3c0ec2f9c (diff)
downloadcmd2-git-e6f65e607c6ad2350dfce05f3d5080d0ffbe35f1.tar.gz
Added bold and underline to style()
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py20
1 files changed, 16 insertions, 4 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py
index ae3bb535..a5b7f43f 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -12,6 +12,7 @@ import threading
import unicodedata
from typing import Any, Iterable, List, Optional, TextIO, Union
+from colorama import Style
from wcwidth import wcswidth
from . import constants
@@ -36,14 +37,15 @@ def ansi_safe_wcswidth(text: str) -> int:
return wcswidth(strip_ansi(text))
-def style(text: Any, *, fg: str = '', bg: str = '') -> str:
+def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underline: bool = False) -> str:
"""
Applies 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 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.
"""
values = []
text = "{}".format(text)
@@ -57,11 +59,21 @@ def style(text: Any, *, fg: str = '', bg: str = '') -> str:
values.append(constants.BG_COLORS[bg.lower()])
except KeyError:
raise ValueError('Color {} does not exist.'.format(bg))
+ if bold:
+ values.append(Style.BRIGHT)
+ if underline:
+ values.append(constants.Underline.ENABLE)
+
values.append(text)
if fg:
values.append(constants.FG_COLORS['reset'])
if bg:
values.append(constants.BG_COLORS['reset'])
+ if bold:
+ values.append(Style.NORMAL)
+ if underline:
+ values.append(constants.Underline.DISABLE)
+
return "".join(values)