mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
Replace rich formatting stripper (#706)
* Revert "Fix overly greedy _strip_rich_formatting (#703)"
This reverts commit ae6a31463e
.
* Improve rich formatter stripper
reverts and replaces #703
This commit is contained in:
parent
8947ffbc94
commit
fa6bc59b29
10
kasa/cli.py
10
kasa/cli.py
@ -37,9 +37,11 @@ except ImportError:
|
|||||||
try:
|
try:
|
||||||
from rich import print as _do_echo
|
from rich import print as _do_echo
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Remove 7-bit C1 ANSI sequences
|
# Strip out rich formatting if rich is not installed
|
||||||
# https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python
|
# but only lower case tags to avoid stripping out
|
||||||
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
|
# raw data from the device that is printed from
|
||||||
|
# the device state.
|
||||||
|
rich_formatting = re.compile(r"\[/?[a-z]+]")
|
||||||
|
|
||||||
def _strip_rich_formatting(echo_func):
|
def _strip_rich_formatting(echo_func):
|
||||||
"""Strip rich formatting from messages."""
|
"""Strip rich formatting from messages."""
|
||||||
@ -47,7 +49,7 @@ except ImportError:
|
|||||||
@wraps(echo_func)
|
@wraps(echo_func)
|
||||||
def wrapper(message=None, *args, **kwargs):
|
def wrapper(message=None, *args, **kwargs):
|
||||||
if message is not None:
|
if message is not None:
|
||||||
message = ansi_escape.sub("", message)
|
message = rich_formatting.sub("", message)
|
||||||
echo_func(message, *args, **kwargs)
|
echo_func(message, *args, **kwargs)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
Loading…
Reference in New Issue
Block a user