From 5da7b8a59ae812002c52bc0459947e90ec744eb4 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Sun, 13 Feb 2022 11:41:31 +0100 Subject: [PATCH] Fix automerge in tables. (#333) --- CHANGELOG.md | 3 +++ CMakeLists.txt | 1 + include/ftxui/dom/elements.hpp | 1 + src/ftxui/component/dropdown.cpp | 4 +-- src/ftxui/dom/automerge.cpp | 36 +++++++++++++++++++++++++++ src/ftxui/dom/table.cpp | 42 ++++++++++++++++++++------------ 6 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 src/ftxui/dom/automerge.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 248c285..e6a040b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ Element gaugeUp(float ratio); Element gaugeDown(float ratio); Element gaugeDirection(float ratio, GaugeDirection); ``` +- Add the `automerge` decorator. This makes separator characters to be merged + with others nearby. +- Fix the `Table` rendering function, to allow automerging characters. #### Component - Support SIGTSTP. (ctrl+z). diff --git a/CMakeLists.txt b/CMakeLists.txt index 51e25f4..8222352 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ add_library(dom include/ftxui/dom/node.hpp include/ftxui/dom/requirement.hpp include/ftxui/dom/take_any_args.hpp + src/ftxui/dom/automerge.cpp src/ftxui/dom/blink.cpp src/ftxui/dom/bold.cpp src/ftxui/dom/border.cpp diff --git a/include/ftxui/dom/elements.hpp b/include/ftxui/dom/elements.hpp index 95c09ff..dc3961c 100644 --- a/include/ftxui/dom/elements.hpp +++ b/include/ftxui/dom/elements.hpp @@ -81,6 +81,7 @@ Element color(Color, Element); Element bgcolor(Color, Element); Decorator focusPosition(int x, int y); Decorator focusPositionRelative(float x, float y); +Element automerge(Element); // --- Layout is // Horizontal, Vertical or stacked set of elements. diff --git a/src/ftxui/component/dropdown.cpp b/src/ftxui/component/dropdown.cpp index 9d720ae..12cdaa7 100644 --- a/src/ftxui/component/dropdown.cpp +++ b/src/ftxui/component/dropdown.cpp @@ -17,8 +17,8 @@ Component Dropdown(ConstStringListRef entries, int* selected) { Impl(ConstStringListRef entries, int* selected) : entries_(std::move(entries)), selected_(selected) { CheckboxOption option; - option.style_checked = "↓│"; - option.style_unchecked = "→│"; + option.style_checked = "↓ "; + option.style_unchecked = "→ "; checkbox_ = Checkbox(&title_, &show_, option), radiobox_ = Radiobox(entries_, selected_); diff --git a/src/ftxui/dom/automerge.cpp b/src/ftxui/dom/automerge.cpp new file mode 100644 index 0000000..8a341dc --- /dev/null +++ b/src/ftxui/dom/automerge.cpp @@ -0,0 +1,36 @@ +#include // for make_shared +#include // 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(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. diff --git a/src/ftxui/dom/table.cpp b/src/ftxui/dom/table.cpp index b36332a..200a6c7 100644 --- a/src/ftxui/dom/table.cpp +++ b/src/ftxui/dom/table.cpp @@ -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