Add Table constructor from Elements. (#310)

This commit is contained in:
Arthur Sonzogni 2022-01-16 16:46:32 +01:00 committed by GitHub
parent feb24b9498
commit 382205c057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 2 deletions

View File

@ -20,6 +20,7 @@ unreleased (development)
- `paragraphAlignJustify` - `paragraphAlignJustify`
- Add the helper elements based on `flexbox`: `hflow()`, `vflow()`. - Add the helper elements based on `flexbox`: `hflow()`, `vflow()`.
- Add: `focusPositionRelative` and `focusPosition` - Add: `focusPositionRelative` and `focusPosition`
- Add `Table` constructor from 2D vector of Element, instead of string.
#### Component #### Component
- Add the `collapsible` component. - Add the `collapsible` component.

View File

@ -33,7 +33,9 @@ class TableSelection;
class Table { class Table {
public: public:
Table();
Table(std::vector<std::vector<std::string>>); Table(std::vector<std::vector<std::string>>);
Table(std::vector<std::vector<Element>>);
TableSelection SelectAll(); TableSelection SelectAll();
TableSelection SelectCell(int column, int row); TableSelection SelectCell(int column, int row);
TableSelection SelectRow(int row_index); TableSelection SelectRow(int row_index);
@ -47,6 +49,7 @@ class Table {
Element Render(); Element Render();
private: private:
void Initialize(std::vector<std::vector<Element>>);
friend TableSelection; friend TableSelection;
std::vector<std::vector<Element>> elements_; std::vector<std::vector<Element>> elements_;
int input_dim_x_; int input_dim_x_;

View File

@ -35,7 +35,27 @@ void Order(int& a, int& b) {
} // namespace } // namespace
Table::Table() {
Initialize({});
}
Table::Table(std::vector<std::vector<std::string>> input) { Table::Table(std::vector<std::vector<std::string>> input) {
std::vector<std::vector<Element>> 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<std::vector<Element>> input) {
Initialize(std::move(input));
}
void Table::Initialize(std::vector<std::vector<Element>> input) {
input_dim_y_ = input.size(); input_dim_y_ = input.size();
input_dim_x_ = 0; input_dim_x_ = 0;
for (auto& row : input) for (auto& row : input)
@ -55,7 +75,7 @@ Table::Table(std::vector<std::vector<std::string>> input) {
for (auto& row : input) { for (auto& row : input) {
int x = 1; int x = 1;
for (auto& cell : row) { for (auto& cell : row) {
elements_[y][x] = text(cell); elements_[y][x] = std::move(cell);
x += 2; x += 2;
} }
y += 2; y += 2;

View File

@ -12,7 +12,7 @@
using namespace ftxui; using namespace ftxui;
TEST(TableTest, Empty) { TEST(TableTest, Empty) {
auto table = Table({}); auto table = Table();
Screen screen(5, 5); Screen screen(5, 5);
Render(screen, table.Render()); Render(screen, table.Render());
EXPECT_EQ( EXPECT_EQ(