FTXUI/src/ftxui/dom/separator.cpp

244 lines
4.6 KiB
C++
Raw Normal View History

2021-05-10 02:32:27 +08:00
#include <memory> // for make_shared
#include <string> // for string
2021-05-02 02:40:35 +08:00
2021-05-10 02:32:27 +08: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-20 03:52:25 +08:00
namespace ftxui {
using ftxui::Screen;
2018-09-20 03:52:25 +08:00
2021-09-12 06:36:59 +08:00
const std::string charset[][2] = {
{"", ""},
{"", ""},
{"", ""},
{"", ""},
};
2018-09-20 03:52:25 +08:00
class Separator : public Node {
public:
2021-09-12 06:36:59 +08:00
Separator(BorderStyle style) : style_(style) {}
2018-09-20 03:52:25 +08:00
void ComputeRequirement() override {
2020-06-01 22:13:29 +08:00
requirement_.min_x = 1;
requirement_.min_y = 1;
2018-09-20 03:52:25 +08:00
}
void Render(Screen& screen) override {
2019-01-20 05:06:05 +08:00
bool is_column = (box_.x_max == box_.x_min);
bool is_line = (box_.y_min == box_.y_max);
2018-09-20 03:52:25 +08:00
2021-09-12 06:36:59 +08:00
const std::string c = charset[style_][is_line && !is_column];
2018-09-20 03:52:25 +08:00
2019-01-20 05:06:05 +08:00
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 = c;
2018-09-20 03:52:25 +08:00
}
}
}
2021-09-12 06:36:59 +08:00
BorderStyle style_;
2018-09-20 03:52:25 +08:00
};
2019-01-27 09:33:06 +08:00
class SeparatorWithPixel : public Separator {
public:
2021-09-12 06:36:59 +08:00
SeparatorWithPixel(Pixel pixel) : Separator(LIGHT), pixel_(pixel) {}
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_;
}
}
}
private:
Pixel pixel_;
2019-01-27 09:33:06 +08:00
};
/// @brief Draw a vertical or horizontal separator in between two elements.
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorHeavy
/// @see separatorDouble
/// @see separatorStyled
///
/// ### Example
///
/// ```cpp
/// Element document = vbox({
/// text("Up"),
/// separator(),
/// text("Down"),
/// })
/// ```
///
/// ### Output
///
/// ```bash
/// Up
/// ────
/// Down
/// ```
Element separator() {
2021-09-12 06:36:59 +08:00
return std::make_shared<Separator>(LIGHT);
2018-09-20 03:52:25 +08:00
}
/// @brief Draw a vertical or horizontal separator in between two elements.
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorHeavy
/// @see separatorDouble
/// @see separatorStyled
///
/// ### Example
///
/// ```cpp
/// Element document = vbox({
/// text("Up"),
/// separatorStyled(BorderStyle::LIGHT),
/// text("Down"),
/// })
/// ```
///
/// ### Output
///
/// ```bash
/// Up
/// ────
/// Down
/// ```
2021-09-12 06:36:59 +08:00
Element separatorStyled(BorderStyle style) {
return std::make_shared<Separator>(style);
}
/// @brief Draw a vertical or horizontal light separator in between two
/// elements.
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorHeavy
/// @see separatorDouble
/// @see separatorStyled
///
/// ### Example
///
/// ```cpp
/// Element document = vbox({
/// text("Up"),
/// separatorLight(),
/// text("Down"),
/// })
/// ```
///
/// ### Output
///
/// ```bash
/// Up
/// ────
/// Down
/// ```
2021-09-12 06:36:59 +08:00
Element separatorLight() {
return std::make_shared<Separator>(LIGHT);
}
/// @brief Draw a vertical or horizontal heavy separator in between two
/// elements.
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorHeavy
/// @see separatorDouble
/// @see separatorStyled
///
/// ### Example
///
/// ```cpp
/// Element document = vbox({
/// text("Up"),
/// separatorHeavy(),
/// text("Down"),
/// })
/// ```
///
/// ### Output
///
/// ```bash
/// Up
/// ━━━━
/// Down
/// ```
2021-09-12 06:36:59 +08:00
Element separatorHeavy() {
return std::make_shared<Separator>(HEAVY);
}
/// @brief Draw a vertical or horizontal double separator in between two
/// elements.
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorHeavy
/// @see separatorDouble
/// @see separatorStyled
///
/// ### Example
///
/// ```cpp
/// Element document = vbox({
/// text("Up"),
/// separatorDouble(),
/// text("Down"),
/// })
/// ```
///
/// ### Output
///
/// ```bash
/// Up
/// ════
/// Down
/// ```
2021-09-12 06:36:59 +08:00
Element separatorDouble() {
return std::make_shared<Separator>(DOUBLE);
2019-01-27 09:33:06 +08: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-12 04:44:55 +08:00
} // 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.