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
15int main() {
16 using namespace ftxui;
17
18 auto screen = ScreenInteractive::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 "點擊此按鈕後,ScreenInteractive 將會被暫停,並暫時恢復對 stdin/stdout 的訪問,以執行一個函數。");
41 auto element = vbox({
42 explanation | borderEmpty,
43 hbox({
44 btn_run->Render(),
45 filler(),
46 btn_quit->Render(),
47 }),
48 });
49
50 element = element | borderEmpty | border | size(WIDTH, LESS_THAN, 80) |
51 size(HEIGHT, LESS_THAN, 20) | center;
52 return element;
53 });
54
55 screen.Loop(renderer);
56 return EXIT_SUCCESS;
57}
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
int main()