FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
with_restored_io.cpp
Go to the documentation of this file.
1// Copyright 2022 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <cstdlib> // for system, EXIT_SUCCESS
5#include <iostream> // for operator<<, basic_ostream, basic_ostream::operator<<, cout, endl, flush, ostream, basic_ostream<>::__ostream_type, cin
6#include <memory> // for shared_ptr, __shared_ptr_access, allocator
7#include <string> // for getline, string
8
9#include "ftxui/component/captured_mouse.hpp" // for ftxui
10#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
11#include "ftxui/component/component_base.hpp" // for ComponentBase
12#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
13#include "ftxui/dom/elements.hpp" // for operator|, filler, Element, borderEmpty, hbox, size, paragraph, vbox, LESS_THAN, border, center, HEIGHT, WIDTH
14
15 int main() {
16 using namespace ftxui;
17
18 auto screen = ScreenInteractive::Fullscreen();
19
20 // When pressing this button, "screen.WithRestoredIO" will execute the
21 // temporarily uninstall the terminal hook and execute the provided callback
22 // function. This allow running the application in a non-interactive mode.
23 auto btn_run = Button("Execute with restored IO", screen.WithRestoredIO([] {
24 std::cout << "This is a child program using stdin/stdout." << std::endl;
25 for (int i = 0; i < 10; ++i) {
26 std::cout << "Please enter 10 strings (" << i << "/10)" << std::flush;
27 std::string input;
28 std::getline(std::cin, input);
29 }
30 }));
31
32 auto btn_quit = Button("Quit", screen.ExitLoopClosure());
33
34 auto layout = Container::Horizontal({
35 btn_run,
36 btn_quit,
37 });
38
39 auto renderer = Renderer(layout, [&] {
40 auto explanation = paragraph(
41 "After clicking this button, the ScreenInteractive will be "
42 "suspended and access to stdin/stdout will temporarilly be "
43 "restore for running a function.");
44 auto element = vbox({
45 explanation | borderEmpty,
46 hbox({
47 btn_run->Render(),
48 filler(),
49 btn_quit->Render(),
50 }),
51 });
52
53 element = element | borderEmpty | border | size(WIDTH, LESS_THAN, 80) |
54 size(HEIGHT, LESS_THAN, 20) | center;
55 return element;
56 });
57
58 screen.Loop(renderer);
59 return EXIT_SUCCESS;
60}
captured_mouse.hpp
component.hpp
component_base.hpp
elements.hpp
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
screen_interactive.hpp
main
int main()
Definition with_restored_io.cpp:15