15bool IsCell(
int x,
int y) {
16 return x % 2 == 1 && y % 2 == 1;
20static std::string charset[6][6] = {
21 {
"┌",
"┐",
"└",
"┘",
"─",
"│"},
22 {
"┏",
"┓",
"┗",
"┛",
"╍",
"╏"},
23 {
"┏",
"┓",
"┗",
"┛",
"━",
"┃"},
24 {
"╔",
"╗",
"╚",
"╝",
"═",
"║"},
25 {
"╭",
"╮",
"╰",
"╯",
"─",
"│"},
26 {
" ",
" ",
" ",
" ",
" ",
" "},
29int Wrap(
int input,
int modulo) {
36void Order(
int& a,
int& b) {
54 std::vector<std::vector<Element>> output;
55 output.reserve(input.size());
56 for (
auto& row : input) {
57 output.emplace_back();
58 auto& output_row = output.back();
59 output_row.reserve(row.size());
60 for (
auto& cell : row) {
61 output_row.push_back(
text(std::move(cell)));
64 Initialize(std::move(output));
71 Initialize(std::move(input));
75void Table::Initialize(std::vector<std::vector<Element>> input) {
76 input_dim_y_ = input.size();
78 for (
auto& row : input) {
79 input_dim_x_ = std::max(input_dim_x_,
int(row.size()));
82 dim_y_ = 2 * input_dim_y_ + 1;
83 dim_x_ = 2 * input_dim_x_ + 1;
86 elements_.resize(dim_y_);
87 for (
int y = 0; y < dim_y_; ++y) {
88 elements_[y].resize(dim_x_);
94 for (
auto& row : input) {
96 for (
auto& cell : row) {
97 elements_[y][x] = std::move(cell);
105 for (
int y = 0; y < dim_y_; ++y) {
106 for (
int x = 0; x < dim_x_; ++x) {
107 auto& element = elements_[y][x];
175 column_min = Wrap(column_min, input_dim_x_);
176 column_max = Wrap(column_max, input_dim_x_);
177 Order(column_min, column_max);
178 row_min = Wrap(row_min, input_dim_y_);
179 row_max = Wrap(row_max, input_dim_y_);
180 Order(row_min, row_max);
183 output.table_ =
this;
184 output.x_min_ = 2 * column_min;
185 output.x_max_ = 2 * column_max + 2;
186 output.y_min_ = 2 * row_min;
187 output.y_max_ = 2 * row_max + 2;
195 output.table_ =
this;
197 output.x_max_ = dim_x_ - 1;
199 output.y_max_ = dim_y_ - 1;
207 for (
int y = 0; y < dim_y_; ++y) {
208 for (
int x = 0; x < dim_x_; ++x) {
209 auto& it = elements_[y][x];
212 if ((x + y) % 2 == 1) {
213 it = std::move(it) |
flex;
218 if ((x % 2) == 1 && (y % 2) == 1) {
229 return gridbox(std::move(elements_));
238 for (
int y = y_min_; y <= y_max_; ++y) {
239 for (
int x = x_min_; x <= x_max_; ++x) {
240 Element& e = table_->elements_[y][x];
241 e = std::move(e) | decorator;
252 for (
int y = y_min_; y <= y_max_; ++y) {
253 for (
int x = x_min_; x <= x_max_; ++x) {
254 if (y % 2 == 1 && x % 2 == 1) {
255 Element& e = table_->elements_[y][x];
256 e = std::move(e) | decorator;
272 for (
int y = y_min_; y <= y_max_; ++y) {
273 for (
int x = x_min_; x <= x_max_; ++x) {
274 if (y % 2 == 1 && (x / 2) % modulo == shift) {
275 Element& e = table_->elements_[y][x];
276 e = std::move(e) | decorator;
292 for (
int y = y_min_ + 1; y <= y_max_ - 1; ++y) {
293 for (
int x = x_min_; x <= x_max_; ++x) {
294 if (y % 2 == 1 && (y / 2) % modulo == shift) {
295 Element& e = table_->elements_[y][x];
296 e = std::move(e) | decorator;
312 for (
int y = y_min_; y <= y_max_; ++y) {
313 for (
int x = x_min_; x <= x_max_; ++x) {
314 if (y % 2 == 1 && x % 2 == 1 && ((x / 2) % modulo == shift)) {
315 Element& e = table_->elements_[y][x];
316 e = std::move(e) | decorator;
332 for (
int y = y_min_; y <= y_max_; ++y) {
333 for (
int x = x_min_; x <= x_max_; ++x) {
334 if (y % 2 == 1 && x % 2 == 1 && ((y / 2) % modulo == shift)) {
335 Element& e = table_->elements_[y][x];
336 e = std::move(e) | decorator;
365 for (
int y = y_min_ + 1; y <= y_max_ - 1; ++y) {
366 for (
int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
367 if (y % 2 == 0 || x % 2 == 0) {
368 Element& e = table_->elements_[y][x];
381 for (
int y = y_min_ + 1; y <= y_max_ - 1; ++y) {
382 for (
int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
384 table_->elements_[y][x] =
395 for (
int y = y_min_ + 1; y <= y_max_ - 1; ++y) {
396 for (
int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
398 table_->elements_[y][x] =
409 for (
int y = y_min_; y <= y_max_; y++) {
410 table_->elements_[y][x_min_] =
419 for (
int y = y_min_; y <= y_max_; y++) {
420 table_->elements_[y][x_max_] =
429 for (
int x = x_min_; x <= x_max_; x++) {
430 table_->elements_[y_min_][x] =
439 for (
int x = x_min_; x <= x_max_; x++) {
440 table_->elements_[y_max_][x] =
void DecorateAlternateColumn(Decorator, int modulo=2, int shift=0)
Apply the decorator to the selection. This decorate only the lines modulo modulo with a shift of shif...
void SeparatorVertical(BorderStyle border=LIGHT)
Draw some vertical separator lines in the selection.
void DecorateCells(Decorator)
Apply the decorator to the selection.
void BorderLeft(BorderStyle border=LIGHT)
Draw some separator lines to the left side of the selection.
void DecorateCellsAlternateColumn(Decorator, int modulo=2, int shift=0)
Apply the decorator to the selection. This decorate only the corners modulo modulo with a shift of sh...
void Decorate(Decorator)
Apply the decorator to the selection. This decorate both the cells, the lines and the corners.
void DecorateAlternateRow(Decorator, int modulo=2, int shift=0)
Apply the decorator to the selection. This decorate only the lines modulo modulo with a shift of shif...
void BorderTop(BorderStyle border=LIGHT)
Draw some separator lines to the top side of the selection.
void Separator(BorderStyle border=LIGHT)
Draw some separator lines in the selection.
void BorderBottom(BorderStyle border=LIGHT)
Draw some separator lines to the bottom side of the selection.
void DecorateCellsAlternateRow(Decorator, int modulo=2, int shift=0)
Apply the decorator to the selection. This decorate only the corners modulo modulo with a shift of sh...
void BorderRight(BorderStyle border=LIGHT)
Draw some separator lines to the right side of the selection.
void Border(BorderStyle border=LIGHT)
Apply a border around the selection.
void SeparatorHorizontal(BorderStyle border=LIGHT)
Draw some horizontal separator lines in the selection.
Element Render()
Render the table.
Table()
Create an empty table.
TableSelection SelectCell(int column, int row)
Select a cell of the table.
TableSelection SelectColumn(int column_index)
Select a column of the table.
TableSelection SelectRow(int row_index)
Select a row of the table.
TableSelection SelectColumns(int column_min, int column_max)
Select a range of columns of the table.
TableSelection SelectRows(int row_min, int row_max)
Select a range of rows of the table.
TableSelection SelectAll()
Select all the table.
TableSelection SelectRectangle(int column_min, int column_max, int row_min, int row_max)
Select a rectangle of the table.
std::function< Element(Element)> Decorator
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Element flex(Element)
Make a child element to expand proportionnally to the space left in a container.
std::shared_ptr< Node > Element
Element flex_shrink(Element)
Minimize if needed.
Element text(std::wstring text)
Display a piece of unicode text.
Element separatorCharacter(std::string)
Draw a vertical or horizontal separation in between two other elements.
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
Element automerge(Element child)
Enable character to be automatically merged with others nearby.
Element border(Element)
Draw a border around the element.