49 registry.insert(std::pair<std::string, ICommand *>(command->
key(), command));
79 static std::string cache =
"";
80 static uint8_t cache_count = 0;
88 std::stringstream stream;
91 std::vector<entry> entries;
95 [&entries, &size](std::pair <std::string, ICommand *> e)
100 const std::string &raw = e.second->usage ();
102 char *temp =
new char[raw.size ()+1];
104 strncpy (temp, raw.c_str (), raw.size ())[raw.size ()] = 0;
106 std::vector <char *> lines;
108 char *saveptr = NULL;
109 for (
char *p = strtok_r (temp,
"\n", &saveptr); p != NULL; p = strtok_r (NULL,
"\n", &saveptr))
114 std::for_each (lines.begin (), lines.end (), [&entries, &size](
char *line)
118 char *saveptr = NULL;
119 char *p = strtok_r (line,
":", &saveptr);
120 e.cmd = std::string (p);
122 p = strtok_r (NULL,
":", &saveptr);
123 e.help = std::string (p);
125 entries.push_back (e);
127 if (size < e.cmd.size ())
129 size = e.cmd.size ();
141 stream <<
"================================================" << std::endl;
142 stream <<
"HAN-FUN Example Application : v" << HF_VERSION << std::endl;
143 stream <<
"================================================" << std::endl << std::endl;
145 stream << std::setfill(
' ');
147 std::for_each (entries.begin (), entries.end (), [&stream, size](entry &e)
149 stream << std::left << std::setw (size) << e.cmd <<
"\t: " << e.help << std::endl;
153 stream << std::left << std::setw(size) <<
"h/?" <<
"\t: " <<
"this help menu." 154 << std::endl << std::endl;
155 stream <<
"Select an Option (Q/q to exit): " << std::endl;
157 cache = stream.str();
177 LOG(ERROR) <<
"Command '" <<
key <<
"' not found !" <<
NL;
182 it->second->run(args);
199 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
201 if (command.empty() || (command.size() == 1 && command[0] ==
'\n'))
207 std::istringstream buf(command);
208 std::istream_iterator<std::string> beg(buf), end;
210 std::vector<std::string> tokens(beg, end);
212 std::string cmd = *tokens.begin();
214 std::vector<std::string> args(tokens.begin() + 1, tokens.end());
216 if (cmd ==
"q" || cmd ==
"Q")
220 else if (cmd ==
"h" || cmd ==
"?")
230 LOG(APP) <<
"> " << std::flush;
bool Handle(std::string command)
Handle the command.
This file contains the definitions for the HAN-FUN example applications.
static void add(ICommand *command)
Add a command to the registry.
Example application menu entry API.
static std::map< std::string, ICommand * > registry
Command registry.
virtual void run(std::vector< std::string > &args)=0
Execute the command code.
#define NL
Helper define for new-line and stream clear.
static void remove(ICommand *command)
Remove a command to the registry.
virtual const std::string & key() const =0
Get the command key.
This file contains the definitions for the common functionality in the HAN-FUN example applications...
#define LOG(X)
Log messages with the level given by X.
static std::ostream & help(std::ostream &stream)
Generate the help screen, based on the commands in the registry.