From 5af5ada4d2eaeb5ab0c1598ad7d3d8dfaa620d45 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 3 Mar 2017 04:56:50 +0100 Subject: [PATCH] refs #894, commands can be registered for help --- src/blackmisc/simplecommandparser.cpp | 50 +++++++++++++++++++++++++++ src/blackmisc/simplecommandparser.h | 30 ++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/blackmisc/simplecommandparser.cpp b/src/blackmisc/simplecommandparser.cpp index 71fef6878..511dcd913 100644 --- a/src/blackmisc/simplecommandparser.cpp +++ b/src/blackmisc/simplecommandparser.cpp @@ -19,6 +19,9 @@ using namespace BlackMisc::PhysicalQuantities; namespace BlackMisc { + QList CSimpleCommandParser::s_commands = QList(); + QSet CSimpleCommandParser::s_registered = QSet(); + CSimpleCommandParser::CSimpleCommandParser(const QStringList &knownCommands) { this->setCheckedCommandList(knownCommands); @@ -130,6 +133,53 @@ namespace BlackMisc return (p.length() == toMatch.length() && p.startsWith(toMatch, cs)); } + void CSimpleCommandParser::registerCommand(const CSimpleCommandParser::CommandHtmlHelp &command) + { + for (const CommandHtmlHelp &help : as_const(CSimpleCommandParser::s_commands)) + { + // avoid duplicates + if (help.command == command.command) { return; } + } + CSimpleCommandParser::s_commands.append(command); + } + + bool CSimpleCommandParser::registered(const QString &helpContext) + { + if (CSimpleCommandParser::s_registered.contains(helpContext)) { return true; }; + CSimpleCommandParser::s_registered.insert(helpContext); + return false; + } + + QString CSimpleCommandParser::commandsHtmlHelp() + { + if (CSimpleCommandParser::s_commands.isEmpty()) { return ""; } + + static const QString html("\n%1\n
"); + static const QString row("%1%2"); + + QString rows; + QList cmds(CSimpleCommandParser::s_commands); + qSort(cmds.begin(), cmds.end(), CommandHtmlHelp::less); + for (int i = 0; i < cmds.size(); i++) + { + CommandHtmlHelp help = cmds[i]; + rows += ""; + rows += row.arg(help.command, help.help); + i++; + if (i < cmds.size()) + { + help = cmds[i]; + rows += row.arg(help.command, help.help); + } + else + { + rows += row.arg("", ""); + } + rows += "\n"; + } + return html.arg(rows); + } + QString CSimpleCommandParser::removeLeadingDot(const QString &candidate) { if (!candidate.startsWith('.')) { return candidate; } diff --git a/src/blackmisc/simplecommandparser.h b/src/blackmisc/simplecommandparser.h index 03a95cfc6..e402e275b 100644 --- a/src/blackmisc/simplecommandparser.h +++ b/src/blackmisc/simplecommandparser.h @@ -76,6 +76,33 @@ namespace BlackMisc //! Matches given part bool matchesPart(int index, const QString &toMatch, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const; + //! Help info + struct CommandHtmlHelp + { + QString command; //!< command + QString help; //!< help text + + //! Constructor + CommandHtmlHelp(const QString &command, const QString &help) : command(command), help(help) + { } + + //! Compare by command + static bool less(const CommandHtmlHelp &a, const CommandHtmlHelp &b) + { + // may want to check that the pointers aren't zero... + return a.command < b.command; + } + }; + + //! Register a command + static void registerCommand(const CommandHtmlHelp &command); + + //! Help already registered + static bool registered(const QString &helpContext); + + //! HTML commans HELP + static QString commandsHtmlHelp(); + private: QString m_originalLine; //!< line as entered by user QString m_cleanedLine; //!< trimmed, no double spaces etc. @@ -84,6 +111,9 @@ namespace BlackMisc QStringList m_knownCommands; //!< known / handled commands bool m_knownCommand = false; //!< known command + static QList s_commands; //!< all registered commands + static QSet s_registered; //!< registered commands + //! Avoid wrong usage void setCheckedCommandList(const QStringList &commands);