aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2018-01-14 02:34:21 +0100
committerRenaud G <renaud@rolisteam.org>2018-01-14 02:34:21 +0100
commit8904bc61ad71f407fbefa4b80793ba424f2ce88b (patch)
tree47b0962c82a45b8cd5291132c7d96ef5cfbe5425
parent12bfa92a9d35299a2eeb456b11238a6bd9669146 (diff)
downloadOneRoll-8904bc61ad71f407fbefa4b80793ba424f2ce88b.tar.gz
OneRoll-8904bc61ad71f407fbefa4b80793ba424f2ce88b.zip
-rework display
-add support for new output format: SVG, IMAGE, JSON.
-rw-r--r--cli/CMakeLists.txt2
-rw-r--r--cli/displaytoolbox.cpp378
-rw-r--r--cli/displaytoolbox.h13
-rw-r--r--cli/main.cpp586
-rw-r--r--diceparser.cpp24
5 files changed, 538 insertions, 465 deletions
diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt
index bc337da..748410c 100644
--- a/cli/CMakeLists.txt
+++ b/cli/CMakeLists.txt
@@ -100,7 +100,7 @@ SET( dice_sources
../node/splitnode.cpp
../node/groupnode.cpp
main.cpp
- generateimage.cpp
+ displaytoolbox.cpp
../highlightdice.cpp
../node/variablenode.cpp
)
diff --git a/cli/displaytoolbox.cpp b/cli/displaytoolbox.cpp
index 3785eab..0aef6a7 100644
--- a/cli/displaytoolbox.cpp
+++ b/cli/displaytoolbox.cpp
@@ -1,28 +1,229 @@
-#include "generateimage.h"
+#include "displaytoolbox.h"
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QFontMetrics>
+#include <QPainter>
+#include <QBuffer>
+#include <QFont>
+#include <QDebug>
-ImageGenerator::ImageGenerator()
+#define LINE_SPACING 5
+
+DisplayToolBox::DisplayToolBox()
{
}
-QString ImageGenerator::makeImage(QJsonArray array,bool allSameFaceCount)
+QString DisplayToolBox::makeImage(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString comment, bool allSameFaceCount,bool allSameColor)
{
- QString lengthStr("%1");
- int length=0;
+ QString textTotal("%1 Details:[%3 %2]");
+
+
+ int lineCount=2;
+ if(!comment.isEmpty())
+ {
+ lineCount = 3;
+ }
+ if(resultStr.isEmpty())
+ {
+ auto list = diceToText(array,false,allSameFaceCount,allSameColor);
+ textTotal = textTotal.arg(scalarText).arg(list).arg(cmd);
+ }
+ else
+ {
+ --lineCount;
+ textTotal = resultStr;
+ }
+ if(comment.size()>textTotal.size())
+ {
+ textTotal.prepend(QStringLiteral("%1\n").arg(comment));
+ }
+ QFont font("Helvetica", 15);// = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
+ QFontMetrics fm(font);
+ QRect rect = fm.boundingRect(textTotal);
+
+ QImage img(rect.width(),(rect.height()+LINE_SPACING)*lineCount,QImage::Format_ARGB32);
+ img.fill(Qt::transparent);
+ QPainter painter(&img);
+ painter.setFont(font);
+ int y = rect.height();
+ if(!comment.isNull())
+ {
+ QPen pen = painter.pen();
+ pen.setColor(Qt::black);
+ painter.setPen(pen);
+ painter.drawText(QPoint(0,y),comment);
+ y += rect.height()+LINE_SPACING;
+ }
+ if(!resultStr.isEmpty())
+ {
+ QPen pen = painter.pen();
+ pen.setColor(Qt::black);
+ painter.setPen(pen);
+ painter.drawText(QPoint(0,y),resultStr);
+ }
+ else
+ {
+ painter.save();
+ QPen pen = painter.pen();
+ pen.setColor(Qt::red);
+ painter.setPen(pen);
+ painter.drawText(QPoint(0,y),scalarText);
+ y += rect.height()+LINE_SPACING;
+ painter.restore();
+ int x = 0;
+ QString text=QStringLiteral("Details:[%1 (").arg(cmd);
+ painter.drawText(QPoint(x,y),text);
+ x += fm.boundingRect(text).width();
+
+ if(allSameFaceCount)
+ {
+ int i = 0;
+ for(auto item : array)
+ {
+ QStringList result;
+ bool last = (array.size()-1==i);
+ auto obj = item.toObject();
+ auto values= obj["values"].toArray();
+ for(auto valRef : values)
+ {
+ result.append(diceResultToString(valRef.toObject()));
+ }
+ painter.save();
+ QPen pen = painter.pen();
+ QColor color;
+ color.setNamedColor(obj["color"].toString());
+ pen.setColor(color);
+
+ painter.setPen(pen);
+ text = QStringLiteral("%1").arg(result.join(','));
+ if(!last)
+ {
+ text.append(",");
+ }
+ painter.drawText(QPoint(x,y),text);
+ x += fm.boundingRect(text).width();
+ painter.restore();
+ ++i;
+ }
+ }
+ else
+ {
+ int i = 0;
+ for(auto item : array)
+ {
+ QStringList result;
+ auto obj = item.toObject();
+ bool last = (array.size()-1==i);
+ auto values= obj["values"].toArray();
+ for(auto valRef : values)
+ {
+ result.append(diceResultToString(valRef.toObject()));
+ }
+ text =QStringLiteral("d%1:(").arg(obj["face"].toDouble());
+ painter.drawText(QPoint(x,y),text);
+ x += fm.boundingRect(text).width();
+
+ painter.save();
+ QPen pen = painter.pen();
+ QColor color;
+ color.setNamedColor(obj["color"].toString());
+ pen.setColor(color);
+ painter.setPen(pen);
+
+ text = QStringLiteral("%1").arg(result.join(','));
+ painter.drawText(QPoint(x,y),text);
+ x += fm.boundingRect(text).width();
+ painter.restore();
+
+ text = QStringLiteral(")");
+ if(!last)
+ {
+ text.append(",");
+ }
+ painter.drawText(QPoint(x,y),text);
+ x += fm.boundingRect(text).width();
+ ++i;
+ }
+ }
+ text = QStringLiteral(")");
+ x += fm.boundingRect(text).width();
+ painter.drawText(QPoint(x,y),text);
+ }
+
+ painter.end();
+
+ img.save("/home/renaud/image.png","PNG");
+ QByteArray ba;
+ QBuffer buffer(&ba);
+ buffer.open(QIODevice::WriteOnly);
+ img.save(&buffer, "PNG");
+
+ //return {};
+ return ba.toBase64();
+}
+
+QString DisplayToolBox::colorToTermCode(QString str)
+{
+ if(str.isEmpty()|| str==QStringLiteral("black"))
+ {
+ return QStringLiteral("\e[0;31m");
+ }
+ if(str==QStringLiteral("white"))
+ {
+ return QStringLiteral("\e[97m");
+ }
+ if(str==QStringLiteral("blue"))
+ {
+ return QStringLiteral("\e[34m");
+ }
+ if(str==QStringLiteral("red"))
+ {
+ return QStringLiteral("\e[31m");
+ }
+ if(str==QStringLiteral("black"))
+ {
+ return QStringLiteral("\e[30m");
+ }
+ if(str==QStringLiteral("green"))
+ {
+ return QStringLiteral("\e[32m");
+ }
+ if(str==QStringLiteral("yellow"))
+ {
+ return QStringLiteral("\e[33m");
+ }
+ return {};
+}
+
+QString DisplayToolBox::diceToSvg(QJsonArray array,bool withColor,bool allSameColor,bool allSameFaceCount )
+{
+ Q_UNUSED(allSameColor)
if(allSameFaceCount)
{
QStringList result;
for(auto item : array)
{
+ QStringList subResult;
auto obj = item.toObject();
auto values= obj["values"].toArray();
- for(auto val : values)
+ for(auto valRef : values)
+ {
+ subResult.append(diceResultToString(valRef.toObject()));
+ }
+ if(withColor)
+ {
+ result.append(QStringLiteral("<tspan fill=\"%1\">").arg(obj["color"].toString()));
+ result.append(subResult.join(','));
+ result.append(QStringLiteral("</tspan>"));
+ }
+ else
{
- result.append(val.toString());
+ result.append(subResult.join(','));
}
}
- return result.join(',');
+ return result.join("");
}
else
{
@@ -32,17 +233,168 @@ QString ImageGenerator::makeImage(QJsonArray array,bool allSameFaceCount)
QStringList subResult;
auto obj = item.toObject();
auto values= obj["values"].toArray();
- for(auto val : values)
+
+ for(auto valRef : values)
{
- result.append(val.toString());
+ subResult.append(diceResultToString(valRef.toObject()));
}
result.append(QStringLiteral("d%1:(").arg(obj["face"].toString()));
+ if(withColor)
+ {
+ result.append(QStringLiteral("<tspan fill=\"%1\">").arg(obj["color"].toString()));
+ }
result.append(subResult.join(','));
- result.append(QStringLiteral(")"));
+ if(withColor)
+ {
+ result.append(QStringLiteral("</tspan>)"));
+ }
+ else
+ {
+ result.append(QStringLiteral(")"));
+ }
+ }
+ return result.join("");
+ }
+}
+QJsonArray DisplayToolBox::diceToJson(QList<ExportedDiceResult>& diceList,bool& allSameFaceCount,bool& allSameColor)
+{
+ allSameFaceCount = true;
+ allSameColor = true;
+ QJsonArray array;
+ for(auto dice : diceList)
+ {
+ if(dice.size()>1)
+ {
+ allSameFaceCount = false;
+ }
+ for(int face: dice.keys())
+ {
+ ListDiceResult diceResults = dice.value(face);
+ std::vector<std::vector<HighLightDice>> sameColorDice;
+ std::vector<QString> alreadyDoneColor;
+ for(auto & dice : diceResults)
+ {
+ auto it=std::find_if(alreadyDoneColor.begin(), alreadyDoneColor.end(),[dice](QString color){
+ return color == dice.getColor();
+ });
+
+ if( it == alreadyDoneColor.end())
+ {
+ sameColorDice.push_back(std::vector<HighLightDice>());
+ alreadyDoneColor.push_back(dice.getColor());
+ it = alreadyDoneColor.end();
+ --it;
+ }
+ int i = std::distance(alreadyDoneColor.begin(), it);
+ sameColorDice[i].push_back(dice);
+ }
+ int i = 0;
+ if(alreadyDoneColor.size()>1)
+ {
+ allSameColor = false;
+ }
+ for(auto it = alreadyDoneColor.begin() ; it != alreadyDoneColor.end(); ++it)
+ {
+ auto list = sameColorDice[i];
+ QJsonObject object;
+ object["color"]=*it;
+ object["face"]=face;
+ QJsonArray values;
+ for(auto const dice : list)
+ {
+ QJsonObject diceObj;
+ auto listValues = dice.getResult();
+ if(!listValues.isEmpty())
+ {
+ diceObj["total"]=(qint64)listValues.takeFirst();
+ QJsonArray subValues;
+ for(auto result : listValues)
+ {
+ subValues.push_back((qint64)result);
+ }
+ diceObj["subvalues"]=subValues;
+ }
+ values.push_back(diceObj);
+ }
+ object["values"]=values;
+ ++i;
+ array.push_back(object);
+ }
+ }
+ }
+ return array;
+}
+QString DisplayToolBox::diceResultToString(QJsonObject val)
+{
+ auto total = QString::number(val["total"].toDouble());
+ auto subvalues = val["subvalues"].toArray();
+ QStringList subStr;
+ for(auto subval : subvalues)
+ {
+ subStr << QString::number(subval.toDouble());
+ }
+ if(!subStr.isEmpty())
+ {
+ total.append(QStringLiteral(" [%1]").arg(subStr.join(',')));
+ }
+ return total;
+}
+QString DisplayToolBox::diceToText(QJsonArray array, bool withColor,bool allSameFaceCount, bool allSameColor)
+{
+ Q_UNUSED(allSameColor)
+ if(allSameFaceCount)
+ {
+ QStringList result;
+ for(auto item : array)
+ {
+ QStringList subResult;
+ auto obj = item.toObject();
+ auto values= obj["values"].toArray();
+ for(auto valRef : values)
+ {
+ subResult.append(diceResultToString(valRef.toObject()));
+ }
+ if(withColor)
+ {
+ result.append(DisplayToolBox::colorToTermCode(obj["color"].toString()));
+ result.append(subResult.join(','));
+ result.append(QStringLiteral("\e[0m"));
+ }
+ else
+ {
+ result.append(subResult.join(','));
+ }
+ }
+ return result.join("");
+ }
+ else
+ {
+ QStringList result;
+ for(auto item : array)
+ {
+ QStringList subResult;
+ auto obj = item.toObject();
+ auto values= obj["values"].toArray();
+ for(auto valRef : values)
+ {
+ subResult.append(diceResultToString(valRef.toObject()));
+ }
+ result.append(QStringLiteral("d%1:(").arg(obj["face"].toString()));
+ if(withColor)
+ {
+ result.append(DisplayToolBox::colorToTermCode(obj["color"].toString()));
+ }
+ result.append(subResult.join(','));
+ if(withColor)
+ {
+ result.append(QStringLiteral("\e[0m)"));
+ }
+ else
+ {
+ result.append(QStringLiteral(")"));
+ }
}
return result.join(' ');
}
- QImage img;
- img.fill(Qt::transparent);
}
diff --git a/cli/displaytoolbox.h b/cli/displaytoolbox.h
index 077831f..0b6cd12 100644
--- a/cli/displaytoolbox.h
+++ b/cli/displaytoolbox.h
@@ -5,11 +5,18 @@
#include <QString>
#include <QJsonArray>
-class ImageGenerator
+#include "diceparser.h"
+
+class DisplayToolBox
{
public:
- ImageGenerator();
- static QString makeImage(QJsonArray array,bool allSameFaceCount);
+ DisplayToolBox();
+ static QString makeImage(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString comment, bool allSameFaceCount,bool allSameColor);
+ 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);
};
diff --git a/cli/main.cpp b/cli/main.cpp
index ed071bc..1455fa3 100644
--- a/cli/main.cpp
+++ b/cli/main.cpp
@@ -26,10 +26,12 @@
#include <QTextStream>
#include <QJsonArray>
#include <QJsonObject>
+#include <QJsonDocument>
+#include <QGuiApplication>
+#include "displaytoolbox.h"
#include "diceparser.h"
#include "highlightdice.h"
-#include "generateimage.h"
/**
* @page Dice
@@ -46,189 +48,48 @@
QTextStream out(stdout, QIODevice::WriteOnly);
bool markdown = false;
-enum EXPORTFORMAT {TERMINAL, SVG, IMAGE, MARKDOWN, JSON, UNKNOWN};
+enum EXPORTFORMAT {TERMINAL, SVG, IMAGE, MARKDOWN, JSON, BOT};
+int returnValue = 0;
-QJsonArray diceToJson(QList<ExportedDiceResult>& diceList,bool& allSameFaceCount,bool& allSameColor)
-{
- allSameFaceCount = true;
- allSameColor = true;
- QJsonArray array;
- for(auto dice : diceList)
- {
- if(dice.size()>1)
- {
- allSameFaceCount = false;
- }
- for(int face: dice.keys())
- {
- ListDiceResult diceResults = dice.value(face);
- std::vector<std::vector<HighLightDice>> sameColorDice;
- std::vector<QString> alreadyDoneColor;
- for(auto & dice : diceResults)
- {
- auto it=std::find_if(alreadyDoneColor.begin(), alreadyDoneColor.end(),[dice](QString color){
- return color == dice.getColor();
- });
- if( it == alreadyDoneColor.end())
- {
- sameColorDice.push_back(std::vector<HighLightDice>());
- alreadyDoneColor.push_back(dice.getColor());
- it = alreadyDoneColor.end();
- --it;
- }
- int i = std::distance(alreadyDoneColor.begin(), it);
- sameColorDice[i].push_back(dice);
- }
- int i = 0;
- if(alreadyDoneColor.size()>0)
- {
- allSameColor = false;
- }
- for(auto it = alreadyDoneColor.begin() ; it != alreadyDoneColor.end(); ++it)
- {
- auto list = sameColorDice[i];
- QJsonObject object;
- object["color"]=*it;
- object["face"]=face;
- QJsonArray values;
- for(auto const dice : list)
- {
- for(auto result : dice.getResult())
- {
- values.push_back((qint64)result);
- }
- }
- object["values"]=values;
- ++i;
- array.push_back(object);
- }
- }
- }
- return array;
-}
-
-QString startDiceParsingSvg(QJsonArray array)
+QString diceToMarkdown(QJsonArray array,bool withColor,bool allSameColor,bool allSameFaceCount )
{
- QString result("");
- bool highlight = true;
- DiceParser parser;
- //setAlias
- if(parser.parseLine(cmd))
+ if(allSameFaceCount)
{
- parser.start();
- if(!parser.getErrorMap().isEmpty())
- {
- result += "```markdown\n# Error:\n" + parser.humanReadableError() + "\n```";
- }
- else
+ QStringList result;
+ for(auto item : array)
{
- QList<ExportedDiceResult> list;
- bool homogeneous = true;
- parser.getLastDiceResult(list,homogeneous);
- QString listText = diceToText(list,false,homogeneous);
- QString diceText = diceToMarkdown(list,highlight,homogeneous);
- QString scalarText;
- QString str;
-
- if(parser.hasIntegerResultNotInFirst())
- {
- auto values = parser.getLastIntegerResults();
- QStringList strLst;
- for(auto val : values )
- {
- strLst << QString::number(val);
- }
- scalarText = QString("%1").arg(strLst.join(','));
- }
- else if(!list.isEmpty())
- {
- auto values = parser.getSumOfDiceResult();
- QStringList strLst;
- for(auto val : values )
- {
- strLst << QString::number(val);
- }
- scalarText = QString("%1").arg(strLst.join(','));
- }
-
- str = QString("<?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"
- "<text font-size=\"16\" x=\"0\" y=\"20\">\n"
- "<tspan fill=\"red\">%1</tspan> details:[%3 (%2)]"
- "</text>\n"
- "</svg>\n").arg(scalarText).arg(diceText).arg(parser.getDiceCommand());
-
- if(parser.hasStringResult())
- {
- bool ok;
- QStringList allStringlist = parser.getAllStringResult(ok);
- QString stringResult = allStringlist.join(" ; ");
- stringResult.replace("%1",scalarText);
- stringResult.replace("%2",listText.trimmed());
- str = stringResult;
- }
- if(!parser.getComment().isEmpty())
+ auto obj = item.toObject();
+ auto values= obj["values"].toArray();
+ for(auto val : values)
{
- str.prepend(parser.getComment()+ QStringLiteral("\n"));
+ result.append(val.toString());
}
- result += str + "\n";
}
-
+ return result.join(',');
}
else
{
- result += "markdown\n#Error:" + parser.humanReadableError() + "\n```";
- }
-
- out << result;
-}
-
-QString diceToMarkdown(QList<ExportedDiceResult>& diceList,bool highlight,bool homogeneous)
-{
- bool allSameFaceCount,allSameColor;
- auto array = diceToJson(diceList,allSameFaceCount,allSameColor);
- if(allSameColor)
- {
- if(allSameFaceCount)
+ QStringList result;
+ for(auto item : array)
{
- QStringList result;
- for(auto item : array)
+ QStringList subResult;
+ auto obj = item.toObject();
+ auto values= obj["values"].toArray();
+ for(auto val : values)
{
- auto obj = item.toObject();
- auto values= obj["values"].toArray();
- for(auto val : values)
- {
- result.append(val.toString());
- }
+ subResult.append(val.toString());
}
- return result.join(',');
- }
- else
- {
- QStringList result;
- for(auto item : array)
- {
- QStringList subResult;
- auto obj = item.toObject();
- auto values= obj["values"].toArray();
- for(auto val : values)
- {
- subResult.append(val.toString());
- }
- result.append(QStringLiteral("d%1:(").arg(obj["face"].toString()));
- result.append(subResult.join(','));
- result.append(QStringLiteral(")"));
+ result.append(QStringLiteral("d%1:(").arg(obj["face"].toString()));
+ result.append(subResult.join(','));
+ result.append(QStringLiteral(")"));
- }
- return result.join(' ');
}
- }
- else
- {
- return ImageGenerator::makeImage(array,allSameFaceCount);
+ return result.join(' ');
}
+
+
/* QStringList global;
for(auto dice : diceList)
{
@@ -334,295 +195,142 @@ QString diceToMarkdown(QList<ExportedDiceResult>& diceList,bool highlight,bool h
}
return global.join(";");*/
}
-QString colorToTermCode(QString str)
+
+void displayImage(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString comment, bool allSameFaceCount,bool allSameColor)
{
- if(str.isEmpty()|| str==QStringLiteral("black"))
- {
- return QStringLiteral("\e[0;31m");
- }
- if(str==QStringLiteral("white"))
- {
- return QStringLiteral("\e[97m");
- }
- if(str==QStringLiteral("blue"))
- {
- return QStringLiteral("\e[34m");
- }
- if(str==QStringLiteral("red"))
- {
- return QStringLiteral("\e[31m");
- }
- if(str==QStringLiteral("black"))
- {
- return QStringLiteral("\e[30m");
- }
- if(str==QStringLiteral("green"))
- {
- return QStringLiteral("\e[32m");
- }
- if(str==QStringLiteral("yellow"))
- {
- return QStringLiteral("\e[33m");
- }
- return {};
+ out << DisplayToolBox::makeImage( scalarText, resultStr, array, withColor, cmd, comment, allSameFaceCount, allSameColor);
}
-QString diceToText(QList<ExportedDiceResult>& diceList,bool highlight,bool homogeneous)
+void displayJSon(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString comment, bool allSameFaceCount,bool allSameColor)
{
- bool allSameFaceCount,allSameColor;
- auto array = diceToJson(diceList,allSameFaceCount,allSameColor);
-
- if(allSameFaceCount)
+ 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["command"]=cmd;
+ doc.setObject(obj);
+ out << doc.toJson() << "\n";
+}
+void displayMarkdown(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString comment, bool allSameFaceCount,bool allSameColor)
+{
+ Q_UNUSED(withColor);
+ QString str("```Markdown\n");
+ if(!error.isEmpty())
{
- QStringList result;
- for(auto item : array)
- {
- QStringList subResult;
- auto obj = item.toObject();
- auto values= obj["values"].toArray();
- for(auto val : values)
- {
- subResult.append(val.toString());
- }
- result.append(colorToTermCode(obj["color"].toString()));
- result.append(subResult.join(','));
- result.append(QStringLiteral("\e[0m"));
- }
- return result.join(',');
+ str.append(QStringLiteral("Error: %1\n").arg(error));
}
else
{
- QStringList result;
- for(auto item : array)
+ if(!comment.isEmpty())
{
- QStringList subResult;
- auto obj = item.toObject();
- auto values= obj["values"].toArray();
-
- for(auto val : values)
- {
- subResult.append(val.toString());
- }
- result.append(QStringLiteral("d%1:(").arg(obj["face"].toString()));
- result.append(colorToTermCode(obj["color"].toString()));
- result.append(subResult.join(','));
- result.append(QStringLiteral("\e[0m)"));
+ 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())
+ {
+ resultStr.replace("%2",diceList.trimmed());
+ str.append(QStringLiteral("%1\n").arg(resultStr));
}
- return result.join(' ');
}
- /*
- QStringList global;
- for(auto dice : diceList)
- {
- QStringList resultGlobal;
- foreach(int face, dice.keys())
- {
- QStringList result;
- ListDiceResult diceResult = dice.value(face);
- for (const HighLightDice& tmp : diceResult)
- {
- QStringList diceListStr;
- QStringList diceListChildren;
-
-
- for(int i =0; i < tmp.getResult().size(); ++i)
- {
- qint64 dievalue = tmp.getResult()[i];
- QString prefix("%1");
-
- if((tmp.isHighlighted())&&(highlight))
- {
- if(tmp.getColor().isEmpty()|| tmp.getColor()=="black")
- {
- prefix = "\e[0;31m%1\e[0m";
- }
- if(tmp.getColor()=="white")
- {
- prefix = "\e[97m%1\e[0m";
- }
- if(tmp.getColor()=="blue")
- {
- prefix = "\e[34m%1\e[0m";
- }
- if(tmp.getColor()=="red")
- {
- prefix = "\e[31m%1\e[0m";
- }
- if(tmp.getColor()=="black")
- {
- prefix = "\e[30m%1\e[0m";
- }
- if(tmp.getColor()=="green")
- {
- prefix = "\e[32m%1\e[0m";
- }
- if(tmp.getColor()=="yellow")
- {
- prefix = "\e[33m%1\e[0m";
- }
- }
-
- if(i==0)
- {
- diceListStr << prefix.arg(QString::number(dievalue));
- }
- else
- {
- diceListChildren << prefix.arg(QString::number(dievalue));
- }
- }
- if(!diceListChildren.isEmpty())
- {
- diceListStr << QString("[%1]").arg(diceListChildren.join(' '));
- }
-
- result << diceListStr.join(' ');
- // qDebug() << result << tmp.first << tmp.second;
- }
-
- if(dice.keys().size()>1)
- {
- resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face);
- }
- else
- {
- resultGlobal << result;
- }
- }
- global << resultGlobal.join(' ');
- }
- return global.join(" ; ");*/
+ str.append(QStringLiteral("```"));
+ out << str;
}
-void startDiceParsingMarkdown(QString cmd)
+void displaySVG(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString comment, bool allSameFaceCount,bool allSameColor)
{
- QString result("");
- bool highlight = true;
- DiceParser parser;
- //setAlias
- parser.insertAlias(new DiceAlias("L5R5R","L[-,⨀,⨀⬢,❂⬢,❁,❁⬢]"),0);
- parser.insertAlias(new DiceAlias("L5R5S","L[-,-,⨀,⨀,⨀❁,⨀⬢,⨀⬢,❂,❂⬢,❁,❁,❁]"),1);
-
- if(parser.parseLine(cmd))
+ 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");
+ if(!error.isEmpty())
+ {
+ str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"20\"><tspan fill=\"red\">%1</tspan></text>").arg(error));
+ }
+ else
{
- parser.start();
- if(!parser.getErrorMap().isEmpty())
+ int y = 20;
+ if(!comment.isEmpty())
{
- result += "```markdown\n# Error:\n" + parser.humanReadableError() + "\n```";
+ str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%2\"><tspan fill=\"blue\">%1</tspan></text>").arg(comment).arg(y));
+ y+=20;
}
- else
+ auto diceList = DisplayToolBox::diceToSvg(array,withColor,allSameColor,allSameFaceCount);
+ if(resultStr.isEmpty())
{
- QList<ExportedDiceResult> list;
- bool homogeneous = true;
- parser.getLastDiceResult(list,homogeneous);
- QString listText = diceToText(list,false,homogeneous);
- QString diceText = diceToMarkdown(list,highlight,homogeneous);
- QString scalarText;
- QString str;
-
- if(parser.hasIntegerResultNotInFirst())
- {
- auto values = parser.getLastIntegerResults();
- QStringList strLst;
- for(auto val : values )
- {
- strLst << QString::number(val);
- }
- scalarText = QString("%1").arg(strLst.join(','));
- }
- else if(!list.isEmpty())
- {
- auto values = parser.getSumOfDiceResult();
- QStringList strLst;
- for(auto val : values )
- {
- strLst << QString::number(val);
- }
- scalarText = QString("%1").arg(strLst.join(','));
- }
- if(homogeneous)
- {
- if(highlight)
- {
- str = QString("```markdown\n# %1\nDetails:[%3 (%2)]\n```").arg(scalarText).arg(diceText).arg(parser.getDiceCommand());
- }
- else
- {
- str = QString("```markdown\n#%1, details:[%3 (%2)]\n```").arg(scalarText).arg(diceText).arg(parser.getDiceCommand());
- }
- }
+ if(withColor)
+ str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%4\"><tspan fill=\"red\">%1</tspan> details:[%3 (%2)]</text>").arg(scalarText).arg(diceList).arg(cmd).arg(y));
else
- {
- str = QString("<?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"
- "<text font-size=\"16\" x=\"0\" y=\"20\">\n"
- "<tspan fill=\"red\">%1</tspan> details:[%3 (%2)]"
- "</text>\n"
- "</svg>\n").arg(scalarText).arg(diceText).arg(parser.getDiceCommand());
- }
- if(parser.hasStringResult())
- {
- bool ok;
- QStringList allStringlist = parser.getAllStringResult(ok);
- QString stringResult = allStringlist.join(" ; ");
- stringResult.replace("%1",scalarText);
- stringResult.replace("%2",listText.trimmed());
- str = stringResult;
- }
- if(!parser.getComment().isEmpty())
- {
- str.prepend(parser.getComment()+ QStringLiteral("\n"));
- }
- result += str + "\n";
+ str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%4\"><tspan>%1</tspan> details:[%3 (%2)]</text>").arg(scalarText).arg(diceList).arg(cmd).arg(y));
+ }
+ 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));
}
}
- else
- {
- result += "markdown\n#Error:" + parser.humanReadableError() + "\n```";
- }
- out << result;
+ str.append(QStringLiteral("</svg>\n"));
+ out << str << "\n";
}
-void displayCommandResult(DiceParser* parser, QJsonArray array, QList<ExportedDiceResult> list, bool highlight)
+
+void displayCommandResult(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString comment, bool allSameFaceCount,bool allSameColor)
{
- if(!parser->getErrorMap().isEmpty())
+ if(!error.isEmpty())
{
- out << "Error" << parser->humanReadableError() << "\n";
+ out << "Error" << error << "\n";
return;
}
- QString listText = diceToText(list,false,homogeneous);
-
- QString scalarText;
QString str;
- if(highlight)
- str = QString("Result: \e[0;31m%1\e[0m, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(parser->getDiceCommand());
+ 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(diceText).arg(parser->getDiceCommand());
+ str = QString("Result: %1 - details:[%3 (%2)]").arg(scalarText).arg(diceList).arg(cmd);
+
+ if(!resultStr.isEmpty())
+ {
+ resultStr.replace("%2",diceList.trimmed());
+ str = resultStr;
+ }
- if(!parser->getComment().isEmpty())
+ if(!comment.isEmpty())
{
- out << "\033[1m" <<parser->getComment() << "\033[0m\n";
+ out << "\033[1m" << comment << "\033[0m\n";
}
out << str << "\n";
}
-void startDiceParsing(QStringList& cmds,QString& treeFile,bool highlight, EXPORTFORMAT format)
+int startDiceParsing(QStringList& cmds,QString& treeFile,bool withColor, EXPORTFORMAT format)
{
- DiceParser* parser = new DiceParser();
+ DiceParser parser;
+ parser.insertAlias(new DiceAlias("L5R5R","L[-,⨀,⨀⬢,❂⬢,❁,❁⬢]"),0);
+ parser.insertAlias(new DiceAlias("L5R5S","L[-,-,⨀,⨀,⨀❁,⨀⬢,⨀⬢,❂,❂⬢,❁,❁,❁]"),1);
+ int rt=0;
for(QString cmd : cmds)
{
- if(parser->parseLine(cmd))
+ if(parser.parseLine(cmd))
{
- parser->start();
+ parser.start();
QList<ExportedDiceResult> list;
bool homogeneous = true;
- parser->getLastDiceResult(list,homogeneous);
+ parser.getLastDiceResult(list,homogeneous);
bool allSameFaceCount, allSameColor;
- auto array = diceToJson(diceList,allSameFaceCount,allSameColor);
+ auto array = DisplayToolBox::diceToJson(list,allSameFaceCount,allSameColor);
QString resultStr;
QString scalarText;
+ QString comment = parser.getComment();
+ QString error = parser.humanReadableError();
- if(parser->hasIntegerResultNotInFirst())
+ if(parser.hasIntegerResultNotInFirst())
{
- auto values = parser->getLastIntegerResults();
+ auto values = parser.getLastIntegerResults();
QStringList strLst;
for(auto val : values )
{
@@ -632,7 +340,7 @@ void startDiceParsing(QStringList& cmds,QString& treeFile,bool highlight, EXPORT
}
else if(!list.isEmpty())
{
- auto values = parser->getSumOfDiceResult();
+ auto values = parser.getSumOfDiceResult();
QStringList strLst;
for(auto val : values )
{
@@ -640,49 +348,73 @@ void startDiceParsing(QStringList& cmds,QString& treeFile,bool highlight, EXPORT
}
scalarText = QString("%1").arg(strLst.join(','));
}
- if(parser->hasStringResult())
+
+ if(parser.hasStringResult())
{
bool ok;
- QStringList allStringlist = parser->getAllStringResult(ok);
+ QStringList allStringlist = parser.getAllStringResult(ok);
QString stringResult = allStringlist.join(" ; ");
stringResult.replace("%1",scalarText);
- stringResult.replace("%2",diceText.trimmed());
+
resultStr = stringResult;
}
+ if(format == BOT)
+ {
+ if(allSameColor)
+ {
+ format = MARKDOWN;
+ }
+ else
+ {
+ format = IMAGE;
+ }
+ if(!error.isEmpty())
+ {
+ format = MARKDOWN;
+ }
+ }
switch(format)
{
case TERMINAL:
- displayCommandResult(parser, array, list, highlight);
+ displayCommandResult(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor);
break;
case SVG:
-
+ displaySVG(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor);
break;
case MARKDOWN:
-
+ displayMarkdown(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor);
break;
case JSON:
-
+ displayJSon(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor);
break;
- case UNKNOWN:
-
+ case IMAGE:
+ displayImage(scalarText, resultStr, array, withColor, cmd, comment, allSameFaceCount, allSameColor);
break;
}
if(!treeFile.isEmpty())
{
- parser->writeDownDotTree(treeFile);
+ parser.writeDownDotTree(treeFile);
+ }
+
+ if(!error.isEmpty())
+ {
+ rt = 1;
}
}
else
{
- out << parser->humanReadableError() << "\n";
+ rt = 1;
}
}
- delete parser;
+
+ return rt;
}
#include <QTextCodec>
int main(int argc, char *argv[])
{
+ QGuiApplication a(argc, argv);
+
QStringList commands;
QString cmd;
QString dotFileStr;
@@ -749,7 +481,7 @@ int main(int argc, char *argv[])
}
else if(optionParser.isSet(bot))
{
- format = UNKNOWN;
+ format = BOT;
}
else if(optionParser.isSet(svg))
{
@@ -770,10 +502,10 @@ int main(int argc, char *argv[])
{
aliasstr = optionParser.value(alias);
}
- startDiceParsing(cmdList,dotFileStr,colorb,format);
+ returnValue = startDiceParsing(cmdList,dotFileStr,colorb,format);
if(optionParser.isSet(help))
{
out << optionParser.helpText();
}
- return 0;
+ return returnValue;
}
diff --git a/diceparser.cpp b/diceparser.cpp
index 4337c46..b4dba75 100644
--- a/diceparser.cpp
+++ b/diceparser.cpp
@@ -158,25 +158,6 @@ bool DiceParser::parseLine(QString str)
m_currentTreeHasSeparator=false;
str = convertAlias(str);
m_command = str;
- /*StartingNode* start = new StartingNode();
- m_startNodes.push_back(start);
- ExecutionNode* newNode = nullptr;
- m_current = start;
-
- bool keepParsing = readExpression(str,newNode);
-
- if(keepParsing)
- {
- m_current->setNextNode(newNode);
- m_current = ParsingToolBox::getLatestNode(m_current);
- keepParsing =!str.isEmpty();
- if(keepParsing)
- {
- // keepParsing =
- readOperator(str,m_current);
- m_current = ParsingToolBox::getLatestNode(m_current);
- }
- }*/
bool hasInstruction = readInstructionList(str);
if((m_errorMap.isEmpty())&&(hasInstruction))
@@ -815,6 +796,7 @@ bool DiceParser::readInstructionList(QString& str)
if(str.isEmpty())
return false;
+ bool hasInstruction = false;
bool readInstruction = true;
while(readInstruction)
{
@@ -822,6 +804,7 @@ bool DiceParser::readInstructionList(QString& str)
bool keepParsing = readExpression(str,startNode);
if(nullptr != startNode)
{
+ hasInstruction = true;
m_startNodes.push_back(startNode);
auto latest = startNode;
if(keepParsing)
@@ -834,7 +817,6 @@ bool DiceParser::readInstructionList(QString& str)
latest = ParsingToolBox::getLatestNode(latest);
}
}
-
if( !str.isEmpty() && readInstructionOperator(str[0]))
{
str=str.remove(0,1);
@@ -856,7 +838,7 @@ bool DiceParser::readInstructionList(QString& str)
readInstruction = false;
}
}
- return true;
+ return hasInstruction;
}
bool DiceParser::readOperator(QString& str,ExecutionNode* previous)