1
0

gui: PreviewGeneratorPlainText: Show line numbers

Generate previews that show the line number and surrounding
lines (like grep -C) for context.
Este cometimento está contido em:
Albert S. 2022-08-06 10:01:24 +02:00
ascendente 89bf65d9bb
cometimento 9a70a821bd
2 ficheiros modificados com 80 adições e 3 eliminações

Ver ficheiro

@ -2,6 +2,7 @@
#include "previewgeneratorplaintext.h" #include "previewgeneratorplaintext.h"
#include "previewresultplaintext.h" #include "previewresultplaintext.h"
#include "../shared/limitqueue.h"
QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderConfig config, QString fileName) QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderConfig config, QString fileName)
{ {
@ -73,6 +74,82 @@ QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderCo
return header + resulText.replace("\n", "<br>").mid(0, 1000); return header + resulText.replace("\n", "<br>").mid(0, 1000);
} }
QString PreviewGeneratorPlainText::generateLineBasedPreviewText(QTextStream &in, RenderConfig config, QString fileName)
{
QString resultText;
const unsigned int contextLinesCount = 2;
LimitQueue<QString> queue(contextLinesCount);
QString currentLine;
currentLine.reserve(512);
/* How many lines to read after a line with a match (like grep -A ) */
int justReadLinesCount = -1;
auto appendLine = [&resultText](int lineNumber, QString &line)
{ resultText.append(QString("<b>%1</b>%2<br>").arg(lineNumber).arg(line)); };
QHash<QString, int> countmap;
QString header = "<b>" + fileName + "</b> ";
unsigned int snippetsCount = 0;
unsigned int lineCount = 0;
while(in.readLineInto(&currentLine) && snippetsCount < MAX_SNIPPETS)
{
++lineCount;
bool matched = false;
if(justReadLinesCount > 0)
{
appendLine(lineCount, currentLine);
--justReadLinesCount;
continue;
}
if(justReadLinesCount == 0)
{
resultText += "---<br>";
justReadLinesCount = -1;
++snippetsCount;
}
for(QString &word : config.wordsToHighlight)
{
if(currentLine.contains(word, Qt::CaseInsensitive))
{
countmap[word] = countmap.value(word, 0) + 1;
matched = true;
currentLine.replace(word, "<span style=\"background-color: yellow;\">" + word + "</span>",
Qt::CaseInsensitive);
}
}
if(matched)
{
while(queue.size() > 0)
{
int queuedLineCount = lineCount - queue.size();
QString queuedLine = queue.dequeue();
appendLine(queuedLineCount, queuedLine);
}
appendLine(lineCount, currentLine);
justReadLinesCount = contextLinesCount;
}
else
{
queue.enqueue(currentLine);
}
}
for(QString &word : config.wordsToHighlight)
{
header += word + ": " + QString::number(countmap[word]) + " ";
}
if(snippetsCount == MAX_SNIPPETS)
{
header += "(truncated)";
}
header += "<hr>";
return header + resultText;
}
QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig config, QString documentPath, QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig config, QString documentPath,
unsigned int page) unsigned int page)
{ {
@ -83,9 +160,7 @@ QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig c
return QSharedPointer<PreviewResultPlainText>(result); return QSharedPointer<PreviewResultPlainText>(result);
} }
QTextStream in(&file); QTextStream in(&file);
QString content = in.readAll();
QFileInfo info{documentPath}; QFileInfo info{documentPath};
result->setText(generatePreviewText(content, config, info.fileName())); result->setText(generateLineBasedPreviewText(in, config, info.fileName()));
return QSharedPointer<PreviewResultPlainText>(result); return QSharedPointer<PreviewResultPlainText>(result);
} }

Ver ficheiro

@ -1,5 +1,6 @@
#ifndef PREVIEWGENERATORPLAINTEXT_H #ifndef PREVIEWGENERATORPLAINTEXT_H
#define PREVIEWGENERATORPLAINTEXT_H #define PREVIEWGENERATORPLAINTEXT_H
#include <QTextStream>
#include "previewgenerator.h" #include "previewgenerator.h"
class PreviewGeneratorPlainText : public PreviewGenerator class PreviewGeneratorPlainText : public PreviewGenerator
@ -10,6 +11,7 @@ class PreviewGeneratorPlainText : public PreviewGenerator
public: public:
using PreviewGenerator::PreviewGenerator; using PreviewGenerator::PreviewGenerator;
QString generatePreviewText(QString content, RenderConfig config, QString fileName); QString generatePreviewText(QString content, RenderConfig config, QString fileName);
QString generateLineBasedPreviewText(QTextStream &in, RenderConfig config, QString fileName);
QSharedPointer<PreviewResult> generate(RenderConfig config, QString documentPath, unsigned int page); QSharedPointer<PreviewResult> generate(RenderConfig config, QString documentPath, unsigned int page);
}; };