Files
FTXUI/src/ftxui/dom/paragraph.cpp

96 lines
3.2 KiB
C++
Raw Normal View History

2023-08-19 13:56:36 +02:00
// 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.
2025-03-22 17:30:34 +01:00
#include <functional> // for function
#include <sstream> // for basic_istream, stringstream
#include <string> // for string, allocator, getline
#include <utility> // for move
2020-03-22 22:32:44 +01:00
#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
2019-01-23 00:26:36 +01:00
namespace ftxui {
namespace {
2022-03-31 02:17:43 +02:00
Elements Split(const std::string& the_text) {
Elements output;
std::stringstream ss(the_text);
std::string word;
2022-03-31 02:17:43 +02:00
while (std::getline(ss, word, ' ')) {
output.push_back(text(word));
2022-03-31 02:17:43 +02:00
}
2019-01-23 00:26:36 +01:00
return output;
}
Element Split(const std::string& paragraph,
2025-03-22 17:30:34 +01:00
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.
2022-03-31 02:17:43 +02:00
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.
2022-03-31 02:17:43 +02:00
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.
2022-03-31 02:17:43 +02:00
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.
2022-03-31 02:17:43 +02:00
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.
2022-03-31 02:17:43 +02:00
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);
});
}
2019-01-23 00:26:36 +01:00
} // namespace ftxui