forked from Random832/ttyemu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
slowpty.py
executable file
·48 lines (44 loc) · 1.37 KB
/
slowpty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
"Simple script to reduce output speed."
# Doing it inside the terminal emulator alone causes serious problems for
# non-pty backends. This allows e.g. interrupting a long output to work more
# like how it would have on classic systems. Works in WSL and Mac, but not
# Linux. There seems to be no way to get nice behavior at all on Linux.
import pty
import os
import select
import time
import sys
import termios
import tty
# pylint: disable=invalid-name,protected-access,broad-except
def main():
"Main function"
attr = termios.tcgetattr(0)
pid, fd = pty.fork()
cmd = sys.argv[1:] or ['sh']
CHARS_PER_SEC = 10
CHAR_DELAY = 1/CHARS_PER_SEC
if pid == 0:
try:
attr[4] = attr[5] = termios.B110
termios.tcsetattr(0, termios.TCSAFLUSH, attr)
os.execvp(cmd[0], cmd)
except Exception as e:
print(e)
os._exit(126)
os._exit(126)
tty.setraw(0)
try:
while True:
rl = select.select((0, fd), (), ())[0]
if 0 in rl:
data = os.read(0, 64)
os.write(fd, data)
if fd in rl:
data = os.read(fd, 1)
os.write(1, data)
time.sleep(0.1)
finally:
termios.tcsetattr(0, termios.TCSAFLUSH, attr)
main()