Skip to content

Instantly share code, notes, and snippets.

@simonLeary42
Created December 19, 2024 19:05
Show Gist options
  • Save simonLeary42/5e8521ab2804346568c51e3ed0918da6 to your computer and use it in GitHub Desktop.
Save simonLeary42/5e8521ab2804346568c51e3ed0918da6 to your computer and use it in GitHub Desktop.
import re
import sys
import random
def replace_char(x):
if x.isalpha():
return random.choice("αβγδεζηθικλμνξοπρστυφχψω")
else:
return x
def censor(input_string):
"""
Replaces all characters in a string with random characters,
while preserving ANSI escape codes.
"""
# https://stackoverflow.com/a/14693789/18696276
ansi_escape_pattern = r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])"
parts = re.split(f"({ansi_escape_pattern})", input_string)
replaced_parts = [
(
"".join(replace_char(c) for c in part)
if not re.match(ansi_escape_pattern, part)
else part
)
for part in parts
]
return "".join(replaced_parts)
x = sys.stdin.read()
print(censor(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment