Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions include/ftxui/dom/take_any_args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
#define FTXUI_DOM_TAKE_ANY_ARGS_HPP

// IWYU pragma: private, include "ftxui/dom/elements.hpp"
#include <concepts>
#include <ftxui/dom/node.hpp>
#include <queue>
#include <ranges>
#include <stack>

namespace ftxui {

Expand All @@ -17,13 +21,34 @@ inline void Merge(Elements& container, Element element) {
container.push_back(std::move(element));
}

template <>
inline void Merge(Elements& container, Elements elements) {
template <class Container>
concept ElementRange =
std::ranges::range<Container> &&
std::same_as<std::ranges::range_value_t<Container>, Element>;

template <ElementRange T>
inline void Merge(Elements& container, T elements) {
for (auto& element : elements) {
container.push_back(std::move(element));
}
}

template <>
inline void Merge(Elements& container, std::stack<Element> elements) {
while (!elements.empty()) {
container.push_back(std::move(elements.top()));
elements.pop();
}
}

template <>
inline void Merge(Elements& container, std::queue<Element> elements) {
while (!elements.empty()) {
container.push_back(std::move(elements.back()));
elements.pop();
}
}

// Turn a set of arguments into a vector.
template <class... Args>
Elements unpack(Args... args) {
Expand Down
43 changes: 40 additions & 3 deletions src/ftxui/dom/hbox_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <gtest/gtest.h> // for Test, TestInfo (ptr only), EXPECT_EQ, Message, TEST, TestPartResult
#include <cstddef> // for size_t
#include <string> // for allocator, basic_string, string
#include <vector> // for vector
#include <array> // for array
#include <cstddef> // for size_t
#include <stack> // for stack
#include <string> // for allocator, basic_string, string
#include <unordered_set> // for unordered_set
#include <vector> // for vector

#include "ftxui/dom/elements.hpp" // for text, operator|, Element, flex_grow, flex_shrink, hbox
#include "ftxui/dom/node.hpp" // for Render
Expand Down Expand Up @@ -358,5 +361,39 @@ TEST(HBoxTest, FlexGrow_NoFlex_FlewShrink) {
}
}

TEST(HBoxTest, FromElementsContainer) {
Elements elements_vector{text("0"), text("1")};

std::array<Element, 2> elements_array{text("0"), text("1")};

std::deque<Element> elements_deque{text("0"), text("1")};

std::stack<Element> elements_stack;
elements_stack.push(text("1"));
elements_stack.push(text("0"));

std::queue<Element> elements_queue;
elements_queue.emplace(text("0"));
elements_queue.emplace(text("1"));

const std::vector<Element> collection_hboxes{
hbox(std::move(elements_vector)), hbox(std::move(elements_array)),
hbox(std::move(elements_stack)), hbox(std::move(elements_deque)),
hbox(std::move(elements_queue)),
};

for (const Element& collection_hbox : collection_hboxes) {
Screen screen(2, 1);
Render(screen, collection_hbox);
EXPECT_EQ("01", screen.ToString());
}

// Exception: unordered set, which has no guaranteed order.
std::unordered_set<Element> elements_set{text("0"), text("0")};
Screen screen(2, 1);
Render(screen, hbox(elements_set));
EXPECT_EQ("00", screen.ToString());
};

} // namespace ftxui
// NOLINTEND