FTXUI  5.0.0
C++ functional terminal UI.
examples/dom/graph.cpp
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <chrono> // for operator""s, chrono_literals
#include <cmath> // for sin
#include <ftxui/dom/elements.hpp> // for graph, operator|, separator, color, Element, vbox, flex, inverted, operator|=, Fit, hbox, size, border, GREATER_THAN, HEIGHT
#include <ftxui/screen/screen.hpp> // for Full, Screen
#include <functional> // for ref, reference_wrapper
#include <iostream> // for cout, ostream
#include <memory> // for shared_ptr
#include <string> // for operator<<, string
#include <thread> // for sleep_for
#include <vector> // for vector
#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/color.hpp" // for Color, Color::BlueLight, Color::RedLight, Color::YellowLight, ftxui
class Graph {
public:
std::vector<int> operator()(int width, int height) const {
std::vector<int> output(width);
for (int i = 0; i < width; ++i) {
float v = 0;
v += 0.1f * sin((i + shift) * 0.1f); // NOLINT
v += 0.2f * sin((i + shift + 10) * 0.15f); // NOLINT
v += 0.1f * sin((i + shift) * 0.03f); // NOLINT
v *= height; // NOLINT
v += 0.5f * height; // NOLINT
output[i] = static_cast<int>(v);
}
return output;
}
int shift = 0;
};
std::vector<int> triangle(int width, int height) {
std::vector<int> output(width);
for (int i = 0; i < width; ++i) {
output[i] = i % (height - 4) + 2;
}
return output;
}
int main() {
using namespace ftxui;
using namespace std::chrono_literals;
Graph my_graph;
std::string reset_position;
for (int i = 0;; ++i) {
auto document = hbox({
vbox({
graph(std::ref(my_graph)),
graph(triangle) | inverted,
}) | flex,
vbox({
graph(std::ref(my_graph)) | color(Color::BlueLight),
graph(std::ref(my_graph)) | color(Color::RedLight),
graph(std::ref(my_graph)) | color(Color::YellowLight),
}) | flex,
});
document |= border;
const int min_width = 40;
document |= size(HEIGHT, GREATER_THAN, min_width);
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document);
std::cout << reset_position;
screen.Print();
reset_position = screen.ResetPosition();
const auto sleep_time = 0.03s;
std::this_thread::sleep_for(sleep_time);
my_graph.shift++;
}
return 0;
}
@ YellowLight
Definition: color.hpp:63
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition: screen.cpp:388
Dimensions Fit(Element &)
Definition: util.cpp:93
Dimensions Full()
Definition: screen.cpp:376
@ HEIGHT
Definition: elements.hpp:147
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:89
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definition: flex.cpp:122
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition: hbox.cpp:83
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition: inverted.cpp:34
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:134
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47
@ GREATER_THAN
Definition: elements.hpp:148
Element graph(GraphFunction)
Draw a graph using a GraphFunction.
Definition: graph.cpp:71
Element border(Element)
Draw a border around the element.
Definition: border.cpp:228
Decorator color(Color)
Decorate using a foreground color.
Definition: color.cpp:110
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition: vbox.cpp:83