diff --git a/CHANGELOG.md b/CHANGELOG.md index 28a6e38..1c4f162 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ unreleased (development) - `paragraphAlignJustify` - Add the helper elements based on `flexbox`: `hflow()`, `vflow()`. - Add: `focusPositionRelative` and `focusPosition` +- Add `Table` constructor from 2D vector of Element, instead of string. #### Component - Add the `collapsible` component. diff --git a/include/ftxui/dom/table.hpp b/include/ftxui/dom/table.hpp index e4a4a41..23e0fa4 100644 --- a/include/ftxui/dom/table.hpp +++ b/include/ftxui/dom/table.hpp @@ -33,7 +33,9 @@ class TableSelection; class Table { public: + Table(); Table(std::vector>); + Table(std::vector>); TableSelection SelectAll(); TableSelection SelectCell(int column, int row); TableSelection SelectRow(int row_index); @@ -47,6 +49,7 @@ class Table { Element Render(); private: + void Initialize(std::vector>); friend TableSelection; std::vector> elements_; int input_dim_x_; diff --git a/src/ftxui/dom/table.cpp b/src/ftxui/dom/table.cpp index 5a93445..6333a25 100644 --- a/src/ftxui/dom/table.cpp +++ b/src/ftxui/dom/table.cpp @@ -35,7 +35,27 @@ void Order(int& a, int& b) { } // namespace +Table::Table() { + Initialize({}); +} + Table::Table(std::vector> input) { + std::vector> output; + for(auto& row : input) { + output.push_back({}); + auto& output_row = output.back(); + for(auto& cell : row) { + output_row.push_back(text(cell)); + } + } + Initialize(std::move(output)); +} + +Table::Table(std::vector> input) { + Initialize(std::move(input)); +} + +void Table::Initialize(std::vector> input) { input_dim_y_ = input.size(); input_dim_x_ = 0; for (auto& row : input) @@ -55,7 +75,7 @@ Table::Table(std::vector> input) { for (auto& row : input) { int x = 1; for (auto& cell : row) { - elements_[y][x] = text(cell); + elements_[y][x] = std::move(cell); x += 2; } y += 2; diff --git a/src/ftxui/dom/table_test.cpp b/src/ftxui/dom/table_test.cpp index de72ba0..0cb4a92 100644 --- a/src/ftxui/dom/table_test.cpp +++ b/src/ftxui/dom/table_test.cpp @@ -12,7 +12,7 @@ using namespace ftxui; TEST(TableTest, Empty) { - auto table = Table({}); + auto table = Table(); Screen screen(5, 5); Render(screen, table.Render()); EXPECT_EQ(