FTXUI  6.0.2
C++ functional terminal UI.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <utility> // for move
8
9#include "ftxui/dom/elements.hpp" // for flexbox, Element, text, Elements, operator|, xflex, paragraph, paragraphAlignCenter, paragraphAlignJustify, paragraphAlignLeft, paragraphAlignRight
10#include "ftxui/dom/flexbox_config.hpp" // for FlexboxConfig, FlexboxConfig::JustifyContent, FlexboxConfig::JustifyContent::Center, FlexboxConfig::JustifyContent::FlexEnd, FlexboxConfig::JustifyContent::SpaceBetween
11
12namespace ftxui {
13
14namespace {
15Elements Split(const std::string& the_text) {
17 std::stringstream ss(the_text);
18 std::string word;
19 while (std::getline(ss, word, ' ')) {
20 output.push_back(text(word));
21 }
22 return output;
23}
24
25Element Split(const std::string& paragraph,
26 const std::function<Element(std::string)>& f) {
28 std::stringstream ss(paragraph);
29 std::string line;
30 while (std::getline(ss, line, '\n')) {
31 output.push_back(f(line));
32 }
33 return vbox(std::move(output));
34}
35
36} // namespace
37
38/// @brief Return an element drawing the paragraph on multiple lines.
39/// @ingroup dom
40/// @see flexbox.
41Element paragraph(const std::string& the_text) {
43}
44
45/// @brief Return an element drawing the paragraph on multiple lines, aligned on
46/// the left.
47/// @ingroup dom
48/// @see flexbox.
49Element paragraphAlignLeft(const std::string& the_text) {
50 return Split(the_text, [](const std::string& line) {
51 static const auto config = FlexboxConfig().SetGap(1, 0);
52 return flexbox(Split(line), config);
53 });
54};
55
56/// @brief Return an element drawing the paragraph on multiple lines, aligned on
57/// the right.
58/// @ingroup dom
59/// @see flexbox.
61 return Split(the_text, [](const std::string& line) {
62 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
64 return flexbox(Split(line), config);
65 });
66}
67
68/// @brief Return an element drawing the paragraph on multiple lines, aligned on
69/// the center.
70/// @ingroup dom
71/// @see flexbox.
73 return Split(the_text, [](const std::string& line) {
74 static const auto config =
76 return flexbox(Split(line), config);
77 });
78}
79
80/// @brief Return an element drawing the paragraph on multiple lines, aligned
81/// using a justified alignment.
82/// the center.
83/// @ingroup dom
84/// @see flexbox.
86 return Split(the_text, [](const std::string& line) {
87 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
89 Elements words = Split(line);
90 words.push_back(text("") | xflex);
91 return flexbox(std::move(words), config);
92 });
93}
94
95} // 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:60
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:72
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
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:49
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:85
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
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.