QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
processoutputpanel.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
9#include "processinfo.h"
10#include <QHBoxLayout>
11#include <QScrollBar>
12#include <QTextCursor>
13#include <QTextDocument>
14#include <QTextEdit>
15#include <QToolButton>
16
18 : QDockWidget(tr("Process output"), parent) {
19 setObjectName(QStringLiteral("processOutputDock"));
20 setFeatures(QDockWidget::DockWidgetMovable |
21 QDockWidget::DockWidgetFloatable);
22 setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea);
23
24 auto *contents = new QWidget(this);
25 contents->setObjectName(QStringLiteral("processOutputWidget"));
26 auto *layout = new QHBoxLayout(contents);
27 layout->setObjectName(QStringLiteral("processOutputLayout"));
28 layout->setContentsMargins(0, 0, 0, 0);
29 m_clearButton = new QToolButton(contents);
30 m_clearButton->setObjectName(QStringLiteral("clearOutputButton"));
31 m_clearButton->setText(tr("Clear"));
32 m_clearButton->setToolTip(tr("Clear output"));
33 layout->addWidget(m_clearButton);
34 m_edit = new QTextEdit(contents);
35 m_edit->setObjectName(QStringLiteral("processOutputEdit"));
36 m_edit->setReadOnly(true);
37 m_edit->setAcceptRichText(false);
38 // Read-only: nothing to undo, and the undo stack kept every line the cap
39 // dropped alive.
40 m_edit->setUndoRedoEnabled(false);
41 layout->addWidget(m_edit);
42 setWidget(contents);
43
44 connect(m_clearButton, &QToolButton::clicked, this,
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 QScrollBar *bar = m_edit->verticalScrollBar();
53 connect(bar, &QScrollBar::valueChanged, this, [this, bar]() {
54 if (bar->isSliderDown())
55 return;
56 m_autoScroll = bar->value() >= bar->maximum();
57 });
58 connect(bar, &QScrollBar::sliderReleased, this,
59 [this, bar]() { m_autoScroll = bar->value() >= bar->maximum(); });
60}
61
62void ProcessOutputPanel::append(const QString &output, bool isError,
63 const QString &linePrefix) {
64 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 QStringList kept;
70 for (QString &line : lines) {
71 // Right-trim only: remove trailing CR and whitespace, preserve leading
72 // indentation
73 line.remove('\r');
74 while (!line.isEmpty() && line.back().isSpace()) {
75 line.chop(1);
76 }
77 if (!line.isEmpty()) {
78 kept.append(line);
79 }
80 }
81 const qsizetype skipped = qMax<qsizetype>(0, kept.size() - MaxLines);
82 m_counter += static_cast<int>(skipped);
83 const QColor textColor =
84 isError ? QColor(Qt::red) : m_edit->palette().color(QPalette::Text);
85 for (qsizetype i = skipped; i < kept.size(); ++i) {
86 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 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 m_edit->append(
96 QStringLiteral(
97 "<span style=\"color: %1; white-space: pre;\">%2: %3</span>")
98 .arg(textColor.name(), QString::number(m_counter),
99 prefixed.toHtmlEscaped()));
100 }
101
102 limitLines();
103
104 if (m_autoScroll) {
105 m_edit->verticalScrollBar()->setValue(
106 m_edit->verticalScrollBar()->maximum());
107 }
108}
109
111 QTextDocument *doc = m_edit->document();
112 const int excess = doc->blockCount() - MaxLines;
113 if (excess <= 0) {
114 return;
115 }
116 QTextCursor cursor(doc);
117 cursor.movePosition(QTextCursor::Start);
118 cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, excess);
119 cursor.removeSelectedText();
120}
121
123 m_edit->clear();
124 m_counter = 0;
125 m_autoScroll = true;
126}
127
129 return Enums::processLabel(pid);
130}
131
133 return Enums::processInfo(pid).secret;
134}
static constexpr int MaxLines
Oldest lines are dropped once the document holds more than this.
void limitLines()
Drop the oldest text blocks once the document holds more than MaxLines; called after every append().
static auto isSensitiveProcess(Enums::PROCESS pid) -> bool
Whether a process's output may carry secrets and must never reach this long-lived panel (pass show,...
void clear()
Drop all output, reset the counter, re-arm auto-scroll.
int m_counter
Running line number shown in front of each line; reset by clear(). Keeps counting past MaxLines.
ProcessOutputPanel(QWidget *parent=nullptr)
Build the console (Clear button + read-only text edit).
static auto processName(Enums::PROCESS pid) -> QString
Human-readable command label for a process id.
bool m_autoScroll
Whether append() scrolls to the bottom: cleared when the user scrolls up, set again when the scroll b...
QTextEdit * m_edit
Read-only text edit holding the output; child of the dock.
QToolButton * m_clearButton
The "Clear" button wired to clear().
void append(const QString &output, bool isError, const QString &linePrefix=QString())
Append output, one numbered line per non-empty input line.
auto processLabel(PROCESS pid) -> QString
The command label for pid, as the output panel shows it.
PROCESS
Identifies different subprocess operations used in QtPass.
Definition enums.h:43
auto processInfo(PROCESS pid) -> const ProcessInfo &
What is known about pid.
Enums::ProcessInfo, the one table that names each kind of subprocess and says whether its output may ...
ProcessOutputPanel: the dockable console that shows numbered, colour-coded git/pass/gpg output.