Skip to content

Commit f970c16

Browse files
committed
Added dim text style support
1 parent e6f251f commit f970c16

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
* Bug Fixes
33
* Fixed bug where startup script containing a single quote in its file name was incorrectly quoted
44
* Added missing implicit dependency on `setuptools` due to build with `setuptools_scm`
5+
* Enhancements
6+
* Added dim text style support via `style()` function and `ansi.INTENSITY_DIM` setting.
57
* Breaking changes
68
* Renamed the following `ansi` members for accuracy in what types of ANSI escape sequences are handled
79
* `ansi.allow_ansi` -> `ansi.allow_style`
810
* `ansi.ansi_safe_wcswidth()` -> `ansi.style_aware_wcswidth()`
911
* `ansi.ansi_aware_write()` -> `ansi.style_aware_write()`
12+
* Renamed the following `ansi` members for clarification
13+
* `ansi.BRIGHT` -> `ansi.INTENSITY_BRIGHT`
14+
* `ansi.NORMAL` -> `ansi.INTENSITY_NORMAL`
1015

1116
## 0.9.22 (December 9, 2019)
1217
* Bug Fixes

cmd2/ansi.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
# Controls when ANSI style style sequences are allowed in output
2323
allow_style = STYLE_TERMINAL
2424

25-
# Regular expression to match ANSI style sequences
26-
# This matches: colorama.ansi.CSI + 0 or more digits + m
27-
ANSI_STYLE_RE = re.compile(r'\033\[[0-9]*m')
25+
# Regular expression to match ANSI style sequences (including 8-bit and 24-bit colors)
26+
ANSI_STYLE_RE = re.compile(r'\x1b\[[^m]*m')
2827

2928
# Foreground color presets
3029
FG_COLORS = {
@@ -72,8 +71,10 @@
7271
BG_RESET = BG_COLORS['reset']
7372
RESET_ALL = Style.RESET_ALL
7473

75-
BRIGHT = Style.BRIGHT
76-
NORMAL = Style.NORMAL
74+
# Text intensities
75+
INTENSITY_BRIGHT = Style.BRIGHT
76+
INTENSITY_DIM = Style.DIM
77+
INTENSITY_NORMAL = Style.NORMAL
7778

7879
# ANSI style sequences not provided by colorama
7980
UNDERLINE_ENABLE = colorama.ansi.code_to_chars(4)
@@ -139,7 +140,8 @@ def bg_lookup(bg_name: str) -> str:
139140
return ansi_escape
140141

141142

142-
def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underline: bool = False) -> str:
143+
def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False,
144+
dim: bool = False, underline: bool = False) -> str:
143145
"""
144146
Apply ANSI colors and/or styles to a string and return it.
145147
The styling is self contained which means that at the end of the string reset code(s) are issued
@@ -148,7 +150,8 @@ def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underlin
148150
:param text: Any object compatible with str.format()
149151
:param fg: foreground color. Relies on `fg_lookup()` to retrieve ANSI escape based on name. Defaults to no color.
150152
:param bg: background color. Relies on `bg_lookup()` to retrieve ANSI escape based on name. Defaults to no color.
151-
:param bold: apply the bold style if True. Defaults to False.
153+
:param bold: apply the bold style if True. Can be combined with dim. Defaults to False.
154+
:param dim: apply the dim style if True. Can be combined with bold. Defaults to False.
152155
:param underline: apply the underline style if True. Defaults to False.
153156
:return: the stylized string
154157
"""
@@ -171,8 +174,12 @@ def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underlin
171174
removals.append(BG_RESET)
172175

173176
if bold:
174-
additions.append(Style.BRIGHT)
175-
removals.append(Style.NORMAL)
177+
additions.append(INTENSITY_BRIGHT)
178+
removals.append(INTENSITY_NORMAL)
179+
180+
if dim:
181+
additions.append(INTENSITY_DIM)
182+
removals.append(INTENSITY_NORMAL)
176183

177184
if underline:
178185
additions.append(UNDERLINE_ENABLE)

tests/test_ansi.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ def test_style_bg():
4545

4646
def test_style_bold():
4747
base_str = HELLO_WORLD
48-
ansi_str = ansi.BRIGHT + base_str + ansi.NORMAL
48+
ansi_str = ansi.INTENSITY_BRIGHT + base_str + ansi.INTENSITY_NORMAL
4949
assert ansi.style(base_str, bold=True) == ansi_str
5050

5151

52+
def test_style_dim():
53+
base_str = HELLO_WORLD
54+
ansi_str = ansi.INTENSITY_DIM + base_str + ansi.INTENSITY_NORMAL
55+
assert ansi.style(base_str, dim=True) == ansi_str
56+
57+
5258
def test_style_underline():
5359
base_str = HELLO_WORLD
5460
ansi_str = ansi.UNDERLINE_ENABLE + base_str + ansi.UNDERLINE_DISABLE
@@ -59,9 +65,12 @@ def test_style_multi():
5965
base_str = HELLO_WORLD
6066
fg_color = 'blue'
6167
bg_color = 'green'
62-
ansi_str = ansi.FG_COLORS[fg_color] + ansi.BG_COLORS[bg_color] + ansi.BRIGHT + ansi.UNDERLINE_ENABLE + \
63-
base_str + ansi.FG_RESET + ansi.BG_RESET + ansi.NORMAL + ansi.UNDERLINE_DISABLE
64-
assert ansi.style(base_str, fg=fg_color, bg=bg_color, bold=True, underline=True) == ansi_str
68+
ansi_str = (ansi.FG_COLORS[fg_color] + ansi.BG_COLORS[bg_color] +
69+
ansi.INTENSITY_BRIGHT + ansi.INTENSITY_DIM + ansi.UNDERLINE_ENABLE +
70+
base_str +
71+
ansi.FG_RESET + ansi.BG_RESET +
72+
ansi.INTENSITY_NORMAL + ansi.INTENSITY_NORMAL + ansi.UNDERLINE_DISABLE)
73+
assert ansi.style(base_str, fg=fg_color, bg=bg_color, bold=True, dim=True, underline=True) == ansi_str
6574

6675

6776
def test_style_color_not_exist():

0 commit comments

Comments
 (0)