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 回傳一個在多行上繪製段落的元素。
46/// @ingroup dom
47/// @see flexbox.
48Element paragraph(std::string_view the_text) {
49 return paragraphAlignLeft(the_text);
50}
51
52/// @brief 回傳一個在多行上繪製段落並靠左對齊的元素。
53/// @ingroup dom
54/// @see flexbox.
55Element paragraphAlignLeft(std::string_view the_text) {
56 return Split(the_text, [](std::string_view line) {
57 static const auto config = FlexboxConfig().SetGap(1, 0);
58 return flexbox(Split(line), config);
59 });
60};
61
62/// @brief 回傳一個在多行上繪製段落並靠右對齊的元素。
63/// @ingroup dom
64/// @see flexbox.
65Element paragraphAlignRight(std::string_view the_text) {
66 return Split(the_text, [](std::string_view line) {
67 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
68 FlexboxConfig::JustifyContent::FlexEnd);
69 return flexbox(Split(line), config);
70 });
71}
72
73/// @brief 回傳一個在多行上繪製段落並置中對齊的元素。
74/// @ingroup dom
75/// @see flexbox.
76Element paragraphAlignCenter(std::string_view the_text) {
77 return Split(the_text, [](std::string_view line) {
78 static const auto config =
79 FlexboxConfig().SetGap(1, 0).Set(FlexboxConfig::JustifyContent::Center);
80 return flexbox(Split(line), config);
81 });
82}
83
84/// @brief 回傳一個在多行上繪製段落並使用左右對齊的元素。
85/// @ingroup dom
86/// @see flexbox.
87Element paragraphAlignJustify(std::string_view the_text) {
88 return Split(the_text, [](std::string_view line) {
89 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
90 FlexboxConfig::JustifyContent::SpaceBetween);
91 Elements words = Split(line);
92 words.push_back(text("") | xflex);
93 return flexbox(std::move(words), config);
94 });
95}
96
97} // namespace ftxui
Element text(std::string_view text)
顯示一段 UTF8 編碼的 Unicode 文字。
Element paragraphAlignLeft(std::string_view the_text)
回傳一個在多行上繪製段落並靠左對齊的元素。
Element paragraphAlignJustify(std::string_view the_text)
回傳一個在多行上繪製段落並使用左右對齊的元素。
Element paragraphAlignCenter(std::string_view the_text)
回傳一個在多行上繪製段落並置中對齊的元素。
Element paragraph(std::string_view the_text)
回傳一個在多行上繪製段落的元素。
Element paragraphAlignRight(std::string_view the_text)
回傳一個在多行上繪製段落並靠右對齊的元素。
Element vbox(Elements children)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:96
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
一個容器,用於在行/列中顯示元素,並在滿時能夠換行到下一列/行。
Definition flexbox.cpp:251
std::shared_ptr< Node > Element
Definition elements.hpp:25
std::vector< Element > Elements
Definition elements.hpp:26