30#ifndef CLI_CLISESSION_H_
31#define CLI_CLISESSION_H_
42#include "detail/history.h"
43#include "detail/split.h"
53 CliSession(
Cli& _cli, std::ostream& _out, std::size_t historySize = 100);
61 void Feed(
const std::string& cmd);
66 void Help(
const std::string& commandName)
const;
80 auto cmds = history.GetCommands();
81 cli.StoreCommands(cmds);
86 void EnterAction(
const std::function<
void(std::ostream&)>& action)
91 void ExitAction(
const std::function<
void(std::ostream&)>& action)
100 history.ForgetLatest();
101 const auto cmd = history.At(index);
107 return history.Previous(line);
112 return history.Next();
115 std::vector<std::string>
GetCompletions(std::string currentLine)
const;
122 std::shared_ptr<cli::OutStream> coutPtr;
124 std::unique_ptr<Menu> globalScopeMenu;
126 std::function<void(std::ostream&)> enterAction = [](std::ostream&)
noexcept {};
127 std::function<void(std::ostream&)> exitAction = [](std::ostream&)
noexcept {};
128 detail::History history;
134 coutPtr(
Cli::CoutPtr()),
135 current(
cli.RootMenu()),
136 globalScopeMenu(std::make_unique<
Menu>()),
140 history.LoadCommands(
cli.GetCommands());
142 coutPtr->Register(out);
143 globalScopeMenu->Insert(
144 cli.GetMessages().helpName,
145 {cli.GetMessages().helpArgName},
146 [
this](std::ostream&,
const std::vector<std::string>& args)
150 else if (args.size() == 1)
153 out << cli.GetMessages().helpUsage <<
'\n';
155 cli.GetMessages().helpCommand
157 globalScopeMenu->Insert(
158 cli.GetMessages().exitName,
159 [
this](std::ostream&) { Exit(); },
160 cli.GetMessages().exitCommand
162 globalScopeMenu->Insert(
163 cli.GetMessages().historyName,
164 [
this](std::ostream&) { ShowHistory(); },
165 cli.GetMessages().historyCommand
167 globalScopeMenu->Insert(
169 {cli.GetMessages().historyIndexArg},
170 [
this](std::ostream&,
unsigned cmdIndex) { ExecFromHistory(cmdIndex); },
171 cli.GetMessages().historyExecCommand
177 std::vector<std::string> strs;
178 detail::split(strs, cmd);
179 if (strs.empty())
return;
181 history.NewCommand(cmd);
185 bool found = globalScopeMenu->ScanCmds(strs, *
this);
186 if (!found) found = current->ScanCmds(strs, *
this);
188 cli.WrongCommandHandler(out, cmd);
190 catch (
const std::exception& e)
192 cli.StdExceptionHandler(out, cmd, e);
196 out <<
cli.GetMessages().unknownException << cmd <<
"\"\n";
212 detail::HelpGroup global, local;
213 globalScopeMenu->MainHelp(global);
214 current->MainHelp(local);
220 #pragma GCC diagnostic push
221 #pragma GCC diagnostic ignored "-Wstrict-overflow"
224 std::stable_sort(local.begin(), local.end(),
225 [](
const detail::HelpEntry& a,
const detail::HelpEntry& b) { return a.name < b.name; });
228 #pragma GCC diagnostic pop
231 detail::HelpGroup commands, menus;
232 for (
auto& e : local)
233 (e.isMenu ? menus : commands).push_back(std::move(e));
235 out <<
cli.GetMessages().helpHeader;
236 detail::PrintHelp(out, {global, commands, menus},
TerminalWidth());
241 detail::HelpGroup matches;
242 auto appendMatches = [&](
const detail::HelpGroup& entries)
244 for (
const auto& entry : entries)
245 if (entry.name == commandName)
246 matches.push_back(entry);
249 detail::HelpGroup global, local;
250 globalScopeMenu->MainHelp(global);
251 current->MainHelp(local);
252 appendMatches(global);
253 appendMatches(local);
257 out <<
cli.GetMessages().commandNotFound << commandName <<
'\n';
266 currentLine.erase(currentLine.begin(), std::find_if(currentLine.begin(), currentLine.end(), [](
int ch) { return !std::isspace(ch); }));
267 auto v1 = globalScopeMenu->GetCompletions(currentLine);
268 auto v3 = current->GetCompletions(currentLine);
269 v1.insert(v1.end(), std::make_move_iterator(v3.begin()), std::make_move_iterator(v3.end()));
271 std::sort(v1.begin(), v1.end());
272 auto ip = std::unique(v1.begin(), v1.end());
273 v1.resize(
static_cast<std::size_t
>(std::distance(v1.begin(), ip)));
278 template <
typename F,
typename ... Args>
282 const std::size_t paramSize =
sizeof...(Args);
283 if (cmdLine.size() != paramSize + 1)
return false;
284 if (
Name() == cmdLine[0])
288 auto g = [&](
auto ... pars) { func(session.
OutStream(), pars...); };
291 catch (std::bad_cast&)
300 template <
typename F,
typename ... Args>
306 e.params = parameterDesc;
307 if (e.params.empty())
309 e.description = description;
310 entries.push_back(std::move(e));
313 template <
typename F>
317 assert(!cmdLine.empty());
318 if (
Name() == cmdLine[0])
320 func(session.
OutStream(), std::vector<std::string>(std::next(cmdLine.begin()), cmdLine.end()));
326 template <
typename F>
332 e.params = parameterDesc;
333 if (e.params.empty())
335 e.description = description;
336 entries.push_back(std::move(e));
339 inline bool Menu::HandleCommand(
const std::vector<std::string>& cmdNames,
const std::vector<std::string>& cmdLine,
CliSession& session)
344 assert(!cmdLine.empty());
346 if (std::find(cmdNames.begin(), cmdNames.end(), cmdLine[0]) != cmdNames.end())
348 if (cmdLine.size() == 1)
355 std::vector<std::string> subCmdLine(cmdLine.begin() + 1, cmdLine.end());
356 for (
auto& cmd : *cmds)
357 if (cmd->Exec(subCmdLine, session))
return true;
358 return (parent && parent->
ExecParent(subCmdLine, session));
373 coutPtr->UnRegister(out);
CliSession(Cli &_cli, std::ostream &_out, std::size_t historySize=100)
CliSession & operator=(const CliSession &)=delete
std::ostream & OutStream()
virtual std::size_t TerminalWidth() const
CliSession(CliSession &&)=delete
CliSession & operator=(CliSession &&)=delete
void ExecFromHistory(unsigned index)
void EnterAction(const std::function< void(std::ostream &)> &action)
void Feed(const std::string &cmd)
std::string PreviousCmd(const std::string &line)
std::vector< std::string > GetCompletions(std::string currentLine) const
virtual ~CliSession() noexcept
void ExitAction(const std::function< void(std::ostream &)> &action)
CliSession(const CliSession &)=delete
const std::string & Name() const
bool Exec(const std::vector< std::string > &cmdLine, CliSession &session) override
void Help(detail::HelpGroup &entries) const override