diff --git a/include/ftxui/dom/table.hpp b/include/ftxui/dom/table.hpp index 6cfc86f46b8e8abe390b4a2374163b4cb23300ee..5460237dc7c4b99e2ca9fec2a6bb532c6474a4eb 100644 --- a/include/ftxui/dom/table.hpp +++ b/include/ftxui/dom/table.hpp @@ -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); diff --git a/src/ftxui/dom/table.cpp b/src/ftxui/dom/table.cpp index eb63259607226419f15df1788d932fbbeb28561b..e65b2dda5f3ab580e1167d0d853b9355030cef37 100644 --- a/src/ftxui/dom/table.cpp +++ b/src/ftxui/dom/table.cpp @@ -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()); diff --git a/src/ftxui/dom/table_test.cpp b/src/ftxui/dom/table_test.cpp index 215f1f1dd4c2c797cd531c7462c8021f63e393e8..bae224bb98ea9ddac3c0d8ca70087471ebbeba7f 100644 --- a/src/ftxui/dom/table_test.cpp +++ b/src/ftxui/dom/table_test.cpp @@ -733,5 +733,17 @@ TEST(TableTest, Merge) { screen.ToString()); } +TEST(TableTest, Issue912) { + Table({ + {"a"}, + }); + Table({ + {"a", "b"}, + }); + Table({ + {"a", "b", "c"}, + }); +} + } // namespace ftxui // NOLINTEND