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