fenrir/play zone/terminalManagement

124 lines
4.0 KiB
Plaintext
Raw Normal View History

2017-08-21 07:12:15 -04:00
import os
2017-08-22 07:08:48 -04:00
import struct
import sys
2017-08-21 07:12:15 -04:00
import pty
2017-08-22 07:08:48 -04:00
import tty
2017-08-22 10:01:27 -04:00
import termios
2017-08-21 07:12:15 -04:00
import shlex
import signal
2017-08-22 07:08:48 -04:00
import select
2017-08-21 07:12:15 -04:00
import pyte
2017-08-22 10:01:27 -04:00
import time
2017-08-21 07:12:15 -04:00
class Terminal:
def __init__(self, columns, lines, p_in):
self.screen = pyte.HistoryScreen(columns, lines)
self.screen.set_mode(pyte.modes.LNM)
self.screen.write_process_input = \
lambda data: p_in.write(data.encode())
self.stream = pyte.ByteStream()
self.stream.attach(self.screen)
def feed(self, data):
self.stream.feed(data)
def dump(self):
cursor = self.screen.cursor
lines = []
for y in self.screen.dirty:
line = self.screen.buffer[y]
data = [(char.data, char.reverse, char.fg, char.bg)
for char in (line[x] for x in range(self.screen.columns))]
lines.append((y, data))
self.screen.dirty.clear()
return {"c": (cursor.x, cursor.y), "lines": lines}
2017-08-24 04:11:07 -04:00
def open_terminal(command="bash", columns=80, lines=25):
2017-08-21 07:12:15 -04:00
p_pid, master_fd = pty.fork()
2017-08-23 08:10:23 -04:00
print('PID',p_pid)
2017-08-21 07:12:15 -04:00
if p_pid == 0: # Child.
argv = shlex.split(command)
env = dict(TERM="linux", LC_ALL="de_DE.UTF-8",
COLUMNS=str(columns), LINES=str(lines))
os.execvpe(argv[0], argv, env)
# File-like object for I/O with the child process aka command.
p_out = os.fdopen(master_fd, "w+b", 0)
return Terminal(columns, lines, p_out), p_pid, p_out
2017-08-21 11:45:03 -04:00
def HandleTerminal():
2017-08-22 10:01:27 -04:00
debug = False
running = True
2017-08-23 03:28:35 -04:00
#attr = termios.tcgetattr(sys.stdin.fileno())
2017-08-22 07:08:48 -04:00
try:
tty.setraw(0)
2017-08-22 10:01:27 -04:00
terminal, p_pid, p_out = open_terminal()
2017-08-24 04:11:07 -04:00
std_out = os.fdopen(sys.stdout.fileno(), "w+b", 0)
2017-08-23 03:28:35 -04:00
#termios.tcdrain(p_pid)
#termios.tcdrain(0)
2017-08-22 10:01:27 -04:00
while running:
2017-08-23 08:10:23 -04:00
time.sleep(0.04)
r, w, x = select.select([sys.stdin, p_out],[],[])
2017-08-22 10:01:27 -04:00
if r == []:
continue
if p_out in r:
if debug:
print('pre p_out')
try:
2017-08-24 04:11:07 -04:00
msgBytes = read_all(p_out.fileno())
#msgBytes = p_out.read(65536)
#msgBytes = os.read(p_out.fileno(), 4)
2017-08-22 10:01:27 -04:00
#p_out.read(4096)
except (EOFError, OSError):
running = False
2017-08-24 04:11:07 -04:00
#sys.exit(0)
2017-08-22 10:01:27 -04:00
os.write(sys.stdout.fileno(), msgBytes)
2017-08-23 08:10:23 -04:00
sys.stdout.flush()
2017-08-22 10:01:27 -04:00
#print(terminal.screen.display)
if debug:
print('after p_out')
if sys.stdin in r:
if debug:
2017-08-24 04:11:07 -04:00
print('pre stdin')
msgBytes = read_all(sys.stdin.fileno())
#msgBytes = os.read(sys.stdin.fileno(), 1)
terminal.feed(msgBytes)
os.write(p_out.fileno(), msgBytes)
2017-08-22 10:01:27 -04:00
if debug:
2017-08-23 08:10:23 -04:00
print('after stdin')
2017-08-22 07:08:48 -04:00
except Exception as e: # Process died?
print(e)
2017-08-22 10:01:27 -04:00
running = False
2017-08-22 07:08:48 -04:00
finally:
2017-08-23 08:10:23 -04:00
#termios.tcsetattr(0, termios.TCSADRAIN, attr)
2017-08-22 07:08:48 -04:00
os.kill(p_pid, signal.SIGTERM)
2017-08-23 08:10:23 -04:00
p_out.close()
2017-08-22 07:08:48 -04:00
def get_terminal_size(fd):
s = struct.pack('HHHH', 0, 0, 0, 0)
rows, cols, _, _ = struct.unpack('HHHH', fcntl.ioctl(fd, termios.TIOCGWINSZ, s))
return rows, cols
def resize_terminal(fd):
s = struct.pack('HHHH', 0, 0, 0, 0)
s = fcntl.ioctl(0, termios.TIOCGWINSZ, s)
fcntl.ioctl(fd, termios.TIOCSWINSZ, s)
rows, cols, _, _ = struct.unpack('hhhh', s)
return rows, cols
2017-08-21 11:45:03 -04:00
2017-08-24 04:11:07 -04:00
def read_all(fd):
bytes = os.read(fd, 4096)
if bytes == b'':
raise EOFError
while has_more(fd):
data = os.read(fd, 4096)
if data == b'':
raise EOFError
bytes += data
return bytes
def has_more(fd):
r, w, e = select.select([fd], [], [], 0)
return (fd in r)
2017-08-21 11:45:03 -04:00
if __name__ == "__main__":
HandleTerminal()