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
|
import sys
import logging
import platform
from qtpy.QtWidgets import QApplication, QWidget, QHBoxLayout, QScrollBar
from qtpy.QtCore import Qt, QCoreApplication
from qtpy import QT_VERSION
from qtpy.QtGui import QFont
import termqt
from termqt import Terminal
if __name__ == "__main__":
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
formatter = logging.Formatter(
"[%(asctime)s] > "
"[%(filename)s:%(lineno)d] %(message)s"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
if QT_VERSION.startswith("5"):
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QApplication([])
window = QWidget()
window.setWindowTitle("termqt on {}".format(platform.system()))
layout = QHBoxLayout()
terminal = Terminal(800, 600, logger=logger)
terminal.set_font()
terminal.maximum_line_history = 2000
scroll = QScrollBar(Qt.Vertical, terminal)
terminal.connect_scroll_bar(scroll)
layout.addWidget(terminal)
layout.addWidget(scroll)
layout.setSpacing(0)
window.setLayout(layout)
window.show()
auto_wrap_enabled = True
platform = platform.system()
if platform in ["Linux", "Darwin"]:
bin = "/bin/bash"
from termqt import TerminalPOSIXExecIO
terminal_io = TerminalPOSIXExecIO(
terminal.row_len,
terminal.col_len,
bin,
logger=logger
)
elif platform == "Windows":
bin = "cmd"
from termqt import TerminalWinptyIO
terminal_io = TerminalWinptyIO(
terminal.row_len,
terminal.col_len,
bin,
logger=logger
)
# it turned out that cmd prefers to handle resize by itself
# see https://github.com/TerryGeng/termqt/issues/7
auto_wrap_enabled = False
else:
logger.error(f"Not supported platform: {platform}")
sys.exit(-1)
terminal.enable_auto_wrap(auto_wrap_enabled)
terminal_io.stdout_callback = terminal.stdout
terminal.stdin_callback = terminal_io.write
terminal.resize_callback = terminal_io.resize
terminal_io.spawn()
sys.exit(app.exec())
|