Skip to content

Commit 61aa696

Browse files
committed
Switched to only searching for styles.
1 parent dda9990 commit 61aa696

File tree

3 files changed

+6
-43
lines changed

3 files changed

+6
-43
lines changed

cmd2/rich_utils.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,8 @@
2929

3030
from .styles import DEFAULT_CMD2_STYLES
3131

32-
# A compiled regular expression to detect ANSI escape sequences.
33-
_ANSI_ESCAPE_SEQUENCE_RE = re.compile(
34-
r"""
35-
\x1b # Match the Escape character (ESC)
36-
(?: # Start of non-capturing group for the different sequence types
37-
\[[0-9;?]*[a-zA-Z] # Match a CSI sequence (e.g., \x1b[31m)
38-
| # OR
39-
\].*?(?:\x07|\x1b\x5c) # Match an OSC sequence (e.g., \x1b]2;Hello\x07)
40-
| # OR
41-
\x37|\x38 # Match DEC cursor save/restore sequences
42-
) # End of non-capturing group
43-
""",
44-
re.VERBOSE,
45-
)
32+
# A compiled regular expression to detect ANSI style sequences.
33+
ANSI_STYLE_SEQUENCE_RE = re.compile(r"\x1b\[[0-9;?]*m")
4634

4735

4836
class AllowStyle(Enum):
@@ -339,7 +327,7 @@ def prepare_objects_for_rendering(*objects: Any) -> tuple[Any, ...]:
339327
"""Prepare a tuple of objects for printing by Rich's Console.print().
340328
341329
This function converts any non-Rich object whose string representation contains
342-
ANSI escape sequences into a rich.Text object. This ensures correct display width
330+
ANSI style sequences into a rich.Text object. This ensures correct display width
343331
calculation, as Rich can then properly parse and account for these non-printing
344332
codes. All other objects are left untouched, allowing Rich's native
345333
renderers to handle them.
@@ -360,8 +348,8 @@ def prepare_objects_for_rendering(*objects: Any) -> tuple[Any, ...]:
360348

361349
renderable_as_str = str(renderable)
362350

363-
# Check for any ANSI escape sequences in the string.
364-
if _ANSI_ESCAPE_SEQUENCE_RE.search(renderable_as_str):
351+
# Check for any ANSI style sequences in the string.
352+
if ANSI_STYLE_SEQUENCE_RE.search(renderable_as_str):
365353
object_list[i] = string_to_rich_text(renderable_as_str)
366354

367355
return tuple(object_list)

cmd2/string_utils.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@
55
full-width characters (like those used in CJK languages).
66
"""
77

8-
import re
9-
108
from rich.align import AlignMethod
119
from rich.style import StyleType
1210

1311
from . import rich_utils as ru
1412

15-
# A compiled regular expression to detect ANSI style sequences.
16-
_ANSI_STYLE_SEQUENCE_RE = re.compile(r"\x1b\[[0-9;?]*m")
17-
1813

1914
def align(
2015
val: str,
@@ -107,7 +102,7 @@ def strip_style(val: str) -> str:
107102
:param val: string to be stripped
108103
:return: the stripped string
109104
"""
110-
return _ANSI_STYLE_SEQUENCE_RE.sub("", val)
105+
return ru.ANSI_STYLE_SEQUENCE_RE.sub("", val)
111106

112107

113108
def str_width(val: str) -> int:

tests/test_rich_utils.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,3 @@ def test_set_theme() -> None:
139139

140140
assert ru.APP_THEME.styles[rich_style_key] != orig_rich_style
141141
assert ru.APP_THEME.styles[rich_style_key] == theme[rich_style_key]
142-
143-
144-
def test_ansi_escape_sequence_re() -> None:
145-
import cmd2.terminal_utils as tu
146-
147-
# Test a CSI sequence
148-
cursor_contol = tu.Cursor.UP(1)
149-
assert ru._ANSI_ESCAPE_SEQUENCE_RE.search(cursor_contol)
150-
151-
# Test an OSC sequence
152-
set_title = tu.set_title_str("Hello")
153-
assert ru._ANSI_ESCAPE_SEQUENCE_RE.search(set_title)
154-
155-
# Test DEC cursor save
156-
cursor_save = "\x1b\x37"
157-
assert ru._ANSI_ESCAPE_SEQUENCE_RE.search(cursor_save)
158-
159-
# Test DEC cursor restore
160-
cursor_restore = "\x1b\x38"
161-
assert ru._ANSI_ESCAPE_SEQUENCE_RE.search(cursor_restore)

0 commit comments

Comments
 (0)