aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2020-08-22 17:09:57 +0200
committerRenaud G <renaud@rolisteam.org>2020-08-22 17:09:57 +0200
commit8209ac895fac8f993c13187195287f4021457da5 (patch)
tree98a318b93febe66426f2ab01d88ceabb8bdc9c1b /cli
parent8ee9c4372efce878e817c49b375a8fd904ea5a43 (diff)
downloadOneRoll-8209ac895fac8f993c13187195287f4021457da5.tar.gz
OneRoll-8209ac895fac8f993c13187195287f4021457da5.zip
Fix repeat node.
Diffstat (limited to 'cli')
-rw-r--r--cli/displaytoolbox.cpp90
-rw-r--r--cli/displaytoolbox.h2
-rw-r--r--cli/main.cpp332
3 files changed, 149 insertions, 275 deletions
diff --git a/cli/displaytoolbox.cpp b/cli/displaytoolbox.cpp
index 9dc8fb6..0c527e0 100644
--- a/cli/displaytoolbox.cpp
+++ b/cli/displaytoolbox.cpp
@@ -122,54 +122,7 @@ QString DisplayToolBox::diceToSvg(QJsonArray array, bool withColor, bool allSame
}
}
#include <QVariantList>
-QJsonArray DisplayToolBox::diceToJson(QList<ExportedDiceResult>& diceList, bool& allSameFaceCount, bool& allSameColor)
-{
- allSameFaceCount= true;
- QJsonArray array;
- QStringList colorList;
- for(auto dice : diceList)
- {
- if(dice.size() > 1)
- {
- allSameFaceCount= false;
- }
- for(quint64 face : dice.keys())
- {
- ListDiceResult diceResults= dice.value(face);
- QJsonObject object;
- QVariantList listVariant;
- object["face"]= static_cast<int>(face);
- listVariant.reserve(diceResults.size());
- for(auto const& dice : diceResults)
- {
- QJsonObject diceObj;
- auto listValues= dice.getResult();
- if(!listValues.isEmpty())
- {
- diceObj["total"]= static_cast<qint64>(listValues.takeFirst());
- diceObj["face"]= static_cast<int>(face);
- auto color= dice.getColor();
- diceObj["color"]= color;
- if(!colorList.contains(color))
- colorList.append(color);
- QJsonArray subValues;
- for(auto result : listValues)
- {
- subValues.push_back(static_cast<qint64>(result));
- }
- diceObj["subvalues"]= subValues;
- }
- listVariant.append(QVariant::fromValue(diceObj));
- }
- object["values"]= QJsonArray::fromVariantList(listVariant);
- array.push_back(object);
- }
- }
- if(colorList.size() > 1)
- allSameColor= false;
- return array;
-}
QString DisplayToolBox::diceResultToString(QJsonObject val, Output type, bool hasColor)
{
auto total= QString::number(val["total"].toDouble());
@@ -204,46 +157,3 @@ QString DisplayToolBox::diceResultToString(QJsonObject val, Output type, bool ha
}
return total;
}
-QString DisplayToolBox::diceToText(QJsonArray array, bool withColor, bool allSameFaceCount, bool allSameColor)
-{
- Q_UNUSED(allSameColor)
- QStringList result;
- for(auto item : array)
- {
- QString subResult("");
- auto obj= item.toObject();
- auto values= obj["values"].toArray();
-
- QStringList diceResult;
- for(auto valRef : values)
- {
- diceResult+= diceResultToString(valRef.toObject(), Output::Terminal, withColor);
- }
- if(!diceResult.isEmpty())
- {
- if(!allSameFaceCount)
- {
- subResult+= QStringLiteral("d%1:(").arg(obj["face"].toInt());
- }
- if(withColor)
- {
- subResult+= DisplayToolBox::colorToTermCode(obj["color"].toString());
- }
- subResult+= diceResult.join(" ");
- if(withColor)
- {
- subResult+= DisplayToolBox::colorToTermCode(QStringLiteral("reset"));
- }
- if(!allSameFaceCount)
- {
- subResult+= QStringLiteral(")");
- }
- }
-
- if(!subResult.isEmpty())
- {
- result+= subResult;
- }
- }
- return result.join(" - ");
-}
diff --git a/cli/displaytoolbox.h b/cli/displaytoolbox.h
index 30b7ba3..11f92c3 100644
--- a/cli/displaytoolbox.h
+++ b/cli/displaytoolbox.h
@@ -21,8 +21,6 @@ public:
static QString makeImage(QByteArray svgCode);
#endif
static QString colorToTermCode(QString str);
- static QString diceToText(QJsonArray array, bool withColor, bool allSameFaceCount, bool allSameColor);
- static QJsonArray diceToJson(QList<ExportedDiceResult>& diceList, bool& allSameFaceCount, bool& allSameColor);
static QString diceToSvg(QJsonArray array, bool withColor, bool allSameColor, bool allSameFaceCount);
static QString diceResultToString(QJsonObject val, Output type, bool hasColor);
};
diff --git a/cli/main.cpp b/cli/main.cpp
index 8362cdb..392d555 100644
--- a/cli/main.cpp
+++ b/cli/main.cpp
@@ -118,28 +118,35 @@ QString diceToMarkdown(QJsonArray array, bool withColor, bool allSameColor, bool
}
}
-void displayJSon(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error,
- QString warning, QString comment, bool allSameFaceCount, bool allSameColor)
+void displayJSon(QString json)
{
- Q_UNUSED(withColor);
- QJsonDocument doc;
- QJsonObject obj;
- obj["values"]= array;
- obj["comment"]= comment;
- obj["error"]= error;
- obj["scalar"]= scalarText;
- obj["string"]= resultStr;
- obj["allSameFace"]= allSameFaceCount;
- obj["allSameColor"]= allSameColor;
- obj["warning"]= warning;
- obj["command"]= cmd;
- doc.setObject(obj);
- out << doc.toJson() << "\n";
+ out << json << "\n";
}
-void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd,
- QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor)
+void displayMarkdown(QString json)
{
- Q_UNUSED(withColor);
+ QJsonDocument doc= QJsonDocument::fromJson(json.toUtf8());
+ auto obj= doc.object();
+ auto error= obj["error"].toString();
+ auto warning= obj["warning"].toString();
+ auto comment= obj["warning"].toString();
+ auto arrayInst= obj["instructions"].toArray();
+ QStringList diceResults;
+ for(auto inst : arrayInst)
+ {
+ auto obj= inst.toObject();
+ auto diceVals= obj["diceval"].toArray();
+ for(auto diceval : diceVals)
+ {
+ auto objval= diceval.toObject();
+ auto resultStr= QString::number(objval["value"].toDouble());
+ diceResults << resultStr;
+ }
+ }
+ auto diceList= diceResults.join(",");
+ auto resultStr= obj["string"].toString();
+ auto scalarText= obj["scalar"].toString();
+ auto cmd= obj["command"].toString();
+
QString str("```Markdown\n");
if(!error.isEmpty())
{
@@ -154,23 +161,44 @@ void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bo
{
str.prepend(QStringLiteral("%1\n").arg(comment));
}
- auto diceList= DisplayToolBox::diceToText(array, false, allSameFaceCount, allSameColor);
- if(resultStr.isEmpty())
- {
- str.append(QStringLiteral("# %1\nDetails:[%3 (%2)]\n").arg(scalarText).arg(diceList).arg(cmd));
- }
- else if(!resultStr.isEmpty())
+ if(!resultStr.isEmpty() && resultStr != scalarText)
{
// resultStr.replace("%2", diceList.trimmed());
str.append(QStringLiteral("%1\n").arg(resultStr));
}
+ else
+ {
+ str.append(QStringLiteral("# %1\nDetails:[%3 (%2)]\n").arg(scalarText).arg(diceList).arg(cmd));
+ }
}
str.append(QStringLiteral("```"));
out << str;
}
-QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error,
- QString warning, QString comment, bool allSameFaceCount, bool allSameColor)
+QString displaySVG(QString json, QJsonArray array, bool withColor, bool allSameColor)
{
+ QJsonDocument doc= QJsonDocument::fromJson(json.toUtf8());
+ auto obj= doc.object();
+ auto error= obj["error"].toString();
+ auto warning= obj["warning"].toString();
+ auto comment= obj["warning"].toString();
+ auto arrayInst= obj["instructions"].toArray();
+ QStringList diceResults;
+ for(auto inst : arrayInst)
+ {
+ auto obj= inst.toObject();
+ auto diceVals= obj["diceval"].toArray();
+ for(auto diceval : diceVals)
+ {
+ auto objval= diceval.toObject();
+ auto resultStr= objval["string"].toString();
+ diceResults << resultStr;
+ }
+ }
+ auto diceList= diceResults.join(",");
+ auto resultStr= obj["string"].toString();
+ auto scalarText= obj["scalar"].toString();
+ auto cmd= obj["command"].toString();
+
QString str("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<svg version=\"1.1\" "
"xmlns=\"http://www.w3.org/2000/svg\" "
"xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n");
@@ -196,8 +224,13 @@ QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool
.arg(y));
y+= 20;
}
- auto diceList= DisplayToolBox::diceToSvg(array, withColor, allSameColor, allSameFaceCount);
- if(resultStr.isEmpty())
+ // auto diceList= DisplayToolBox::diceToSvg(array, withColor, allSameColor, allSameFaceCount);
+ if(!resultStr.isEmpty() && resultStr != scalarText)
+ {
+ resultStr.replace("%2", diceList.trimmed());
+ str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%2\">%1</text>").arg(resultStr).arg(y));
+ }
+ else
{
if(withColor)
str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%4\"><tspan "
@@ -217,30 +250,44 @@ QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool
.arg(y)
.arg(y * 2));
}
- else if(!resultStr.isEmpty())
- {
- resultStr.replace("%2", diceList.trimmed());
- str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%2\">%1</text>").arg(resultStr).arg(y));
- }
}
str.append(QStringLiteral("</svg>\n"));
return str;
}
#ifdef PAINTER_OP
-void displayImage(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error,
- QString warning, QString comment, bool allSameFaceCount, bool allSameColor)
+void displayImage(QString json, QJsonArray array, bool withColor, bool allSameColor)
{
- auto svg= displaySVG(scalarText, resultStr, array, withColor, cmd, error, warning, comment, allSameFaceCount,
- allSameColor);
+ auto svg= displaySVG(json, array, withColor, allSameColor);
out << DisplayToolBox::makeImage(svg.toUtf8());
}
#endif
-void displayCommandResult(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd,
- QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor)
+void displayCommandResult(QString json, QJsonArray array, bool withColor, bool allSameColor)
{
- // TODO display warning
+ QJsonDocument doc= QJsonDocument::fromJson(json.toUtf8());
+ auto obj= doc.object();
+ auto error= obj["error"].toString();
+ auto warning= obj["warning"].toString();
+ auto comment= obj["comment"].toString();
+ auto arrayInst= obj["instructions"].toArray();
+ QStringList diceResults;
+ for(auto inst : arrayInst)
+ {
+ auto obj= inst.toObject();
+ auto diceVals= obj["diceval"].toArray();
+ for(auto diceval : diceVals)
+ {
+ auto objval= diceval.toObject();
+ auto resultStr= objval["string"].toString();
+ diceResults << resultStr;
+ }
+ }
+ auto diceList= diceResults.join(",");
+ auto scalarText= obj["scalar"].toString();
+ auto cmd= obj["command"].toString();
+ auto resultStr= obj["string"].toString();
+
if(!error.isEmpty())
{
err << "Error" << error << "\n";
@@ -252,14 +299,12 @@ void displayCommandResult(QString scalarText, QString resultStr, QJsonArray arra
QString str;
- auto diceList= DisplayToolBox::diceToText(array, withColor, allSameFaceCount, allSameColor);
-
if(withColor)
str= QString("Result: \e[0;31m%1\e[0m - details:[%3 (%2)]").arg(scalarText).arg(diceList).arg(cmd);
else
str= QString("Result: %1 - details:[%3 (%2)]").arg(scalarText).arg(diceList).arg(cmd);
- if(!resultStr.isEmpty())
+ if(!resultStr.isEmpty() && resultStr != scalarText)
{
resultStr.replace("%2", diceList.trimmed());
str= resultStr;
@@ -271,7 +316,7 @@ void displayCommandResult(QString scalarText, QString resultStr, QJsonArray arra
}
out << str << "\n";
}
-
+#include <QDebug>
int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPORTFORMAT format, QJsonArray array)
{
DiceParser parser;
@@ -301,118 +346,62 @@ int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPOR
if(parser.parseLine(cmd))
{
parser.start();
- QList<ExportedDiceResult> list;
- QList<ExportedDiceResult> listFull;
- bool homogeneous= true;
- parser.getLastDiceResult(list, homogeneous);
- parser.getDiceResultFromAllInstruction(listFull);
- bool allSameFaceCount= true;
- bool allSameColor= true;
- auto array= DisplayToolBox::diceToJson(list, allSameFaceCount, allSameColor);
- QString resultStr;
- QString scalarText;
- QString lastScalarText;
- QString comment= parser.getComment();
- QString error= parser.humanReadableError();
- QString warnings= parser.humanReadableWarning();
- QStringList strLst;
- QStringList listOfDiceResult;
- QString cmdRework= parser.getDiceCommand();
-
- if(parser.hasIntegerResultNotInFirst())
- {
- auto values= parser.getLastIntegerResults();
- for(auto val : values)
- {
- strLst << QString::number(val);
- }
- scalarText= QString("%1").arg(strLst.join(','));
- lastScalarText= strLst.last();
- }
- else if(!list.isEmpty())
- {
- auto values= parser.getSumOfDiceResult();
- for(auto val : values)
- {
- strLst << QString::number(val);
- }
- scalarText= QString("%1").arg(strLst.join(','));
- }
- for(auto map : list)
+ std::set<EXPORTFORMAT> svgFormat({SVG, IMAGE, BOT});
+ QString json;
+ bool allSameColor= true;
+ if(svgFormat.find(format) != svgFormat.end())
{
- for(auto key : map.keys())
- {
- auto dice= map[key];
- QString stringVal;
- for(auto val : dice)
- {
- qint64 total= 0;
- QStringList dicelist;
- for(auto score : val.getResult())
- {
- total+= score;
- dicelist << QString::number(score);
- }
- if(val.getResult().size() > 1)
+ allSameColor= true;
+ QString colorP;
+ json= parser.resultAsJSon(
+ [&colorP, &allSameColor](const QString& value, const QString& color, bool highlight) {
+ if(colorP.isNull())
+ colorP= color;
+ else if(colorP != color)
+ allSameColor= false;
+
+ QString result= value;
+ bool hasColor= !color.isEmpty();
+ QString style;
+ if(hasColor)
{
- stringVal= QString("%1 [%2]").arg(total).arg(dicelist.join(','));
- listOfDiceResult << stringVal;
+ style+= QStringLiteral("fill=\"%1\" ").arg(color);
}
- else
+ if(highlight)
{
- listOfDiceResult << QString::number(total);
+ if(style.isEmpty())
+ style+= QStringLiteral("fill=\"%1\" ")
+ .arg("red"); // default color must get the value from the setting object
+ style+= QStringLiteral("font-weight=\"bold\" ");
}
- }
- }
+ if(!style.isEmpty())
+ result= QString("<tspan %2>%1</tspan>").arg(value).arg(style);
+ return result;
+ });
}
-
- if(parser.hasStringResult())
+ else if(TERMINAL == format)
{
- bool ok;
- QStringList allStringlist= parser.allFirstResultAsString(ok);
-
- QStringList resultWithPlaceHolder;
- std::for_each(allStringlist.begin(), allStringlist.end(), [&resultWithPlaceHolder](const QString& sub) {
- QRegularExpression ex("%[1-3]?|\\$[1-9]+|@[1-9]+");
- if(sub.contains(ex))
- resultWithPlaceHolder.append(sub);
- });
-
- if(resultWithPlaceHolder.isEmpty())
- allStringlist.erase(std::remove_if(allStringlist.begin(), allStringlist.end(),
- [](const QString& result) {
- bool ok;
- result.toInt(&ok);
- return ok;
- }),
- allStringlist.end());
- auto stringResult
- = resultWithPlaceHolder.isEmpty() ? allStringlist.join(" ; ") : resultWithPlaceHolder.join(" ; ");
-
- stringResult.replace("%1", scalarText);
- stringResult.replace("%2", listOfDiceResult.join(",").trimmed());
- stringResult.replace("%3", lastScalarText);
- stringResult.replace("\\n", "\n");
-
- QMap<Dice::ERROR_CODE, QString> errorMap;
- stringResult= ParsingToolBox::replaceVariableToValue(stringResult, allStringlist, errorMap);
- stringResult= ParsingToolBox::replacePlaceHolderToValue(stringResult, listFull);
-
- error= errorMap.values().join(", ");
-
- int i= strLst.size();
- for(auto it= strLst.rbegin(); it != strLst.rend(); ++it)
- {
- stringResult.replace(QStringLiteral("µ%1").arg(i), (*it));
- --i;
- }
-
- bool isInt= true;
- stringResult.toInt(&isInt);
- if(!isInt || !resultWithPlaceHolder.isEmpty())
- resultStr= stringResult;
+ allSameColor= true;
+ QString colorP;
+ json= parser.resultAsJSon(
+ [&colorP, &allSameColor](const QString& result, const QString& color, bool highlight) {
+ 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;
+ });
+ }
+ else
+ {
+ allSameColor= true;
+ json= parser.resultAsJSon([](const QString& result, const QString&, bool) { return result; });
}
+
if(format == BOT)
{
if(allSameColor)
@@ -427,58 +416,40 @@ int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPOR
format= MARKDOWN;
#endif
}
- if(!error.isEmpty())
+ if(!parser.humanReadableError().isEmpty())
{
format= MARKDOWN;
}
}
+ if(!withColor && format == TERMINAL)
+ format= TEXT;
+
+ // qDebug().noquote() << json << format;
switch(format)
{
case TERMINAL:
- displayCommandResult(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment,
- allSameFaceCount, allSameColor);
+ displayCommandResult(json, array, true, allSameColor);
break;
case SVG:
- out << displaySVG(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment,
- allSameFaceCount, allSameColor)
- << "\n";
+ out << displaySVG(json, array, withColor, allSameColor) << "\n";
break;
case BOT:
case MARKDOWN:
- displayMarkdown(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment,
- allSameFaceCount, allSameColor);
+ displayMarkdown(json);
break;
case TEXT:
- displayCommandResult(scalarText, resultStr, array, false, cmdRework, error, warnings, comment,
- allSameFaceCount, allSameColor);
+ displayCommandResult(json, array, false, allSameColor);
break;
case JSON:
- displayJSon(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment,
- allSameFaceCount, allSameColor);
+ displayJSon(json);
break;
#ifdef PAINTER_OP
case IMAGE:
- displayImage(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment,
- allSameFaceCount, allSameColor);
+ displayImage(json, array, withColor, allSameColor);
break;
#endif
}
- if(!treeFile.isEmpty())
- {
- parser.writeDownDotTree(treeFile);
- }
-
- if(!error.isEmpty())
- {
- rt= 1;
- }
- }
- else
- {
- QString error= parser.humanReadableError();
- err << "Error: " << error << "\n";
- rt= 1;
}
}
return rt;
@@ -604,9 +575,6 @@ int main(int argc, char* argv[])
}
QStringList cmdList= optionParser.positionalArguments();
- cmdList.append("repeat(2d6c[<4];1d6;2d6c[<4];\"échecs: $1 [@1], succès $3 [@3], le dé "
- "de balance: $2\",5)");
-
if(!cmdList.isEmpty())
{
if(cmdList[0].startsWith('&'))
@@ -614,9 +582,7 @@ int main(int argc, char* argv[])
colorb= false;
}
}
- // cmdList << "8d10;\$1c[>6];\$1c[=1];\$2-\$3i:[>0]{\"%3
- // Success[%2]\"}{i:[<0]{\"Critical fail %3 [%2]\"}{\"Fail %3
- // [%2]\"}}";
+
QJsonArray aliases;
if(optionParser.isSet(alias))
{