daniele77 / cli

A library for interactive command line interfaces in modern C++
Boost Software License 1.0
1.18k stars 139 forks source link

`CliLocalSession` waits for keypress when exiting #209

Open marcogmaia opened 1 year ago

marcogmaia commented 1 year ago

I'm running the software on Window10.

The following snippet reproduces the behavior:

int main() {
  std::atexit([]() { std::cout << "Exiting from main."; });

  auto cli = cli::Cli(std::make_unique<cli::Menu>("cli"));
  cli.ExitAction([](auto &out) { out << "Waiting for extra keypress...\n"; });

  auto scheduler = cli::LoopScheduler();
  auto session = cli::CliLocalSession(cli, scheduler, std::cout, 400);
  session.ExitAction([&scheduler](auto &out) {
    scheduler.Stop();
    out << "Stopping scheduler...\n";
  });

  scheduler.Run();

  return 0;
}

produces the following behavior when exiting

cli> exit 
Stopping scheduler...
Waiting for extra keypress...

but the expected is:

cli> exit 
Stopping scheduler...
Waiting for extra keypress...
Exiting from main.
daniele77 commented 1 year ago

Hi @marcogmaia ,

first of all, thank you very much for using this library.

I tried your code in the following environment:

In all three cases, I've got the expected output:

c:\Progetti\github\esempi_cli\cli_example\x64\Release>cli_example.exe
cli> exit
Stopping scheduler...
Waiting for extra keypress...
Exiting from main.
c:\Progetti\github\esempi_cli\cli_example\x64\Release>

(no extra keypress needed, by the way: I've just entered "exit" followed by key).

The complete code is:

#include <cli/loopscheduler.h>
#include "cli/clilocalsession.h"
#include "cli/cli.h"

int main() {
    std::atexit([]() { std::cout << "Exiting from main."; });

    auto cli = cli::Cli(std::make_unique<cli::Menu>("cli"));
    cli.ExitAction([](auto& out) { out << "Waiting for extra keypress...\n"; });

    auto scheduler = cli::LoopScheduler();
    auto session = cli::CliLocalSession(cli, scheduler, std::cout, 400);
    session.ExitAction([&scheduler](auto& out) {
        scheduler.Stop();
        out << "Stopping scheduler...\n";
    });

    scheduler.Run();

    return 0;
}

using C++20 standard and the latest version of cli library (i.e., v. 2.1).

Please, let me know what differs from your environment.

marcogmaia commented 1 year ago

@daniele77, thanks man. I'll test with your configs when I get the time. I compiled with clang on windows.

marcogmaia commented 1 year ago

It seems to correctly work with PowerShell, the issue that I was experiencing was with some integrated terminals, like the VSCode integrated terminal and Windows terminal, but if the application is used in a standalone mode it works correctly. I've tested with msvc2022 and clang17.

marcogmaia commented 1 year ago

https://github.com/daniele77/cli/assets/13483472/ed4b9c5d-5893-4f57-8ce4-58f45b39e352

An example using the code that you've provided.