FTXUI  4.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
separator.cpp
Go to the documentation of this file.
1#include <array> // for array, array<>::value_type
2#include <memory> // for make_shared, allocator
3#include <string> // for basic_string, string
4#include <utility> // for move
5
6#include "ftxui/dom/elements.hpp" // for Element, BorderStyle, LIGHT, separator, DOUBLE, EMPTY, HEAVY, separatorCharacter, separatorDouble, separatorEmpty, separatorHSelector, separatorHeavy, separatorLight, separatorStyled, separatorVSelector
7#include "ftxui/dom/node.hpp" // for Node
8#include "ftxui/dom/requirement.hpp" // for Requirement
9#include "ftxui/screen/box.hpp" // for Box
10#include "ftxui/screen/color.hpp" // for Color
11#include "ftxui/screen/screen.hpp" // for Pixel, Screen
12
13namespace ftxui {
14
15namespace {
16using Charset = std::array<std::string, 2>; // NOLINT
17using Charsets = std::array<Charset, 5>; // NOLINT
18// NOLINTNEXTLINE
19const Charsets charsets = {
20 Charset{"│", "─"}, //
21 Charset{"┃", "━"}, //
22 Charset{"║", "═"}, //
23 Charset{"│", "─"}, //
24 Charset{" ", " "}, //
25};
26
27} // namespace
28
29class Separator : public Node {
30 public:
31 explicit Separator(std::string value) : value_(std::move(value)) {}
32
33 void ComputeRequirement() override {
36 }
37
38 void Render(Screen& screen) override {
39 for (int y = box_.y_min; y <= box_.y_max; ++y) {
40 for (int x = box_.x_min; x <= box_.x_max; ++x) {
41 Pixel& pixel = screen.PixelAt(x, y);
42 pixel.character = value_;
43 pixel.automerge = true;
44 }
45 }
46 }
47
48 std::string value_;
49};
50
51class SeparatorAuto : public Node {
52 public:
53 explicit SeparatorAuto(BorderStyle style) : style_(style) {}
54
55 void ComputeRequirement() override {
58 }
59
60 void Render(Screen& screen) override {
61 const bool is_column = (box_.x_max == box_.x_min);
62 const bool is_line = (box_.y_min == box_.y_max);
63
64 const std::string c = charsets[style_][int(is_line && !is_column)];
65
66 for (int y = box_.y_min; y <= box_.y_max; ++y) {
67 for (int x = box_.x_min; x <= box_.x_max; ++x) {
68 Pixel& pixel = screen.PixelAt(x, y);
69 pixel.character = c;
70 pixel.automerge = true;
71 }
72 }
73 }
74
75 BorderStyle style_;
76};
77
78class SeparatorWithPixel : public SeparatorAuto {
79 public:
80 explicit SeparatorWithPixel(Pixel pixel)
81 : SeparatorAuto(LIGHT), pixel_(std::move(pixel)) {
82 pixel_.automerge = true;
83 }
84 void Render(Screen& screen) override {
85 for (int y = box_.y_min; y <= box_.y_max; ++y) {
86 for (int x = box_.x_min; x <= box_.x_max; ++x) {
87 screen.PixelAt(x, y) = pixel_;
88 }
89 }
90 }
91
92 private:
93 Pixel pixel_;
94};
95
96/// @brief Draw a vertical or horizontal separation in between two other
97/// elements.
98/// @ingroup dom
99/// @see separator
100/// @see separatorLight
101/// @see separatorDouble
102/// @see separatorHeavy
103/// @see separatorEmpty
104/// @see separatorRounded
105/// @see separatorStyled
106/// @see separatorCharacter
107///
108/// Add a visual separation in between two elements.
109///
110/// ### Example
111///
112/// ```cpp
113/// // Use 'border' as a function...
114/// Element document = vbox({
115/// text("up"),
116/// separator(),
117/// text("down"),
118/// });
119/// ```
120///
121/// ### Output
122///
123/// ```bash
124/// up
125/// ────
126/// down
127/// ```
129 return std::make_shared<SeparatorAuto>(LIGHT);
130}
131
132/// @brief Draw a vertical or horizontal separation in between two other
133/// elements.
134/// @param style the style of the separator.
135/// @ingroup dom
136/// @see separator
137/// @see separatorLight
138/// @see separatorDouble
139/// @see separatorHeavy
140/// @see separatorEmpty
141/// @see separatorRounded
142/// @see separatorStyled
143/// @see separatorCharacter
144///
145/// Add a visual separation in between two elements.
146///
147/// ### Example
148///
149/// ```cpp
150/// // Use 'border' as a function...
151/// Element document = vbox({
152/// text("up"),
153/// separatorStyled(DOUBLE),
154/// text("down"),
155/// });
156/// ```
157///
158/// ### Output
159///
160/// ```bash
161/// up
162/// ════
163/// down
164/// ```
166 return std::make_shared<SeparatorAuto>(style);
167}
168
169/// @brief Draw a vertical or horizontal separation in between two other
170/// elements, using the LIGHT style.
171/// @ingroup dom
172/// @see separator
173/// @see separatorLight
174/// @see separatorDouble
175/// @see separatorHeavy
176/// @see separatorEmpty
177/// @see separatorRounded
178/// @see separatorStyled
179/// @see separatorCharacter
180///
181/// Add a visual separation in between two elements.
182///
183/// ### Example
184///
185/// ```cpp
186/// // Use 'border' as a function...
187/// Element document = vbox({
188/// text("up"),
189/// separatorLight(),
190/// text("down"),
191/// });
192/// ```
193///
194/// ### Output
195///
196/// ```bash
197/// up
198/// ────
199/// down
200/// ```
202 return std::make_shared<SeparatorAuto>(LIGHT);
203}
204
205/// @brief Draw a vertical or horizontal separation in between two other
206/// elements, using the HEAVY style.
207/// @ingroup dom
208/// @see separator
209/// @see separatorLight
210/// @see separatorDouble
211/// @see separatorHeavy
212/// @see separatorEmpty
213/// @see separatorRounded
214/// @see separatorStyled
215/// @see separatorCharacter
216///
217/// Add a visual separation in between two elements.
218///
219/// ### Example
220///
221/// ```cpp
222/// // Use 'border' as a function...
223/// Element document = vbox({
224/// text("up"),
225/// separatorHeavy(),
226/// text("down"),
227/// });
228/// ```
229///
230/// ### Output
231///
232/// ```bash
233/// up
234/// ━━━━
235/// down
236/// ```
238 return std::make_shared<SeparatorAuto>(HEAVY);
239}
240
241/// @brief Draw a vertical or horizontal separation in between two other
242/// elements, using the DOUBLE style.
243/// @ingroup dom
244/// @see separator
245/// @see separatorLight
246/// @see separatorDouble
247/// @see separatorHeavy
248/// @see separatorEmpty
249/// @see separatorRounded
250/// @see separatorStyled
251/// @see separatorCharacter
252///
253/// Add a visual separation in between two elements.
254///
255/// ### Example
256///
257/// ```cpp
258/// // Use 'border' as a function...
259/// Element document = vbox({
260/// text("up"),
261/// separatorDouble(),
262/// text("down"),
263/// });
264/// ```
265///
266/// ### Output
267///
268/// ```bash
269/// up
270/// ════
271/// down
272/// ```
274 return std::make_shared<SeparatorAuto>(DOUBLE);
275}
276
277/// @brief Draw a vertical or horizontal separation in between two other
278/// elements, using the EMPTY style.
279/// @ingroup dom
280/// @see separator
281/// @see separatorLight
282/// @see separatorDouble
283/// @see separatorHeavy
284/// @see separatorEmpty
285/// @see separatorRounded
286/// @see separatorStyled
287/// @see separatorCharacter
288///
289/// Add a visual separation in between two elements.
290///
291/// ### Example
292///
293/// ```cpp
294/// // Use 'border' as a function...
295/// Element document = vbox({
296/// text("up"),
297/// separator(),
298/// text("down"),
299/// });
300/// ```
301///
302/// ### Output
303///
304/// ```bash
305/// up
306///
307/// down
308/// ```
310 return std::make_shared<SeparatorAuto>(EMPTY);
311}
312
313/// @brief Draw a vertical or horizontal separation in between two other
314/// elements.
315/// @param value the character to fill the separator area.
316/// @ingroup dom
317/// @see separator
318/// @see separatorLight
319/// @see separatorDouble
320/// @see separatorHeavy
321/// @see separatorEmpty
322/// @see separatorRounded
323/// @see separatorStyled
324/// @see separatorCharacter
325///
326/// Add a visual separation in between two elements.
327///
328/// ### Example
329///
330/// ```cpp
331/// // Use 'border' as a function...
332/// Element document = vbox({
333/// text("up"),
334/// separator(),
335/// text("down"),
336/// });
337/// ```
338///
339/// ### Output
340///
341/// ```bash
342/// up
343/// ────
344/// down
345/// ```
346Element separatorCharacter(std::string value) {
347 return std::make_shared<Separator>(std::move(value));
348}
349
350/// @brief Draw a separator in between two element filled with a given pixel.
351/// @ingroup dom
352/// @see separator
353/// @see separatorLight
354/// @see separatorHeavy
355/// @see separatorDouble
356/// @see separatorStyled
357///
358/// ### Example
359///
360/// ```cpp
361/// Pixel empty;
362/// Element document = vbox({
363/// text("Up"),
364/// separator(empty),
365/// text("Down"),
366/// })
367/// ```
368///
369/// ### Output
370///
371/// ```bash
372/// Up
373///
374/// Down
375/// ```
377 return std::make_shared<SeparatorWithPixel>(std::move(pixel));
378}
379
380/// @brief Draw an horizontal bar, with the area in between left/right colored
381/// differently.
382/// @param left the left limit of the active area.
383/// @param right the right limit of the active area.
384/// @param selected_color the color of the selected area.
385/// @param unselected_color the color of the unselected area.
386///
387/// ### Example
388///
389/// ```cpp
390/// Element document = separatorHSelector(2,5, Color::White, Color::Blue);
391/// ```
393 float right,
394 Color unselected_color,
395 Color selected_color) {
396 class Impl : public Node {
397 public:
398 Impl(float left, float right, Color selected_color, Color unselected_color)
399 : left_(left),
400 right_(right),
401 unselected_color_(unselected_color),
402 selected_color_(selected_color) {}
403 void ComputeRequirement() override {
404 requirement_.min_x = 1;
405 requirement_.min_y = 1;
406 }
407
408 void Render(Screen& screen) override {
409 if (box_.y_max < box_.y_min) {
410 return;
411 }
412
413 // This are the two location with an empty demi-cell.
414 int demi_cell_left = int(left_ * 2.F - 1.F); // NOLINT
415 int demi_cell_right = int(right_ * 2.F + 2.F); // NOLINT
416
417 const int y = box_.y_min;
418 for (int x = box_.x_min; x <= box_.x_max; ++x) {
419 Pixel& pixel = screen.PixelAt(x, y);
420
421 const int a = (x - box_.x_min) * 2;
422 const int b = a + 1;
423 const bool a_empty = demi_cell_left == a || demi_cell_right == a;
424 const bool b_empty = demi_cell_left == b || demi_cell_right == b;
425
426 if (!a_empty && !b_empty) {
427 pixel.character = "─";
428 pixel.automerge = true;
429 } else {
430 pixel.character = a_empty ? "╶" : "╴"; // NOLINT
431 pixel.automerge = false;
432 }
433
434 if (demi_cell_left <= a && b <= demi_cell_right) {
435 pixel.foreground_color = selected_color_;
436 } else {
437 pixel.foreground_color = unselected_color_;
438 }
439 }
440 }
441
442 float left_;
443 float right_;
444 Color unselected_color_;
445 Color selected_color_;
446 };
447 return std::make_shared<Impl>(left, right, unselected_color, selected_color);
448}
449
450/// @brief Draw an vertical bar, with the area in between up/downcolored
451/// differently.
452/// @param up the left limit of the active area.
453/// @param down the right limit of the active area.
454/// @param selected_color the color of the selected area.
455/// @param unselected_color the color of the unselected area.
456///
457/// ### Example
458///
459/// ```cpp
460/// Element document = separatorHSelector(2,5, Color::White, Color::Blue);
461/// ```
463 float down,
464 Color unselected_color,
465 Color selected_color) {
466 class Impl : public Node {
467 public:
468 Impl(float up, float down, Color unselected_color, Color selected_color)
469 : up_(up),
470 down_(down),
471 unselected_color_(unselected_color),
472 selected_color_(selected_color) {}
473 void ComputeRequirement() override {
474 requirement_.min_x = 1;
475 requirement_.min_y = 1;
476 }
477
478 void Render(Screen& screen) override {
479 if (box_.x_max < box_.x_min) {
480 return;
481 }
482
483 // This are the two location with an empty demi-cell.
484 const int demi_cell_up = int(up_ * 2 - 1);
485 const int demi_cell_down = int(down_ * 2 + 2);
486
487 const int x = box_.x_min;
488 for (int y = box_.y_min; y <= box_.y_max; ++y) {
489 Pixel& pixel = screen.PixelAt(x, y);
490
491 const int a = (y - box_.y_min) * 2;
492 const int b = a + 1;
493 const bool a_empty = demi_cell_up == a || demi_cell_down == a;
494 const bool b_empty = demi_cell_up == b || demi_cell_down == b;
495
496 if (!a_empty && !b_empty) {
497 pixel.character = "│";
498 pixel.automerge = true;
499 } else {
500 pixel.character = a_empty ? "╷" : "╵"; // NOLINT
501 pixel.automerge = false;
502 }
503
504 if (demi_cell_up <= a && b <= demi_cell_down) {
505 pixel.foreground_color = selected_color_;
506 } else {
507 pixel.foreground_color = unselected_color_;
508 }
509 }
510 }
511
512 float up_;
513 float down_;
514 Color unselected_color_;
515 Color selected_color_;
516 };
517 return std::make_shared<Impl>(up, down, unselected_color, selected_color);
518}
519
520} // namespace ftxui
521
522// Copyright 2020 Arthur Sonzogni. All rights reserved.
523// Use of this source code is governed by the MIT license that can be found in
524// the LICENSE file.
A class representing terminal colors.
Definition color.hpp:17
Requirement requirement_
Definition node.hpp:54
Box box_
Definition node.hpp:55
A rectangular grid of Pixel.
Definition screen.hpp:57
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
Definition screen.cpp:468
Element separatorStyled(BorderStyle)
Draw a vertical or horizontal separation in between two other elements.
Element separatorEmpty()
Draw a vertical or horizontal separation in between two other elements, using the EMPTY style.
Element separatorVSelector(float up, float down, Color unselected_color, Color selected_color)
Draw an vertical bar, with the area in between up/downcolored differently.
std::shared_ptr< Node > Element
Definition elements.hpp:18
Element separatorLight()
Draw a vertical or horizontal separation in between two other elements, using the LIGHT style.
Element separatorHSelector(float left, float right, Color unselected_color, Color selected_color)
Draw an horizontal bar, with the area in between left/right colored differently.
std::array< Charset, 5 > Charsets
Definition border.cpp:17
Element separatorCharacter(std::string)
Draw a vertical or horizontal separation in between two other elements.
Element separator()
Draw a vertical or horizontal separation in between two other elements.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:44
Element separatorDouble()
Draw a vertical or horizontal separation in between two other elements, using the DOUBLE style.
std::array< std::string, 6 > Charset
Definition border.cpp:16
Element separatorHeavy()
Draw a vertical or horizontal separation in between two other elements, using the HEAVY style.
BorderStyle
Definition elements.hpp:23
@ EMPTY
Definition elements.hpp:23
@ DOUBLE
Definition elements.hpp:23
@ HEAVY
Definition elements.hpp:23
@ LIGHT
Definition elements.hpp:23
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
Color foreground_color
Definition screen.hpp:25
std::string character
Definition screen.hpp:21
bool automerge
Definition screen.hpp:35