FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
custom_loop.cpp
浏览该文件的文档.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// 本源代码的使用受MIT许可证的约束,该许可证可在LICENSE文件中找到。
3#include <stdlib.h> // for EXIT_SUCCESS
4#include <chrono> // for milliseconds
5#include <ftxui/component/event.hpp> // for Event
6#include <ftxui/component/mouse.hpp> // for ftxui
7#include <ftxui/dom/elements.hpp> // for text, separator, Element, operator|, vbox, border
8#include <memory> // for allocator, shared_ptr
9#include <string> // for operator+, to_string
10#include <thread> // for sleep_for
11
12#include "ftxui/component/component.hpp" // for CatchEvent, Renderer, operator|=
13#include "ftxui/component/loop.hpp" // for Loop
14#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
15
16int main() {
17 using namespace ftxui;
18 auto screen = ScreenInteractive::FitComponent();
19
20 // 创建一个组件,用于计算绘制的帧数和处理的事件数。
21 int custom_loop_count = 0;
22 int frame_count = 0;
23 int event_count = 0;
24 auto component = Renderer([&] {
25 frame_count++;
26 return vbox({
27 text("This demonstrates using a custom ftxui::Loop. It "),
28 text("runs at 100 iterations per seconds. The FTXUI events "),
29 text("are all processed once per iteration and a new frame "),
30 text("is rendered as needed"),
31 separator(),
32 text("ftxui event count: " + std::to_string(event_count)),
33 text("ftxui frame count: " + std::to_string(frame_count)),
34 text("Custom loop count: " + std::to_string(custom_loop_count)),
35 }) |
36 border;
37 });
38
39 component |= CatchEvent([&](Event) -> bool {
40 event_count++;
41 return false;
42 });
43
44 Loop loop(&screen, component);
45
46 while (!loop.HasQuitted()) {
47 custom_loop_count++;
48 loop.RunOnce();
49 std::this_thread::sleep_for(std::chrono::milliseconds(10));
50 }
51
52 return EXIT_SUCCESS;
53}
int main()
Loop 是一个管理组件事件循环的类。
代表一个事件。它可以是按键事件、终端大小调整等等...
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
return EXIT_SUCCESS