FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
examples/dom/graph.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 <chrono> // for operator""s, chrono_literals
5#include <cmath> // for sin
6#include <ftxui/dom/elements.hpp> // for graph, operator|, separator, color, Element, vbox, flex, inverted, operator|=, Fit, hbox, size, border, GREATER_THAN, HEIGHT
7#include <ftxui/screen/screen.hpp> // for Full, Screen
8#include <functional> // for ref, reference_wrapper
9#include <iostream> // for cout, ostream
10#include <memory> // for shared_ptr
11#include <string> // for operator<<, string
12#include <thread> // for sleep_for
13#include <utility> // for ignore
14#include <vector> // for vector
15
16#include "ftxui/dom/node.hpp" // for Render
17#include "ftxui/screen/color.hpp" // for Color, Color::BlueLight, Color::RedLight, Color::YellowLight, ftxui
18
19class Graph {
20 public:
21 std::vector<int> operator()(int width, int height) const {
22 std::vector<int> output(width);
23 for (int i = 0; i < width; ++i) {
24 float v = 0;
25 v += 0.1f * sin((i + shift) * 0.1f); // NOLINT
26 v += 0.2f * sin((i + shift + 10) * 0.15f); // NOLINT
27 v += 0.1f * sin((i + shift) * 0.03f); // NOLINT
28 v *= height; // NOLINT
29 v += 0.5f * height; // NOLINT
30 output[i] = static_cast<int>(v);
31 }
32 return output;
33 }
34 int shift = 0;
35};
36
37std::vector<int> triangle(int width, int height) {
38 std::vector<int> output(width);
39 for (int i = 0; i < width; ++i) {
40 output[i] = i % (height - 4) + 2;
41 }
42 return output;
43}
44
45int main() {
46 using namespace ftxui;
47 using namespace std::chrono_literals;
48
49 Graph my_graph;
50
51 std::string reset_position;
52 for (int i = 0;; ++i) {
53 std::ignore = i;
54 auto document = hbox({
55 vbox({
56 graph(std::ref(my_graph)),
57 separator(),
58 graph(triangle) | inverted,
59 }) | flex,
60 separator(),
61 vbox({
62 graph(std::ref(my_graph)) | color(Color::BlueLight),
63 separator(),
64 graph(std::ref(my_graph)) | color(Color::RedLight),
65 separator(),
66 graph(std::ref(my_graph)) | color(Color::YellowLight),
67 }) | flex,
68 });
69
70 document |= border;
71
72 const int min_width = 40;
73 document |= size(HEIGHT, GREATER_THAN, min_width);
74
75 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
76 Render(screen, document);
77 std::cout << reset_position;
78 screen.Print();
79 reset_position = screen.ResetPosition();
80
81 const auto sleep_time = 0.03s;
82 std::this_thread::sleep_for(sleep_time);
83 my_graph.shift++;
84 }
85
86 return 0;
87}
std::vector< int > triangle(int width, int height)
int main()
The FTXUI ftxui:: namespace.
Definition animation.hpp:10