Created
November 3, 2022 16:21
-
-
Save aayushmau5/1c02dce084c747e397d7f8d9edfd1f7d to your computer and use it in GitHub Desktop.
Elixir strip ansi
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
def strip_ansi(text) do | |
# Taken from: https://github.com/chalk/strip-ansi/blob/main/index.js | |
# Original regex from https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js#L2 contained \u | |
# But PCRE doesn't support \u, it uses \x instead | |
# See: https://stackoverflow.com/questions/3538293/regular-expression-pcre-does-not-support-l-l-n-p | |
ansi_regex = | |
~r/[\x{001B}\x{009B}][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\x{0007})|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))/ | |
String.replace(text, ansi_regex, "") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment