MauiKit Terminal

HistorySearch.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Christian Surlykke
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU General Public License for more details.
10
11 You should have received a copy of the GNU General Public License
12 along with this program; if not, write to the Free Software
13 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14 02110-1301 USA.
15*/
16#include <QApplication>
17#include <QDebug>
18#include <QRegExp>
19#include <QTextStream>
20
21#include "Emulation.h"
22#include "HistorySearch.h"
23#include "TerminalCharacterDecoder.h"
24
25HistorySearch::HistorySearch(EmulationPtr emulation, const QRegExp &regExp, bool forwards, int startColumn, int startLine, QObject *parent)
26 : QObject(parent)
27 , m_emulation(emulation)
28 , m_regExp(regExp)
29 , m_forwards(forwards)
30 , m_startColumn(startColumn)
31 , m_startLine(startLine)
32{
33}
34
35HistorySearch::~HistorySearch()
36{
37}
38
39void HistorySearch::search()
40{
41 bool found = false;
42
43 if (!m_regExp.isEmpty()) {
44 if (m_forwards) {
45 found = search(m_startColumn, m_startLine, -1, m_emulation->lineCount()) || search(0, 0, m_startColumn, m_startLine);
46 } else {
47 found = search(0, 0, m_startColumn, m_startLine) || search(m_startColumn, m_startLine, -1, m_emulation->lineCount());
48 }
49
50 if (found) {
51 Q_EMIT matchFound(m_foundStartColumn, m_foundStartLine, m_foundEndColumn, m_foundEndLine);
52 } else {
53 Q_EMIT noMatchFound();
54 }
55 }
56
58}
59
60bool HistorySearch::search(int startColumn, int startLine, int endColumn, int endLine)
61{
62 qDebug() << "search from" << startColumn << "," << startLine << "to" << endColumn << "," << endLine;
63
64 int linesRead = 0;
65 int linesToRead = endLine - startLine + 1;
66
67 qDebug() << "linesToRead:" << linesToRead;
68
69 // We read process history from (and including) startLine to (and including) endLine in
70 // blocks of at most 10K lines so that we do not use unhealthy amounts of memory
71 int blockSize;
72 while ((blockSize = qMin(10000, linesToRead - linesRead)) > 0) {
73 QString string;
74 QTextStream searchStream(&string);
75 PlainTextDecoder decoder;
76 decoder.begin(&searchStream);
77 decoder.setRecordLinePositions(true);
78
79 // Calculate lines to read and read them
80 int blockStartLine = m_forwards ? startLine + linesRead : endLine - linesRead - blockSize + 1;
81 int chunkEndLine = blockStartLine + blockSize - 1;
82 m_emulation->writeToStream(&decoder, blockStartLine, chunkEndLine);
83
84 // We search between startColumn in the first line of the string and endColumn in the last
85 // line of the string. First we calculate the position (in the string) of endColumn in the
86 // last line of the string
87 int endPosition;
88
89 // The String that Emulator.writeToStream produces has a newline at the end, and so ends with an
90 // empty line - we ignore that.
91 int numberOfLinesInString = decoder.linePositions().size() - 1;
92 if (numberOfLinesInString > 0 && endColumn > -1) {
93 endPosition = decoder.linePositions().at(numberOfLinesInString - 1) + endColumn;
94 } else {
95 endPosition = string.size();
96 }
97
98 // So now we can log for m_regExp in the string between startColumn and endPosition
99 int matchStart;
100 if (m_forwards) {
101 matchStart = m_regExp.indexIn(string, startColumn);
102 if (matchStart >= endPosition)
103 matchStart = -1;
104 } else {
105 matchStart = m_regExp.lastIndexIn(string, endPosition - 1);
106 if (matchStart < startColumn)
107 matchStart = -1;
108 }
109
110 if (matchStart > -1) {
111 int matchEnd = matchStart + m_regExp.matchedLength() - 1;
112 qDebug() << "Found in string from" << matchStart << "to" << matchEnd;
113
114 // Translate startPos and endPos to startColum, startLine, endColumn and endLine in history.
115 int startLineNumberInString = findLineNumberInString(decoder.linePositions(), matchStart);
116 m_foundStartColumn = matchStart - decoder.linePositions().at(startLineNumberInString);
117 m_foundStartLine = startLineNumberInString + startLine + linesRead;
118
119 int endLineNumberInString = findLineNumberInString(decoder.linePositions(), matchEnd);
120 m_foundEndColumn = matchEnd - decoder.linePositions().at(endLineNumberInString);
121 m_foundEndLine = endLineNumberInString + startLine + linesRead;
122
123 qDebug() << "m_foundStartColumn" << m_foundStartColumn << "m_foundStartLine" << m_foundEndLine << "m_foundEndColumn" << m_foundEndColumn
124 << "m_foundEndLine" << m_foundEndLine;
125
126 return true;
127 }
128
129 linesRead += blockSize;
130 }
131
132 qDebug() << "Not found";
133 return false;
134}
135
136int HistorySearch::findLineNumberInString(QList<int> linePositions, int position)
137{
138 int lineNum = 0;
139 while (lineNum + 1 < linePositions.size() && linePositions[lineNum + 1] <= position)
140 lineNum++;
141
142 return lineNum;
143}
A terminal character decoder which produces plain text, ignoring colours and other appearance-related...
void setRecordLinePositions(bool record)
Enables recording of character positions at which new lines are added.
void begin(QTextStream *output) override
Begin decoding characters.
QList< int > linePositions() const
Returns of character positions in the output stream at which new lines where added.
const_reference at(qsizetype i) const const
qsizetype size() const const
Q_EMITQ_EMIT
void deleteLater()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:10:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.