Skip to content

Commit

Permalink
πŸ“… Commit Sun, 29 Aug 2021 20:27:29
Browse files Browse the repository at this point in the history
πŸ”οΈ strip_ansi added
  • Loading branch information
securisec committed Aug 30, 2021
1 parent 15546e6 commit 68df17f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Misc:
☐ cyberchef recipe to chepy recipe converter

Archive:
βœ” strip ansi color codes @project(New ideas)
βœ” build pyinstaller artifact for osx, linux, windows @project(Github Actions)
βœ” @high from morse word delimiter is broken - .... . / ..-. .-.. .- --. / .. ... / --- .--- --.- -..- . -.-- ...-- ..- -- --.. ..... .-- .. -- .-.. . .-.. ..... ....- - .- ..... -.- --... --- .- --.. - --. ..--- ..--- --... --. -... --.. ..-. -.... -. .-.. --.- .--. . --... - -.... .--. --.. --... .-.. ..... - --. -.-. -. -.. -... -- -- ...-- -.. .- -. .-.. ..... / -... .- ... . ...-- ..--- @project(Bug)
βœ” ecc generate @project(New ideas)
Expand Down
18 changes: 18 additions & 0 deletions chepy/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,24 @@ def slice(self, start: int = 0, end: int = None) -> UtilsT:
self.state = self.state[start:end]
return self

@ChepyDecorators.call_stack
def strip_ansi(self) -> UtilsT:
"""Strip ANSI escape codes from string
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("\033[31mThis is a string\033[0m").strip_ansi().o
"This is a string"
"""
self.state = re.sub(
"[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))",
"",
self._convert_to_str(),
)
return self

@ChepyDecorators.call_stack
def strip(self, pattern: str, ignore_case=True) -> UtilsT:
"""Strip matched pattern
Expand Down
5 changes: 3 additions & 2 deletions chepy/modules/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class Utils(ChepyCore):
def filter_dict_key(self: UtilsT, by: str) -> UtilsT: ...
def filter_dict_value(self: UtilsT, by: str) -> UtilsT: ...
def slice(self: UtilsT, start: int=..., end: int=...) -> UtilsT: ...
def strip(self: UtilsT, pattern: str, ignore_case: Any=...) -> UtilsT: ...
def find_replace(self: UtilsT, pattern: str, repl: str, ignore_case: Any=...) -> UtilsT: ...
def strip_ansi(self: UtilsT) -> UtilsT: ...
def strip(self: UtilsT, pattern: str, ignore_case: bool=...) -> UtilsT: ...
def find_replace(self: UtilsT, pattern: str, repl: str, ignore_case: bool=...) -> UtilsT: ...
def escape_string(self: UtilsT) -> UtilsT: ...
def unescape_string(self: UtilsT) -> UtilsT: ...
def color_hex_to_rgb(self: UtilsT) -> UtilsT: ...
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def test_slice():
assert Chepy("some data").slice(3, 6).o == "e d"


def test_strip_ansi():
assert Chepy(";-- main:").strip_ansi().o == ";-- main:"


def test_strip():
assert Chepy("some some data").strip(r"some\s").o == "data"

Expand Down

0 comments on commit 68df17f

Please sign in to comment.