2021-05-09 20:32:27 +02:00
|
|
|
#include <memory> // for make_shared
|
2021-08-08 23:25:20 +02:00
|
|
|
#include <string> // for string
|
2021-05-01 20:40:35 +02:00
|
|
|
|
2021-05-09 20:32:27 +02:00
|
|
|
#include "ftxui/dom/elements.hpp" // for Element, separator
|
|
|
|
#include "ftxui/dom/node.hpp" // for Node
|
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
2018-09-19 21:52:25 +02:00
|
|
|
|
2019-01-12 15:00:08 +01:00
|
|
|
namespace ftxui {
|
2019-01-06 17:10:35 +01:00
|
|
|
|
2019-01-12 15:00:08 +01:00
|
|
|
using ftxui::Screen;
|
2018-09-19 21:52:25 +02:00
|
|
|
|
2021-09-12 00:36:59 +02:00
|
|
|
const std::string charset[][2] = {
|
2021-10-15 23:04:11 +02:00
|
|
|
{"│", "─"}, //
|
|
|
|
{"┃", "━"}, //
|
|
|
|
{"║", "═"}, //
|
|
|
|
{"│", "─"}, //
|
|
|
|
{" ", " "}, //
|
2021-09-12 00:36:59 +02:00
|
|
|
};
|
|
|
|
|
2018-09-19 21:52:25 +02:00
|
|
|
class Separator : public Node {
|
|
|
|
public:
|
2021-10-15 23:04:11 +02:00
|
|
|
Separator(std::string value) : value_(value) {}
|
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
|
|
|
requirement_.min_x = 1;
|
|
|
|
requirement_.min_y = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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).character = value_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SeparatorAuto : public Node {
|
|
|
|
public:
|
|
|
|
SeparatorAuto(BorderStyle style) : style_(style) {}
|
2021-09-12 00:36:59 +02:00
|
|
|
|
2018-09-19 21:52:25 +02:00
|
|
|
void ComputeRequirement() override {
|
2020-06-01 16:13:29 +02:00
|
|
|
requirement_.min_x = 1;
|
|
|
|
requirement_.min_y = 1;
|
2018-09-19 21:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
2019-01-19 22:06:05 +01:00
|
|
|
bool is_column = (box_.x_max == box_.x_min);
|
|
|
|
bool is_line = (box_.y_min == box_.y_max);
|
2018-09-19 21:52:25 +02:00
|
|
|
|
2021-09-12 00:36:59 +02:00
|
|
|
const std::string c = charset[style_][is_line && !is_column];
|
2018-09-19 21:52:25 +02:00
|
|
|
|
2019-01-19 22:06:05 +01:00
|
|
|
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
|
|
|
for (int x = box_.x_min; x <= box_.x_max; ++x) {
|
2021-05-17 01:34:53 +02:00
|
|
|
screen.PixelAt(x, y).character = c;
|
2018-09-19 21:52:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-12 00:36:59 +02:00
|
|
|
|
|
|
|
BorderStyle style_;
|
2018-09-19 21:52:25 +02:00
|
|
|
};
|
|
|
|
|
2021-10-15 23:04:11 +02:00
|
|
|
class SeparatorWithPixel : public SeparatorAuto {
|
2019-01-27 02:33:06 +01:00
|
|
|
public:
|
2021-10-15 23:04:11 +02:00
|
|
|
SeparatorWithPixel(Pixel pixel) : SeparatorAuto(LIGHT), pixel_(pixel) {}
|
2021-05-17 01:34:53 +02:00
|
|
|
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) = pixel_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 17:18:11 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
Pixel pixel_;
|
2019-01-27 02:33:06 +01:00
|
|
|
};
|
|
|
|
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements.
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @see separatorStyled
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 10:36:04 +02:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-15 23:04:11 +02:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 10:36:04 +02:00
|
|
|
/// Element document = vbox({
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("up"),
|
2021-10-03 10:36:04 +02:00
|
|
|
/// separator(),
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-15 23:04:11 +02:00
|
|
|
/// up
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ────
|
2021-10-15 23:04:11 +02:00
|
|
|
/// down
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
2020-05-20 20:36:47 +02:00
|
|
|
Element separator() {
|
2021-10-15 23:04:11 +02:00
|
|
|
return std::make_shared<SeparatorAuto>(LIGHT);
|
2018-09-19 21:52:25 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements.
|
|
|
|
/// @param style the style of the separator.
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @see separatorStyled
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 10:36:04 +02:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-15 23:04:11 +02:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 10:36:04 +02:00
|
|
|
/// Element document = vbox({
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("up"),
|
|
|
|
/// separatorStyled(DOUBLE),
|
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-15 23:04:11 +02:00
|
|
|
/// up
|
|
|
|
/// ════
|
|
|
|
/// down
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
2021-09-12 00:36:59 +02:00
|
|
|
Element separatorStyled(BorderStyle style) {
|
2021-10-15 23:04:11 +02:00
|
|
|
return std::make_shared<SeparatorAuto>(style);
|
2021-09-12 00:36:59 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements, using the LIGHT style.
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @see separatorStyled
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 10:36:04 +02:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-15 23:04:11 +02:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 10:36:04 +02:00
|
|
|
/// Element document = vbox({
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("up"),
|
2021-10-03 10:36:04 +02:00
|
|
|
/// separatorLight(),
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-15 23:04:11 +02:00
|
|
|
/// up
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ────
|
2021-10-15 23:04:11 +02:00
|
|
|
/// down
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
2021-09-12 00:36:59 +02:00
|
|
|
Element separatorLight() {
|
2021-10-15 23:04:11 +02:00
|
|
|
return std::make_shared<SeparatorAuto>(LIGHT);
|
2021-09-12 00:36:59 +02:00
|
|
|
}
|
2021-10-03 10:36:04 +02:00
|
|
|
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements, using the HEAVY style.
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @see separatorStyled
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 10:36:04 +02:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-15 23:04:11 +02:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 10:36:04 +02:00
|
|
|
/// Element document = vbox({
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("up"),
|
2021-10-03 10:36:04 +02:00
|
|
|
/// separatorHeavy(),
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-15 23:04:11 +02:00
|
|
|
/// up
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ━━━━
|
2021-10-15 23:04:11 +02:00
|
|
|
/// down
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
2021-09-12 00:36:59 +02:00
|
|
|
Element separatorHeavy() {
|
2021-10-15 23:04:11 +02:00
|
|
|
return std::make_shared<SeparatorAuto>(HEAVY);
|
2021-09-12 00:36:59 +02:00
|
|
|
}
|
2021-10-03 10:36:04 +02:00
|
|
|
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements, using the DOUBLE style.
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @see separatorStyled
|
2021-10-15 23:04:11 +02:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 10:36:04 +02:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-15 23:04:11 +02:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 10:36:04 +02:00
|
|
|
/// Element document = vbox({
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("up"),
|
2021-10-03 10:36:04 +02:00
|
|
|
/// separatorDouble(),
|
2021-10-15 23:04:11 +02:00
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-15 23:04:11 +02:00
|
|
|
/// up
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ════
|
2021-10-15 23:04:11 +02:00
|
|
|
/// down
|
2021-10-03 10:36:04 +02:00
|
|
|
/// ```
|
2021-09-12 00:36:59 +02:00
|
|
|
Element separatorDouble() {
|
2021-10-15 23:04:11 +02:00
|
|
|
return std::make_shared<SeparatorAuto>(DOUBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements, using the EMPTY style.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
|
|
|
/// @see separatorStyled
|
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// // Use 'border' as a function...
|
|
|
|
/// Element document = vbox({
|
|
|
|
/// text("up"),
|
|
|
|
/// separator(),
|
|
|
|
/// text("down"),
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// up
|
|
|
|
///
|
|
|
|
/// down
|
|
|
|
/// ```
|
|
|
|
Element separatorEmpty() {
|
|
|
|
return std::make_shared<SeparatorAuto>(EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements.
|
|
|
|
/// @param value the character to fill the separator area.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
|
|
|
/// @see separatorStyled
|
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// // Use 'border' as a function...
|
|
|
|
/// Element document = vbox({
|
|
|
|
/// text("up"),
|
|
|
|
/// separator(),
|
|
|
|
/// text("down"),
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// up
|
|
|
|
/// ────
|
|
|
|
/// down
|
|
|
|
/// ```
|
|
|
|
Element separatorCharacter(std::string value) {
|
|
|
|
return std::make_shared<Separator>(value);
|
2019-01-27 02:33:06 +01:00
|
|
|
}
|
|
|
|
|
2021-10-03 10:36:04 +02:00
|
|
|
/// @brief Draw a separator in between two element filled with a given pixel.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorDouble
|
|
|
|
/// @see separatorStyled
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Pixel empty;
|
|
|
|
/// Element document = vbox({
|
|
|
|
/// text("Up"),
|
|
|
|
/// separator(empty),
|
|
|
|
/// text("Down"),
|
|
|
|
/// })
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// Up
|
|
|
|
///
|
|
|
|
/// Down
|
|
|
|
/// ```
|
|
|
|
Element separator(Pixel pixel) {
|
|
|
|
return std::make_shared<SeparatorWithPixel>(pixel);
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:44:55 +01:00
|
|
|
} // namespace ftxui
|
2020-08-16 00:24:18 +02:00
|
|
|
|
|
|
|
// 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.
|