Skip to content

Commit

Permalink
feat: support EnterAction (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dup4 authored Jan 17, 2023
1 parent 3e6fecf commit e508a70
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
31 changes: 31 additions & 0 deletions include/cli/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,18 @@ namespace cli
Cli(std::unique_ptr<Menu> _rootMenu, std::unique_ptr<HistoryStorage> historyStorage = std::make_unique<VolatileHistoryStorage>()) :
globalHistoryStorage(std::move(historyStorage)),
rootMenu(std::move(_rootMenu)),
enterAction{},
exitAction{}
{
}

/**
* @brief Add a global enter action that is called every time a session (local or remote) is established.
*
* @param action the function to be called when a session exits, taking a @c std::ostream& parameter to write on that session console.
*/
void EnterAction(const std::function< void(std::ostream&)>& action) { enterAction = action; }

/**
* @brief Add a global exit action that is called every time a session (local or remote) gets the "exit" command.
*
Expand Down Expand Up @@ -184,6 +192,13 @@ namespace cli

Menu* RootMenu() { return rootMenu.get(); }

void EnterAction(std::ostream& out)
{
if (enterAction) {
enterAction(out);
}
}

void ExitAction( std::ostream& out )
{
if ( exitAction )
Expand Down Expand Up @@ -211,6 +226,7 @@ namespace cli
private:
std::unique_ptr<HistoryStorage> globalHistoryStorage;
std::unique_ptr<Menu> rootMenu; // just to keep it alive
std::function<void(std::ostream&)> enterAction;
std::function<void(std::ostream&)> exitAction;
std::function<void(std::ostream&, const std::string& cmd, const std::exception& )> exceptionHandler;
};
Expand Down Expand Up @@ -298,6 +314,15 @@ namespace cli

void Help() const;

void Enter()
{
cli.EnterAction(out);

if (enterAction) {
enterAction(out);
}
}

void Exit()
{
exitAction(out);
Expand All @@ -309,6 +334,11 @@ namespace cli
exit = true; // prevent the prompt to be shown
}

void EnterAction(const std::function<void(std::ostream&)>& action)
{
enterAction = action;
}

void ExitAction(const std::function<void(std::ostream&)>& action)
{
exitAction = action;
Expand All @@ -335,6 +365,7 @@ namespace cli
Menu* current;
std::unique_ptr<Menu> globalScopeMenu;
std::ostream& out;
std::function< void(std::ostream&)> enterAction = []( std::ostream& ){};
std::function< void(std::ostream&)> exitAction = []( std::ostream& ){};
detail::History history;
bool exit{ false }; // to prevent the prompt after exit command
Expand Down
2 changes: 2 additions & 0 deletions include/cli/clifilesession.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class CliFileSession : public CliSession
}
void Start()
{
Enter();

while(!exit)
{
Prompt();
Expand Down
1 change: 1 addition & 0 deletions include/cli/clilocalsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class CliLocalTerminalSession : public CliSession
kb(scheduler),
ih(*this, kb)
{
Enter();
Prompt();
}

Expand Down
8 changes: 8 additions & 0 deletions include/cli/detail/genericasioremotecli.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ class CliTelnetSession : public InputDevice, public TelnetSession, public CliSes
void OnConnect() override
{
TelnetSession::OnConnect();
Enter();
Prompt();
}

Expand Down Expand Up @@ -559,6 +560,12 @@ class CliGenericTelnetServer : public Server<ASIOLIB>
cli(_cli),
historySize(_historySize)
{}

void EnterAction(std::function< void(std::ostream&)> action)
{
enterAction = action;
}

void ExitAction( std::function< void(std::ostream&)> action )
{
exitAction = action;
Expand All @@ -570,6 +577,7 @@ class CliGenericTelnetServer : public Server<ASIOLIB>
private:
Scheduler& scheduler;
Cli& cli;
std::function< void(std::ostream&)> enterAction;
std::function< void(std::ostream&)> exitAction;
std::size_t historySize;
};
Expand Down
23 changes: 23 additions & 0 deletions test/test_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,29 @@ BOOST_AUTO_TEST_CASE(Submenus)
BOOST_CHECK_EQUAL(ExtractContent(oss), "foo");
}

BOOST_AUTO_TEST_CASE(EnterActions)
{
auto rootMenu = make_unique<Menu>("cli");
rootMenu->Insert("int_cmd",
[](ostream &out, int par) { out << par << "\n"; },
"int_cmd help", {"int_par"});
rootMenu->Insert(
"string_cmd",
[](ostream &out, const string &par) { out << par << "\n"; },
"string_cmd help", {"string_par"});

Cli cli(std::move(rootMenu));
bool enterActionDone = false;

cli.EnterAction(
[&enterActionDone](std::ostream &) { enterActionDone = true; });

stringstream oss;

UserInput(cli, oss, "exit");
BOOST_CHECK(enterActionDone);
}

BOOST_AUTO_TEST_CASE(ExitActions)
{
auto rootMenu = make_unique<Menu>("cli");
Expand Down

0 comments on commit e508a70

Please sign in to comment.