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; }