FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
src/ftxui/dom/paragraph.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni. 保留所有权利。
2// 本源代码的使用受 MIT 许可的约束,该许可可在以下文件中找到:
3// LICENSE 文件。
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::JustifyConfig::Center, FlexboxConfig::JustifyConfig::FlexEnd, FlexboxConfig::JustifyConfig::SpaceBetween
11
12namespace ftxui {
13
14namespace {
15Elements 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
25Element 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 返回一个在多行上绘制段落的元素。
39/// @ingroup dom
40/// @see flexbox.
41Element paragraph(const std::string& the_text) {
42 return paragraphAlignLeft(the_text);
43}
44
45/// @brief 返回一个在多行上绘制段落并左对齐的元素。
46/// @ingroup dom
47/// @see flexbox.
48Element paragraphAlignLeft(const std::string& the_text) {
49 return Split(the_text, [](const std::string& line) {
50 static const auto config = FlexboxConfig().SetGap(1, 0);
51 return flexbox(Split(line), config);
52 });
53};
54
55/// @brief 返回一个在多行上绘制段落并右对齐的元素。
56/// @ingroup dom
57/// @see flexbox.
58Element paragraphAlignRight(const std::string& the_text) {
59 return Split(the_text, [](const std::string& line) {
60 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
62 return flexbox(Split(line), config);
63 });
64}
65
66/// @brief 返回一个在多行上绘制段落并居中对齐的元素。
67/// @ingroup dom
68/// @see flexbox.
69Element paragraphAlignCenter(const std::string& the_text) {
70 return Split(the_text, [](const std::string& line) {
71 static const auto config =
73 return flexbox(Split(line), config);
74 });
75}
76
77/// @brief 返回一个在多行上绘制段落并使用两端对齐方式的元素。
78/// 居中对齐。
79/// @ingroup dom
80/// @see flexbox.
81Element paragraphAlignJustify(const std::string& the_text) {
82 return Split(the_text, [](const std::string& line) {
83 static const auto config = FlexboxConfig().SetGap(1, 0).Set(
85 Elements words = Split(line);
86 words.push_back(text("") | xflex);
87 return flexbox(std::move(words), config);
88 });
89}
90
91} // namespace ftxui
FlexboxConfig & SetGap(int gap_x, int gap_y)
设置 flexbox flex direction。
@ 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文本。
Element paragraphAlignLeft(const std::string &text)
返回一个在多行上绘制段落并左对齐的元素。
Element paragraphAlignJustify(const std::string &text)
返回一个在多行上绘制段落并使用两端对齐方式的元素。 居中对齐。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
FlexboxConfig 是一个配置结构体,定义了弹性盒子容器的布局属性。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
一个容器,用于在行/列中显示元素,并且在填满时能够换行到下一列/行。
std::shared_ptr< Node > Element
std::vector< Element > Elements
Elements paragraph(std::wstring text)