diff --git a/gui/previewgeneratorplaintext.cpp b/gui/previewgeneratorplaintext.cpp index e05c3ad..c63f8ea 100644 --- a/gui/previewgeneratorplaintext.cpp +++ b/gui/previewgeneratorplaintext.cpp @@ -2,6 +2,7 @@ #include "previewgeneratorplaintext.h" #include "previewresultplaintext.h" +#include "../shared/limitqueue.h" QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderConfig config, QString fileName) { @@ -73,6 +74,82 @@ QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderCo return header + resulText.replace("\n", "
").mid(0, 1000); } +QString PreviewGeneratorPlainText::generateLineBasedPreviewText(QTextStream &in, RenderConfig config, QString fileName) +{ + QString resultText; + const unsigned int contextLinesCount = 2; + LimitQueue 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("%1%2
").arg(lineNumber).arg(line)); }; + + QHash countmap; + QString header = "" + fileName + " "; + + unsigned int snippetsCount = 0; + unsigned int lineCount = 0; + while(in.readLineInto(¤tLine) && snippetsCount < MAX_SNIPPETS) + { + ++lineCount; + bool matched = false; + if(justReadLinesCount > 0) + { + appendLine(lineCount, currentLine); + --justReadLinesCount; + continue; + } + if(justReadLinesCount == 0) + { + resultText += "---
"; + 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, "" + word + "", + 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 += "
"; + + return header + resultText; +} + QSharedPointer PreviewGeneratorPlainText::generate(RenderConfig config, QString documentPath, unsigned int page) { @@ -83,9 +160,7 @@ QSharedPointer PreviewGeneratorPlainText::generate(RenderConfig c return QSharedPointer(result); } QTextStream in(&file); - - QString content = in.readAll(); QFileInfo info{documentPath}; - result->setText(generatePreviewText(content, config, info.fileName())); + result->setText(generateLineBasedPreviewText(in, config, info.fileName())); return QSharedPointer(result); } diff --git a/gui/previewgeneratorplaintext.h b/gui/previewgeneratorplaintext.h index 648ebfb..de6bb25 100644 --- a/gui/previewgeneratorplaintext.h +++ b/gui/previewgeneratorplaintext.h @@ -1,5 +1,6 @@ #ifndef PREVIEWGENERATORPLAINTEXT_H #define PREVIEWGENERATORPLAINTEXT_H +#include #include "previewgenerator.h" class PreviewGeneratorPlainText : public PreviewGenerator @@ -10,6 +11,7 @@ class PreviewGeneratorPlainText : public PreviewGenerator public: using PreviewGenerator::PreviewGenerator; QString generatePreviewText(QString content, RenderConfig config, QString fileName); + QString generateLineBasedPreviewText(QTextStream &in, RenderConfig config, QString fileName); QSharedPointer generate(RenderConfig config, QString documentPath, unsigned int page); };