FTXUI 7.0.3
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(std::string_view the_text) {
17 Elements output;
18 size_t start = 0;
19 size_t end = the_text.find(' ');
20 while (end != std::string_view::npos) {
21 output.push_back(text(the_text.substr(start, end - start)));
22 start = end + 1;
23 end = the_text.find(' ', start);
24 }
25 output.push_back(text(the_text.substr(start)));
26 return output;
27}
28
29Element Split(std::string_view paragraph,
30 const std::function<Element(std::string_view)>& f) {
31 Elements output;
32 size_t start = 0;
33 size_t end = paragraph.find('\n');
34 while (end != std::string_view::npos) {
35 output.push_back(f(paragraph.substr(start, end - start)));
36 start = end + 1;
37 end = paragraph.find('\n', start);
38 }
39 output.push_back(f(paragraph.substr(start)));
40 return vbox(std::move(output));
41}
42
43} // namespace
44
45/// @brief Renvoie un élément affichant le paragraphe sur plusieurs lignes.
46/// @ingroup dom
47/// @see flexbox.
48Element paragraph(std::string_view the_text) {
49 return paragraphAlignLeft(the_text);
50}
51
52/// @brief Renvoie un élément affichant le paragraphe sur plusieurs lignes, aligné à
53/// gauche.
54/// @ingroup dom
55/// @see flexbox.
56Element paragraphAlignLeft(std::string_view the_text) {
57 return Split(the_text, [](std::string_view line) {
58 static const auto config = FlexboxConfig().SetGap(1, 0);
59 return flexbox(Split(line), config);
60 });
61};
62
63/// @brief Renvoie un élément affichant le paragraphe sur plusieurs lignes, aligné à
64/// droite.
65/// @ingroup dom
66/// @see flexbox.
67Element paragraphAlignRight(std::string_view the_text) {
68 return Split(the_text, [](std::string_view line) {
69 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
70 FlexboxConfig::JustifyContent::FlexEnd);
71 return flexbox(Split(line), config);
72 });
73}
74
75/// @brief Renvoie un élément affichant le paragraphe sur plusieurs lignes, aligné au
76/// centre.
77/// @ingroup dom
78/// @see flexbox.
79Element paragraphAlignCenter(std::string_view the_text) {
80 return Split(the_text, [](std::string_view line) {
81 static const auto config =
82 FlexboxConfig().SetGap(1, 0).Set(FlexboxConfig::JustifyContent::Center);
83 return flexbox(Split(line), config);
84 });
85}
86
87/// @brief Renvoie un élément affichant le paragraphe sur plusieurs lignes, aligné
88/// en utilisant un alignement justifié.
89/// @ingroup dom
90/// @see flexbox.
91Element paragraphAlignJustify(std::string_view the_text) {
92 return Split(the_text, [](std::string_view line) {
93 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
94 FlexboxConfig::JustifyContent::SpaceBetween);
95 Elements words = Split(line);
96 words.push_back(text("") | xflex);
97 return flexbox(std::move(words), config);
98 });
99}
100
101} // namespace ftxui
Element text(std::string_view text)
Affiche un morceau de texte unicode encodé en UTF8.
Element paragraphAlignLeft(std::string_view the_text)
Renvoie un élément affichant le paragraphe sur plusieurs lignes, aligné à gauche.
Element paragraphAlignJustify(std::string_view the_text)
Renvoie un élément affichant le paragraphe sur plusieurs lignes, aligné en utilisant un alignement ju...
Element paragraphAlignCenter(std::string_view the_text)
Renvoie un élément affichant le paragraphe sur plusieurs lignes, aligné au centre.
Element paragraph(std::string_view the_text)
Renvoie un élément affichant le paragraphe sur plusieurs lignes.
Element paragraphAlignRight(std::string_view the_text)
Renvoie un élément affichant le paragraphe sur plusieurs lignes, aligné à droite.
Element vbox(Elements children)
Un conteneur affichant les éléments verticalement un par un.
Definition vbox.cpp:96
L'espace de noms FTXUI ftxui::
Definition animation.hpp:11
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
Un conteneur affichant des éléments sur des lignes/colonnes et capable de passer à la colonne/ligne s...
Definition flexbox.cpp:252
std::shared_ptr< Node > Element
Definition elements.hpp:25
std::vector< Element > Elements
Definition elements.hpp:26