| 1 |
import os |
| 2 |
import pyte |
| 3 |
import pexpect |
| 4 |
|
| 5 |
from terminal_base import TerminalBase |
| 6 |
|
| 7 |
class PyteTerminal(TerminalBase): |
| 8 |
def __init__(self, width=80, height=100): |
| 9 |
self.width = width |
| 10 |
self.height = height |
| 11 |
self.screen = pyte.Screen(width, height) |
| 12 |
self.stream = pyte.Stream(self.screen) |
| 13 |
|
| 14 |
def start(self, command_args, env=None): |
| 15 |
spawn_env = None |
| 16 |
if env: |
| 17 |
spawn_env = os.environ.copy() |
| 18 |
spawn_env.update(env) |
| 19 |
|
| 20 |
command = command_args.pop(0) |
| 21 |
self.terminal = pexpect.spawn(command, command_args, dimensions=(self.height, self.width), env=spawn_env) |
| 22 |
|
| 23 |
def stop(self): |
| 24 |
self.terminal.close() |
| 25 |
|
| 26 |
def resize_window(self, x, y): |
| 27 |
pass # TODO |
| 28 |
|
| 29 |
def send(self, text): |
| 30 |
self.terminal.send(text) |
| 31 |
|
| 32 |
def send_line(self, line): |
| 33 |
self.terminal.sendline(line) |
| 34 |
|
| 35 |
def send_tab(self): |
| 36 |
self.terminal.send("\t") |
| 37 |
|
| 38 |
def send_ctrl(self, key): |
| 39 |
self.terminal.sendcontrol(key) |
| 40 |
|
| 41 |
def _read_output(self): |
| 42 |
try: |
| 43 |
while True: |
| 44 |
data = self.terminal.read_nonblocking(size=1024, timeout=0.1) |
| 45 |
self.stream.feed(data.decode('utf-8')) |
| 46 |
except pexpect.exceptions.TIMEOUT: |
| 47 |
pass |
| 48 |
|
| 49 |
def get_output(self): |
| 50 |
self._read_output() |
| 51 |
return "\n".join(self.screen.display) |