FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
text.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <algorithm> // for min
5#include <memory> // for make_shared
6#include <sstream>
7#include <string> // for string, wstring
8#include <string_view> // for string_view
9#include <utility> // for move
10
11#include "ftxui/dom/deprecated.hpp" // for text, vtext
12#include "ftxui/dom/elements.hpp" // for Element, text, vtext
13#include "ftxui/dom/node.hpp" // for Node
14#include "ftxui/dom/requirement.hpp" // for Requirement
15#include "ftxui/dom/selection.hpp" // for Selection
16#include "ftxui/screen/box.hpp" // for Box
17#include "ftxui/screen/screen.hpp" // for Pixel, Screen
18#include "ftxui/screen/string.hpp" // for string_width, Utf8ToGlyphs, to_string
19
20namespace ftxui {
21
22namespace {
23using ftxui::Screen;
24
25class Text : public Node {
26 public:
27 explicit Text(std::string text) : text_(std::move(text)) {}
28 explicit Text(std::string_view sv) : Text(std::string(sv)) {}
29
30 void ComputeRequirement() override {
31 requirement_.min_x = string_width(text_);
32 requirement_.min_y = 1;
33 has_selection = false;
34 }
35
36 void Select(Selection& selection) override {
37 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
38 return;
39 }
40
41 const Selection selection_saturated = selection.SaturateHorizontal(box_);
42
43 has_selection = true;
44 selection_start_ = selection_saturated.GetBox().x_min;
45 selection_end_ = selection_saturated.GetBox().x_max;
46
47 std::stringstream ss;
48 int x = box_.x_min;
49 for (const auto& cell : Utf8ToGlyphs(text_)) {
50 if (cell == "\n") {
51 continue;
52 }
53 if (selection_start_ <= x && x <= selection_end_) {
54 ss << cell;
55 }
56 x++;
57 }
58 selection.AddPart(ss.str(), box_.y_min, selection_start_, selection_end_);
59 }
60
61 void Render(Screen& screen) override {
62 int x = box_.x_min;
63 const int y = box_.y_min;
64
65 if (y > box_.y_max) {
66 return;
67 }
68
69 for (const auto& cell : Utf8ToGlyphs(text_)) {
70 if (x > box_.x_max) {
71 break;
72 }
73 if (cell == "\n") {
74 continue;
75 }
76 screen.PixelAt(x, y).character = cell;
77
78 if (has_selection) {
79 auto selectionTransform = screen.GetSelectionStyle();
80 if ((x >= selection_start_) && (x <= selection_end_)) {
81 selectionTransform(screen.PixelAt(x, y));
82 }
83 }
84
85 ++x;
86 }
87 }
88
89 private:
90 std::string text_;
91 bool has_selection = false;
92 int selection_start_ = 0;
93 int selection_end_ = -1;
94};
95
96class VText : public Node {
97 public:
98 explicit VText(std::string text)
99 : text_(std::move(text)), width_{std::min(string_width(text_), 1)} {}
100
101 explicit VText(std::string_view sv) : VText(std::string(sv)) {}
102
103 void ComputeRequirement() override {
104 requirement_.min_x = width_;
105 requirement_.min_y = string_width(text_);
106 }
107
108 void Render(Screen& screen) override {
109 const int x = box_.x_min;
110 int y = box_.y_min;
111 if (x + width_ - 1 > box_.x_max) {
112 return;
113 }
114 for (const auto& it : Utf8ToGlyphs(text_)) {
115 if (y > box_.y_max) {
116 return;
117 }
118 screen.PixelAt(x, y).character = it;
119 y += 1;
120 }
121 }
122
123 private:
124 std::string text_;
125 int width_ = 1;
126};
127
128} // namespace
129
130/// @brief Display a piece of UTF8 encoded unicode text.
131/// @ingroup dom
132/// @see ftxui::to_wstring
133///
134/// ### Example
135///
136/// ```cpp
137/// Element document = text("Hello world!");
138/// ```
139///
140/// ### Output
141///
142/// ```bash
143/// Hello world!
144/// ```
145Element text(std::string_view text) {
146 return std::make_shared<Text>(std::string(text));
147}
148
149/// @brief Display a piece of unicode text.
150/// @ingroup dom
151/// @see ftxui::to_wstring
152///
153/// ### Example
154///
155/// ```cpp
156/// Element document = text(L"Hello world!");
157/// ```
158///
159/// ### Output
160///
161/// ```bash
162/// Hello world!
163/// ```
164Element text(std::wstring text) { // NOLINT
165 return std::make_shared<Text>(to_string(text));
166}
167
168/// @brief Display a piece of unicode text.
169/// @ingroup dom
170/// @see ftxui::to_wstring
171///
172/// ### Example
173///
174/// ```cpp
175/// Element document = text(L"Hello world!");
176/// ```
177///
178/// ### Output
179///
180/// ```bash
181/// Hello world!
182/// ```
183Element text(std::wstring_view sv) {
184 return text(std::wstring(sv));
185}
186
187/// @brief Display a piece of unicode text vertically.
188/// @ingroup dom
189/// @see ftxui::to_wstring
190///
191/// ### Example
192///
193/// ```cpp
194/// Element document = vtext("Hello world!");
195/// ```
196///
197/// ### Output
198///
199/// ```bash
200/// H
201/// e
202/// l
203/// l
204/// o
205///
206/// w
207/// o
208/// r
209/// l
210/// d
211/// !
212/// ```
213Element vtext(std::string_view text) {
214 return std::make_shared<VText>(std::string(text));
215}
216
217/// @brief Display a piece unicode text vertically.
218/// @ingroup dom
219/// @see ftxui::to_wstring
220///
221/// ### Example
222///
223/// ```cpp
224/// Element document = vtext(L"Hello world!");
225/// ```
226///
227/// ### Output
228///
229/// ```bash
230/// H
231/// e
232/// l
233/// l
234/// o
235///
236/// w
237/// o
238/// r
239/// l
240/// d
241/// !
242/// ```
243Element vtext(std::wstring text) { // NOLINT
244 return std::make_shared<VText>(to_string(text));
245}
246
247/// @brief Display a piece unicode text vertically.
248/// @ingroup dom
249/// @see ftxui::to_wstring
250///
251/// ### Example
252///
253/// ```cpp
254/// Element document = vtext(L"Hello world!");
255/// ```
256///
257/// ### Output
258///
259/// ```bash
260/// H
261/// e
262/// l
263/// l
264/// o
265///
266/// w
267/// o
268/// r
269/// l
270/// d
271/// !
272/// ```
273Element vtext(std::wstring_view text) { // NOLINT
274 return vtext(std::wstring(text));
275}
276
277} // namespace ftxui
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:164
Element vtext(std::wstring text)
Display a piece unicode text vertically.
Definition text.cpp:243
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
static auto Intersection(Box a, Box b) -> Box
Definition box.cpp:11
A rectangular grid of Pixel.
Definition screen.hpp:26
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:23
std::vector< std::string > Utf8ToGlyphs(std::string_view input)
Definition string.cpp:1358
std::string to_string(std::wstring_view s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:1565
int string_width(std::string_view)
Definition string.cpp:1331