FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
text.cpp
Go to the documentation of this file.
1#include <algorithm> // for min
2#include <memory> // for make_shared
3#include <string> // for string, wstring
4#include <utility> // for move
5#include <vector> // for vector
6
7#include "ftxui/dom/deprecated.hpp" // for text, vtext
8#include "ftxui/dom/elements.hpp" // for Element, text, vtext
9#include "ftxui/dom/node.hpp" // for Node
10#include "ftxui/dom/requirement.hpp" // for Requirement
11#include "ftxui/screen/box.hpp" // for Box
12#include "ftxui/screen/screen.hpp" // for Pixel, Screen
13#include "ftxui/screen/string.hpp" // for string_width, Utf8ToGlyphs, to_string
14
15namespace ftxui {
16
17using ftxui::Screen;
18
19class Text : public Node {
20 public:
21 explicit Text(std::string text) : text_(std::move(text)) {}
22
23 void ComputeRequirement() override {
26 }
27
28 void Render(Screen& screen) override {
29 int x = box_.x_min;
30 const int y = box_.y_min;
31 if (y > box_.y_max) {
32 return;
33 }
34 for (const auto& cell : Utf8ToGlyphs(text_)) {
35 if (x > box_.x_max) {
36 return;
37 }
38 screen.PixelAt(x, y).character = cell;
39 ++x;
40 }
41 }
42
43 private:
44 std::string text_;
45};
46
47class VText : public Node {
48 public:
49 explicit VText(std::string text)
50 : text_(std::move(text)), width_{std::min(string_width(text_), 1)} {}
51
52 void ComputeRequirement() override {
53 requirement_.min_x = width_;
55 }
56
57 void Render(Screen& screen) override {
58 const int x = box_.x_min;
59 int y = box_.y_min;
60 if (x + width_ - 1 > box_.x_max) {
61 return;
62 }
63 for (const auto& it : Utf8ToGlyphs(text_)) {
64 if (y > box_.y_max) {
65 return;
66 }
67 screen.PixelAt(x, y).character = it;
68 y += 1;
69 }
70 }
71
72 private:
73 std::string text_;
74 int width_ = 1;
75};
76
77/// @brief Display a piece of UTF8 encoded unicode text.
78/// @ingroup dom
79/// @see ftxui::to_wstring
80///
81/// ### Example
82///
83/// ```cpp
84/// Element document = text("Hello world!");
85/// ```
86///
87/// ### Output
88///
89/// ```bash
90/// Hello world!
91/// ```
92Element text(std::string text) {
93 return std::make_shared<Text>(std::move(text));
94}
95
96/// @brief Display a piece of unicode text.
97/// @ingroup dom
98/// @see ftxui::to_wstring
99///
100/// ### Example
101///
102/// ```cpp
103/// Element document = text(L"Hello world!");
104/// ```
105///
106/// ### Output
107///
108/// ```bash
109/// Hello world!
110/// ```
111Element text(std::wstring text) { // NOLINT
112 return std::make_shared<Text>(to_string(text));
113}
114
115/// @brief Display a piece of unicode text vertically.
116/// @ingroup dom
117/// @see ftxui::to_wstring
118///
119/// ### Example
120///
121/// ```cpp
122/// Element document = vtext("Hello world!");
123/// ```
124///
125/// ### Output
126///
127/// ```bash
128/// H
129/// e
130/// l
131/// l
132/// o
133///
134/// w
135/// o
136/// r
137/// l
138/// d
139/// !
140/// ```
141Element vtext(std::string text) {
142 return std::make_shared<VText>(std::move(text));
143}
144
145/// @brief Display a piece unicode text vertically.
146/// @ingroup dom
147/// @see ftxui::to_wstring
148///
149/// ### Example
150///
151/// ```cpp
152/// Element document = vtext(L"Hello world!");
153/// ```
154///
155/// ### Output
156///
157/// ```bash
158/// H
159/// e
160/// l
161/// l
162/// o
163///
164/// w
165/// o
166/// r
167/// l
168/// d
169/// !
170/// ```
171Element vtext(std::wstring text) { // NOLINT
172 return std::make_shared<VText>(to_string(text));
173}
174
175} // namespace ftxui
176
177// Copyright 2020 Arthur Sonzogni. All rights reserved.
178// Use of this source code is governed by the MIT license that can be found in
179// the LICENSE file.
Requirement requirement_
Definition node.hpp:54
Box box_
Definition node.hpp:55
A rectangular grid of Pixel.
Definition screen.hpp:57
std::shared_ptr< Node > Element
Definition elements.hpp:19
std::vector< std::string > Utf8ToGlyphs(const std::string &input)
Definition string.cpp:1727
int string_width(const std::string &)
Definition string.cpp:1700
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:1899
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:111
Element vtext(std::wstring text)
Display a piece unicode text vertically.
Definition text.cpp:171
int x_max
Definition box.hpp:8
int y_min
Definition box.hpp:9
int y_max
Definition box.hpp:10
int x_min
Definition box.hpp:7