Skip to content

Instantly share code, notes, and snippets.

@abbradar
Last active December 3, 2019 08:19
Show Gist options
  • Save abbradar/1e737267abcde772f68484e2a7eee4c1 to your computer and use it in GitHub Desktop.
Save abbradar/1e737267abcde772f68484e2a7eee4c1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import pty
import termios
import sys
import tty
import os
FROM_STR = b"#"
TO_STR = "🦃".encode("utf-8")
pid, tty_fd = pty.fork()
if pid == pty.CHILD:
os.execvp(sys.argv[1], sys.argv[1:])
with os.fdopen(tty_fd, "r") as tty_file:
stdout_fd = sys.stdout.fileno()
while True:
try:
remaining = os.read(tty_fd, 1024)
except OSError:
break
if len(remaining) == 0:
break
remaining = remaining.replace(FROM_STR, TO_STR)
while len(remaining) > 0:
written_len = os.write(stdout_fd, remaining)
remaining = remaining[written_len:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment