Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table: support initializer list constructor. #915

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
1 change: 1 addition & 0 deletions include/ftxui/dom/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Table {
Table();
explicit Table(std::vector<std::vector<std::string>>);
explicit Table(std::vector<std::vector<Element>>);
Table(std::initializer_list<std::vector<std::string>> init);
TableSelection SelectAll();
TableSelection SelectCell(int column, int row);
TableSelection SelectRow(int row_index);
Expand Down
16 changes: 16 additions & 0 deletions src/ftxui/dom/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ Table::Table(std::vector<std::vector<Element>> input) {
Initialize(std::move(input));
}

// @brief Create a table from a list of list of string.
// @param init The input data.
// @ingroup dom
Table::Table(std::initializer_list<std::vector<std::string>> init) {
std::vector<std::vector<Element>> input;
for (const auto& row : init) {
std::vector<Element> output_row;
output_row.reserve(row.size());
for (const auto& cell : row) {
output_row.push_back(text(cell));
}
input.push_back(std::move(output_row));
}
Initialize(std::move(input));
}

// private
void Table::Initialize(std::vector<std::vector<Element>> input) {
input_dim_y_ = static_cast<int>(input.size());
Expand Down
12 changes: 12 additions & 0 deletions src/ftxui/dom/table_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,5 +733,17 @@ TEST(TableTest, Merge) {
screen.ToString());
}

TEST(TableTest, Issue912) {
Table({
{"a"},
});
Table({
{"a", "b"},
});
Table({
{"a", "b", "c"},
});
}

} // namespace ftxui
// NOLINTEND
Loading