-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminaltabs.py
93 lines (77 loc) · 3.11 KB
/
terminaltabs.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import sys
import platform
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QTabWidget, QVBoxLayout, QPushButton, QScrollBar
from PyQt5.QtCore import Qt, QCoreApplication
from termqt.termqt import Terminal
from termqt.termqt.terminal_io_windows import TerminalWinptyIO
from termqt.termqt.terminal_io_posix import TerminalPOSIXExecIO
class TerminalTabWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QVBoxLayout(self)
self.tabs = QTabWidget()
self.tabs.setTabsClosable(True)
self.tabs.tabCloseRequested.connect(self.closeTab)
self.layout.addWidget(self.tabs)
self.addButton = QPushButton('New Terminal')
self.addButton.clicked.connect(self.addTerminalTab_button)
self.layout.addWidget(self.addButton)
def addTerminalTab_button(self):
if platform.system() == "Windows":
self.addTerminalTab('cmd', 'cmd')
else:
self.addTerminalTab('/bin/bash', 'bash')
def addTerminalTab(self, cmd, title=None):
terminal, scroll, terminal_io = self.createTerminal(cmd)
tab = QWidget()
layout = QHBoxLayout(tab)
layout.addWidget(terminal)
layout.addWidget(scroll)
layout.setSpacing(0)
tab.setLayout(layout)
index = self.tabs.addTab(tab, title or "Terminal")
self.tabs.setCurrentIndex(index)
# Start the terminal IO
terminal_io.spawn()
def closeTab(self, index):
if self.tabs.count() > index:
widget = self.tabs.widget(index)
terminal_io = widget.findChild(TerminalWinptyIO)
if terminal_io and terminal_io.is_alive():
terminal_io.terminate()
widget.deleteLater()
self.tabs.removeTab(index)
def createTerminal(self, cmd):
width = self.frameGeometry().width()
height = self.frameGeometry().height()
terminal = Terminal(
width, height, # min size
font_size=9)
terminal.set_font()
terminal.maximum_line_history = 2000
scroll = QScrollBar(Qt.Vertical, terminal)
terminal.connect_scroll_bar(scroll)
# Terminal IO setup
if platform.system() == "Windows":
terminal_io = TerminalWinptyIO(
terminal.row_len,
terminal.col_len,
cmd
)
else:
terminal_io = TerminalPOSIXExecIO(
terminal.row_len,
terminal.col_len,
cmd
)
terminal_io.stdout_callback = terminal.stdout
terminal.stdin_callback = terminal_io.write
terminal.resize_callback = terminal_io.resize
return terminal, scroll, terminal_io
if __name__ == "__main__":
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QApplication([])
main_window = TerminalTabWidget()
main_window.setWindowTitle("termqt on {}".format(platform.system()))
main_window.show()
sys.exit(app.exec())