diff --git a/include/ftxui/dom/table.hpp b/include/ftxui/dom/table.hpp index 6cfc86f..5460237 100644 --- a/include/ftxui/dom/table.hpp +++ b/include/ftxui/dom/table.hpp @@ -38,6 +38,7 @@ class Table { Table(); explicit Table(std::vector>); explicit Table(std::vector>); + Table(std::initializer_list> 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 eb63259..e65b2dd 100644 --- a/src/ftxui/dom/table.cpp +++ b/src/ftxui/dom/table.cpp @@ -71,6 +71,22 @@ Table::Table(std::vector> 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> init) { + std::vector> input; + for (const auto& row : init) { + std::vector 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> input) { input_dim_y_ = static_cast(input.size()); diff --git a/src/ftxui/dom/table_test.cpp b/src/ftxui/dom/table_test.cpp index 215f1f1..bae224b 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