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 <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
12 namespace ftxui {
13
14 namespace {
15 Elements Split(const std::string& the_text) {
16 Elements output;
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
25 Element Split(const std::string& paragraph,
26 const std::function<Element(std::string)>& f) {
27 Elements output;
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.
41 Element paragraph(const std::string& the_text) {
42 return paragraphAlignLeft(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.
49 Element 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.
60 Element paragraphAlignRight(const std::string& the_text) {
61 return Split(the_text, [](const std::string& line) {
62 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
63 FlexboxConfig::JustifyContent::FlexEnd);
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.
72 Element paragraphAlignCenter(const std::string& the_text) {
73 return Split(the_text, [](const std::string& line) {
74 static const auto config =
75 FlexboxConfig().SetGap(1, 0).Set(FlexboxConfig::JustifyContent::Center);
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.
85 Element paragraphAlignJustify(const std::string& the_text) {
86 return Split(the_text, [](const std::string& line) {
87 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
88 FlexboxConfig::JustifyContent::SpaceBetween);
89 Elements words = Split(line);
90 words.push_back(text("") | xflex);
91 return flexbox(std::move(words), config);
92 });
93}
94
95} // namespace ftxui
elements.hpp
flexbox_config.hpp
ftxui::FlexboxConfig::SetGap
FlexboxConfig & SetGap(int gap_x, int gap_y)
Set the flexbox flex direction.
Definition flexbox_config.cpp:39
ftxui::FlexboxConfig::JustifyContent::Center
@ Center
Items are centered along the line.
ftxui::FlexboxConfig::JustifyContent::FlexEnd
@ FlexEnd
Items are aligned to the end of flexbox's direction.
ftxui::FlexboxConfig::JustifyContent::SpaceBetween
@ SpaceBetween
Items are evenly distributed in the line; first item is on the start.
ftxui::FlexboxConfig::Set
FlexboxConfig & Set(FlexboxConfig::Direction)
Set the flexbox direction.
Definition flexbox_config.cpp:9
ftxui::paragraphAlignRight
Element paragraphAlignRight(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the right.
Definition src/ftxui/dom/paragraph.cpp:60
ftxui::paragraphAlignCenter
Element paragraphAlignCenter(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the center.
Definition src/ftxui/dom/paragraph.cpp:72
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::paragraphAlignLeft
Element paragraphAlignLeft(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the left.
Definition src/ftxui/dom/paragraph.cpp:49
ftxui::paragraphAlignJustify
Element paragraphAlignJustify(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned using a justified alignment....
Definition src/ftxui/dom/paragraph.cpp:85
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ftxui::FlexboxConfig
FlexboxConfig is a configuration structure that defines the layout properties for a flexbox container...
Definition flexbox_config.hpp:27
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::flexbox
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:251
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Elements
std::vector< Element > Elements
Definition elements.hpp:23
ftxui::paragraph
Elements paragraph(std::wstring text)