-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored text functions out of util module (#2969)
This move was needed for avoiding circular imports from upcoming refactoring of logging.
- Loading branch information
Showing
7 changed files
with
53 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from molecule.text import strip_ansi_color, strip_ansi_escape | ||
|
||
|
||
def test_strip_ansi_escape(): | ||
string = "ls\r\n\x1b[00m\x1b[01;31mfoo\x1b[00m\r\n\x1b[01;31m" | ||
|
||
assert "ls\r\nfoo\r\n" == strip_ansi_escape(string) | ||
|
||
|
||
def test_strip_ansi_color(): | ||
s = "foo\x1b[0m\x1b[0m\x1b[0m\n\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m" | ||
|
||
assert "foo\n" == strip_ansi_color(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Text utils.""" | ||
import re | ||
|
||
|
||
def chomp(text: str) -> str: | ||
"""Remove any training spaces from string.""" | ||
return "\n".join([x.rstrip() for x in text.splitlines()]) | ||
|
||
|
||
def strip_ansi_escape(data): | ||
"""Remove all ANSI escapes from string or bytes. | ||
If bytes is passed instead of string, it will be converted to string | ||
using UTF-8. | ||
""" | ||
if isinstance(data, bytes): | ||
data = data.decode("utf-8") | ||
|
||
return re.sub(r"\x1b[^m]*m", "", data) | ||
|
||
|
||
def strip_ansi_color(data): | ||
"""Remove ANSI colors from string or bytes.""" | ||
if isinstance(data, bytes): | ||
data = data.decode("utf-8") | ||
|
||
# Taken from tabulate | ||
invisible_codes = re.compile(r"\x1b\[\d*m") | ||
|
||
return re.sub(invisible_codes, "", data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters