FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/dom/paragraph.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <functional> // for function
5#include <sstream> // for basic_istream, stringstream
6#include <string> // for string, allocator, getline
7#include <string_view> // for string_view
8#include <utility> // for move
9
10#include "ftxui/dom/elements.hpp" // for flexbox, Element, text, Elements, operator|, xflex, paragraph, paragraphAlignCenter, paragraphAlignJustify, paragraphAlignLeft, paragraphAlignRight
11#include "ftxui/dom/flexbox_config.hpp" // for FlexboxConfig, FlexboxConfig::JustifyContent, FlexboxConfig::JustifyContent::Center, FlexboxConfig::JustifyContent::FlexEnd, FlexboxConfig::JustifyContent::SpaceBetween
12
13namespace ftxui {
14
15namespace {
16Elements Split(std::string_view the_text) {
17 Elements output;
18 size_t start = 0;
19 size_t end = the_text.find(' ');
20 while (end != std::string_view::npos) {
21 output.push_back(text(the_text.substr(start, end - start)));
22 start = end + 1;
23 end = the_text.find(' ', start);
24 }
25 output.push_back(text(the_text.substr(start)));
26 return output;
27}
28
29Element Split(std::string_view paragraph,
30 const std::function<Element(std::string_view)>& f) {
31 Elements output;
32 size_t start = 0;
33 size_t end = paragraph.find('\n');
34 while (end != std::string_view::npos) {
35 output.push_back(f(paragraph.substr(start, end - start)));
36 start = end + 1;
37 end = paragraph.find('\n', start);
38 }
39 output.push_back(f(paragraph.substr(start)));
40 return vbox(std::move(output));
41}
42
43} // namespace
44
45/// @brief Return an element drawing the paragraph on multiple lines.
46/// @ingroup dom
47/// @see flexbox.
48Element paragraph(std::string_view the_text) {
49 return paragraphAlignLeft(the_text);
50}
51
52/// @brief Return an element drawing the paragraph on multiple lines, aligned on
53/// the left.
54/// @ingroup dom
55/// @see flexbox.
56Element paragraphAlignLeft(std::string_view the_text) {
57 return Split(the_text, [](std::string_view line) {
58 static const auto config = FlexboxConfig().SetGap(1, 0);
59 return flexbox(Split(line), config);
60 });
61};
62
63/// @brief Return an element drawing the paragraph on multiple lines, aligned on
64/// the right.
65/// @ingroup dom
66/// @see flexbox.
67Element paragraphAlignRight(std::string_view the_text) {
68 return Split(the_text, [](std::string_view line) {
69 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
70 FlexboxConfig::JustifyContent::FlexEnd);
71 return flexbox(Split(line), config);
72 });
73}
74
75/// @brief Return an element drawing the paragraph on multiple lines, aligned on
76/// the center.
77/// @ingroup dom
78/// @see flexbox.
79Element paragraphAlignCenter(std::string_view the_text) {
80 return Split(the_text, [](std::string_view line) {
81 static const auto config =
82 FlexboxConfig().SetGap(1, 0).Set(FlexboxConfig::JustifyContent::Center);
83 return flexbox(Split(line), config);
84 });
85}
86
87/// @brief Return an element drawing the paragraph on multiple lines, aligned
88/// using a justified alignment.
89/// the center.
90/// @ingroup dom
91/// @see flexbox.
92Element paragraphAlignJustify(std::string_view the_text) {
93 return Split(the_text, [](std::string_view line) {
94 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
95 FlexboxConfig::JustifyContent::SpaceBetween);
96 Elements words = Split(line);
97 words.push_back(text("") | xflex);
98 return flexbox(std::move(words), config);
99 });
100}
101
102} // namespace ftxui
Element text(std::string_view text)
Display a piece of UTF8 encoded unicode text.
Element paragraphAlignLeft(std::string_view the_text)
Return an element drawing the paragraph on multiple lines, aligned on the left.
Element paragraphAlignJustify(std::string_view the_text)
Return an element drawing the paragraph on multiple lines, aligned using a justified alignment....
Element paragraphAlignCenter(std::string_view the_text)
Return an element drawing the paragraph on multiple lines, aligned on the center.
Element paragraph(std::string_view the_text)
Return an element drawing the paragraph on multiple lines.
Element paragraphAlignRight(std::string_view the_text)
Return an element drawing the paragraph on multiple lines, aligned on the right.
Element vbox(Elements children)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
A container displaying elements on row/columns and capable of wrapping on the next column/row when fu...
Definition flexbox.cpp:252
std::shared_ptr< Node > Element
Definition elements.hpp:24
std::vector< Element > Elements
Definition elements.hpp:25