Ejemplo n.º 1
0
 def start(self):
     from console.console_client import ConsoleClient
     self._cc = ConsoleClient("localhost", 8828)
Ejemplo n.º 2
0
class Win32Process(Process):
    KEYMAP = keymap.WIN32
    SIZE_REFRESH_EACH = 50 # reads

    def __init__(self, *args, **kwds):
        self._lines = {}
        self._reads = 1
        self._width = 0
        self._height = 0
        self._last_cursor_pos = None
        self._cc = None
        super(Win32Process, self).__init__(*args, **kwds)

    def start(self):
        from console.console_client import ConsoleClient
        self._cc = ConsoleClient("localhost", 8828)

    def stop(self):
        pass

    def is_running(self):
        return self._cc.is_running

    def send_bytes(self, bytes):
        self._cc.write_console_input(bytes)

    def send_keypress(self, key, ctrl=False, alt=False, shift=False, super=False):
        if (ctrl and not (alt or shift or super) and key=='c'):
            self._cc.send_ctrl_c()
        else:
            self._cc.send_keypress(key, ctrl=ctrl, alt=alt, shift=shift, super=super)
        self.read()

    def send_click(self, row, col, **kwds):
        self._cc.send_click(row, col, **kwds)
        self.read()

    def read(self):
        if not self._cc:
            return # dead or not started yet
        full = False
        with_colors = True
        self._reads = (self._reads + 1) % self.SIZE_REFRESH_EACH
        if not self._reads:
            self._size_refresh()
            full = True

        (_lines, _cursor_pos, _colors) = self._cc.read(full, with_colors)
        cursor_pos = Coord(*_cursor_pos) # we need .x .y access
        if not full and self._last_cursor_pos == cursor_pos:
            cursor_pos = None
        else:
            self._last_cursor_pos = cursor_pos

        translated_colors = self._translate_colors(_colors)
            
        lines = {}
        for k,v in _lines.items():
            lines[int(k)] = v
        for v in self._views:
            if full:
                v.full_refresh(lines, cursor_pos, translated_colors)
            else:
                v.diff_refresh(lines, cursor_pos, translated_colors)

    def _translate_colors(self, colors):
        translated_colors = {}
        for line_str, line in colors.items():
            start = int(line_str) * (len(line) + 1)
            for idx, colornum in enumerate(line):
                scope = fg_color(colornum) + "." + bg_color(colornum)
                reg = (start + idx, start + idx + 1)
                key = line_str + "." + str(idx)
                translated_colors[key] = ColorSpec(scope, [reg], key)    
                
        return translated_colors

    def _size_refresh(self):
        height = self.available_lines()
        width = self.available_columns()
        if self._width == width and self._height == height:
            return 
        self._width = width
        self._height = height
        self._cc.set_window_size(width, height)