10#include <QStandardItemModel>
11#include <QStandardPaths>
14#include <qtermwidget6/qtermwidget.h>
17QStringList TerminalPanel::cachedColorSchemes_;
18bool TerminalPanel::colorSchemesCached_ =
false;
48 currentCommand_ = command;
52 terminal_->startShellProgram();
55 QTimer::singleShot(100,
this,
60 terminal_->sendText(command +
"\n");
67 QString path = QStandardPaths::findExecutable(
"btop");
68 return !path.isEmpty();
73 QString path = QStandardPaths::findExecutable(
"htop");
74 return !path.isEmpty();
95 if (isPaused_ || !terminal_)
102 int pid = terminal_->getShellPID();
106 ::kill(-pid, SIGSTOP);
113 if (!isPaused_ || !terminal_)
119 int pid = terminal_->getShellPID();
122 ::kill(-pid, SIGCONT);
131void TerminalPanel::onMonitorChanged(
int index)
149 command = QString::fromLocal8Bit(qgetenv(
"SHELL"));
150 if (command.isEmpty())
151 command = QStringLiteral(
"/bin/bash");
161 terminal_->sendText(
"exit\n");
162 QTimer::singleShot(200,
this, [
this, command]() {
startCommand(command); });
166void TerminalPanel::onRestartClicked()
168 if (terminal_ && !currentCommand_.isEmpty())
171 terminal_->sendText(
"\x03");
172 QTimer::singleShot(100,
this,
177 terminal_->sendText(currentCommand_ +
"\n");
183void TerminalPanel::onTerminalFinished()
192void TerminalPanel::setupUI()
194 layout_ =
new QVBoxLayout(
this);
195 layout_->setContentsMargins(0, 0, 0, 0);
196 layout_->setSpacing(0);
199 terminal_ =
new QTermWidget(0,
this);
200 terminal_->setScrollBarPosition(QTermWidget::ScrollBarRight);
201 terminal_->setTerminalSizeHint(
true);
202 terminal_->setFocusPolicy(Qt::ClickFocus);
206 terminal_->setHistorySize(500);
209 QFont font(
"Monospace", 10);
210 font.setStyleHint(QFont::Monospace);
211 font.setFixedPitch(
true);
212 font.setKerning(
false);
213 font.setHintingPreference(QFont::PreferNoHinting);
214 terminal_->setTerminalFont(font);
220 connect(terminal_, &QTermWidget::finished,
this, &TerminalPanel::onTerminalFinished);
222 layout_->addWidget(terminal_);
227void TerminalPanel::setupToolBar()
229 toolBar_ =
new QToolBar(
this);
230 toolBar_->setIconSize(QSize(16, 16));
231 toolBar_->setMovable(
false);
234 monitorCombo_ =
new QComboBox(
this);
235 monitorCombo_->addItem(
"btop++",
"btop");
236 monitorCombo_->addItem(
"htop",
"htop");
237 monitorCombo_->addItem(
"top",
"top");
241 QString userShell = QString::fromLocal8Bit(qgetenv(
"SHELL"));
242 if (userShell.isEmpty())
243 userShell = QStringLiteral(
"/bin/bash");
244 monitorCombo_->addItem(
"Shell", userShell);
249 auto* model = qobject_cast<QStandardItemModel*>(monitorCombo_->model());
252 QStandardItem* item = model->item(0);
255 item->setEnabled(
false);
256 item->setText(
"btop++ (not installed)");
265 auto* model = qobject_cast<QStandardItemModel*>(monitorCombo_->model());
268 QStandardItem* item = model->item(1);
271 item->setEnabled(
false);
272 item->setText(
"htop (not installed)");
277 connect(monitorCombo_, QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
278 &TerminalPanel::onMonitorChanged);
280 toolBar_->addWidget(monitorCombo_);
281 toolBar_->addSeparator();
284 restartAction_ =
new QAction(
"Restart",
this);
285 restartAction_->setToolTip(
"Restart the current monitor");
286 connect(restartAction_, &QAction::triggered,
this, &TerminalPanel::onRestartClicked);
287 toolBar_->addAction(restartAction_);
290void TerminalPanel::applyDarkTheme()
298 if (!colorSchemesCached_)
300 cachedColorSchemes_ = terminal_->availableColorSchemes();
301 colorSchemesCached_ =
true;
305 static const QStringList preferredSchemes = {
"Breeze",
"Linux",
"GreenOnBlack",
"WhiteOnBlack",
308 for (
const QString& preferred : preferredSchemes)
310 if (cachedColorSchemes_.contains(preferred))
312 terminal_->setColorScheme(preferred);
318 if (!cachedColorSchemes_.isEmpty())
320 terminal_->setColorScheme(cachedColorSchemes_.first());
void processFinished(int exitCode)
Emitted when the terminal process finishes.
void resumeProcess()
Resume the terminal process Call when panel becomes visible again.
void startBtop()
Start or restart the terminal with btop++.
void pauseProcess()
Pause the terminal process (suspends btop/htop to save CPU) Call when panel is hidden or window is mi...
static bool isBtopAvailable()
Check if btop++ is available on the system.
void startCommand(const QString &command)
Start the terminal with a custom command.
TerminalPanel(QWidget *parent=nullptr)
~TerminalPanel() override
static bool isHtopAvailable()
Check if htop is available as a fallback.
static QString getBestMonitorCommand()
Get the best available system monitor command.