26class Text :
public Node {
28 explicit Text(std::string_view text) : glyphs_(Utf8ToGlyphs(text)) {}
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 {
57 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
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};
71 if (Box::Intersection(selection.GetBox(), row_box).IsEmpty()) {
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_view text) : glyphs_(Utf8ToGlyphs(text)) {
138 for (
const auto& g : glyphs_) {
146 void ComputeRequirement()
override {
148 int current_height = 0;
151 for (
const auto& cell : glyphs_) {
153 max_height = std::max(max_height, current_height);
160 max_height = std::max(max_height, current_height);
162 requirement_.min_x = width_ * columns;
163 requirement_.min_y = max_height;
166 void Render(Screen& screen)
override {
169 if (x + width_ - 1 > box_.x_max) {
172 for (
const auto& it : glyphs_) {
176 if (x + width_ - 1 > box_.x_max) {
181 if (
y > box_.y_max) {
184 screen.CellAt(x,
y).character = it;
190 std::vector<std::string> glyphs_;
212 return std::make_shared<Text>(std::string(text));
261 return std::make_shared<VText>(text);
Element text(std::string_view text)
Display a piece of UTF8 encoded unicode text.
Element vtext(std::string_view text)
Display a piece of unicode text vertically.
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.
void Render(Screen &screen, Node *node, Selection &selection)