File: pyte_driver.py

package info (click to toggle)
crazy-complete 0.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,404 kB
  • sloc: python: 7,949; sh: 4,636; makefile: 74
file content (51 lines) | stat: -rw-r--r-- 1,371 bytes parent folder | download | duplicates (3)
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
49
50
51
import os
import pyte
import pexpect

from terminal_base import TerminalBase

class PyteTerminal(TerminalBase):
    def __init__(self, width=80, height=100):
        self.width = width
        self.height = height
        self.screen = pyte.Screen(width, height)
        self.stream = pyte.Stream(self.screen)
    
    def start(self, command_args, env=None):
        spawn_env = None
        if env:
            spawn_env = os.environ.copy()
            spawn_env.update(env)

        command = command_args.pop(0)
        self.terminal = pexpect.spawn(command, command_args, dimensions=(self.height, self.width), env=spawn_env)

    def stop(self):
        self.terminal.close()

    def resize_window(self, x, y):
        pass # TODO
    
    def send(self, text):
        self.terminal.send(text)

    def send_line(self, line):
        self.terminal.sendline(line)
    
    def send_tab(self):
        self.terminal.send("\t")
    
    def send_ctrl_c(self):
        self.terminal.sendcontrol("c")
    
    def _read_output(self):
        try:
            while True:
                data = self.terminal.read_nonblocking(size=1024, timeout=0.1)
                self.stream.feed(data.decode('utf-8'))
        except pexpect.exceptions.TIMEOUT:
            pass
    
    def get_output(self):
        self._read_output()
        return "\n".join(self.screen.display)