add CtrlC and CtrlZ events handling

This commit is contained in:
ohyzha
2024-12-17 11:45:29 +02:00
parent 387522ba51
commit 36c3bb3668

View File

@@ -11,6 +11,7 @@
#include <ftxui/component/component.hpp>
#include <ftxui/component/component_options.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/component/event.hpp> // for Event, Event::Custom
#include <vector>
#include <string>
@@ -28,9 +29,21 @@ int main(int argc, char** argv)
int selectedExample = 0;
ftxui::MenuOption option;
auto screen = ftxui::ScreenInteractive::TerminalOutput();
screen.ForceHandleCtrlC(true);
//screen.ForceHandleCtrlZ(true);
option.on_enter = screen.ExitLoopClosure();
auto menu = ftxui::Menu(&examples, &selectedExample, option);
menu |= ftxui::CatchEvent(
[&](ftxui::Event event)
{
if (event == ftxui::Event::CtrlC || event == ftxui::Event::CtrlZ)
{
screen.ExitLoopClosure()();
exit(0);
}
return false;
});
screen.Loop(menu);
if (selectedExample >= examples.size()) throw std::runtime_error("Invalid menu selection!");