FTXUI 6.1.9
C++ functional terminal UI.
Chargement...
Recherche...
Aucune correspondance
src/ftxui/dom/gauge.cpp
Aller à la documentation de ce fichier.
1// Copyright 2020 Arthur Sonzogni. Tous droits réservés.
2// L'utilisation de ce code source est régie par la licence MIT qui se trouve
3// dans le fichier LICENSE.
4#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
5#include <memory> // for allocator, make_shared
6#include <string> // for string
7
8#include "ftxui/dom/elements.hpp" // for Element, gauge, gaugeDirection, gaugeDown, gaugeLeft, gaugeRight, gaugeUp
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 Screen, Pixel
13
14namespace ftxui {
15
16namespace {
17// NOLINTNEXTLINE
18static const std::string charset_horizontal[11] = {
19#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
20 // Les terminaux Microsoft utilisent souvent des polices ne gérant pas les 8
21 // caractères Unicode pour représenter la jauge complète. Repli avec moins. " ", " ", " ", " ", "▌", "▌", "▌", "█", "█", "█",
22#else
23 " ", " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█",
24#endif
25 // Un caractère supplémentaire au cas où le fuzzer parviendrait à avoir :
26 // int(9 * (limit - limit_int) = 9 "█"};
27
28// NOLINTNEXTLINE
29static const std::string charset_vertical[10] = {
30 "█",
31 "▇",
32 "▆",
33 "▅",
34 "▄",
35 "▃",
36 "▂",
37 "▁",
38 " ",
39 // Un caractère supplémentaire au cas où le fuzzer parviendrait à avoir :
40 // int(8 * (limit - limit_int) = 8 " ",
41};
42
43class Gauge : public Node {
44 public:
45 Gauge(float progress, Direction direction)
46 : progress_(progress), direction_(direction) {
47 // This handle NAN correctly:
48 if (!(progress_ > 0.F)) {
49 progress_ = 0.F;
50 }
51 if (!(progress_ < 1.F)) {
52 progress_ = 1.F;
53 }
54 }
55
56 void ComputeRequirement() override {
57 switch (direction_) {
58 case Direction::Right:
59 case Direction::Left:
60 requirement_.flex_grow_x = 1;
61 requirement_.flex_grow_y = 0;
62 requirement_.flex_shrink_x = 1;
63 requirement_.flex_shrink_y = 0;
64 break;
65 case Direction::Up:
66 case Direction::Down:
67 requirement_.flex_grow_x = 0;
68 requirement_.flex_grow_y = 1;
69 requirement_.flex_shrink_x = 0;
70 requirement_.flex_shrink_y = 1;
71 break;
72 }
73 requirement_.min_x = 1;
74 requirement_.min_y = 1;
75 }
76
77 void Render(Screen& screen) override {
78 switch (direction_) {
79 case Direction::Right:
80 RenderHorizontal(screen, /*invert=*/false);
81 break;
82 case Direction::Up:
83 RenderVertical(screen, /*invert=*/false);
84 break;
85 case Direction::Left:
86 RenderHorizontal(screen, /*invert=*/true);
87 break;
88 case Direction::Down:
89 RenderVertical(screen, /*invert=*/true);
90 break;
91 }
92 }
93
94 void RenderHorizontal(Screen& screen, bool invert) {
95 const int y = box_.y_min;
96 if (y > box_.y_max) {
97 return;
98 }
99
100 // Draw the progress bar horizontally.
101 {
102 const float progress = invert ? 1.F - progress_ : progress_;
103 const auto limit =
104 float(box_.x_min) + progress * float(box_.x_max - box_.x_min + 1);
105 const int limit_int = static_cast<int>(limit);
106 int x = box_.x_min;
107 while (x < limit_int) {
108 screen.at(x++, y) = charset_horizontal[9]; // NOLINT
109 }
110 // NOLINTNEXTLINE
111 screen.at(x++, y) = charset_horizontal[int(9 * (limit - limit_int))];
112 while (x <= box_.x_max) {
113 screen.at(x++, y) = charset_horizontal[0];
114 }
115 }
116
117 if (invert) {
118 for (int x = box_.x_min; x <= box_.x_max; x++) {
119 screen.PixelAt(x, y).inverted ^= true;
120 }
121 }
122 }
123
124 void RenderVertical(Screen& screen, bool invert) {
125 const int x = box_.x_min;
126 if (x > box_.x_max) {
127 return;
128 }
129
130 // Draw the progress bar vertically:
131 {
132 const float progress = invert ? progress_ : 1.F - progress_;
133 const float limit =
134 float(box_.y_min) + progress * float(box_.y_max - box_.y_min + 1);
135 const int limit_int = static_cast<int>(limit);
136 int y = box_.y_min;
137 while (y < limit_int) {
138 screen.at(x, y++) = charset_vertical[8]; // NOLINT
139 }
140 // NOLINTNEXTLINE
141 screen.at(x, y++) = charset_vertical[int(8 * (limit - limit_int))];
142 while (y <= box_.y_max) {
143 screen.at(x, y++) = charset_vertical[0];
144 }
145 }
146
147 if (invert) {
148 for (int y = box_.y_min; y <= box_.y_max; y++) {
149 screen.PixelAt(x, y).inverted ^= true;
150 }
151 }
152 }
153
154 private:
155 float progress_;
156 Direction direction_;
157};
158
159} // namespace
160
161/// @brief Dessine une barre de progression haute définition progressant dans la
162/// direction spécifiée.
163/// @param progress La proportion de la zone à remplir. Appartient à [0,1].
164/// @param direction Direction of progress bars progression.
165/// @ingroup dom
166Element gaugeDirection(float progress, Direction direction) {
167 return std::make_shared<Gauge>(progress, direction);
168}
169
170/// @brief Dessine une barre de progression haute définition progressant de
171/// gauche à droite.
172/// @param progress La proportion de la zone à remplir. Appartient à [0,1].
173///
174/// ### Exemple
175///
176/// Une jauge. Elle peut être utilisée pour représenter une barre de progression.
177/// ~~~cpp
178/// border(gaugeRight(0.5))
179/// ~~~
180///
181/// #### Output
182///
183/// ~~~bash
184/// ┌──────────────────────────────────────────────────────────────────────────┐
185/// │█████████████████████████████████████ │
186/// └──────────────────────────────────────────────────────────────────────────┘
187/// ~~~
188Element gaugeRight(float progress) {
189 return gaugeDirection(progress, Direction::Right);
190}
191
192/// @brief Dessine une barre de progression haute définition progressant de
193/// droite à gauche.
194/// @param progress La proportion de la zone à remplir. Appartient à [0,1].
195///
196/// ### Exemple
197///
198/// Une jauge. Elle peut être utilisée pour représenter une barre de progression.
199/// ~~~cpp
200/// border(gaugeLeft(0.5))
201/// ~~~
202///
203/// #### Output
204///
205/// ~~~bash
206/// ┌──────────────────────────────────────────────────────────────────────────┐
207/// │ █████████████████████████████████████│
208/// └──────────────────────────────────────────────────────────────────────────┘
209/// ~~~
210Element gaugeLeft(float progress) {
211 return gaugeDirection(progress, Direction::Left);
212}
213
214/// @brief Dessine une barre de progression haute définition progressant de
215/// bas en haut.
216/// @param progress La proportion de la zone à remplir. Appartient à [0,1].
217///
218/// ### Exemple
219///
220/// Une jauge. Elle peut être utilisée pour représenter une barre de progression.
221/// ~~~cpp
222/// border(gaugeUp(0.5))
223/// ~~~
224///
225/// #### Output
226///
227/// ~~~bash
228/// ┌─┐
229/// │ │
230/// │ │
231/// │ │
232/// │ │
233/// │█│
234/// │█│
235/// │█│
236/// │█│
237/// └─┘
238/// ~~~
239Element gaugeUp(float progress) {
240 return gaugeDirection(progress, Direction::Up);
241}
242
243/// @brief Dessine une barre de progression haute définition progressant de
244/// haut en bas.
245/// @param progress La proportion de la zone à remplir. Appartient à [0,1].
246/// @ingroup dom
247///
248/// ### Exemple
249///
250/// Une jauge. Elle peut être utilisée pour représenter une barre de progression.
251/// ~~~cpp
252/// border(gaugeDown(0.5))
253/// ~~~
254///
255/// #### Output
256///
257/// ~~~bash
258/// ┌─┐
259/// │█│
260/// │█│
261/// │█│
262/// │█│
263/// │ │
264/// │ │
265/// │ │
266/// │ │
267/// └─┘
268/// ~~~
269Element gaugeDown(float progress) {
270 return gaugeDirection(progress, Direction::Down);
271}
272
273/// @brief Dessine une barre de progression haute définition.
274/// @param progress La proportion de la zone à remplir. Appartient à [0,1].
275/// @ingroup dom
276///
277/// ### Exemple
278///
279/// Une jauge. Elle peut être utilisée pour représenter une barre de progression.
280/// ~~~cpp
281/// border(gauge(0.5))
282/// ~~~
283///
284/// #### Output
285///
286/// ~~~bash
287/// ┌──────────────────────────────────────────────────────────────────────────┐
288/// │█████████████████████████████████████ │
289/// └──────────────────────────────────────────────────────────────────────────┘
290/// ~~~
291Element gauge(float progress) {
292 return gaugeRight(progress);
293}
294
295} // namespace ftxui
auto screen
Direction
Direction est une énumération qui représente les quatre directions cardinales.
Definition direction.hpp:13
void Render(Screen &screen, const Element &element)
Affiche un élément sur un ftxui::Screen.
Definition node.cpp:83
L'espace de noms FTXUI ftxui::
Definition animation.hpp:10
Element gaugeDirection(float progress, Direction direction)
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element gaugeRight(float progress)
Element gaugeUp(float progress)
Element gaugeLeft(float progress)
Element gauge(float progress)
Element gaugeDown(float progress)