FTXUI  0.8.1
C++ functional terminal UI.
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 
13 namespace ftxui {
14 
15 namespace {
16 Decorator 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
44 Elements 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 /// ```
64 Element 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
72  e->ComputeRequirement();
74  return {std::min(e->requirement().min_x, size.dimx),
75  std::min(e->requirement().min_y, size.dimy)};
76 }
77 
78 } // namespace ftxui
79 
80 // Copyright 2020 Arthur Sonzogni. All rights reserved.
81 // Use of this source code is governed by the MIT license that can be found in
82 // the LICENSE file.
ftxui
Definition: captured_mouse.hpp:6
ftxui::Dimensions
Definition: terminal.hpp:5
node.hpp
ftxui::nothing
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition: util.cpp:25
requirement.hpp
terminal.hpp
ftxui::Elements
std::vector< Element > Elements
Definition: elements.hpp:16
elements.hpp
ftxui::operator|
Element operator|(Element, Decorator)
From an element, apply a decorator.
Definition: util.cpp:64
ftxui::Dimension::Fit
Dimensions Fit(Element &)
Definition: util.cpp:71
ftxui::Element
std::shared_ptr< Node > Element
Definition: elements.hpp:15
ftxui::size
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:86
ftxui::Dimension::Full
Dimensions Full()
Definition: screen.cpp:103
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition: elements.hpp:17
screen.hpp