FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
paragraph.cpp
Go to the documentation of this file.
1#include <sstream> // for basic_istream, stringstream
2#include <string> // for string, allocator, getline
3#include <utility> // for move
4
5#include "ftxui/dom/elements.hpp" // for flexbox, Element, text, Elements, operator|, xflex, paragraph, paragraphAlignCenter, paragraphAlignJustify, paragraphAlignLeft, paragraphAlignRight
6#include "ftxui/dom/flexbox_config.hpp" // for FlexboxConfig, FlexboxConfig::JustifyContent, FlexboxConfig::JustifyContent::Center, FlexboxConfig::JustifyContent::FlexEnd, FlexboxConfig::JustifyContent::SpaceBetween
7
8namespace ftxui {
9
10namespace {
11Elements Split(const std::string& the_text) {
12 Elements output;
13 std::stringstream ss(the_text);
14 std::string word;
15 while (std::getline(ss, word, ' ')) {
16 output.push_back(text(word));
17 }
18 return output;
19}
20} // namespace
21
22/// @brief Return an element drawing the paragraph on multiple lines.
23/// @ingroup dom
24/// @see flexbox.
25Element paragraph(const std::string& the_text) {
26 return paragraphAlignLeft(the_text);
27}
28
29/// @brief Return an element drawing the paragraph on multiple lines, aligned on
30/// the left.
31/// @ingroup dom
32/// @see flexbox.
33Element paragraphAlignLeft(const std::string& the_text) {
34 static const auto config = FlexboxConfig().SetGap(1, 0);
35 return flexbox(Split(the_text), config);
36}
37
38/// @brief Return an element drawing the paragraph on multiple lines, aligned on
39/// the right.
40/// @ingroup dom
41/// @see flexbox.
42Element paragraphAlignRight(const std::string& the_text) {
43 static const auto config =
45 return flexbox(Split(the_text), config);
46}
47
48/// @brief Return an element drawing the paragraph on multiple lines, aligned on
49/// the center.
50/// @ingroup dom
51/// @see flexbox.
52Element paragraphAlignCenter(const std::string& the_text) {
53 static const auto config =
55 return flexbox(Split(the_text), config);
56}
57
58/// @brief Return an element drawing the paragraph on multiple lines, aligned
59/// using a justified alignment.
60/// the center.
61/// @ingroup dom
62/// @see flexbox.
63Element paragraphAlignJustify(const std::string& the_text) {
64 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
66 Elements words = Split(the_text);
67 words.push_back(text("") | xflex);
68 return flexbox(std::move(words), config);
69}
70
71} // namespace ftxui
72
73// Copyright 2020 Arthur Sonzogni. All rights reserved.
74// Use of this source code is governed by the MIT license that can be found in
75// the LICENSE file.
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:126
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
Element paragraphAlignRight(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the right.
Definition paragraph.cpp:42
std::shared_ptr< Node > Element
Definition elements.hpp:19
std::vector< Element > Elements
Definition elements.hpp:20
Element paragraphAlignCenter(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the center.
Definition paragraph.cpp:52
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:111
Element paragraphAlignLeft(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the left.
Definition paragraph.cpp:33
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:63
FlexboxConfig & SetGap(int gap_x, int gap_y)
@ 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)