25class Text :
public Node {
27 explicit Text(std::string text) : glyphs_(Utf8ToGlyphs(text)) {}
28 explicit Text(std::string_view sv) :
Text(std::string(sv)) {}
30 void ComputeRequirement()
override {
32 int current_width = 0;
34 has_selection_ =
false;
35 selection_rows_.clear();
36 lines_offsets_.clear();
37 lines_offsets_.push_back(0);
39 for (
size_t i = 0; i < glyphs_.size(); ++i) {
40 if (glyphs_[i] ==
"\n") {
41 max_width = std::max(max_width, current_width);
44 lines_offsets_.push_back((
int)i + 1);
49 max_width = std::max(max_width, current_width);
50 lines_offsets_.push_back((
int)glyphs_.size() + 1);
52 requirement_.min_x = max_width;
53 requirement_.min_y = lines_count;
56 void Select(Selection& selection)
override {
61 has_selection_ =
true;
62 selection_rows_.assign(lines_offsets_.size() - 1, {-1, -1});
64 for (
size_t i = 0; i < lines_offsets_.size() - 1; ++i) {
65 const int y = box_.y_min + (int)i;
70 const Box row_box{box_.x_min, box_.x_max, y, y};
75 const Selection row_sel = selection.SaturateHorizontal(row_box);
76 const int sel_start = row_sel.GetBox().x_min;
77 const int sel_end = row_sel.GetBox().x_max;
78 selection_rows_[i] = {sel_start, sel_end};
82 const int start = lines_offsets_[i];
83 const int end = lines_offsets_[i + 1] - 1;
84 for (
int j = start; j < end; ++j) {
85 if (sel_start <= x && x <= sel_end) {
90 selection.AddPart(std::move(part), y, sel_start, sel_end);
94 void Render(Screen& screen)
override {
103 for (
const auto& cell : glyphs_) {
108 if (y > box_.y_max) {
114 if (x <= box_.x_max) {
115 screen.CellAt(x, y).character = cell;
117 if (has_selection_ && line < selection_rows_.size()) {
118 const auto& [sel_start, sel_end] = selection_rows_[line];
119 if (sel_start != -1 && x >= sel_start && x <= sel_end) {
120 screen.GetSelectionStyle()(screen.CellAt(x, y));
129 std::vector<std::string> glyphs_;
130 std::vector<int> lines_offsets_;
131 bool has_selection_ =
false;
132 std::vector<std::pair<int, int>> selection_rows_;
135class VText :
public Node {
137 explicit VText(std::string text) : glyphs_(Utf8ToGlyphs(text)) {
138 for (
const auto& g : glyphs_) {
145 explicit VText(std::string_view sv) : VText(std::string(sv)) {}
147 void ComputeRequirement()
override {
149 int current_height = 0;
152 for (
const auto& cell : glyphs_) {
154 max_height = std::max(max_height, current_height);
161 max_height = std::max(max_height, current_height);
163 requirement_.min_x = width_ * columns;
164 requirement_.min_y = max_height;
167 void Render(Screen& screen)
override {
170 if (x + width_ - 1 > box_.x_max) {
173 for (
const auto& it : glyphs_) {
177 if (x + width_ - 1 > box_.x_max) {
182 if (y > box_.y_max) {
185 screen.CellAt(x, y).character = it;
191 std::vector<std::string> glyphs_;
213 return std::make_shared<Text>(std::string(text));
262 return std::make_shared<VText>(std::string(text));
Element vtext(std::wstring_view text)
Display a piece unicode text vertically.
Element text(std::wstring_view text)
Display a piece of unicode text.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
static auto Intersection(Box a, Box b) -> Box
A rectangular grid of Cell.
The FTXUI ftxui:: namespace.
std::shared_ptr< Node > Element
std::string to_string(std::wstring_view s)
Convert a std::wstring into a UTF8 std::string.