FTXUI  0.8.1
C++ functional terminal UI.
border.cpp
Go to the documentation of this file.
1 #include <algorithm> // for max
2 #include <iterator> // for begin, end
3 #include <memory> // for allocator, make_shared, __shared_ptr_access
4 #include <string> // for basic_string, string
5 #include <utility> // for move
6 #include <vector> // for vector, __alloc_traits<>::value_type
7 
8 #include "ftxui/dom/elements.hpp" // for unpack, Element, Decorator, Elements, border, borderWith, window
9 #include "ftxui/dom/node.hpp" // for Node
10 #include "ftxui/dom/requirement.hpp" // for Requirement
11 #include "ftxui/screen/box.hpp" // for Box
12 #include "ftxui/screen/screen.hpp" // for Pixel, Screen
13 
14 namespace ftxui {
15 
16 static std::string simple_border_charset[] = {"╭", "╮", "╰", "╯", "─",
17  "│", "┬", "┴", "┤", "├"};
18 
19 // For reference, here is the charset for normal border:
20 // {"┌", "┐", "└", "┘", "─", "│", "┬", "┴", "┤", "├"};
21 // TODO(arthursonzogni): Consider adding options to choose the kind of borders
22 // to use.
23 
24 class Border : public Node {
25  public:
26  Border(Elements children)
27  : Node(std::move(children)),
28  charset(std::begin(simple_border_charset),
29  std::end(simple_border_charset)) {}
30  Border(Elements children, Pixel pixel)
31  : Node(std::move(children)), charset_pixel(10, pixel) {}
32 
33  std::vector<Pixel> charset_pixel;
34  std::vector<std::string> charset;
35 
36  void ComputeRequirement() override {
38  requirement_ = children_[0]->requirement();
39  requirement_.min_x += 2;
40  requirement_.min_y += 2;
41  if (children_.size() == 2) {
43  std::max(requirement_.min_x, children_[1]->requirement().min_x + 2);
44  }
49  }
50 
51  void SetBox(Box box) override {
52  Node::SetBox(box);
53  if (children_.size() == 2) {
54  Box title_box;
55  title_box.x_min = box.x_min + 1;
56  title_box.x_max = box.x_max - 1;
57  title_box.y_min = box.y_min;
58  title_box.y_max = box.y_min;
59  children_[1]->SetBox(title_box);
60  }
61  box.x_min++;
62  box.x_max--;
63  box.y_min++;
64  box.y_max--;
65  children_[0]->SetBox(box);
66  }
67 
68  void Render(Screen& screen) override {
69  // Draw content.
70  children_[0]->Render(screen);
71 
72  // Draw the border.
73  if (box_.x_min >= box_.x_max || box_.y_min >= box_.y_max)
74  return;
75 
76  if (!charset.empty())
77  RenderPixel(screen);
78  else
79  RenderChar(screen);
80  }
81 
82  void RenderPixel(Screen& screen) {
83  screen.at(box_.x_min, box_.y_min) = charset[0];
84  screen.at(box_.x_max, box_.y_min) = charset[1];
85  screen.at(box_.x_min, box_.y_max) = charset[2];
86  screen.at(box_.x_max, box_.y_max) = charset[3];
87  for (float x = box_.x_min + 1; x < box_.x_max; ++x) {
88  screen.at(x, box_.y_min) = charset[4];
89  screen.at(x, box_.y_max) = charset[4];
90  }
91  for (float y = box_.y_min + 1; y < box_.y_max; ++y) {
92  screen.at(box_.x_min, y) = charset[5];
93  screen.at(box_.x_max, y) = charset[5];
94  }
95 
96  // Draw title.
97  if (children_.size() == 2)
98  children_[1]->Render(screen);
99  }
100 
101  void RenderChar(Screen& screen) {
102  screen.PixelAt(box_.x_min, box_.y_min) = charset_pixel[0];
103  screen.PixelAt(box_.x_max, box_.y_min) = charset_pixel[1];
104  screen.PixelAt(box_.x_min, box_.y_max) = charset_pixel[2];
105  screen.PixelAt(box_.x_max, box_.y_max) = charset_pixel[3];
106  for (float x = box_.x_min + 1; x < box_.x_max; ++x) {
107  screen.PixelAt(x, box_.y_min) = charset_pixel[4];
108  screen.PixelAt(x, box_.y_max) = charset_pixel[4];
109  }
110  for (float y = box_.y_min + 1; y < box_.y_max; ++y) {
111  screen.PixelAt(box_.x_min, y) = charset_pixel[5];
112  screen.PixelAt(box_.x_max, y) = charset_pixel[5];
113  }
114  }
115 };
116 /// @brief Draw a border around the element.
117 /// @ingroup dom
118 ///
119 /// Add a border around an element
120 ///
121 /// ### Example
122 ///
123 /// ```cpp
124 /// // Use 'border' as a function...
125 /// Element document = border(text("The element"));
126 ///
127 /// // ...Or as a 'pipe'.
128 /// Element document = text("The element") | border;
129 /// ```
130 ///
131 /// ### Output
132 ///
133 /// ```bash
134 /// ┌───────────┐
135 /// │The element│
136 /// └───────────┘
137 /// ```
139  return std::make_shared<Border>(unpack(std::move(child)));
140 }
141 
142 /// @brief Draw window with a title and a border around the element.
143 /// @param title The title of the window.
144 /// @param content The element to be wrapped.
145 /// @ingroup dom
146 /// @see border
147 ///
148 /// ### Example
149 ///
150 /// ```cpp
151 /// Element document = window(text("Title"),
152 /// text("content")
153 /// );
154 /// ```
155 ///
156 /// ### Output
157 ///
158 /// ```bash
159 /// ┌Title──┐
160 /// │content│
161 /// └───────┘
162 /// ```
163 Element window(Element title, Element content) {
164  return std::make_shared<Border>(unpack(std::move(content), std::move(title)));
165 }
166 
167 /// @brief Same as border but with a constant Pixel around the element.
168 /// @ingroup dom
169 /// @see border
171  return [pixel](Element child) {
172  return std::make_shared<Border>(unpack(std::move(child)), pixel);
173  };
174 }
175 
176 } // namespace ftxui
177 
178 // Copyright 2020 Arthur Sonzogni. All rights reserved.
179 // Use of this source code is governed by the MIT license that can be found in
180 // the LICENSE file.
ftxui::border
Element border(Element)
Draw a border around the element.
Definition: border.cpp:138
ftxui::Requirement::selected_box
Box selected_box
Definition: requirement.hpp:26
ftxui::Node::SetBox
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition: node.cpp:21
ftxui::Requirement::min_x
int min_x
Definition: requirement.hpp:10
ftxui::window
Element window(Element title, Element content)
Draw window with a title and a border around the element.
Definition: border.cpp:163
ftxui::Box::x_min
int x_min
Definition: box.hpp:7
ftxui::Node::box_
Box box_
Definition: node.hpp:41
ftxui::Box::y_max
int y_max
Definition: box.hpp:10
ftxui
Definition: captured_mouse.hpp:6
node.hpp
ftxui::Node::children_
Elements children_
Definition: node.hpp:39
box.hpp
requirement.hpp
ftxui::Box::x_max
int x_max
Definition: box.hpp:8
ftxui::Elements
std::vector< Element > Elements
Definition: elements.hpp:16
elements.hpp
ftxui::Node::Node
Node()
Definition: node.cpp:8
ftxui::Element
std::shared_ptr< Node > Element
Definition: elements.hpp:15
ftxui::Box::y_min
int y_min
Definition: box.hpp:9
ftxui::borderWith
Decorator borderWith(Pixel)
Same as border but with a constant Pixel around the element.
Definition: border.cpp:170
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition: elements.hpp:17
ftxui::Requirement::min_y
int min_y
Definition: requirement.hpp:11
ftxui::Render
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:34
ftxui::Node::requirement_
Requirement requirement_
Definition: node.hpp:40
ftxui::Pixel
A unicode character and its associated style.
Definition: screen.hpp:16
ftxui::Node::ComputeRequirement
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition: node.cpp:14
screen.hpp