From 4d1ac14c0339e4745345efff9188bc241330a2f8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 24 Jan 2024 11:13:21 -1000 Subject: [PATCH] Fix overly greedy _strip_rich_formatting --- kasa/cli.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kasa/cli.py b/kasa/cli.py index 5f726be05..86aea4367 100755 --- a/kasa/cli.py +++ b/kasa/cli.py @@ -37,6 +37,9 @@ try: from rich import print as _do_echo except ImportError: + # Remove 7-bit C1 ANSI sequences + # https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python + ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") def _strip_rich_formatting(echo_func): """Strip rich formatting from messages.""" @@ -44,7 +47,7 @@ def _strip_rich_formatting(echo_func): @wraps(echo_func) def wrapper(message=None, *args, **kwargs): if message is not None: - message = re.sub(r"\[/?.+?]", "", message) + message = ansi_escape.sub("", message) echo_func(message, *args, **kwargs) return wrapper