Loading...
Searching...
No Matches
paragraph.cpp
Go to the documentation of this file.
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <functional> // for function
#include <sstream> // for basic_istream, stringstream
#include <string> // for string, allocator, getline
#include <utility> // for move
#include "ftxui/dom/elements.hpp" // for flexbox, Element, text, Elements, operator|, xflex, paragraph, paragraphAlignCenter, paragraphAlignJustify, paragraphAlignLeft, paragraphAlignRight
#include "ftxui/dom/flexbox_config.hpp" // for FlexboxConfig, FlexboxConfig::JustifyContent, FlexboxConfig::JustifyContent::Center, FlexboxConfig::JustifyContent::FlexEnd, FlexboxConfig::JustifyContent::SpaceBetween
namespace ftxui {
namespace {
Elements Split(const std::string& the_text) {
Elements output;
std::stringstream ss(the_text);
std::string word;
while (std::getline(ss, word, ' ')) {
output.push_back(text(word));
}
return output;
}
Element Split(const std::string& paragraph,
const std::function<Element(std::string)>& f) {
Elements output;
std::stringstream ss(paragraph);
std::string line;
while (std::getline(ss, line, '\n')) {
output.push_back(f(line));
}
return vbox(std::move(output));
}
} // namespace
/// @brief Return an element drawing the paragraph on multiple lines.
/// @ingroup dom
/// @see flexbox.
Element paragraph(const std::string& the_text) {
return paragraphAlignLeft(the_text);
}
/// @brief Return an element drawing the paragraph on multiple lines, aligned on
/// the left.
/// @ingroup dom
/// @see flexbox.
Element paragraphAlignLeft(const std::string& the_text) {
return Split(the_text, [](const std::string& line) {
static const auto config = FlexboxConfig().SetGap(1, 0);
return flexbox(Split(line), config);
});
};
/// @brief Return an element drawing the paragraph on multiple lines, aligned on
/// the right.
/// @ingroup dom
/// @see flexbox.
Element paragraphAlignRight(const std::string& the_text) {
return Split(the_text, [](const std::string& line) {
static const auto config = FlexboxConfig().SetGap(1, 0).Set(
FlexboxConfig::JustifyContent::FlexEnd);
return flexbox(Split(line), config);
});
}
/// @brief Return an element drawing the paragraph on multiple lines, aligned on
/// the center.
/// @ingroup dom
/// @see flexbox.
Element paragraphAlignCenter(const std::string& the_text) {
return Split(the_text, [](const std::string& line) {
static const auto config =
FlexboxConfig().SetGap(1, 0).Set(FlexboxConfig::JustifyContent::Center);
return flexbox(Split(line), config);
});
}
/// @brief Return an element drawing the paragraph on multiple lines, aligned
/// using a justified alignment.
/// the center.
/// @ingroup dom
/// @see flexbox.
Element paragraphAlignJustify(const std::string& the_text) {
return Split(the_text, [](const std::string& line) {
static const auto config = FlexboxConfig().SetGap(1, 0).Set(
FlexboxConfig::JustifyContent::SpaceBetween);
Elements words = Split(line);
words.push_back(text("") | xflex);
return flexbox(std::move(words), config);
});
}
} // namespace ftxui
elements.hpp
flexbox_config.hpp
ftxui
Definition animation.hpp:10
ftxui::xflex
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:129
ftxui::flexbox
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::paragraphAlignRight
Element paragraphAlignRight(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the right.
Definition paragraph.cpp:60
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::paragraphAlignCenter
Element paragraphAlignCenter(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the center.
Definition paragraph.cpp:72
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::Elements
std::vector< Element > Elements
Definition elements.hpp:23
ftxui::paragraphAlignLeft
Element paragraphAlignLeft(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the left.
Definition paragraph.cpp:49
ftxui::paragraph
Elements paragraph(std::wstring text)
ftxui::paragraphAlignJustify
Element paragraphAlignJustify(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned using a justified alignment....
Definition paragraph.cpp:85
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ftxui::FlexboxConfig
Definition flexbox_config.hpp:15
ftxui::FlexboxConfig::SetGap
FlexboxConfig & SetGap(int gap_x, int gap_y)
Set the flexbox flex direction.
Definition flexbox_config.cpp:45
ftxui::FlexboxConfig::JustifyContent::Center
@ Center
Items are centered along the line.
ftxui::FlexboxConfig::JustifyContent::FlexEnd
@ FlexEnd
Items are aligned to the end of flexbox's direction.
ftxui::FlexboxConfig::JustifyContent::SpaceBetween
@ SpaceBetween
Items are evenly distributed in the line; first item is on the start.
ftxui::FlexboxConfig::Set
FlexboxConfig & Set(FlexboxConfig::Direction)
Set the flexbox direction.
Definition flexbox_config.cpp:10