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