Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ANSI_ESCAPE_PATTERN for discussion #313 #316

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions scrapli/channel/base_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
from scrapli.transport.base import AsyncTransport, Transport

ANSI_ESCAPE_PATTERN = re.compile(
rb"[\x1B\x9B]"
rb"[\[\]()#;?]*"
pattern=rb"[\x1B\x9B\x9D](\s)?" # Prefix ESC (^) or CSI (^[) or OSC (^])
rb"("
rb"(([a-zA-Z0-9]*(;[a-zA-Z\d]*)*)?\x07)"
rb"([78ME])" # control cursor position
rb"|"
rb"((\d{1,4}(;\d{0,4})*)?[\dA-PRZcf-ntqry=><~])"
rb")"
rb"([\x07])" # BEL (Terminal bell)
rb"|"
rb"(\[[{}();#=?0-9]*[A-Zhglnmsu~])" # control codes starts with `[` e.x. ESC [2;37;41m
rb")",
flags=re.VERBOSE,
)


Expand Down
48 changes: 48 additions & 0 deletions tests/unit/channel/test_base_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,54 @@ def test_process_output(base_channel):
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",
),
(
b"foo\x1B[4mcake\x1B[0m'",
b"foocake'",
),
(
b"\x1B[4mcake\x1B[0m",
b"cake",
),
(
b"foo\x1B[4mcake\x1B[0m",
b"foocake",
),
(
b"\x1B[0m\x1B[4m\x1B[42m\x1B[31mfoo\x1B[39m\x1B[49m\x1B[24mfoo\x1B[0m",
b"foofoo",
),
(
b"foo\x1B[mfoo",
b"foofoo",
),
(
b"\x1B[00;38;5;244m\x1B[m\x1B[00;38;5;33mfoo\x1B[0m",
b"foo",
),
(
b"\x1B[0;33;49;3;9;4mbar\x1B[0m",
b"bar",
),
(
b"foo\x1B[0;33;49;3;9;4mbar",
b"foobar",
),
(
b"foo\x1B[0gbar",
b"foobar",
),
(
b"foo\x1B[Kbar",
b"foobar",
),
(
b"foo\x1B[2Jbar",
b"foobar",
),
(
b"\x1b7c\x1b8\x1b[1C\x1b7o\x1b8\x1b[1C\x1b7n\x1b8\x1b[1C\x1b7f\x1b8\x1b[1C\x1b7i\x1b8\x1b[1C\x1b7g\x1b8\x1b[1C\x1b7u\x1b8\x1b[1C\x1b7r\x1b8\x1b[1C\x1b7e\x1b8\x1b[1C",
b"configure",
),
),
)
def test_strip_ansi(base_channel, buf: bytes, expected: bytes):
Expand Down
Loading