FTXUI  5.0.0
C++ functional terminal UI.
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 
11 namespace ftxui {
12 
13 namespace {
14 Elements Split(const std::string& the_text) {
15  Elements output;
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 } // namespace
24 
25 /// @brief Return an element drawing the paragraph on multiple lines.
26 /// @ingroup dom
27 /// @see flexbox.
28 Element paragraph(const std::string& the_text) {
29  return paragraphAlignLeft(the_text);
30 }
31 
32 /// @brief Return an element drawing the paragraph on multiple lines, aligned on
33 /// the left.
34 /// @ingroup dom
35 /// @see flexbox.
36 Element paragraphAlignLeft(const std::string& the_text) {
37  static const auto config = FlexboxConfig().SetGap(1, 0);
38  return flexbox(Split(the_text), config);
39 }
40 
41 /// @brief Return an element drawing the paragraph on multiple lines, aligned on
42 /// the right.
43 /// @ingroup dom
44 /// @see flexbox.
45 Element paragraphAlignRight(const std::string& the_text) {
46  static const auto config =
48  return flexbox(Split(the_text), config);
49 }
50 
51 /// @brief Return an element drawing the paragraph on multiple lines, aligned on
52 /// the center.
53 /// @ingroup dom
54 /// @see flexbox.
55 Element paragraphAlignCenter(const std::string& the_text) {
56  static const auto config =
58  return flexbox(Split(the_text), config);
59 }
60 
61 /// @brief Return an element drawing the paragraph on multiple lines, aligned
62 /// using a justified alignment.
63 /// the center.
64 /// @ingroup dom
65 /// @see flexbox.
66 Element paragraphAlignJustify(const std::string& the_text) {
67  static const auto config = FlexboxConfig().SetGap(1, 0).Set(
69  Elements words = Split(the_text);
70  words.push_back(text("") | xflex);
71  return flexbox(std::move(words), config);
72 }
73 
74 } // namespace ftxui
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition: flex.cpp:128
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:45
Elements paragraph(std::wstring text)
Element paragraphAlignCenter(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the center.
Definition: paragraph.cpp:55
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:119
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:36
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
Element paragraphAlignJustify(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned using a justified alignment....
Definition: paragraph.cpp:66
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.