-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminalwidget.cpp
More file actions
165 lines (135 loc) · 4.81 KB
/
terminalwidget.cpp
File metadata and controls
165 lines (135 loc) · 4.81 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
#include "terminalwidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QProcess>
#include <QScrollBar>
#include <QTextCursor>
TerminalWidget::TerminalWidget(QWidget *parent)
: QWidget(parent)
{
process = new QProcess(this);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
// Top Bar
QHBoxLayout *topLayout = new QHBoxLayout();
QLabel *titleLabel = new QLabel("▶ Console Output", this);
stopButton = new QPushButton("[■ Stop]", this);
stopButton->setEnabled(false);
topLayout->addWidget(titleLabel);
topLayout->addStretch();
topLayout->addWidget(stopButton);
mainLayout->addLayout(topLayout);
// Console text edit
consoleTextEdit = new QTextEdit(this);
consoleTextEdit->setReadOnly(true);
consoleTextEdit->setStyleSheet("background-color: #1e1e1e; color: white; font-family: Consolas; font-size: 11pt;");
mainLayout->addWidget(consoleTextEdit);
// Input area
QHBoxLayout *inputLayout = new QHBoxLayout();
QLabel *promptLabel = new QLabel("› ", this);
promptLabel->setStyleSheet("color: #4CAF50; font-weight: bold; font-family: Consolas; font-size: 11pt;");
inputLine = new QLineEdit(this);
inputLine->setStyleSheet("background-color: #2b2b2b; color: #4CAF50; border: 1px solid #4CAF50; font-family: Consolas; font-size: 11pt;");
inputLine->setEnabled(false);
inputLayout->addWidget(promptLabel);
inputLayout->addWidget(inputLine);
mainLayout->addLayout(inputLayout);
// Connections
connect(process, &QProcess::readyReadStandardOutput, this, &TerminalWidget::onReadyReadStandardOutput);
connect(process, &QProcess::readyReadStandardError, this, &TerminalWidget::onReadyReadStandardError);
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &TerminalWidget::onProcessFinished);
connect(inputLine, &QLineEdit::returnPressed, this, &TerminalWidget::onInputReturned);
connect(stopButton, &QPushButton::clicked, this, &TerminalWidget::onStopClicked);
}
TerminalWidget::~TerminalWidget()
{
if (process->state() != QProcess::NotRunning) {
process->kill();
process->waitForFinished();
}
}
void TerminalWidget::setWorkingDirectory(const QString &dir)
{
process->setWorkingDirectory(dir);
}
void TerminalWidget::setProcessEnvironment(const QProcessEnvironment &env)
{
process->setProcessEnvironment(env);
}
void TerminalWidget::stopProcess()
{
if (process->state() != QProcess::NotRunning) {
process->kill();
process->waitForFinished();
}
}
void TerminalWidget::startProcess(const QString &program, const QStringList &arguments)
{
inputLine->setEnabled(true);
inputLine->clear();
inputLine->setFocus();
stopButton->setEnabled(true);
process->start(program, arguments);
}
void TerminalWidget::appendHtml(const QString &html)
{
consoleTextEdit->moveCursor(QTextCursor::End);
consoleTextEdit->insertHtml(html);
consoleTextEdit->moveCursor(QTextCursor::End);
consoleTextEdit->verticalScrollBar()->setValue(consoleTextEdit->verticalScrollBar()->maximum());
}
void TerminalWidget::appendOutput(const QString &text, const QString &color)
{
// Append html
QString html = QString("<span style='color:%1;'>%2</span>").arg(color, text.toHtmlEscaped().replace("\n", "<br>"));
appendHtml(html);
}
void TerminalWidget::clear()
{
consoleTextEdit->clear();
}
void TerminalWidget::onReadyReadStandardOutput()
{
QByteArray data = process->readAllStandardOutput();
appendOutput(QString::fromLocal8Bit(data), "white");
}
void TerminalWidget::onReadyReadStandardError()
{
QByteArray data = process->readAllStandardError();
appendOutput(QString::fromLocal8Bit(data), "#FFA500"); // Orange
}
void TerminalWidget::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
inputLine->setEnabled(false);
stopButton->setEnabled(false);
if (exitStatus == QProcess::CrashExit) {
appendOutput("\n\n[Process Crashed]\n", "red");
} else {
QString color = (exitCode == 0) ? "#4CAF50" : "red"; // Green if 0, else Red
appendOutput(QString("\n\n[Program exited with code %1]\n").arg(exitCode), color);
}
emit processFinished(exitCode);
}
void TerminalWidget::onInputReturned()
{
QString text = inputLine->text();
inputLine->clear();
if (process->state() == QProcess::Running) {
// Echo the input
appendOutput(text + "\n", "#4CAF50");
QByteArray data = text.toLocal8Bit();
data.append('\n');
process->write(data);
}
}
void TerminalWidget::onStopClicked()
{
if (process->state() != QProcess::NotRunning) {
process->kill();
}
}