Fix automerge in tables. (#333)

This commit is contained in:
Arthur Sonzogni
2022-02-13 11:41:31 +01:00
committed by GitHub
parent 9c4218c2a8
commit 5da7b8a59a
6 changed files with 69 additions and 18 deletions

View File

@@ -0,0 +1,36 @@
#include <memory> // for make_shared
#include <utility> // for move
#include "ftxui/dom/elements.hpp" // for Element, automerge
#include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
namespace ftxui {
/// @brief Enable character to be automatically merged with others nearby.
/// @ingroup dom
Element automerge(Element child) {
class Impl : public NodeDecorator {
public:
using NodeDecorator::NodeDecorator;
void Render(Screen& screen) override {
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
screen.PixelAt(x, y).automerge = true;
}
}
Node::Render(screen);
}
};
return std::make_shared<Impl>(std::move(child));
}
} // namespace ftxui
// 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.

View File

@@ -250,10 +250,10 @@ void TableSelection::Border(BorderStyle style) {
BorderTop(style);
BorderBottom(style);
table_->elements_[y_min_][x_min_] = text(charset[style][0]);
table_->elements_[y_min_][x_max_] = text(charset[style][1]);
table_->elements_[y_max_][x_min_] = text(charset[style][2]);
table_->elements_[y_max_][x_max_] = text(charset[style][3]);
table_->elements_[y_min_][x_min_] = text(charset[style][0]) | automerge;
table_->elements_[y_min_][x_max_] = text(charset[style][1]) | automerge;
table_->elements_[y_max_][x_min_] = text(charset[style][2]) | automerge;
table_->elements_[y_max_][x_max_] = text(charset[style][3]) | automerge;
}
void TableSelection::Separator(BorderStyle style) {
@@ -261,8 +261,8 @@ void TableSelection::Separator(BorderStyle style) {
for (int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
if (y % 2 == 0 || x % 2 == 0) {
Element& e = table_->elements_[y][x];
e = (y % 2) ? separatorCharacter(charset[style][5])
: separatorCharacter(charset[style][4]);
e = (y % 2) ? separatorCharacter(charset[style][5]) | automerge
: separatorCharacter(charset[style][4]) | automerge;
}
}
}
@@ -272,7 +272,8 @@ void TableSelection::SeparatorVertical(BorderStyle style) {
for (int y = y_min_ + 1; y <= y_max_ - 1; ++y) {
for (int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
if (x % 2 == 0) {
table_->elements_[y][x] = separatorCharacter(charset[style][5]);
table_->elements_[y][x] =
separatorCharacter(charset[style][5]) | automerge;
}
}
}
@@ -282,30 +283,39 @@ void TableSelection::SeparatorHorizontal(BorderStyle style) {
for (int y = y_min_ + 1; y <= y_max_ - 1; ++y) {
for (int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
if (y % 2 == 0) {
table_->elements_[y][x] = separatorCharacter(charset[style][4]);
table_->elements_[y][x] =
separatorCharacter(charset[style][4]) | automerge;
}
}
}
}
void TableSelection::BorderLeft(BorderStyle style) {
for (int y = y_min_; y <= y_max_; y++)
table_->elements_[y][x_min_] = separatorCharacter(charset[style][5]);
for (int y = y_min_; y <= y_max_; y++) {
table_->elements_[y][x_min_] =
separatorCharacter(charset[style][5]) | automerge;
}
}
void TableSelection::BorderRight(BorderStyle style) {
for (int y = y_min_; y <= y_max_; y++)
table_->elements_[y][x_max_] = separatorCharacter(charset[style][5]);
for (int y = y_min_; y <= y_max_; y++) {
table_->elements_[y][x_max_] =
separatorCharacter(charset[style][5]) | automerge;
}
}
void TableSelection::BorderTop(BorderStyle style) {
for (int x = x_min_; x <= x_max_; x++)
table_->elements_[y_min_][x] = separatorCharacter(charset[style][4]);
for (int x = x_min_; x <= x_max_; x++) {
table_->elements_[y_min_][x] =
separatorCharacter(charset[style][4]) | automerge;
}
}
void TableSelection::BorderBottom(BorderStyle style) {
for (int x = x_min_; x <= x_max_; x++)
table_->elements_[y_max_][x] = separatorCharacter(charset[style][4]);
for (int x = x_min_; x <= x_max_; x++) {
table_->elements_[y_max_][x] =
separatorCharacter(charset[style][4]) | automerge;
}
}
} // namespace ftxui