diff options
| author | 2022-02-23 02:51:23 +0100 | |
|---|---|---|
| committer | 2022-03-01 03:41:41 +0000 | |
| commit | c31f3d09f2f886147b6f1fef605075d3be1cf5a6 (patch) | |
| tree | 90b4bb71c16ff22a9a24013aa1b9a8f3324ebb0e /cli | |
| parent | b8f1bc738f3abbab3565a39f88a950c049248517 (diff) | |
| download | OneRoll-c31f3d09f2f886147b6f1fef605075d3be1cf5a6.tar.gz OneRoll-c31f3d09f2f886147b6f1fef605075d3be1cf5a6.zip | |
Add C parameter to change the highlight color.
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/displaytoolbox.cpp | 27 | ||||
| -rw-r--r-- | cli/displaytoolbox.h | 1 | ||||
| -rw-r--r-- | cli/main.cpp | 36 |
3 files changed, 45 insertions, 19 deletions
diff --git a/cli/displaytoolbox.cpp b/cli/displaytoolbox.cpp index 0c527e0..1709d18 100644 --- a/cli/displaytoolbox.cpp +++ b/cli/displaytoolbox.cpp @@ -34,43 +34,52 @@ QString DisplayToolBox::makeImage(QByteArray svgCode) } #endif -QString DisplayToolBox::colorToTermCode(QString str) +QString DisplayToolBox::colorToIntCode(QString str) { if(str.isEmpty() || str == QStringLiteral("black")) { - return QStringLiteral("\e[0;31m"); + return QStringLiteral("0;31"); } else if(str == QStringLiteral("white")) { - return QStringLiteral("\e[97m"); + return QStringLiteral("97"); } else if(str == QStringLiteral("blue")) { - return QStringLiteral("\e[34m"); + return QStringLiteral("34"); } else if(str == QStringLiteral("red")) { - return QStringLiteral("\e[31m"); + return QStringLiteral("31"); } else if(str == QStringLiteral("black")) { - return QStringLiteral("\e[30m"); + return QStringLiteral("30"); } else if(str == QStringLiteral("green")) { - return QStringLiteral("\e[32m"); + return QStringLiteral("32"); } else if(str == QStringLiteral("yellow")) { - return QStringLiteral("\e[33m"); + return QStringLiteral("33"); + } + else if(str == QStringLiteral("cyan")) + { + return QStringLiteral("36"); } else if(str == QStringLiteral("reset")) { - return QStringLiteral("\e[0m"); + return QStringLiteral("0"); } return {}; } +QString DisplayToolBox::colorToTermCode(QString str) +{ + return QStringLiteral("\e[").append(DisplayToolBox::colorToIntCode(str)).append("m"); +} + QString DisplayToolBox::diceToSvg(QJsonArray array, bool withColor, bool allSameColor, bool allSameFaceCount) { Q_UNUSED(allSameColor) diff --git a/cli/displaytoolbox.h b/cli/displaytoolbox.h index 11f92c3..53a2a56 100644 --- a/cli/displaytoolbox.h +++ b/cli/displaytoolbox.h @@ -20,6 +20,7 @@ public: #ifdef PAINTER_OP static QString makeImage(QByteArray svgCode); #endif + static QString colorToIntCode(QString str); static QString colorToTermCode(QString str); 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 c506417..8c298eb 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -1,6 +1,6 @@ /*************************************************************************** * Copyright (C) 2014 by Renaud Guezennec * - * http://www.rolisteam.org/contact * + * http://www.rolisteam.org/contact * * * * This file is part of DiceParser * * * @@ -28,7 +28,9 @@ #include <QJsonDocument> #include <QJsonObject> #include <QRegularExpression> +#include <QSettings> #include <QStringList> +#include <QTextCodec> #include <QTextStream> #ifdef PAINTER_OP @@ -59,6 +61,7 @@ QTextStream out(stdout, QIODevice::WriteOnly); QTextStream err(stderr, QIODevice::WriteOnly); bool markdown= false; +constexpr char const* colorkey= {"dicecolor"}; #ifdef PAINTER_OP enum EXPORTFORMAT { @@ -237,7 +240,7 @@ void displayImage(QString json, bool withColor) } #endif -void displayCommandResult(QString json, bool withColor) +void displayCommandResult(QString json, bool withColor, QString color) { QJsonDocument doc= QJsonDocument::fromJson(json.toUtf8()); auto obj= doc.object(); @@ -274,7 +277,8 @@ void displayCommandResult(QString json, bool withColor) QString str; if(withColor) - str= QString("Result: \e[0;31m%1\e[0m - details:[%3 (%2)]").arg(scalarText, diceList, cmd); + str= QString("Result: \e[0;%4;1m%1\e[0m - details:[%3 (%2)]") + .arg(scalarText, diceList, cmd, DisplayToolBox::colorToIntCode(color)); else str= QString("Result: %1 - details:[%3 (%2)]").arg(scalarText, diceList, cmd); @@ -294,7 +298,8 @@ void displayCommandResult(QString json, bool withColor) out << str << "\n"; } -int startDiceParsing(QStringList& cmds, bool withColor, EXPORTFORMAT format, QJsonArray array, const QString& filePath) +int startDiceParsing(QStringList& cmds, bool withColor, QString baseColor, EXPORTFORMAT format, QJsonArray array, + const QString& filePath) { DiceParser parser; parser.insertAlias(new DiceAlias("L5R5R", QStringLiteral("L[-,⨀,⨀⬢,❂⬢,❁,❁⬢]")), 0); @@ -370,10 +375,10 @@ int startDiceParsing(QStringList& cmds, bool withColor, EXPORTFORMAT format, QJs allSameColor= true; QString colorP; json= parser.resultAsJSon( - [&colorP, &allSameColor](const QString& result, const QString& color, bool hightlight) { + [&colorP, &allSameColor, &baseColor](const QString& result, const QString& color, bool hightlight) { auto trueColor= color; if(color.isEmpty()) - trueColor= "red"; + trueColor= baseColor; if(colorP.isEmpty()) colorP= trueColor; @@ -418,7 +423,7 @@ int startDiceParsing(QStringList& cmds, bool withColor, EXPORTFORMAT format, QJs switch(format) { case TERMINAL: - displayCommandResult(json, withColor); + displayCommandResult(json, withColor, baseColor); break; case SVG: out << displaySVG(json, withColor) << "\n"; @@ -428,7 +433,7 @@ int startDiceParsing(QStringList& cmds, bool withColor, EXPORTFORMAT format, QJs displayMarkdown(json); break; case TEXT: - displayCommandResult(json, false); + displayCommandResult(json, false, baseColor); break; case JSON: displayJSon(json); @@ -449,7 +454,7 @@ int startDiceParsing(QStringList& cmds, bool withColor, EXPORTFORMAT format, QJs } return rt; } -#include <QTextCodec> + int main(int argc, char* argv[]) { #ifdef PAINTER_OP @@ -463,6 +468,8 @@ int main(int argc, char* argv[]) QString dotFileStr; QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); bool colorb= true; + QSettings settings("rolisteam", "diceparser"); + QString formatColor; out.setCodec("UTF-8"); EXPORTFORMAT format= TERMINAL; @@ -492,6 +499,9 @@ int main(int argc, char* argv[]) QCommandLineOption svg(QStringList() << "g" << "svg", "The output is formatted in svg."); + QCommandLineOption outColor(QStringList() << "C" + << "color", + "Use color for result: <color>", "color"); QCommandLineOption json(QStringList() << "j" << "json", "The output is formatted in json."); @@ -521,6 +531,7 @@ int main(int argc, char* argv[]) optionParser.addOption(markdown); optionParser.addOption(bot); optionParser.addOption(svg); + optionParser.addOption(outColor); optionParser.addOption(json); optionParser.addOption(translation); optionParser.addOption(help); @@ -572,6 +583,10 @@ int main(int argc, char* argv[]) { format= TEXT; } + if(optionParser.isSet(outColor)) + { + settings.setValue(colorkey, optionParser.value(outColor)); + } if(optionParser.isSet(help)) { @@ -599,7 +614,8 @@ int main(int argc, char* argv[]) aliases= doc.array(); } - returnValue= startDiceParsing(cmdList, colorb, format, aliases, dotFileStr); + formatColor= settings.value(colorkey, "red").value<QString>(); + returnValue= startDiceParsing(cmdList, colorb, formatColor, format, aliases, dotFileStr); if(optionParser.isSet(help)) { out << optionParser.helpText(); |