Skip to content

Commit

Permalink
Table (#229)
Browse files Browse the repository at this point in the history
This class allows rendering stylized table.
  • Loading branch information
ArthurSonzogni authored Oct 15, 2021
1 parent 7298636 commit 026a005
Show file tree
Hide file tree
Showing 21 changed files with 1,507 additions and 119 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ Added:
- Fix the automated merge of borders.

### Dom
- `vscroll_indicator`. Show a scrollbar indicator on the right.
- `Table()` class to build stylised table.
See https://github.com/ArthurSonzogni/FTXUI/discussions/228
- `vscroll_indicator`. Show a scrollbar indicator on the right.
- `separatorEmpty`. A separator drawing nothing.
- `separatorFixed`. A separator drawing the provided character.

### Component
- `Maybe`: Display an component conditionnally based on a boolean.
- `Dropdown`: A dropdown select list.
- `Maybe`: Display an component conditionnally based on a boolean.
- `Dropdown`: A dropdown select list.

0.9 (2021-09-26)
----------------
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ add_library(dom
src/ftxui/dom/separator.cpp
src/ftxui/dom/size.cpp
src/ftxui/dom/spinner.cpp
src/ftxui/dom/table.cpp
src/ftxui/dom/text.cpp
src/ftxui/dom/underlined.cpp
src/ftxui/dom/util.cpp
Expand Down
1 change: 1 addition & 0 deletions cmake/ftxui_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_executable(tests
src/ftxui/component/terminal_input_parser_test.cpp
src/ftxui/component/toggle_test.cpp
src/ftxui/dom/gauge_test.cpp
src/ftxui/dom/table_test.cpp
src/ftxui/dom/gridbox_test.cpp
src/ftxui/dom/hbox_test.cpp
src/ftxui/dom/text_test.cpp
Expand Down
29 changes: 13 additions & 16 deletions examples/component/dropdown.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
#include <functional> // for function
#include <iostream> // for basic_ostream::operator<<, operator<<, endl, basic_ostream, basic_ostream<>::__ostream_type, cout, ostream
#include <string> // for string, basic_string, allocator
#include <vector> // for vector
#include <string> // for basic_string, string, allocator
#include <vector> // for vector

#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Menu
#include "ftxui/component/component_options.hpp" // for MenuOption
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Dropdown, Horizontal, Vertical
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive

int main(int argc, const char* argv[]) {
using namespace ftxui;

std::vector<std::string> entries = {
"tribute", "clearance", "ally", "bend", "electronics",
"module", "era", "cultural", "sniff", "nationalism",
"negotiation", "deliver", "figure", "east",
"tribute", "clearance", "ally", "bend", "electronics",
"module", "era", "cultural", "sniff", "nationalism",
"negotiation", "deliver", "figure", "east",
"tribute", "clearance", "ally", "bend", "electronics",
"module", "era", "cultural", "sniff", "nationalism",
"negotiation", "deliver", "figure", "east",
"tribute", "clearance", "ally", "bend", "electronics",
"module", "era", "cultural", "sniff", "nationalism",
"negotiation", "deliver", "figure", "east", "tribute",
"clearance", "ally", "bend", "electronics", "module",
"era", "cultural", "sniff", "nationalism", "negotiation",
"deliver", "figure", "east", "tribute", "clearance",
"ally", "bend", "electronics", "module", "era",
"cultural", "sniff", "nationalism", "negotiation", "deliver",
"figure", "east",
};

int selected_1 = 0;
Expand Down
17 changes: 8 additions & 9 deletions examples/component/maybe.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#include <functional> // for function
#include <iostream> // for basic_ostream::operator<<, operator<<, endl, basic_ostream, basic_ostream<>::__ostream_type, cout, ostream
#include <string> // for string, basic_string, allocator
#include <vector> // for vector
#include <memory> // for shared_ptr, __shared_ptr_access
#include <string> // for string, basic_string, allocator
#include <vector> // for vector

#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Menu
#include "ftxui/component/component_options.hpp" // for MenuOption
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Maybe, Radiobox, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, border

using namespace ftxui;
Component Border(Component child) {
return Renderer(child, [child] { return child->Render() | border; });
}

int main(int argc, const char* argv[]) {

std::vector<std::string> entries = {
"entry 1",
"entry 2",
Expand Down
9 changes: 5 additions & 4 deletions examples/dom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ set(DIRECTORY_LIB dom)
example(border)
example(border_style)
example(color_gallery)
example(color_info_palette256)
example(color_truecolor_HSV)
example(color_truecolor_RGB)
example(dbox)
example(gauge)
example(graph)
example(gridbox)
example(hflow)
example(html_like)
example(package_manager)
Expand All @@ -17,13 +21,10 @@ example(spinner)
example(style_blink)
example(style_bold)
example(style_color)
example(color_truecolor_RGB)
example(color_truecolor_HSV)
example(color_info_palette256)
example(style_dim)
example(gridbox)
example(style_gallery)
example(style_inverted)
example(style_underlined)
example(table)
example(vbox_hbox)
example(window)
66 changes: 66 additions & 0 deletions examples/dom/table.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <ftxui/dom/elements.hpp> // for color, Fit, LIGHT, align_right, bold, DOUBLE
#include <ftxui/dom/table.hpp> // for Table, TableSelection
#include <ftxui/screen/screen.hpp> // for Screen
#include <iostream> // for endl, cout, ostream
#include <string> // for basic_string, allocator, string
#include <vector> // for vector

#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::Cyan, Color::White

int main(int argc, const char* argv[]) {
using namespace ftxui;

auto table = Table({
{"Version", "Marketing name", "Release date", "API level", "Runtime"},
{"2.3", "Gingerbread", "February 9 2011", "10", "Dalvik 1.4.0"},
{"4.0", "Ice Cream Sandwich", "October 19 2011", "15", "Dalvik"},
{"4.1", "Jelly Bean", "July 9 2012", "16", "Dalvik"},
{"4.2", "Jelly Bean", "November 13 2012", "17", "Dalvik"},
{"4.3", "Jelly Bean", "July 24 2013", "18", "Dalvik"},
{"4.4", "KitKat", "October 31 2013", "19", "Dalvik and ART"},
{"5.0", "Lollipop", "November 3 2014", "21", "ART"},
{"5.1", "Lollipop", "March 9 2015", "22", "ART"},
{"6.0", "Marshmallow", "October 5 2015", "23", "ART"},
{"7.0", "Nougat", "August 22 2016", "24", "ART"},
{"7.1", "Nougat", "October 4 2016", "25", "ART"},
{"8.0", "Oreo", "August 21 2017", "26", "ART"},
{"8.1", "Oreo", "December 5 2017", "27", "ART"},
{"9", "Pie", "August 6 2018", "28", "ART"},
{"10", "10", "September 3 2019", "29", "ART"},
{"11", "11", "September 8 2020", "30", "ART"},
});

table.SelectAll().Border(LIGHT);

// Add border around the first column.
table.SelectColumn(0).Border(LIGHT);

// Make first row bold with a double border.
table.SelectRow(0).Decorate(bold);
table.SelectRow(0).SeparatorVertical(LIGHT);
table.SelectRow(0).Border(DOUBLE);

// Align right the "Release date" column.
table.SelectColumn(2).DecorateCells(align_right);

// Select row from the second to the last.
auto content = table.SelectRows(1, -1);
// Alternate in between 3 colors.
content.DecorateCellsAlternateRow(color(Color::Blue), 3, 0);
content.DecorateCellsAlternateRow(color(Color::Cyan), 3, 1);
content.DecorateCellsAlternateRow(color(Color::White), 3, 2);

auto document = table.Render();
auto screen = Screen::Create(Dimension::Fit(document));
Render(screen, document);
screen.Print();
std::cout << std::endl;

return 0;
}

// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
1 change: 1 addition & 0 deletions include/ftxui/component/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct Event {

//--- State section ----------------------------------------------------------
ScreenInteractive* screen_ = nullptr;

private:
friend ComponentBase;
friend ScreenInteractive;
Expand Down
6 changes: 5 additions & 1 deletion include/ftxui/dom/elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using Elements = std::vector<Element>;
using Decorator = std::function<Element(Element)>;
using GraphFunction = std::function<std::vector<int>(int, int)>;

enum BorderStyle { LIGHT, HEAVY, DOUBLE, ROUNDED };
enum BorderStyle { LIGHT, HEAVY, DOUBLE, ROUNDED, EMPTY };

// Pipe elements into decorator togethers.
// For instance the next lines are equivalents:
Expand All @@ -34,20 +34,24 @@ Element separator(void);
Element separatorLight();
Element separatorHeavy();
Element separatorDouble();
Element separatorEmpty();
Element separatorStyled(BorderStyle);
Element separator(Pixel);
Element separatorCharacter(std::string);
Element gauge(float ratio);
Element border(Element);
Element borderLight(Element);
Element borderHeavy(Element);
Element borderDouble(Element);
Element borderRounded(Element);
Element borderEmpty(Element);
Decorator borderStyled(BorderStyle);
Decorator borderWith(Pixel);
Element window(Element title, Element content);
Element spinner(int charset_index, size_t image_index);
Elements paragraph(std::string text); // Use inside hflow(). Split by space.
Element graph(GraphFunction);
Element emptyElement();

// -- Decorator ---
Element bold(Element);
Expand Down
93 changes: 93 additions & 0 deletions include/ftxui/dom/table.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#ifndef FTXUI_DOM_TABLE
#define FTXUI_DOM_TABLE

#include <memory>
#include <string> // for string
#include <vector> // for vector

#include "ftxui/dom/elements.hpp" // for BorderStyle, LIGHT, Element, Decorator

namespace ftxui {

// Usage:
//
// Initialization:
// ---------------
//
// auto table = Table({
// {"X", "Y"},
// {"-1", "1"},
// {"+0", "0"},
// {"+1", "1"},
// });
//
// table.SelectAll().Border(LIGHT);
//
// table.SelectRow(1).Border(DOUBLE);
// table.SelectRow(1).SeparatorInternal(Light);
//
// std::move(table).Element();

class Table;
class TableSelection;

class Table {
public:
Table(std::vector<std::vector<std::string>>);
TableSelection SelectAll();
TableSelection SelectCell(int column, int row);
TableSelection SelectRow(int row_index);
TableSelection SelectRows(int row_min, int row_max);
TableSelection SelectColumn(int column_index);
TableSelection SelectColumns(int column_min, int column_max);
TableSelection SelectRectangle(int column_min,
int column_max,
int row_min,
int row_max);
Element Render();

private:
friend TableSelection;
std::vector<std::vector<Element>> elements_;
int input_dim_x_;
int input_dim_y_;
int dim_x_;
int dim_y_;
};

class TableSelection {
public:
void Decorate(Decorator);
void DecorateAlternateRow(Decorator, int modulo = 2, int shift = 0);
void DecorateAlternateColumn(Decorator, int modulo = 2, int shift = 0);

void DecorateCells(Decorator);
void DecorateCellsAlternateColumn(Decorator, int modulo = 2, int shift = 0);
void DecorateCellsAlternateRow(Decorator, int modulo = 2, int shift = 0);

void Border(BorderStyle border = LIGHT);
void BorderLeft(BorderStyle border = LIGHT);
void BorderRight(BorderStyle border = LIGHT);
void BorderTop(BorderStyle border = LIGHT);
void BorderBottom(BorderStyle border = LIGHT);

void Separator(BorderStyle border = LIGHT);
void SeparatorVertical(BorderStyle border = LIGHT);
void SeparatorHorizontal(BorderStyle border = LIGHT);

private:
friend Table;
Table* table_;
int x_min_;
int x_max_;
int y_min_;
int y_max_;
};

} // namespace ftxui

#endif /* end of include guard: FTXUI_DOM_TABLE */

// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
16 changes: 13 additions & 3 deletions src/ftxui/component/dropdown.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#include "ftxui/component/component.hpp"
#include "ftxui/component/component_base.hpp"
#include "ftxui/component/event.hpp"
#include <memory> // for __shared_ptr_access
#include <string> // for string
#include <utility> // for move

#include "ftxui/component/component.hpp" // for Maybe, Checkbox, Make, Radiobox, Vertical, Dropdown
#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
#include "ftxui/component/component_options.hpp" // for CheckboxOption
#include "ftxui/dom/elements.hpp" // for operator|, Element, border, filler, separator, size, vbox, frame, vscroll_indicator, HEIGHT, LESS_THAN
#include "ftxui/util/ref.hpp" // for ConstStringListRef

namespace ftxui {

Expand Down Expand Up @@ -52,3 +58,7 @@ Component Dropdown(ConstStringListRef entries, int* selected) {
}

} // namespace ftxui

// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
19 changes: 14 additions & 5 deletions src/ftxui/component/maybe.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#include "ftxui/component/component.hpp"
#include "ftxui/component/component_base.hpp"
#include "ftxui/component/event.hpp"
#include <memory> // for make_unique, __shared_ptr_access, __shared_ptr_access<>::element_type, shared_ptr
#include <utility> // for move

#include "ftxui/component/component.hpp" // for Make, Maybe
#include "ftxui/component/component_base.hpp" // for ComponentBase, Component
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/dom/elements.hpp" // for Element
#include "ftxui/dom/node.hpp" // for Node

namespace ftxui {

Component Maybe(Component child, bool* show) {
class Impl : public ComponentBase {
public:
Impl(bool* show): show_(show) {}
Impl(bool* show) : show_(show) {}

private:
Element Render() override {
Expand All @@ -22,10 +27,14 @@ Component Maybe(Component child, bool* show) {

bool* show_;
};

auto maybe = Make<Impl>(show);
maybe->Add(std::move(child));
return maybe;
}

} // namespace ftxui

// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
Loading

0 comments on commit 026a005

Please sign in to comment.