File: terminal.py

package info (click to toggle)
turing 0.11-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,560 kB
  • sloc: python: 106,582; xml: 101; makefile: 53; sh: 29
file content (66 lines) | stat: -rw-r--r-- 2,329 bytes parent folder | download | duplicates (6)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import sys

from pyqode.core.backend import server
from pyqode.core import modes

from . import output_window


class Terminal(output_window.OutputWindow):
    """
    Simple (rudimentary) terminal widget.

    It will run cmd.exe on Windows and bash on GNU/Linux.

    Please note that this widget does not support all VT100 features, only the most basic one. The goal is to have
    a small widget for running commands in a PyQt application, it does not aim to be a perfect emulator,
    just a quick one.
    """
    def __init__(self, parent=None, color_scheme=None, backend=server.__file__):
        if sys.platform == 'win32':
            input_handler = output_window.BufferedInputHandler()
            program = 'cmd.exe'
            args = []
            use_pty = False
            flg_bash = False
        else:
            program = 'bash'
            args = ['-l']
            input_handler = output_window.ImmediateInputHandler()
            use_pty = True
            flg_bash = True
        working_dir = os.path.expanduser('~')
        super(Terminal, self).__init__(parent=parent, color_scheme=color_scheme, input_handler=input_handler,
                                       backend=backend)
        self._formatter.flg_bash = flg_bash
        self.start_process(program, arguments=args, print_command=False, use_pseudo_terminal=use_pty,
                           working_dir=working_dir)

    def _init_code_edit(self, backend):
        self.modes.append(modes.SymbolMatcherMode())
        self.modes.append(modes.IndenterMode())
        super(Terminal, self)._init_code_edit(backend)
        try:
            self.panels.remove('ReadOnlyPanel')
        except KeyError:
            pass

    def change_directory(self, directory):
        """
        Changes the current directory.

        Change is made by running a "cd" command followed by a "clear" command.
        :param directory:
        :return:
        """
        self._process.write(('cd %s\n' % directory).encode())
        if sys.platform == 'win32':
            self._process.write((os.path.splitdrive(directory)[0] + '\r\n').encode())
            self.clear()
        else:
            self._process.write(b'\x0C')

    def terminate_process(self):
        self._process.write(b'\x04')
        self._process.waitForBytesWritten()