Streamline generation of additional help

- do filtering outside CmdHelp.cpp
- generate only concepts
- use join from libshared

Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
This commit is contained in:
Thomas Lauf 2020-03-01 19:01:06 +01:00
parent f1cf29d0ec
commit 888e715782
2 changed files with 12 additions and 65 deletions

View file

@ -24,69 +24,16 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <commands.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <FS.h>
#include <sstream>
#include <shared.h>
#include "additional-help.h"
template <typename T>
std::string join (const T& strings, size_t indent = 0, size_t max_width = 0)
{
std::stringstream sstr;
auto it = std::begin (strings);
auto end = std::end (strings);
size_t cur = indent + 2; // account for added '{ '
std::string part;
const std::string sep (" | ");
while (it != end)
{
part = *it;
++it;
// Make sure we leave room for closing " }" or " |"
if ((max_width > 0) && (cur + part.length () + sep.length ()) > max_width)
{
sstr << "\n" << std::string (indent + 1, ' ');
cur = indent + 1;
sstr << part;
}
else
{
sstr << part;
}
cur += part.length ();
if (it != end)
{
sstr << sep;
cur += sep.length ();
}
}
return sstr.str ();
}
////////////////////////////////////////////////////////////////////////////////
int CmdHelpUsage (const Extensions& extensions)
{
std::vector <const char *> concepts;
// Any commands will take precedence when using `timew help` and therefore,
// we'll exclude any commands that are also concepts.
std::set_difference (std::begin (documented_concepts), std::end (documented_concepts),
std::begin (documented_commands), std::end (documented_commands),
std::back_inserter (concepts),
[] (const char *a, const char *b)
{
return std::strcmp (a, b) < 0;
});
std::cout << '\n'
<< "Usage: timew [--version]\n"
<< " timew annotate @<id> [@<id> ...] <annotation>\n"
@ -100,7 +47,7 @@ int CmdHelpUsage (const Extensions& extensions)
<< " timew extensions\n"
<< " timew gaps [<interval>] [<tag> ...]\n"
<< " timew get <DOM> [<DOM> ...]\n"
<< " timew help [<command> | " << join (concepts) << "]\n"
<< " timew help [<command> | " << join ( " | ", timew_help_concepts) << "]\n"
<< " timew join @<id> @<id>\n"
<< " timew lengthen @<id> [@<id> ...] <duration>\n"
<< " timew modify (start|end) @<id> <date>\n"
@ -135,7 +82,8 @@ int CmdHelpUsage (const Extensions& extensions)
std::cout << "Additional help:\n"
<< " timew help <command>\n";
for (auto concept : concepts)
for (auto& concept : timew_help_concepts)
{
std::cout << " timew help " << concept << '\n';
}

View file

@ -1,14 +1,13 @@
#!/bin/sh
echo "// Generated file. Do not modify"
echo "static const char * documented_concepts[] = {"
for command in $(/bin/ls doc/man7/*.in | /bin/sed -n 's|doc/man7/timew-\(\w\+\).7.in|\1|p' | /usr/bin/sort); do
echo " \"${command}\","
done
echo "};"
CONCEPTS="$( ls doc/man7/*.in | sed -E 's|doc/man7/timew-(.*).7.in|\1|' | sort )"
COMMANDS="$( ls doc/man1/*.in | sed -E 's|doc/man1/timew-(.*).1.in|\1|' | sort | tr '\n' ' ' )"
echo "static const char * documented_commands[] = {"
for command in $(/bin/ls doc/man1/*.in | /bin/sed -n 's|doc/man1/timew-\(\w\+\).1.in|\1|p' | /usr/bin/sort); do
echo " \"${command}\","
echo "// Generated file. Do not modify"
echo "#include <vector>"
echo "#include <string>"
echo "static const std::vector <std::string> timew_help_concepts = {"
for concept in ${CONCEPTS} ; do
( echo "${COMMANDS}" | grep -vq "${concept}" ) && echo " \"${concept}\","
done
echo "};"