-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunning_app.py
More file actions
239 lines (221 loc) · 9.33 KB
/
running_app.py
File metadata and controls
239 lines (221 loc) · 9.33 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import os
import sys
import subprocess
from functools import partial
from PySide6.QtCore import (
Qt, QDir, QFileInfo, QUrl, QRegularExpression, QCoreApplication, QRect, QSize, QProcess, Slot, QTimer, QRunnable, QThreadPool, QObject, Signal
)
from PySide6.QtGui import (
QFont, QSyntaxHighlighter, QTextCharFormat, QColor, QPalette, QPainter, QTextFormat, QTextCursor
)
from PySide6.QtWidgets import (
QApplication, QMainWindow, QSplitter, QTreeView, QTextEdit,
QVBoxLayout, QWidget, QFileDialog, QTabWidget, QPlainTextEdit,
QMessageBox, QFileSystemModel, QMenuBar, QHeaderView,
QHBoxLayout, QPushButton, QCompleter,
QDialog, QDialogButtonBox, QFontComboBox, QSpinBox, QFormLayout,
QMenu, QInputDialog, QLineEdit,
QStackedWidget, QLabel, QTabBar, QStyledItemDelegate, QStyle
)
try:
from PySide6.QtWebEngineWidgets import QWebEngineView
WEB_ENGINE_AVAILABLE = True
except ImportError:
WEB_ENGINE_AVAILABLE = False
QWebEngineView = None
# Force-disable WebEngine in frozen executables to avoid recursive subprocess spawning
if getattr(sys, "frozen", False):
WEB_ENGINE_AVAILABLE = False
QWebEngineView = None
class InteractiveTerminal(QWidget):
"""
An interactive terminal widget that runs a shell process.
Can be initialized with a startup command to configure the environment.
"""
outputReceived = Signal(str)
def __init__(self, startup_command=None, working_directory=None, parent=None):
super().__init__(parent)
self.process = QProcess(self)
self.output_view = QPlainTextEdit(self)
self.output_view.setFont(QFont("Consolas", 10))
self.output_view.setStyleSheet("""
QPlainTextEdit {
background-color: #121214;
color: #E0E2E6;
padding: 5px;
border: none;
}
QScrollBar:vertical {
background: #1E1F22;
width: 12px;
border: none;
border-radius: 0px;
margin: 0px;
}
QScrollBar::handle:vertical {
background: rgba(85, 85, 85, 0.6);
border-radius: 0px;
min-height: 20px;
margin: 2px;
}
QScrollBar::handle:vertical:hover {
background: rgba(102, 102, 102, 0.9);
}
QScrollBar::handle:vertical:pressed {
background: rgba(119, 119, 119, 1.0);
}
QScrollBar::add-line:vertical,
QScrollBar::sub-line:vertical {
height: 0px;
background: none;
}
QScrollBar::add-page:vertical,
QScrollBar::sub-page:vertical {
background: none;
}
QScrollBar:horizontal {
background: #1E1F22;
height: 12px;
border: none;
border-radius: 0px;
margin: 0px;
}
QScrollBar::handle:horizontal {
background: rgba(85, 85, 85, 0.6);
border-radius: 0px;
min-width: 20px;
margin: 2px;
}
QScrollBar::handle:horizontal:hover {
background: rgba(102, 102, 102, 0.9);
}
QScrollBar::handle:horizontal:pressed {
background: rgba(119, 119, 119, 1.0);
}
QScrollBar::add-line:horizontal,
QScrollBar::sub-line:horizontal {
width: 0px;
background: none;
}
QScrollBar::add-page:horizontal,
QScrollBar::sub-page:horizontal {
background: none;
}
""")
self.output_view.setReadOnly(False)
# Store base font to support Ctrl+0 reset for zoom
try:
self._base_font = QFont(self.output_view.font())
except Exception:
self._base_font = None
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.output_view)
self.process.readyReadStandardOutput.connect(self.handle_stdout)
self.process.readyReadStandardError.connect(self.handle_stderr)
self.output_view.keyPressEvent = self.terminal_key_press
# --- FIX: Set working directory before starting the process ---
if working_directory and os.path.isdir(working_directory):
self.process.setWorkingDirectory(working_directory)
shell_program = "powershell.exe" if sys.platform == "win32" else "bash"
self.process.start(shell_program)
# --- NEW: Run startup command if provided ---
if startup_command:
self.process.waitForStarted()
self.process.write((startup_command + "\n").encode())
def terminal_key_press(self, event):
""" Handles key presses to send commands to the shell. """
# VS Code-like zoom shortcuts for terminal: Ctrl + / Ctrl - / Ctrl 0
try:
if event.modifiers() & Qt.ControlModifier:
if event.key() in (Qt.Key_Plus, Qt.Key_Equal):
f = self.output_view.font()
f.setPointSizeF(f.pointSizeF() + 1)
self.output_view.setFont(f)
event.accept()
return
if event.key() == Qt.Key_Minus:
f = self.output_view.font()
f.setPointSizeF(max(1, f.pointSizeF() - 1))
self.output_view.setFont(f)
event.accept()
return
if event.key() == Qt.Key_0:
if getattr(self, '_base_font', None):
self.output_view.setFont(self._base_font)
event.accept()
return
except Exception:
pass
if event.key() in (Qt.Key_Return, Qt.Key_Enter):
# On Enter, get the full text and find the last line entered
full_text = self.output_view.toPlainText()
last_line = full_text.strip().split('\n')[-1]
# A simple way to find the actual command after the prompt (e.g., after '>')
prompt_marker = ">"
prompt_end_index = last_line.rfind(prompt_marker)
command = last_line[prompt_end_index + 1:].strip() if prompt_end_index != -1 else last_line
# Let the text edit handle the newline first
QPlainTextEdit.keyPressEvent(self.output_view, event)
# Then write the captured command to the process
if command:
self.process.write((command + "\n").encode())
else:
# For all other keys, just let the text edit do its normal job
QPlainTextEdit.keyPressEvent(self.output_view, event)
def handle_stdout(self):
data = self.process.readAllStandardOutput().data()
try:
import locale as _locale
enc = (getattr(sys.stdout, 'encoding', None) or _locale.getpreferredencoding(False) or 'utf-8')
except Exception:
enc = 'utf-8'
text = data.decode(enc, errors='replace')
# Emit full text for internal listeners (e.g., Manage activity parser)
try:
self.outputReceived.emit(text)
except Exception:
pass
# Filter [tracer] lines and blank-only lines from the visible console output
try:
filtered_lines = [ln for ln in text.splitlines(True) if (not ln.lstrip().startswith('[tracer]') and ln.strip() != '')]
filtered_text = ''.join(filtered_lines)
if filtered_text:
self.output_view.moveCursor(QTextCursor.End)
self.output_view.insertPlainText(filtered_text)
self.output_view.moveCursor(QTextCursor.End)
except Exception:
# Fallback to raw output if filtering fails
self.output_view.moveCursor(QTextCursor.End)
self.output_view.insertPlainText(text)
self.output_view.moveCursor(QTextCursor.End)
def handle_stderr(self):
data = self.process.readAllStandardError().data()
try:
import locale as _locale
enc = (getattr(sys.stderr, 'encoding', None) or _locale.getpreferredencoding(False) or 'utf-8')
except Exception:
enc = 'utf-8'
text = data.decode(enc, errors='replace')
# Emit full text for internal listeners
try:
self.outputReceived.emit(text)
except Exception:
pass
# Filter [tracer] lines and blank-only lines from visible console output
try:
filtered_lines = [ln for ln in text.splitlines(True) if (not ln.lstrip().startswith('[tracer]') and ln.strip() != '')]
filtered_text = ''.join(filtered_lines)
if filtered_text:
self.output_view.moveCursor(QTextCursor.End)
self.output_view.insertPlainText(filtered_text)
self.output_view.moveCursor(QTextCursor.End)
except Exception:
# Fallback
self.output_view.moveCursor(QTextCursor.End)
self.output_view.insertPlainText(text)
self.output_view.moveCursor(QTextCursor.End)
def kill_process(self):
""" Kills the terminal's shell process. """
self.process.kill()
self.process.waitForFinished()