diff options
| author | 2020-10-21 11:34:58 +0200 | |
|---|---|---|
| committer | 2020-10-21 11:34:58 +0200 | |
| commit | 671bf7bdf20ccc61339d0959b1fb2f81a316825b (patch) | |
| tree | 595be6924b831977a8680bb3be9c5bccdf2bc4a6 | |
| parent | d1a947bed91963e788287e186f831202b8355eea (diff) | |
| download | OneRoll-671bf7bdf20ccc61339d0959b1fb2f81a316825b.tar.gz OneRoll-671bf7bdf20ccc61339d0959b1fb2f81a316825b.zip | |
fix output
| -rw-r--r-- | cli/main.cpp | 35 | ||||
| -rw-r--r-- | diceparser.cpp | 9 | ||||
| -rw-r--r-- | include/diceparser.h | 5 | ||||
| -rw-r--r-- | include/parsingtoolbox.h | 8 | ||||
| -rw-r--r-- | node/repeaternode.cpp | 3 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 54 | ||||
| -rw-r--r-- | tests/dice/tst_dice.cpp | 3 |
7 files changed, 76 insertions, 41 deletions
diff --git a/cli/main.cpp b/cli/main.cpp index e28e0e6..981c1a0 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -328,12 +328,19 @@ int startDiceParsing(QStringList& cmds, bool withColor, EXPORTFORMAT format, QJs allSameColor= true; QString colorP; json= parser.resultAsJSon( - [&colorP, &allSameColor](const QString& value, const QString& color, bool highlight) { + [&colorP, &allSameColor](const QString& value, const QString& color, bool) { if(colorP.isNull()) colorP= color; else if(colorP != color) allSameColor= false; + return value; + }, + true); + + if(!allSameColor) + { + json= parser.resultAsJSon([](const QString& value, const QString& color, bool highlight) { QString result= value; bool hasColor= !color.isEmpty(); QString style; @@ -352,21 +359,27 @@ int startDiceParsing(QStringList& cmds, bool withColor, EXPORTFORMAT format, QJs result= QString("<tspan %2>%1</tspan>").arg(value).arg(style); return result; }); + } } else if(TERMINAL == format) { allSameColor= true; QString colorP; - json= parser.resultAsJSon([&colorP, &allSameColor](const QString& result, const QString& color, bool) { - if(colorP.isNull()) - colorP= color; - else if(colorP != color) - allSameColor= false; - - auto front= DisplayToolBox::colorToTermCode(color); - auto end= front.isEmpty() ? "" : DisplayToolBox::colorToTermCode("reset"); - return result; - }); + json= parser.resultAsJSon( + [&colorP, &allSameColor](const QString& result, const QString& color, bool hightlight) { + auto trueColor= color; + if(color.isEmpty()) + trueColor= "red"; + + if(colorP.isEmpty()) + colorP= trueColor; + else if(colorP != trueColor) + allSameColor= false; + + auto front= DisplayToolBox::colorToTermCode(trueColor); + auto end= front.isEmpty() ? "" : DisplayToolBox::colorToTermCode("reset"); + return hightlight ? QString("%1%2%3").arg(front).arg(result).arg(end) : result; + }); } else { diff --git a/diceparser.cpp b/diceparser.cpp index d42716a..5b91994 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -204,12 +204,13 @@ QString DiceParser::humanReadableWarning() const return str; } -QString DiceParser::finalStringResult() const +QString DiceParser::finalStringResult(std::function<QString(const QString&, const QString&, bool)> colorize) const { - return m_parsingToolbox->finalStringResult(); + return m_parsingToolbox->finalStringResult(colorize); } -QString DiceParser::resultAsJSon(std::function<QString(const QString&, const QString&, bool)> colorize) const +QString DiceParser::resultAsJSon(std::function<QString(const QString&, const QString&, bool)> colorize, + bool removeUnhighligthed) const { QJsonObject obj; QJsonArray instructions; @@ -227,7 +228,7 @@ QString DiceParser::resultAsJSon(std::function<QString(const QString&, const QSt obj["comment"]= m_parsingToolbox->getComment(); obj["error"]= humanReadableError(); obj["scalar"]= m_parsingToolbox->finalScalarResult().first; - obj["string"]= m_parsingToolbox->finalStringResult(); + obj["string"]= m_parsingToolbox->finalStringResult(colorize, removeUnhighligthed); obj["warning"]= humanReadableWarning(); obj["command"]= m_command; diff --git a/include/diceparser.h b/include/diceparser.h index 8951a99..3c42bcc 100644 --- a/include/diceparser.h +++ b/include/diceparser.h @@ -99,14 +99,15 @@ public: QList<qreal> scalarResultsFromEachInstruction() const; QStringList stringResultFromEachInstruction(bool& hasAlias) const; void diceResultFromEachInstruction(QList<ExportedDiceResult>& resultList) const; - QString finalStringResult() const; + QString finalStringResult(std::function<QString(const QString&, const QString&, bool)> colorize) const; QString diceCommand() const; QMap<Dice::ERROR_CODE, QString> errorMap() const; QString comment() const; QString humanReadableWarning() const; QString humanReadableError() const; - QString resultAsJSon(std::function<QString(const QString&, const QString&, bool)> colorize) const; + QString resultAsJSon(std::function<QString(const QString&, const QString&, bool)> colorize, + bool removeUnhighligthed= false) const; // QStringList stringResult() const; // QStringList allDiceResult(bool& hasAlias) const; diff --git a/include/parsingtoolbox.h b/include/parsingtoolbox.h index f91c2f6..a58aabd 100644 --- a/include/parsingtoolbox.h +++ b/include/parsingtoolbox.h @@ -23,6 +23,7 @@ #define PARSINGTOOLBOX_H #include <QMap> +#include <functional> #include <vector> #include "booleancondition.h" @@ -197,7 +198,8 @@ public: std::pair<bool, QVariant> hasResultOfType(Dice::RESULT_TYPE, ExecutionNode* node, bool notthelast= false) const; QList<qreal> scalarResultsFromEachInstruction() const; std::pair<QString, QString> finalScalarResult() const; - QString finalStringResult() const; + QString finalStringResult(std::function<QString(const QString&, const QString&, bool)> colorize, + bool removeUnhighlighted= false) const; QStringList allFirstResultAsString(bool& hasAlias) const; QList<qreal> sumOfDiceResult() const; QList<ExportedDiceResult> diceResultFromEachInstruction() const; @@ -208,7 +210,9 @@ public: // result static QString replaceVariableToValue(const QString& source, QStringList values, QMap<Dice::ERROR_CODE, QString>& errorMap); - static QString replacePlaceHolderToValue(const QString& source, const QList<ExportedDiceResult>& list); + static QString replacePlaceHolderToValue(const QString& source, const QList<ExportedDiceResult>& list, + bool removeUnhighlighted, + std::function<QString(const QString&, const QString&, bool)> colorize); static SubtituteInfo readVariableFromString(const QString& source, int& start); static SubtituteInfo readPlaceHolderFromString(const QString& source, int& start); static ExportedDiceResult finalDiceResultFromInstruction(ExecutionNode* start); diff --git a/node/repeaternode.cpp b/node/repeaternode.cpp index 14ed956..9fd4f5a 100644 --- a/node/repeaternode.cpp +++ b/node/repeaternode.cpp @@ -112,7 +112,8 @@ void RepeaterNode::run(ExecutionNode* previousNode) { ParsingToolBox parsingBox; parsingBox.setStartNodes(instructions); - auto finalString= parsingBox.finalStringResult(); + auto finalString + = parsingBox.finalStringResult([](const QString& result, const QString&, bool) { return result; }); listOfStrResult << finalString; } if(!listOfStrResult.isEmpty()) diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index adf1222..0b8a3d2 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -21,6 +21,7 @@ ***************************************************************************/ #include "parsingtoolbox.h" +#include <QDebug> #include <QJsonArray> #include <QJsonObject> #include <QRegularExpression> @@ -658,8 +659,7 @@ QList<ExportedDiceResult> ParsingToolBox::diceResultFromEachInstruction() const QList<ExportedDiceResult> resultList; for(auto start : m_startNodes) { - auto result= ParsingToolBox::finalDiceResultFromInstruction(start); - resultList.append(result); + resultList.append(ParsingToolBox::finalDiceResultFromInstruction(start)); } return resultList; } @@ -706,7 +706,8 @@ QStringList listOfDiceResult(const QList<ExportedDiceResult>& list, bool removeD return listOfDiceResult; } -QString ParsingToolBox::finalStringResult() const +QString ParsingToolBox::finalStringResult(std::function<QString(const QString&, const QString&, bool)> colorize, + bool removeUnhighlighted) const { bool ok; QStringList allStringlist= allFirstResultAsString(ok); @@ -729,7 +730,7 @@ QString ParsingToolBox::finalStringResult() const QMap<Dice::ERROR_CODE, QString> errorMap; stringResult= ParsingToolBox::replaceVariableToValue(stringResult, allStringlist, errorMap); - stringResult= ParsingToolBox::replacePlaceHolderToValue(stringResult, listFull); + stringResult= ParsingToolBox::replacePlaceHolderToValue(stringResult, listFull, removeUnhighlighted, colorize); return stringResult; } @@ -1240,28 +1241,43 @@ QString ParsingToolBox::replacePlaceHolderFromJson(const QString& source, const return result; } -QString ParsingToolBox::replacePlaceHolderToValue(const QString& source, const QList<ExportedDiceResult>& list) +QString ParsingToolBox::replacePlaceHolderToValue(const QString& source, const QList<ExportedDiceResult>& list, + bool removeUnhighlighted, + std::function<QString(const QString&, const QString&, bool)> colorize) { QStringList resultList; std::transform( - std::begin(list), std::end(list), std::back_inserter(resultList), [](const ExportedDiceResult& dice) { + std::begin(list), std::end(list), std::back_inserter(resultList), + [removeUnhighlighted, colorize](const ExportedDiceResult& dice) { QStringList valuesStr; if(dice.size() == 1) { auto values= dice.values(); - std::transform(std::begin(values), std::end(values), std::back_inserter(valuesStr), - [](const QList<ListDiceResult>& dice) { - QStringList textList; - std::transform(std::begin(dice), std::end(dice), std::back_inserter(textList), - [](const ListDiceResult& dice) { - QStringList list; - std::transform( - std::begin(dice), std::end(dice), std::back_inserter(list), - [](const HighLightDice& hl) { return hl.getResultString(); }); - return list.join(","); - }); - return textList.join(","); - }); + std::transform( + std::begin(values), std::end(values), std::back_inserter(valuesStr), + [removeUnhighlighted, colorize](const QList<ListDiceResult>& dice) { + QStringList textList; + std::transform( + std::begin(dice), std::end(dice), std::back_inserter(textList), + [removeUnhighlighted, colorize](const ListDiceResult& dice) { + QStringList list; + ListDiceResult values= dice; + if(removeUnhighlighted) + { + values.clear(); + std::copy_if(std::begin(dice), std::end(dice), std::back_inserter(values), + [](const HighLightDice& hl) { return hl.isHighlighted(); }); + } + + std::transform(std::begin(values), std::end(values), std::back_inserter(list), + [colorize](const HighLightDice& hl) { + return colorize(hl.getResultString(), {}, hl.isHighlighted()); + }); + return list.join(","); + }); + textList.removeAll(QString()); + return textList.join(","); + }); } else if(dice.size() > 1) { diff --git a/tests/dice/tst_dice.cpp b/tests/dice/tst_dice.cpp index 8cff99b..0ede904 100644 --- a/tests/dice/tst_dice.cpp +++ b/tests/dice/tst_dice.cpp @@ -1136,8 +1136,7 @@ void TestDice::ifCommandTest() QVERIFY2(test, cmd.toStdString().c_str()); m_diceParser->start(); auto results= m_diceParser->scalarResultsFromEachInstruction(); - auto strResult= m_diceParser->finalStringResult(); - qDebug() << strResult; + auto strResult= m_diceParser->finalStringResult([](const QString& result, const QString&, bool) { return result; }); QCOMPARE(results.size(), 1); |