2021-08-06 20:32:33 +02:00
|
|
|
#include <algorithm> // for min
|
2021-05-01 20:40:35 +02:00
|
|
|
#include <functional> // for function
|
2021-08-06 20:32:33 +02:00
|
|
|
#include <memory> // for __shared_ptr_access
|
2021-05-01 20:40:35 +02:00
|
|
|
#include <utility> // for move
|
2021-05-14 22:00:49 +02:00
|
|
|
#include <vector> // for vector
|
2021-05-01 20:40:35 +02:00
|
|
|
|
2021-08-06 20:32:33 +02:00
|
|
|
#include "ftxui/dom/elements.hpp" // for Element, Decorator, Elements, operator|, Fit, nothing
|
|
|
|
|
#include "ftxui/dom/node.hpp" // for Node
|
|
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement
|
|
|
|
|
#include "ftxui/screen/screen.hpp" // for Full
|
|
|
|
|
#include "ftxui/screen/terminal.hpp" // for Dimensions
|
2019-01-02 22:33:59 +01:00
|
|
|
|
2019-01-12 15:00:08 +01:00
|
|
|
namespace ftxui {
|
2019-01-02 22:33:59 +01:00
|
|
|
|
2021-07-10 14:23:46 +02:00
|
|
|
namespace {
|
2019-01-03 00:35:59 +01:00
|
|
|
Decorator compose(Decorator a, Decorator b) {
|
2020-03-22 22:32:44 +01:00
|
|
|
return [a = std::move(a), b = std::move(b)](Element element) {
|
2019-01-05 02:03:49 +01:00
|
|
|
return b(a(std::move(element)));
|
2019-01-03 00:35:59 +01:00
|
|
|
};
|
|
|
|
|
}
|
2021-07-10 14:23:46 +02:00
|
|
|
} // namespace
|
2020-08-16 02:24:50 +02:00
|
|
|
|
|
|
|
|
/// @brief A decoration doing absolutely nothing.
|
|
|
|
|
/// @ingroup dom
|
|
|
|
|
Element nothing(Element element) {
|
|
|
|
|
return element;
|
|
|
|
|
}
|
2019-01-03 00:35:59 +01:00
|
|
|
|
2020-08-16 02:24:50 +02:00
|
|
|
/// @brief Compose two decorator into one.
|
|
|
|
|
/// @ingroup dom
|
|
|
|
|
///
|
|
|
|
|
/// ### Example
|
|
|
|
|
///
|
|
|
|
|
/// ```cpp
|
|
|
|
|
/// auto decorator = bold | blink;
|
|
|
|
|
/// ```
|
2019-01-05 02:03:49 +01:00
|
|
|
Decorator operator|(Decorator a, Decorator b) {
|
|
|
|
|
return compose(a, b);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 02:24:50 +02:00
|
|
|
/// @brief From a set of element, apply a decorator to every elements.
|
|
|
|
|
/// @return the set of decorated element.
|
|
|
|
|
/// @ingroup dom
|
|
|
|
|
Elements operator|(Elements elements, Decorator decorator) {
|
2019-01-26 21:52:55 +01:00
|
|
|
Elements output;
|
2020-08-16 02:24:50 +02:00
|
|
|
for (auto& it : elements)
|
|
|
|
|
output.push_back(std::move(it) | decorator);
|
2019-01-26 21:52:55 +01:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 02:24:50 +02:00
|
|
|
/// @brief From an element, apply a decorator.
|
|
|
|
|
/// @return the decorated element.
|
|
|
|
|
/// @ingroup dom
|
2020-09-06 13:46:56 +02:00
|
|
|
///
|
2020-08-16 02:24:50 +02:00
|
|
|
/// ### Example
|
|
|
|
|
///
|
|
|
|
|
/// Both of these are equivalent:
|
|
|
|
|
/// ```cpp
|
2021-08-08 23:25:20 +02:00
|
|
|
/// bold(text("Hello"));
|
2020-08-16 02:24:50 +02:00
|
|
|
/// ```
|
|
|
|
|
/// ```cpp
|
2021-08-08 23:25:20 +02:00
|
|
|
/// text("Hello") | bold;
|
2020-08-16 02:24:50 +02:00
|
|
|
/// ```
|
|
|
|
|
Element operator|(Element element, Decorator decorator) {
|
|
|
|
|
return decorator(std::move(element));
|
2019-01-05 02:03:49 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 02:49:29 +05:30
|
|
|
/// The minimal dimension that will fit the given element.
|
|
|
|
|
/// @see Fixed
|
|
|
|
|
/// @see Full
|
|
|
|
|
Dimensions Dimension::Fit(Element& e) {
|
|
|
|
|
e->ComputeRequirement();
|
|
|
|
|
Dimensions size = Dimension::Full();
|
|
|
|
|
return {std::min(e->requirement().min_x, size.dimx),
|
|
|
|
|
std::min(e->requirement().min_y, size.dimy)};
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 23:04:11 +02:00
|
|
|
/// An element of size 0x0 drawing nothing.
|
|
|
|
|
/// @ingroup dom
|
|
|
|
|
Element emptyElement() {
|
|
|
|
|
class Impl : public Node {
|
|
|
|
|
void ComputeRequirement() override {
|
|
|
|
|
requirement_.min_x = 0;
|
|
|
|
|
requirement_.min_x = 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return std::make_unique<Impl>();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:00:08 +01:00
|
|
|
} // namespace ftxui
|
2020-08-16 00:24:18 +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.
|