#!/usr/bin/env python3
"""Strip terminal escape sequences from a file or stdin."""
# std imports
import sys

# local
from wcwidth import strip_sequences

# some care is taken regarding '\r\n' because this program is meant to show
# raw terminal data to terminals that are *not* in raw mode, so different
# interpretation of '\r\n' is needed.

if len(sys.argv) > 1:
    with open(sys.argv[1], "rb") as f:
        text = f.read().replace(b"\r\n", b"\n").replace(b"\r", b"").decode()
else:
    text = sys.stdin.buffer.read().replace(b"\r\n", b"\n").replace(b"\r", b"").decode()
sys.stdout.write(strip_sequences(text))
