diff options
| author | 2018-04-09 10:49:59 +0200 | |
|---|---|---|
| committer | 2018-04-09 10:49:59 +0200 | |
| commit | 58ac8a893a96b5a2034cfd0635b397c66070c0d3 (patch) | |
| tree | af51dca11c8b1bc0ee2173965d3929bce45e760f | |
| parent | 65b549f5675f16b7045e7fe55a08f759dc8fd25c (diff) | |
| parent | 0ca59c3eb254d5d2650948e3f72662678290d633 (diff) | |
| download | OneRoll-58ac8a893a96b5a2034cfd0635b397c66070c0d3.tar.gz OneRoll-58ac8a893a96b5a2034cfd0635b397c66070c0d3.zip | |
Merge pull request #9 from robinmoussu/diceToText
Fix diceToText function() when multiple commands are involved
| -rw-r--r-- | cli/displaytoolbox.cpp | 108 |
1 files changed, 48 insertions, 60 deletions
diff --git a/cli/displaytoolbox.cpp b/cli/displaytoolbox.cpp index 0aef6a7..8a2a681 100644 --- a/cli/displaytoolbox.cpp +++ b/cli/displaytoolbox.cpp @@ -170,30 +170,34 @@ QString DisplayToolBox::colorToTermCode(QString str) { return QStringLiteral("\e[0;31m"); } - if(str==QStringLiteral("white")) + else if(str==QStringLiteral("white")) { return QStringLiteral("\e[97m"); } - if(str==QStringLiteral("blue")) + else if(str==QStringLiteral("blue")) { return QStringLiteral("\e[34m"); } - if(str==QStringLiteral("red")) + else if(str==QStringLiteral("red")) { return QStringLiteral("\e[31m"); } - if(str==QStringLiteral("black")) + else if(str==QStringLiteral("black")) { return QStringLiteral("\e[30m"); } - if(str==QStringLiteral("green")) + else if(str==QStringLiteral("green")) { return QStringLiteral("\e[32m"); } - if(str==QStringLiteral("yellow")) + else if(str==QStringLiteral("yellow")) { return QStringLiteral("\e[33m"); } + else if(str==QStringLiteral("reset")) + { + return QStringLiteral("\e[0m"); + } return {}; } @@ -342,59 +346,43 @@ QString DisplayToolBox::diceResultToString(QJsonObject val) 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(); + QStringList result; + for(auto item : array) + { + QString 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(' '); - } + QStringList diceResult; + for(auto valRef : values) + { + diceResult += diceResultToString(valRef.toObject()); + } + if (!diceResult.isEmpty()) + { + if(!allSameFaceCount) + { + subResult += QStringLiteral("d%1:(").arg(obj["face"].toString()); + } + 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(" - "); } |