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