FTXUI  3.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
1#include <algorithm> // for min
2#include <functional> // for function
3#include <memory> // for __shared_ptr_access, make_unique
4#include <utility> // for move
5#include <vector> // for vector
6
7#include "ftxui/dom/elements.hpp" // for Element, Decorator, Elements, operator|, Fit, emptyElement, nothing, operator|=
8#include "ftxui/dom/node.hpp" // for Node, Node::Status
9#include "ftxui/dom/requirement.hpp" // for Requirement
10#include "ftxui/screen/box.hpp" // for Box
11#include "ftxui/screen/screen.hpp" // for Full
12#include "ftxui/screen/terminal.hpp" // for Dimensions
13
14namespace ftxui {
15
16namespace {
17Decorator compose(Decorator a, Decorator b) {
18 return [a = std::move(a), b = std::move(b)](Element element) {
19 return b(a(std::move(element)));
20 };
21}
22} // namespace
23
24/// @brief A decoration doing absolutely nothing.
25/// @ingroup dom
27 return element;
28}
29
30/// @brief Compose two decorator into one.
31/// @ingroup dom
32///
33/// ### Example
34///
35/// ```cpp
36/// auto decorator = bold | blink;
37/// ```
39 return compose(std::move(a), //
40 std::move(b));
41}
42
43/// @brief From a set of element, apply a decorator to every elements.
44/// @return the set of decorated element.
45/// @ingroup dom
46Elements operator|(Elements elements, Decorator decorator) { // NOLINT
47 Elements output;
48 for (auto& it : elements) {
49 output.push_back(std::move(it) | decorator);
50 }
51 return output;
52}
53
54/// @brief From an element, apply a decorator.
55/// @return the decorated element.
56/// @ingroup dom
57///
58/// ### Example
59///
60/// Both of these are equivalent:
61/// ```cpp
62/// bold(text("Hello"));
63/// ```
64/// ```cpp
65/// text("Hello") | bold;
66/// ```
67Element operator|(Element element, Decorator decorator) { // NOLINT
68 return decorator(std::move(element));
69}
70
71/// @brief Apply a decorator to an element.
72/// @return the decorated element.
73/// @ingroup dom
74///
75/// ### Example
76///
77/// Both of these are equivalent:
78/// ```cpp
79/// auto element = text("Hello");
80/// element |= bold;
81/// ```
83 e = e | std::move(d);
84 return e;
85}
86
87/// The minimal dimension that will fit the given element.
88/// @see Fixed
89/// @see Full
90Dimensions Dimension::Fit(Element& e) {
91 Dimensions fullsize = Dimension::Full();
92 Box box;
93 box.x_min = 0;
94 box.y_min = 0;
95 box.x_max = fullsize.dimx;
96 box.y_max = fullsize.dimy;
97
98 Node::Status status;
99 e->Check(&status);
100 const int max_iteration = 20;
101 while (status.need_iteration && status.iteration < max_iteration) {
102 e->ComputeRequirement();
103
104 // Don't give the element more space than it needs:
105 box.x_max = std::min(box.x_max, e->requirement().min_x);
106 box.y_max = std::min(box.y_max, e->requirement().min_y);
107
108 e->SetBox(box);
109 status.need_iteration = false;
110 status.iteration++;
111 e->Check(&status);
112
113 if (!status.need_iteration) {
114 break;
115 }
116 // Increase the size of the box until it fits, but not more than the with of
117 // the terminal emulator:
118 box.x_max = std::min(e->requirement().min_x, fullsize.dimx);
119 box.y_max = std::min(e->requirement().min_y, fullsize.dimy);
120 }
121
122 return {
123 box.x_max,
124 box.y_max,
125 };
126}
127
128/// An element of size 0x0 drawing nothing.
129/// @ingroup dom
131 class Impl : public Node {
132 void ComputeRequirement() override {
133 requirement_.min_x = 0;
134 requirement_.min_x = 0;
135 }
136 };
137 return std::make_unique<Impl>();
138}
139
140} // namespace ftxui
141
142// Copyright 2020 Arthur Sonzogni. All rights reserved.
143// Use of this source code is governed by the MIT license that can be found in
144// the LICENSE file.
Dimensions Fit(Element &)
Dimensions Full()
std::function< Element(Element)> Decorator
Definition elements.hpp:20
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:26
std::shared_ptr< Node > Element
Definition elements.hpp:18
Element emptyElement()
Definition util.cpp:130
std::vector< Element > Elements
Definition elements.hpp:19
Component operator|(Component component, ComponentDecorator decorator)
Definition util.cpp:9
Component & operator|=(Component &component, ComponentDecorator decorator)
Definition util.cpp:19