FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
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 <sstream> // for basic_istream, stringstream
5#include <string> // for string, allocator, getline
6#include <utility> // for move
7
8#include "ftxui/dom/elements.hpp" // for flexbox, Element, text, Elements, operator|, xflex, paragraph, paragraphAlignCenter, paragraphAlignJustify, paragraphAlignLeft, paragraphAlignRight
9#include "ftxui/dom/flexbox_config.hpp" // for FlexboxConfig, FlexboxConfig::JustifyContent, FlexboxConfig::JustifyContent::Center, FlexboxConfig::JustifyContent::FlexEnd, FlexboxConfig::JustifyContent::SpaceBetween
10
11namespace ftxui {
12
13namespace {
14Elements Split(const std::string& the_text) {
16 std::stringstream ss(the_text);
17 std::string word;
18 while (std::getline(ss, word, ' ')) {
19 output.push_back(text(word));
20 }
21 return output;
22}
23
24Element Split(const std::string& paragraph,
25 std::function<Element(std::string)> f) {
27 std::stringstream ss(paragraph);
28 std::string line;
29 while (std::getline(ss, line, '\n')) {
30 output.push_back(f(line));
31 }
32 return vbox(std::move(output));
33}
34
35} // namespace
36
37/// @brief Return an element drawing the paragraph on multiple lines.
38/// @ingroup dom
39/// @see flexbox.
40Element paragraph(const std::string& the_text) {
42}
43
44/// @brief Return an element drawing the paragraph on multiple lines, aligned on
45/// the left.
46/// @ingroup dom
47/// @see flexbox.
48Element paragraphAlignLeft(const std::string& the_text) {
49 return Split(the_text, [](const std::string& line) {
50 static const auto config = FlexboxConfig().SetGap(1, 0);
51 return flexbox(Split(line), config);
52 });
53};
54
55/// @brief Return an element drawing the paragraph on multiple lines, aligned on
56/// the right.
57/// @ingroup dom
58/// @see flexbox.
60 return Split(the_text, [](const std::string& line) {
61 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
63 return flexbox(Split(line), config);
64 });
65}
66
67/// @brief Return an element drawing the paragraph on multiple lines, aligned on
68/// the center.
69/// @ingroup dom
70/// @see flexbox.
72 return Split(the_text, [](const std::string& line) {
73 static const auto config =
75 return flexbox(Split(line), config);
76 });
77}
78
79/// @brief Return an element drawing the paragraph on multiple lines, aligned
80/// using a justified alignment.
81/// the center.
82/// @ingroup dom
83/// @see flexbox.
85 return Split(the_text, [](const std::string& line) {
86 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
88 Elements words = Split(line);
89 words.push_back(text("") | xflex);
90 return flexbox(std::move(words), config);
91 });
92}
93
94} // namespace ftxui
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:129
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element paragraphAlignRight(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the right.
Definition paragraph.cpp:59
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
Element paragraphAlignCenter(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the center.
Definition paragraph.cpp:71
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:159
std::vector< Element > Elements
Definition elements.hpp:23
Element paragraphAlignLeft(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the left.
Definition paragraph.cpp:48
Elements paragraph(std::wstring text)
Element paragraphAlignJustify(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned using a justified alignment....
Definition paragraph.cpp:84
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:97
FlexboxConfig & SetGap(int gap_x, int gap_y)
Set the flexbox flex direction.
@ Center
Items are centered along the line.
@ FlexEnd
Items are aligned to the end of flexbox's direction.
@ SpaceBetween
Items are evenly distributed in the line; first item is on the start.
FlexboxConfig & Set(FlexboxConfig::Direction)
Set the flexbox direction.