FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
with_restored_io.cpp
浏览该文件的文档.
1// 版权所有 2022 Arthur Sonzogni。保留所有权利。
2// 本源代码的使用受 MIT 许可协议的约束,该协议可在
3// the LICENSE 文件中找到。
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
15int main() {
16 using namespace ftxui;
17
18 auto screen = ScreenInteractive::Fullscreen();
19
20 // 按下此按钮时,"screen.WithRestoredIO" 将暂时卸载终端钩子并执行提供的回调
21 // 函数。这允许应用程序以非交互模式运行。 auto btn_run = Button("Execute with restored IO", screen.WithRestoredIO([] {
22 std::cout << "This is a child program using stdin/stdout." << std::endl;
23 for (int i = 0; i < 10; ++i) {
24 std::cout << "Please enter 10 strings (" << i << "/10)" << std::flush;
25 std::string input;
26 std::getline(std::cin, input);
27 }
28 }));
29
30 auto btn_quit = Button("Quit", screen.ExitLoopClosure());
31
32 auto layout = Container::Horizontal({
33 btn_run,
35 });
36
37 auto renderer = Renderer(layout, [&] {
38 auto explanation = paragraph(
39 "单击此按钮后,ScreenInteractive 将被 "
40 "暂停,并且对 stdin/stdout 的访问将暂时 "
41 "恢复以运行函数。");
42 auto element = vbox({
43 explanation | borderEmpty,
44 hbox({
45 btn_run->Render(),
46 filler(),
47 btn_quit->Render(),
48 }),
49 });
50
51 element = element | borderEmpty | border | size(WIDTH, LESS_THAN, 80) |
52 size(HEIGHT, LESS_THAN, 20) | center;
53 return element;
54 });
55
56 screen.Loop(renderer);
58}
Element borderEmpty(Element child)
在元素周围绘制一个空边框。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
return EXIT_SUCCESS