Created
December 19, 2024 19:05
-
-
Save simonLeary42/5e8521ab2804346568c51e3ed0918da6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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