FTXUI  0.11.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
4#include <utility> // for move
5#include <vector> // for vector
6
7#include "ftxui/dom/elements.hpp" // for Element, Decorator, Elements, operator|, Fit, nothing
8#include "ftxui/dom/node.hpp" // for Node
9#include "ftxui/dom/requirement.hpp" // for Requirement
10#include "ftxui/screen/screen.hpp" // for Full
11#include "ftxui/screen/terminal.hpp" // for Dimensions
12
13namespace ftxui {
14
15namespace {
16Decorator compose(Decorator a, Decorator b) {
17 return [a = std::move(a), b = std::move(b)](Element element) {
18 return b(a(std::move(element)));
19 };
20}
21} // namespace
22
23/// @brief A decoration doing absolutely nothing.
24/// @ingroup dom
26 return element;
27}
28
29/// @brief Compose two decorator into one.
30/// @ingroup dom
31///
32/// ### Example
33///
34/// ```cpp
35/// auto decorator = bold | blink;
36/// ```
38 return compose(a, b);
39}
40
41/// @brief From a set of element, apply a decorator to every elements.
42/// @return the set of decorated element.
43/// @ingroup dom
44Elements operator|(Elements elements, Decorator decorator) {
45 Elements output;
46 for (auto& it : elements)
47 output.push_back(std::move(it) | decorator);
48 return output;
49}
50
51/// @brief From an element, apply a decorator.
52/// @return the decorated element.
53/// @ingroup dom
54///
55/// ### Example
56///
57/// Both of these are equivalent:
58/// ```cpp
59/// bold(text("Hello"));
60/// ```
61/// ```cpp
62/// text("Hello") | bold;
63/// ```
64Element operator|(Element element, Decorator decorator) {
65 return decorator(std::move(element));
66}
67
68/// The minimal dimension that will fit the given element.
69/// @see Fixed
70/// @see Full
71Dimensions Dimension::Fit(Element& e) {
72 e->ComputeRequirement();
73 Dimensions size = Dimension::Full();
74 return {std::min(e->requirement().min_x, size.dimx),
75 std::min(e->requirement().min_y, size.dimy)};
76}
77
78/// An element of size 0x0 drawing nothing.
79/// @ingroup dom
81 class Impl : public Node {
82 void ComputeRequirement() override {
83 requirement_.min_x = 0;
84 requirement_.min_x = 0;
85 }
86 };
87 return std::make_unique<Impl>();
88}
89
90} // namespace ftxui
91
92// Copyright 2020 Arthur Sonzogni. All rights reserved.
93// Use of this source code is governed by the MIT license that can be found in
94// the LICENSE file.
Dimensions Fit(Element &)
Dimensions Full()
std::function< Element(Element)> Decorator
Definition elements.hpp:17
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:15
Element emptyElement()
Definition util.cpp:80
std::vector< Element > Elements
Definition elements.hpp:16
Element operator|(Element, Decorator)
From an element, apply a decorator.
Definition util.cpp:64
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:86