Skip to content

Commit

Permalink
Merge pull request #302 from denisbondar/main
Browse files Browse the repository at this point in the history
Remove all ANSI codes from buf
  • Loading branch information
carlmontanari authored Oct 21, 2023
2 parents b67ea75 + 7358489 commit d0eb38b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion scrapli/channel/base_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
from scrapli.logging import get_instance_logger
from scrapli.transport.base import AsyncTransport, Transport

ANSI_ESCAPE_PATTERN = re.compile(rb"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\)|E)")
ANSI_ESCAPE_PATTERN = re.compile(
rb"[\x1B\x9B]"
rb"[\[\]()#;?]*"
rb"("
rb"(([a-zA-Z0-9]*(;[a-zA-Z\d]*)*)?\x07)"
rb"|"
rb"((\d{1,4}(;\d{0,4})*)?[\dA-PRZcf-ntqry=><~])"
rb")"
)


@dataclass()
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/channel/test_base_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ def test_process_output(base_channel):
b"VeryLong\x1bECommand",
b"VeryLongCommand",
),
(
b"command-prompt# \x1b7",
b"command-prompt# ",
),
(
b"\x1b[7mCTRL+C\x1b[0m \x1b[7mESC\x1b[0m \x1b[7mq\x1b[0m Quit \x1b[7mSPACE\x1b[0m \x1b[7mn\x1b[0m Next Page \x1b[7mENTER\x1b[0m Next Entry \x1b[7ma\x1b[0m All\x1b[1A\x1b[59C\x1b[27m",
b"CTRL+C ESC q Quit SPACE n Next Page ENTER Next Entry a All",
),
),
)
def test_strip_ansi(base_channel, buf: bytes, expected: bytes):
Expand Down

0 comments on commit d0eb38b

Please sign in to comment.