refs #894, commands can be registered for help

This commit is contained in:
Klaus Basan
2017-03-03 04:56:50 +01:00
committed by Mathew Sutcliffe
parent 188669501b
commit 5af5ada4d2
2 changed files with 80 additions and 0 deletions

View File

@@ -19,6 +19,9 @@ using namespace BlackMisc::PhysicalQuantities;
namespace BlackMisc
{
QList<CSimpleCommandParser::CommandHtmlHelp> CSimpleCommandParser::s_commands = QList<CSimpleCommandParser::CommandHtmlHelp>();
QSet<QString> CSimpleCommandParser::s_registered = QSet<QString>();
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("<table style=\"font-size: 8pt; white-space: nowrap;\">\n%1\n</table>");
static const QString row("<td>%1</td><td>%2</td>");
QString rows;
QList<CommandHtmlHelp> cmds(CSimpleCommandParser::s_commands);
qSort(cmds.begin(), cmds.end(), CommandHtmlHelp::less);
for (int i = 0; i < cmds.size(); i++)
{
CommandHtmlHelp help = cmds[i];
rows += "<tr>";
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 += "</tr>\n";
}
return html.arg(rows);
}
QString CSimpleCommandParser::removeLeadingDot(const QString &candidate)
{
if (!candidate.startsWith('.')) { return candidate; }

View File

@@ -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<CommandHtmlHelp> s_commands; //!< all registered commands
static QSet<QString> s_registered; //!< registered commands
//! Avoid wrong usage
void setCheckedCommandList(const QStringList &commands);