Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Implements ProcessOutputPanel; the sticky auto-scroll hysteresis is
6 : : * set up in the constructor, the line cap in limitLines().
7 : : */
8 : : #include "processoutputpanel.h"
9 : : #include "processinfo.h"
10 : : #include <QHBoxLayout>
11 : : #include <QScrollBar>
12 : : #include <QTextCursor>
13 : : #include <QTextDocument>
14 : : #include <QTextEdit>
15 : : #include <QToolButton>
16 : :
17 : 216 : ProcessOutputPanel::ProcessOutputPanel(QWidget *parent)
18 : 216 : : QDockWidget(tr("Process output"), parent) {
19 : 432 : setObjectName(QStringLiteral("processOutputDock"));
20 : 216 : setFeatures(QDockWidget::DockWidgetMovable |
21 : : QDockWidget::DockWidgetFloatable);
22 : 216 : setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea);
23 : :
24 : 216 : auto *contents = new QWidget(this);
25 : 432 : contents->setObjectName(QStringLiteral("processOutputWidget"));
26 : 216 : auto *layout = new QHBoxLayout(contents);
27 : 432 : layout->setObjectName(QStringLiteral("processOutputLayout"));
28 : 216 : layout->setContentsMargins(0, 0, 0, 0);
29 : 216 : m_clearButton = new QToolButton(contents);
30 : 432 : m_clearButton->setObjectName(QStringLiteral("clearOutputButton"));
31 : 432 : m_clearButton->setText(tr("Clear"));
32 : 216 : m_clearButton->setToolTip(tr("Clear output"));
33 : 216 : layout->addWidget(m_clearButton);
34 : 216 : m_edit = new QTextEdit(contents);
35 : 432 : m_edit->setObjectName(QStringLiteral("processOutputEdit"));
36 : 216 : m_edit->setReadOnly(true);
37 : 216 : m_edit->setAcceptRichText(false);
38 : : // Read-only: nothing to undo, and the undo stack kept every line the cap
39 : : // dropped alive.
40 : 216 : m_edit->setUndoRedoEnabled(false);
41 : 216 : layout->addWidget(m_edit);
42 : 216 : setWidget(contents);
43 : :
44 : 216 : connect(m_clearButton, &QToolButton::clicked, this,
45 : 216 : &ProcessOutputPanel::clear);
46 : :
47 : : // Hysteresis: while the user is actively dragging the slider, don't
48 : : // touch m_autoScroll on every tick — a brief overshoot at maximum
49 : : // would silently re-arm auto-scroll without an explicit release. Only
50 : : // commit on slider release. Wheel/keyboard scroll never sets
51 : : // isSliderDown(), so they still update immediately.
52 : 216 : QScrollBar *bar = m_edit->verticalScrollBar();
53 : 216 : connect(bar, &QScrollBar::valueChanged, this, [this, bar]() {
54 [ + + ]: 116 : if (bar->isSliderDown())
55 : : return;
56 : 113 : m_autoScroll = bar->value() >= bar->maximum();
57 : : });
58 : 216 : connect(bar, &QScrollBar::sliderReleased, this,
59 : 218 : [this, bar]() { m_autoScroll = bar->value() >= bar->maximum(); });
60 : 216 : }
61 : :
62 : 23 : void ProcessOutputPanel::append(const QString &output, bool isError,
63 : : const QString &linePrefix) {
64 : 23 : QStringList lines = output.split('\n', Qt::SkipEmptyParts);
65 : : // Trim first, render after: only the last MaxLines survive limitLines(),
66 : : // so the rest are counted but never laid out. A Git host or a store
67 : : // writer chooses how many lines git prints; laying out every one of them
68 : : // before the cap froze the window for seconds per hundred thousand.
69 : 23 : QStringList kept;
70 [ + + + ]: 22170 : for (QString &line : lines) {
71 : : // Right-trim only: remove trailing CR and whitespace, preserve leading
72 : : // indentation
73 : 22147 : line.remove('\r');
74 [ + + + + ]: 44305 : while (!line.isEmpty() && line.back().isSpace()) {
75 : 6 : line.chop(1);
76 : : }
77 [ + + ]: 22147 : if (!line.isEmpty()) {
78 : : kept.append(line);
79 : : }
80 : : }
81 : 23 : const qsizetype skipped = qMax<qsizetype>(0, kept.size() - MaxLines);
82 : 23 : m_counter += static_cast<int>(skipped);
83 : : const QColor textColor =
84 [ + + + ]: 39 : isError ? QColor(Qt::red) : m_edit->palette().color(QPalette::Text);
85 [ + + ]: 3164 : for (qsizetype i = skipped; i < kept.size(); ++i) {
86 : 3141 : m_counter++;
87 : : // Apply the optional prefix per line so multi-line output stays
88 : : // attributed to its command (e.g. all 3 lines of a `git push` show
89 : : // "git push: ..." rather than only the first).
90 : : const QString &line = kept.at(i);
91 : : const QString prefixed =
92 [ + + + + : 6292 : linePrefix.isEmpty() ? line : linePrefix + QStringLiteral(": ") + line;
+ + + + -
- ]
93 : : // white-space: pre keeps the indentation the trimming above preserved;
94 : : // rich text would otherwise collapse it.
95 : 6282 : m_edit->append(
96 : 3141 : QStringLiteral(
97 : : "<span style=\"color: %1; white-space: pre;\">%2: %3</span>")
98 [ + + + ]: 9423 : .arg(textColor.name(), QString::number(m_counter),
99 : 6282 : prefixed.toHtmlEscaped()));
100 : : }
101 : :
102 : 23 : limitLines();
103 : :
104 [ + + ]: 23 : if (m_autoScroll) {
105 : 44 : m_edit->verticalScrollBar()->setValue(
106 : 22 : m_edit->verticalScrollBar()->maximum());
107 : : }
108 : 23 : }
109 : :
110 : 23 : void ProcessOutputPanel::limitLines() {
111 : 23 : QTextDocument *doc = m_edit->document();
112 : 23 : const int excess = doc->blockCount() - MaxLines;
113 [ + + ]: 23 : if (excess <= 0) {
114 : 22 : return;
115 : : }
116 : 1 : QTextCursor cursor(doc);
117 : 1 : cursor.movePosition(QTextCursor::Start);
118 : 1 : cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, excess);
119 : 1 : cursor.removeSelectedText();
120 : 1 : }
121 : :
122 : 1 : void ProcessOutputPanel::clear() {
123 : 1 : m_edit->clear();
124 : 1 : m_counter = 0;
125 : 1 : m_autoScroll = true;
126 : 1 : }
127 : :
128 : 31 : auto ProcessOutputPanel::processName(Enums::PROCESS pid) -> QString {
129 : 31 : return Enums::processLabel(pid);
130 : : }
131 : :
132 : 24 : auto ProcessOutputPanel::isSensitiveProcess(Enums::PROCESS pid) -> bool {
133 : 24 : return Enums::processInfo(pid).secret;
134 : : }
|