FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/dom/gauge.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// Copyright 2020 Arthur Sonzogni. 全著作権所有。
5// このソースコードの使用は、LICENSE ファイルにある MIT ライセンスに従います。
6#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
7#include <memory> // for allocator, make_shared
8#include <string> // for string
9
10#include "ftxui/dom/elements.hpp" // for Element, gauge, gaugeDirection, gaugeDown, gaugeLeft, gaugeRight, gaugeUp
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 Screen, Pixel
15
16namespace ftxui {
17
18namespace {
19// NOLINTNEXTLINE
20static const std::string charset_horizontal[11] = {
21#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
22 // Microsoftのターミナルは、完全なゲージを表す8つのUnicode文字を処理できないフォントを使用していることがよくあります。
23 // そのため、より少ない文字でフォールバックします。
24 " ", " ", " ", " ", "▌", "▌", "▌", "█", "█", "█",
25#else
26 " ", " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█",
27#endif
28 // ファザーが以下のような状況を管理する場合の追加文字:
29 // int(9 * (limit - limit_int) = 9
30 "█"};
31
32// NOLINTNEXTLINE
33static const std::string charset_vertical[10] = {
34 "█",
35 "▇",
36 "▆",
37 "▅",
38 "▄",
39 "▃",
40 "▂",
41 " ",
42 " ",
43 // ファザーが以下のような状況を管理する場合の追加文字:
44 // int(8 * (limit - limit_int) = 8
45 " ",
46};
47
48class Gauge : public Node {
49 public:
50 Gauge(float progress, Direction direction)
51 : progress_(progress), direction_(direction) {
52 // This handle NAN correctly:
53 // これはNANを正しく処理します:
54 if (!(progress_ > 0.F)) {
55 progress_ = 0.F;
56 }
57 if (!(progress_ < 1.F)) {
58 progress_ = 1.F;
59 }
60 }
61
62 void ComputeRequirement() override {
63 switch (direction_) {
65 case Direction::Left:
66 requirement_.flex_grow_x = 1;
67 requirement_.flex_grow_y = 0;
68 requirement_.flex_shrink_x = 1;
69 requirement_.flex_shrink_y = 0;
70 break;
71 case Direction::Up:
72 case Direction::Down:
73 requirement_.flex_grow_x = 0;
74 requirement_.flex_grow_y = 1;
75 requirement_.flex_shrink_x = 0;
76 requirement_.flex_shrink_y = 1;
77 break;
78 }
79 requirement_.min_x = 1;
80 requirement_.min_y = 1;
81 }
82
83 void Render(Screen& screen) override {
84 switch (direction_) {
86 RenderHorizontal(screen, /*invert=*/false);
87 break;
88 case Direction::Up:
89 RenderVertical(screen, /*invert=*/false);
90 break;
91 case Direction::Left:
92 RenderHorizontal(screen, /*invert=*/true);
93 break;
94 case Direction::Down:
95 RenderVertical(screen, /*invert=*/true);
96 break;
97 }
98 }
99
100 void RenderHorizontal(Screen& screen, bool invert) {
101 const int y = box_.y_min;
102 if (y > box_.y_max) {
103 return;
104 }
105
106 // Draw the progress bar horizontally.
107 // プログレスバーを水平に描画します。
108 {
109 const float progress = invert ? 1.F - progress_ : progress_;
110 const auto limit =
111 float(box_.x_min) + progress * float(box_.x_max - box_.x_min + 1);
112 const int limit_int = static_cast<int>(limit);
113 int x = box_.x_min;
114 while (x < limit_int) {
115 screen.at(x++, y) = charset_horizontal[9]; // NOLINT
116 }
117 // NOLINTNEXTLINE
118 screen.at(x++, y) = charset_horizontal[int(9 * (limit - limit_int))];
119 while (x <= box_.x_max) {
120 screen.at(x++, y) = charset_horizontal[0];
121 }
122 }
123
124 if (invert) {
125 for (int x = box_.x_min; x <= box_.x_max; x++) {
126 screen.PixelAt(x, y).inverted ^= true;
127 }
128 }
129 }
130
131 void RenderVertical(Screen& screen, bool invert) {
132 const int x = box_.x_min;
133 if (x > box_.x_max) {
134 return;
135 }
136
137 // Draw the progress bar vertically:
138 // プログレスバーを垂直に描画します。
139 {
140 const float progress = invert ? progress_ : 1.F - progress_;
141 const float limit =
142 float(box_.y_min) + progress * float(box_.y_max - box_.y_min + 1);
143 const int limit_int = static_cast<int>(limit);
144 int y = box_.y_min;
145 while (y < limit_int) {
146 screen.at(x, y++) = charset_vertical[8]; // NOLINT
147 }
148 // NOLINTNEXTLINE
149 screen.at(x, y++) = charset_vertical[int(8 * (limit - limit_int))];
150 while (y <= box_.y_max) {
151 screen.at(x, y++) = charset_vertical[0];
152 }
153 }
154
155 if (invert) {
156 for (int y = box_.y_min; y <= box_.y_max; y++) {
157 screen.PixelAt(x, y).inverted ^= true;
158 }
159 }
160 }
161
162 private:
163 float progress_;
164 Direction direction_;
165};
166
167} // namespace
168
169/// @brief 指定された方向に進行する高精細プログレスバーを描画します。
170/// @param progress 塗りつぶされる領域の割合。[0,1]に属します。
171/// @param direction プログレスバーの進行方向。
172/// @ingroup dom
173Element gaugeDirection(float progress, Direction direction) {
174 return std::make_shared<Gauge>(progress, direction);
175}
176
177/// @brief 左から右へ進行する高精細プログレスバーを描画します。
178/// @param progress 塗りつぶされる領域の割合。[0,1]に属します。
179/// @ingroup dom
180///
181/// ### Example
182///
183/// ゲージ。プログレスバーとして使用できます。
184/// ~~~cpp
185/// border(gaugeRight(0.5))
186/// ~~~
187///
188/// #### Output
189///
190/// ~~~bash
191/// ┌──────────────────────────────────────────────────────────────────────────┐
192/// │█████████████████████████████████████ │
193/// └──────────────────────────────────────────────────────────────────────────┘
194/// ~~~
195Element gaugeRight(float progress) {
196 return gaugeDirection(progress, Direction::Right);
197}
198
199/// @brief 右から左へ進行する高精細プログレスバーを描画します。
200/// @param progress 塗りつぶされる領域の割合。[0,1]に属します。
201/// @ingroup dom
202///
203/// ### Example
204///
205/// ゲージ。プログレスバーとして使用できます。
206/// ~~~cpp
207/// border(gaugeLeft(0.5))
208/// ~~~
209///
210/// #### Output
211///
212/// ~~~bash
213/// ┌──────────────────────────────────────────────────────────────────────────┐
214/// │ █████████████████████████████████████│
215/// └──────────────────────────────────────────────────────────────────────────┘
216/// ~~~
217Element gaugeLeft(float progress) {
218 return gaugeDirection(progress, Direction::Left);
219}
220
221/// @brief 下から上へ進行する高精細プログレスバーを描画します。
222/// @param progress 塗りつぶされる領域の割合。[0,1]に属します。
223/// @ingroup dom
224///
225/// ### Example
226///
227/// ゲージ。プログレスバーとして使用できます。
228/// ~~~cpp
229/// border(gaugeUp(0.5))
230/// ~~~
231///
232/// #### Output
233///
234/// ~~~bash
235/// ┌─┐
236/// │ │
237/// │ │
238/// │ │
239/// │ │
240/// │█│
241/// │█│
242/// │█│
243/// │█│
244/// └─┘
245/// ~~~
246Element gaugeUp(float progress) {
247 return gaugeDirection(progress, Direction::Up);
248}
249
250/// @brief 上から下へ進行する高精細プログレスバーを描画します。
251/// @param progress 塗りつぶされる領域の割合。[0,1]に属します。
252/// @ingroup dom
253///
254/// ### Example
255///
256/// ゲージ。プログレスバーとして使用できます。
257/// ~~~cpp
258/// border(gaugeDown(0.5))
259/// ~~~
260///
261/// #### Output
262///
263/// ~~~bash
264/// ┌─┐
265/// │█│
266/// │█│
267/// │█│
268/// │█│
269/// │ │
270/// │ │
271/// │ │
272/// │ │
273/// └─┘
274/// ~~~
275Element gaugeDown(float progress) {
276 return gaugeDirection(progress, Direction::Down);
277}
278
279/// @brief 高精細プログレスバーを描画します。
280/// @param progress 塗りつぶされる領域の割合。[0,1]に属します。
281/// @ingroup dom
282///
283/// ### Example
284///
285/// ゲージ。プログレスバーとして使用できます。
286/// ~~~cpp
287/// border(gauge(0.5))
288/// ~~~
289///
290/// #### Output
291///
292/// ~~~bash
293/// ┌──────────────────────────────────────────────────────────────────────────┐
294/// │█████████████████████████████████████ │
295/// └──────────────────────────────────────────────────────────────────────────┘
296/// ~~~
297Element gauge(float progress) {
298 return gaugeRight(progress);
299}
300
301} // namespace ftxui
Element gaugeDirection(float progress, Direction direction)
指定された方向に進行する高精細プログレスバーを描画します。
Direction
Directionは、東西南北の4つの基本方向を表す列挙型です。
Definition direction.hpp:11
Element gaugeRight(float progress)
左から右へ進行する高精細プログレスバーを描画します。
Element gaugeUp(float progress)
下から上へ進行する高精細プログレスバーを描画します。
Element gaugeLeft(float progress)
右から左へ進行する高精細プログレスバーを描画します。
void Render(Screen &screen, const Element &element)
要素をftxui::Screenに表示します。
Definition node.cpp:84
Element gauge(float progress)
高精細プログレスバーを描画します。
Element gaugeDown(float progress)
上から下へ進行する高精細プログレスバーを描画します。
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
std::shared_ptr< Node > Element
Definition elements.hpp:21