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