Skip to content

Instantly share code, notes, and snippets.

@developer-sdk
Created August 13, 2023 00:49
Show Gist options
  • Save developer-sdk/3ede5192967e9bcbc66838bb9c8d8988 to your computer and use it in GitHub Desktop.
Save developer-sdk/3ede5192967e9bcbc66838bb9c8d8988 to your computer and use it in GitHub Desktop.
ansi color code 제외
def escape_ansi_color_pattern(ansi_text: str) -> str:
# 7-bit C1 ANSI sequences
ansi_escape = re.compile(r'''
\x1B # ESC
(?: # 7-bit C1 Fe (except CSI)
[@-Z\\-_]
| # or [ for CSI, followed by a control sequence
\[
[0-?]* # Parameter bytes
[ -/]* # Intermediate bytes
[@-~] # Final byte
)
''', re.VERBOSE)
result = ansi_escape.sub('', ansi_text)
return result
if __name__ == '__main__':
stderr = r"""╷
│ Error: Error creating openstack_networking_port_v2: Bad request with: [POST https://network.openstack.io/v2.0/ports], error message: {"NeutronError": {"type": "HTTPBadRequest", "message": "Invalid input for network_id. Reason: 'a' is not a valid UUID.", "detail": ""}}
│ 
│  with openstack_networking_port_v2.a,
│  on instance.tf line 1, in resource "openstack_networking_port_v2" "a":
│  1: resource "openstack_networking_port_v2" "a" {
│ 
╵"""
# print(stderr)
escape_error = escape_ansi_color_pattern(stderr)
errors = escape_error.split("\n")
error = ""
if len(errors) > 2:
error = errors[1]
print(error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment