FTXUI 7.0.3
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/app.hpp" // for App
10#include "ftxui/component/captured_mouse.hpp" // for ftxui
11#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/dom/elements.hpp" // for operator|, filler, Element, borderEmpty, hbox, size, paragraph, vbox, LESS_THAN, border, center, HEIGHT, WIDTH
14
15int main() {
16 using namespace ftxui;
17
18 auto screen = App::Fullscreen();
19
20 // 當按下此按鈕時,"screen.WithRestoredIO" 將暫時卸載終端鉤子並執行所提供的回調函數。
21 // 這允許應用程式以非互動模式運行。
22 auto btn_run = Button("Execute with restored IO", screen.WithRestoredIO([] {
23 std::cout << "This is a child program using stdin/stdout." << std::endl;
24 for (int i = 0; i < 10; ++i) {
25 std::cout << "Please enter 10 strings (" << i << "/10)" << std::flush;
26 std::string input;
27 std::getline(std::cin, input);
28 }
29 }));
30
31 auto btn_quit = Button("Quit", screen.ExitLoopClosure());
32
33 auto layout = Container::Horizontal({
34 btn_run,
35 btn_quit,
36 });
37
38 auto renderer = Renderer(layout, [&] {
39 auto explanation = paragraph(
40 "After clicking this button, the App will be "
41 "suspended and access to stdin/stdout will temporarily be "
42 "restore for running a function.");
43 auto element = vbox({
44 explanation | borderEmpty,
45 hbox({
46 btn_run->Render(),
47 filler(),
48 btn_quit->Render(),
49 }),
50 });
51
52 element = element | borderEmpty | border | size(WIDTH, LESS_THAN, 80) |
53 size(HEIGHT, LESS_THAN, 20) | center;
54 return element;
55 });
56
57 screen.Loop(renderer);
58 return EXIT_SUCCESS;
59}
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
const Element & element
Definition node.hpp:92
int main()