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// 版權所有 2020 Arthur Sonzogni。保留所有權利。
2// 本原始碼的使用受 MIT 授權條款約束,該條款可在 LICENSE 檔案中找到。
3#include <functional> // for function
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
11namespace ftxui {
12
13namespace {
14Elements 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
24Element Split(const std::string& paragraph,
25 const std::function<Element(std::string)>& f) {
26 Elements output;
27 std::stringstream ss(paragraph);
28 std::string line;
29 while (std::getline(ss, line, '\n')) {
30 output.push_back(f(line));
31 }
32 return vbox(std::move(output));
33}
34
35} // namespace
36
37/// @brief 回傳一個在多行上繪製段落的元素。
38/// @ingroup dom
39/// @see flexbox.
40Element paragraph(const std::string& the_text) {
41 return paragraphAlignLeft(the_text);
42}
43
44/// @brief 回傳一個在多行上繪製段落並靠左對齊的元素。
45/// @ingroup dom
46/// @see flexbox.
47Element paragraphAlignLeft(const std::string& the_text) {
48 return Split(the_text, [](const std::string& line) {
49 static const auto config = FlexboxConfig().SetGap(1, 0);
50 return flexbox(Split(line), config);
51 });
52};
53
54/// @brief 回傳一個在多行上繪製段落並靠右對齊的元素。
55/// @ingroup dom
56/// @see flexbox.
57Element paragraphAlignRight(const std::string& the_text) {
58 return Split(the_text, [](const std::string& line) {
59 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
61 return flexbox(Split(line), config);
62 });
63}
64
65/// @brief 回傳一個在多行上繪製段落並置中對齊的元素。
66/// @ingroup dom
67/// @see flexbox.
68Element paragraphAlignCenter(const std::string& the_text) {
69 return Split(the_text, [](const std::string& line) {
70 static const auto config =
72 return flexbox(Split(line), config);
73 });
74}
75
76/// @brief 回傳一個在多行上繪製段落並使用左右對齊的元素。
77/// @ingroup dom
78/// @see flexbox.
79Element paragraphAlignJustify(const std::string& the_text) {
80 return Split(the_text, [](const std::string& line) {
81 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
83 Elements words = Split(line);
84 words.push_back(text("") | xflex);
85 return flexbox(std::move(words), config);
86 });
87}
88
89} // namespace ftxui
FlexboxConfig & SetGap(int gap_x, int gap_y)
設定 flexbox 彈性方向。
@ FlexEnd
項目對齊到彈性盒方向的終點。
@ SpaceBetween
項目在行中平均分佈;第一個項目在起始線,最後一個項目在結束線。
FlexboxConfig & Set(FlexboxConfig::Direction)
設定 flexbox 方向。
Element paragraphAlignRight(const std::string &text)
回傳一個在多行上繪製段落並靠右對齊的元素。
Element paragraphAlignCenter(const std::string &text)
回傳一個在多行上繪製段落並置中對齊的元素。
Element text(std::wstring text)
顯示一段 Unicode 文字。
Definition text.cpp:160
Element paragraphAlignLeft(const std::string &text)
回傳一個在多行上繪製段落並靠左對齊的元素。
Element paragraphAlignJustify(const std::string &text)
回傳一個在多行上繪製段落並使用左右對齊的元素。
Element vbox(Elements)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:95
FlexboxConfig 是一個配置結構,用於定義彈性盒容器的佈局屬性。
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
一個容器,用於在行/列中顯示元素,並在滿時能夠換行到下一列/行。
Definition flexbox.cpp:249
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::vector< Element > Elements
Definition elements.hpp:23
Elements paragraph(std::wstring text)