Skip to content

Instantly share code, notes, and snippets.

@aayushmau5
Created November 3, 2022 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aayushmau5/1c02dce084c747e397d7f8d9edfd1f7d to your computer and use it in GitHub Desktop.
Save aayushmau5/1c02dce084c747e397d7f8d9edfd1f7d to your computer and use it in GitHub Desktop.
Elixir strip ansi
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