2023-08-19 19:56:36 +08: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.
|
2021-08-09 06:27:37 +08:00
|
|
|
#include <algorithm> // for min
|
|
|
|
#include <memory> // for make_shared
|
2024-12-27 16:45:13 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <string> // for string, wstring
|
|
|
|
#include <utility> // for move
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
#include "ftxui/dom/deprecated.hpp" // for text, vtext
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for Element, text, vtext
|
|
|
|
#include "ftxui/dom/node.hpp" // for Node
|
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
2021-08-09 05:25:20 +08:00
|
|
|
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
|
|
|
#include "ftxui/screen/string.hpp" // for string_width, Utf8ToGlyphs, to_string
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2023-08-19 20:56:28 +08:00
|
|
|
namespace {
|
2019-01-12 22:00:08 +08:00
|
|
|
using ftxui::Screen;
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
class Text : public Node {
|
|
|
|
public:
|
2022-03-31 08:17:43 +08:00
|
|
|
explicit Text(std::string text) : text_(std::move(text)) {}
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
2021-08-09 05:25:20 +08:00
|
|
|
requirement_.min_x = string_width(text_);
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.min_y = 1;
|
2024-12-27 16:45:13 +08:00
|
|
|
has_selection = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Select(Selection& selection) override {
|
|
|
|
if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Selection selection_saturated = selection.SaturateHorizontal(box_);
|
|
|
|
|
|
|
|
has_selection = true;
|
|
|
|
selection_start_ = selection_saturated.GetBox().x_min;
|
|
|
|
selection_end_ = selection_saturated.GetBox().x_max;
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
int x = box_.x_min;
|
|
|
|
for (const auto& cell : Utf8ToGlyphs(text_)) {
|
|
|
|
if (cell == "\n") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (selection_start_ <= x && x <= selection_end_) {
|
|
|
|
ss << cell;
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
selection.AddPart(ss.str(), box_.y_min, selection_start_, selection_end_);
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
2019-01-20 05:06:05 +08:00
|
|
|
int x = box_.x_min;
|
2022-12-20 01:51:25 +08:00
|
|
|
const int y = box_.y_min;
|
2024-12-27 16:45:13 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (y > box_.y_max) {
|
2018-09-18 14:48:40 +08:00
|
|
|
return;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2024-12-27 16:45:13 +08:00
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
for (const auto& cell : Utf8ToGlyphs(text_)) {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (x > box_.x_max) {
|
2024-12-27 16:45:13 +08:00
|
|
|
break;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2023-05-02 19:32:37 +08:00
|
|
|
if (cell == "\n") {
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-09 05:25:20 +08:00
|
|
|
screen.PixelAt(x, y).character = cell;
|
2024-12-27 16:45:13 +08:00
|
|
|
|
|
|
|
if (has_selection) {
|
|
|
|
auto selectionTransform = screen.GetSelectionStyle();
|
|
|
|
if ((x >= selection_start_) && (x <= selection_end_)) {
|
|
|
|
selectionTransform(screen.PixelAt(x, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
++x;
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string text_;
|
2024-12-27 16:45:13 +08:00
|
|
|
bool has_selection = false;
|
|
|
|
int selection_start_ = 0;
|
|
|
|
int selection_end_ = -1;
|
|
|
|
std::function<void(Pixel& pixel)> selectionTransform;
|
2018-09-18 14:48:40 +08:00
|
|
|
};
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
class VText : public Node {
|
|
|
|
public:
|
2022-03-31 08:17:43 +08:00
|
|
|
explicit VText(std::string text)
|
|
|
|
: text_(std::move(text)), width_{std::min(string_width(text_), 1)} {}
|
2020-06-02 05:40:32 +08:00
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
|
|
|
requirement_.min_x = width_;
|
2021-08-09 05:25:20 +08:00
|
|
|
requirement_.min_y = string_width(text_);
|
2020-06-02 05:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
2022-12-20 01:51:25 +08:00
|
|
|
const int x = box_.x_min;
|
2020-06-02 05:40:32 +08:00
|
|
|
int y = box_.y_min;
|
2022-03-31 08:17:43 +08:00
|
|
|
if (x + width_ - 1 > box_.x_max) {
|
2020-06-02 05:40:32 +08:00
|
|
|
return;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-08-09 05:25:20 +08:00
|
|
|
for (const auto& it : Utf8ToGlyphs(text_)) {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (y > box_.y_max) {
|
2020-06-02 05:40:32 +08:00
|
|
|
return;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-08-09 05:25:20 +08:00
|
|
|
screen.PixelAt(x, y).character = it;
|
2020-06-02 05:40:32 +08:00
|
|
|
y += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string text_;
|
2020-06-02 05:40:32 +08:00
|
|
|
int width_ = 1;
|
|
|
|
};
|
|
|
|
|
2023-08-19 20:56:28 +08:00
|
|
|
} // namespace
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
/// @brief Display a piece of UTF8 encoded unicode text.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see ftxui::to_wstring
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = text("Hello world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// Hello world!
|
|
|
|
/// ```
|
|
|
|
Element text(std::string text) {
|
2022-03-31 08:17:43 +08:00
|
|
|
return std::make_shared<Text>(std::move(text));
|
2021-08-09 05:25:20 +08:00
|
|
|
}
|
|
|
|
|
2021-08-09 06:27:37 +08:00
|
|
|
/// @brief Display a piece of unicode text.
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see ftxui::to_wstring
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = text(L"Hello world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// Hello world!
|
|
|
|
/// ```
|
2022-03-31 08:17:43 +08:00
|
|
|
Element text(std::wstring text) { // NOLINT
|
2021-08-09 05:25:20 +08:00
|
|
|
return std::make_shared<Text>(to_string(text));
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
/// @brief Display a piece of unicode text vertically.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see ftxui::to_wstring
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = vtext("Hello world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// H
|
|
|
|
/// e
|
|
|
|
/// l
|
|
|
|
/// l
|
|
|
|
/// o
|
|
|
|
///
|
|
|
|
/// w
|
|
|
|
/// o
|
|
|
|
/// r
|
|
|
|
/// l
|
|
|
|
/// d
|
|
|
|
/// !
|
|
|
|
/// ```
|
|
|
|
Element vtext(std::string text) {
|
2022-03-31 08:17:43 +08:00
|
|
|
return std::make_shared<VText>(std::move(text));
|
2021-08-09 05:25:20 +08:00
|
|
|
}
|
|
|
|
|
2021-08-09 06:27:37 +08:00
|
|
|
/// @brief Display a piece unicode text vertically.
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see ftxui::to_wstring
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = vtext(L"Hello world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// H
|
|
|
|
/// e
|
|
|
|
/// l
|
|
|
|
/// l
|
|
|
|
/// o
|
|
|
|
///
|
|
|
|
/// w
|
|
|
|
/// o
|
|
|
|
/// r
|
|
|
|
/// l
|
|
|
|
/// d
|
|
|
|
/// !
|
|
|
|
/// ```
|
2022-03-31 08:17:43 +08:00
|
|
|
Element vtext(std::wstring text) { // NOLINT
|
2021-08-09 05:25:20 +08:00
|
|
|
return std::make_shared<VText>(to_string(text));
|
2020-06-02 05:40:32 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
} // namespace ftxui
|