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