FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
custom_loop.cpp
Go to the documentation of this file.
1// Copyright 2020 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 <stdlib.h> // for EXIT_SUCCESS
5#include <chrono> // for milliseconds
6#include <ftxui/component/event.hpp> // for Event
7#include <ftxui/component/mouse.hpp> // for ftxui
8#include <ftxui/dom/elements.hpp> // for text, separator, Element, operator|, vbox, border
9#include <memory> // for allocator, shared_ptr
10#include <string> // for operator+, to_string
11#include <thread> // for sleep_for
12
13#include "ftxui/component/component.hpp" // for CatchEvent, Renderer, operator|=
14#include "ftxui/component/loop.hpp" // for Loop
15#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
16
17int main() {
18 using namespace ftxui;
19 auto screen = ScreenInteractive::FitComponent();
20
21 // Create a component counting the number of frames drawn and event handled.
22 int custom_loop_count = 0;
23 int frame_count = 0;
24 int event_count = 0;
25 auto component = Renderer([&] {
26 frame_count++;
27 return vbox({
28 text("This demonstrates using a custom ftxui::Loop. It "),
29 text("runs at 100 iterations per seconds. The FTXUI events "),
30 text("are all processed once per iteration and a new frame "),
31 text("is rendered as needed"),
32 separator(),
33 text("ftxui event count: " + std::to_string(event_count)),
34 text("ftxui frame count: " + std::to_string(frame_count)),
35 text("Custom loop count: " + std::to_string(custom_loop_count)),
36 }) |
37 border;
38 });
39
40 component |= CatchEvent([&](Event) -> bool {
41 event_count++;
42 return false;
43 });
44
45 Loop loop(&screen, component);
46
47 while (!loop.HasQuitted()) {
48 custom_loop_count++;
49 loop.RunOnce();
50 std::this_thread::sleep_for(std::chrono::milliseconds(10));
51 }
52
53 return EXIT_SUCCESS;
54}
int main()
Loop is a class that manages the event loop for a component.
Definition loop.hpp:56
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
The FTXUI ftxui:: namespace.
Definition animation.hpp:10