FTXUI  0.11.1
C++ functional terminal UI.
separator.cpp
Go to the documentation of this file.
1 #include <memory> // for make_shared
2 #include <string> // for string
3 
4 #include "ftxui/dom/elements.hpp" // for Element, separator
5 #include "ftxui/dom/node.hpp" // for Node
6 #include "ftxui/dom/requirement.hpp" // for Requirement
7 #include "ftxui/screen/box.hpp" // for Box
8 #include "ftxui/screen/screen.hpp" // for Pixel, Screen
9 
10 namespace ftxui {
11 
12 using ftxui::Screen;
13 
14 const std::string charset[][2] = {
15  {"│", "─"}, //
16  {"┃", "━"}, //
17  {"║", "═"}, //
18  {"│", "─"}, //
19  {" ", " "}, //
20 };
21 
22 class Separator : public Node {
23  public:
24  Separator(std::string value) : value_(value) {}
25 
26  void ComputeRequirement() override {
27  requirement_.min_x = 1;
28  requirement_.min_y = 1;
29  }
30 
31  void Render(Screen& screen) override {
32  for (int y = box_.y_min; y <= box_.y_max; ++y) {
33  for (int x = box_.x_min; x <= box_.x_max; ++x) {
34  screen.PixelAt(x, y).character = value_;
35  }
36  }
37  }
38 
39  std::string value_;
40 };
41 
42 class SeparatorAuto : public Node {
43  public:
44  SeparatorAuto(BorderStyle style) : style_(style) {}
45 
46  void ComputeRequirement() override {
47  requirement_.min_x = 1;
48  requirement_.min_y = 1;
49  }
50 
51  void Render(Screen& screen) override {
52  bool is_column = (box_.x_max == box_.x_min);
53  bool is_line = (box_.y_min == box_.y_max);
54 
55  const std::string c = charset[style_][is_line && !is_column];
56 
57  for (int y = box_.y_min; y <= box_.y_max; ++y) {
58  for (int x = box_.x_min; x <= box_.x_max; ++x) {
59  screen.PixelAt(x, y).character = c;
60  }
61  }
62  }
63 
64  BorderStyle style_;
65 };
66 
67 class SeparatorWithPixel : public SeparatorAuto {
68  public:
69  SeparatorWithPixel(Pixel pixel) : SeparatorAuto(LIGHT), pixel_(pixel) {}
70  void Render(Screen& screen) override {
71  for (int y = box_.y_min; y <= box_.y_max; ++y) {
72  for (int x = box_.x_min; x <= box_.x_max; ++x) {
73  screen.PixelAt(x, y) = pixel_;
74  }
75  }
76  }
77 
78  private:
79  Pixel pixel_;
80 };
81 
82 /// @brief Draw a vertical or horizontal separation in between two other
83 /// elements.
84 /// @ingroup dom
85 /// @see separator
86 /// @see separatorLight
87 /// @see separatorDouble
88 /// @see separatorHeavy
89 /// @see separatorEmpty
90 /// @see separatorRounded
91 /// @see separatorStyled
92 /// @see separatorCharacter
93 ///
94 /// Add a visual separation in between two elements.
95 ///
96 /// ### Example
97 ///
98 /// ```cpp
99 /// // Use 'border' as a function...
100 /// Element document = vbox({
101 /// text("up"),
102 /// separator(),
103 /// text("down"),
104 /// });
105 /// ```
106 ///
107 /// ### Output
108 ///
109 /// ```bash
110 /// up
111 /// ────
112 /// down
113 /// ```
115  return std::make_shared<SeparatorAuto>(LIGHT);
116 }
117 
118 /// @brief Draw a vertical or horizontal separation in between two other
119 /// elements.
120 /// @param style the style of the separator.
121 /// @ingroup dom
122 /// @see separator
123 /// @see separatorLight
124 /// @see separatorDouble
125 /// @see separatorHeavy
126 /// @see separatorEmpty
127 /// @see separatorRounded
128 /// @see separatorStyled
129 /// @see separatorCharacter
130 ///
131 /// Add a visual separation in between two elements.
132 ///
133 /// ### Example
134 ///
135 /// ```cpp
136 /// // Use 'border' as a function...
137 /// Element document = vbox({
138 /// text("up"),
139 /// separatorStyled(DOUBLE),
140 /// text("down"),
141 /// });
142 /// ```
143 ///
144 /// ### Output
145 ///
146 /// ```bash
147 /// up
148 /// ════
149 /// down
150 /// ```
152  return std::make_shared<SeparatorAuto>(style);
153 }
154 
155 /// @brief Draw a vertical or horizontal separation in between two other
156 /// elements, using the LIGHT style.
157 /// @ingroup dom
158 /// @see separator
159 /// @see separatorLight
160 /// @see separatorDouble
161 /// @see separatorHeavy
162 /// @see separatorEmpty
163 /// @see separatorRounded
164 /// @see separatorStyled
165 /// @see separatorCharacter
166 ///
167 /// Add a visual separation in between two elements.
168 ///
169 /// ### Example
170 ///
171 /// ```cpp
172 /// // Use 'border' as a function...
173 /// Element document = vbox({
174 /// text("up"),
175 /// separatorLight(),
176 /// text("down"),
177 /// });
178 /// ```
179 ///
180 /// ### Output
181 ///
182 /// ```bash
183 /// up
184 /// ────
185 /// down
186 /// ```
188  return std::make_shared<SeparatorAuto>(LIGHT);
189 }
190 
191 /// @brief Draw a vertical or horizontal separation in between two other
192 /// elements, using the HEAVY style.
193 /// @ingroup dom
194 /// @see separator
195 /// @see separatorLight
196 /// @see separatorDouble
197 /// @see separatorHeavy
198 /// @see separatorEmpty
199 /// @see separatorRounded
200 /// @see separatorStyled
201 /// @see separatorCharacter
202 ///
203 /// Add a visual separation in between two elements.
204 ///
205 /// ### Example
206 ///
207 /// ```cpp
208 /// // Use 'border' as a function...
209 /// Element document = vbox({
210 /// text("up"),
211 /// separatorHeavy(),
212 /// text("down"),
213 /// });
214 /// ```
215 ///
216 /// ### Output
217 ///
218 /// ```bash
219 /// up
220 /// ━━━━
221 /// down
222 /// ```
224  return std::make_shared<SeparatorAuto>(HEAVY);
225 }
226 
227 /// @brief Draw a vertical or horizontal separation in between two other
228 /// elements, using the DOUBLE style.
229 /// @ingroup dom
230 /// @see separator
231 /// @see separatorLight
232 /// @see separatorDouble
233 /// @see separatorHeavy
234 /// @see separatorEmpty
235 /// @see separatorRounded
236 /// @see separatorStyled
237 /// @see separatorCharacter
238 ///
239 /// Add a visual separation in between two elements.
240 ///
241 /// ### Example
242 ///
243 /// ```cpp
244 /// // Use 'border' as a function...
245 /// Element document = vbox({
246 /// text("up"),
247 /// separatorDouble(),
248 /// text("down"),
249 /// });
250 /// ```
251 ///
252 /// ### Output
253 ///
254 /// ```bash
255 /// up
256 /// ════
257 /// down
258 /// ```
260  return std::make_shared<SeparatorAuto>(DOUBLE);
261 }
262 
263 /// @brief Draw a vertical or horizontal separation in between two other
264 /// elements, using the EMPTY style.
265 /// @ingroup dom
266 /// @see separator
267 /// @see separatorLight
268 /// @see separatorDouble
269 /// @see separatorHeavy
270 /// @see separatorEmpty
271 /// @see separatorRounded
272 /// @see separatorStyled
273 /// @see separatorCharacter
274 ///
275 /// Add a visual separation in between two elements.
276 ///
277 /// ### Example
278 ///
279 /// ```cpp
280 /// // Use 'border' as a function...
281 /// Element document = vbox({
282 /// text("up"),
283 /// separator(),
284 /// text("down"),
285 /// });
286 /// ```
287 ///
288 /// ### Output
289 ///
290 /// ```bash
291 /// up
292 ///
293 /// down
294 /// ```
296  return std::make_shared<SeparatorAuto>(EMPTY);
297 }
298 
299 /// @brief Draw a vertical or horizontal separation in between two other
300 /// elements.
301 /// @param value the character to fill the separator area.
302 /// @ingroup dom
303 /// @see separator
304 /// @see separatorLight
305 /// @see separatorDouble
306 /// @see separatorHeavy
307 /// @see separatorEmpty
308 /// @see separatorRounded
309 /// @see separatorStyled
310 /// @see separatorCharacter
311 ///
312 /// Add a visual separation in between two elements.
313 ///
314 /// ### Example
315 ///
316 /// ```cpp
317 /// // Use 'border' as a function...
318 /// Element document = vbox({
319 /// text("up"),
320 /// separator(),
321 /// text("down"),
322 /// });
323 /// ```
324 ///
325 /// ### Output
326 ///
327 /// ```bash
328 /// up
329 /// ────
330 /// down
331 /// ```
332 Element separatorCharacter(std::string value) {
333  return std::make_shared<Separator>(value);
334 }
335 
336 /// @brief Draw a separator in between two element filled with a given pixel.
337 /// @ingroup dom
338 /// @see separator
339 /// @see separatorLight
340 /// @see separatorHeavy
341 /// @see separatorDouble
342 /// @see separatorStyled
343 ///
344 /// ### Example
345 ///
346 /// ```cpp
347 /// Pixel empty;
348 /// Element document = vbox({
349 /// text("Up"),
350 /// separator(empty),
351 /// text("Down"),
352 /// })
353 /// ```
354 ///
355 /// ### Output
356 ///
357 /// ```bash
358 /// Up
359 ///
360 /// Down
361 /// ```
363  return std::make_shared<SeparatorWithPixel>(pixel);
364 }
365 
366 } // namespace ftxui
367 
368 // Copyright 2020 Arthur Sonzogni. All rights reserved.
369 // Use of this source code is governed by the MIT license that can be found in
370 // the LICENSE file.
Requirement requirement_
Definition: node.hpp:49
Box box_
Definition: node.hpp:50
A rectangular grid of Pixel.
Definition: screen.hpp:49
Element separatorStyled(BorderStyle)
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:151
Element separatorEmpty()
Draw a vertical or horizontal separation in between two other elements, using the EMPTY style.
Definition: separator.cpp:295
std::shared_ptr< Node > Element
Definition: elements.hpp:16
Element separatorLight()
Draw a vertical or horizontal separation in between two other elements, using the LIGHT style.
Definition: separator.cpp:187
Element separatorCharacter(std::string)
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:332
Element separator(void)
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:114
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:40
Element separatorDouble()
Draw a vertical or horizontal separation in between two other elements, using the DOUBLE style.
Definition: separator.cpp:259
Element separatorHeavy()
Draw a vertical or horizontal separation in between two other elements, using the HEAVY style.
Definition: separator.cpp:223
BorderStyle
Definition: elements.hpp:21
@ EMPTY
Definition: elements.hpp:21
@ DOUBLE
Definition: elements.hpp:21
@ HEAVY
Definition: elements.hpp:21
@ LIGHT
Definition: elements.hpp:21
int x_max
Definition: box.hpp:8
int y_min
Definition: box.hpp:9
int y_max
Definition: box.hpp:10
int x_min
Definition: box.hpp:7
A unicode character and its associated style.
Definition: screen.hpp:16