FTXUI 6.1.9
C++ functional terminal UI.
Chargement...
Recherche...
Aucune correspondance
component_fuzzer.cpp
Aller à la documentation de ce fichier.
1// Copyright 2021 Arthur Sonzogni. Tous droits réservés.
2// L'utilisation de ce code source est régie par la licence MIT qui peut être trouvée dans
3// le fichier LICENSE.
4#include <cassert>
6#include <vector>
9
10using namespace ftxui;
11namespace {
12
13bool GeneratorBool(const char*& data, size_t& size) {
14 if (size == 0) {
15 return false;
16 }
17
18 auto out = bool(data[0] % 2);
19 data++;
20 size--;
21 return out;
22}
23
24std::string GeneratorString(const char*& data, size_t& size) {
25 int index = 0;
26 while (index < size && data[index]) {
27 ++index;
28 }
29
30 auto out = std::string(data, data + index);
31 data += index;
32 size -= index;
33
34 // Le composant d'entrée ne supporte pas encore l'UTF8 invalide.
35 try {
36 to_wstring(out);
37 } catch (...) {
38 return "0";
39 }
40 return std::move(out);
41}
42
43int GeneratorInt(const char* data, size_t size) {
44 if (size == 0) {
45 return 0;
46 }
47 auto out = int(data[0]);
48 data++;
49 size--;
50 return out;
51}
52
53Color GeneratorColor(const char* data, size_t size) {
54 return Color::RGB(GeneratorInt(data, size), GeneratorInt(data, size),
55 GeneratorInt(data, size));
56}
57
58AnimatedColorOption GeneratorAnimatedColorOption(const char* data,
59 size_t size) {
61 option.enabled = GeneratorBool(data, size);
62 option.inactive = GeneratorColor(data, size);
63 option.active = GeneratorColor(data, size);
64 option.duration = std::chrono::milliseconds(GeneratorInt(data, size));
65 return option;
66}
67
68AnimatedColorsOption GeneratorAnimatedColorsOptions(const char* data,
69 size_t size) {
71 option.background = GeneratorAnimatedColorOption(data, size);
72 option.foreground = GeneratorAnimatedColorOption(data, size);
73 return option;
74}
75
76ButtonOption GeneratorButtonOption(const char* data, size_t size) {
77 ButtonOption option;
78 option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
79 return option;
80}
81
82UnderlineOption GeneratorUnderlineOption(const char* data, size_t size) {
83 UnderlineOption option;
84 option.enabled = GeneratorBool(data, size);
85 option.color_active = GeneratorColor(data, size);
86 option.color_inactive = GeneratorColor(data, size);
87 option.leader_duration = std::chrono::milliseconds(GeneratorInt(data, size));
88 option.follower_duration =
89 std::chrono::milliseconds(GeneratorInt(data, size));
90 option.leader_delay = std::chrono::milliseconds(GeneratorInt(data, size));
91 option.follower_delay = std::chrono::milliseconds(GeneratorInt(data, size));
92 return option;
93}
94
95MenuEntryOption GeneratorMenuEntryOption(const char* data, size_t size) {
96 MenuEntryOption option;
97 option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
98 return option;
99}
100
101MenuOption GeneratorMenuOption(const char* data, size_t size) {
102 MenuOption option;
103 option.underline = GeneratorUnderlineOption(data, size);
104 option.entries_option = GeneratorMenuEntryOption(data, size);
105 option.direction = static_cast<Direction>(GeneratorInt(data, size) % 4);
106 return option;
107}
108
109bool g_bool;
110int g_int;
111std::vector<std::string> g_list;
112
113Components GeneratorComponents(const char*& data, size_t& size, int depth);
114
115Component GeneratorComponent(const char*& data, size_t& size, int depth) {
116 depth--;
117 int value = GeneratorInt(data, size);
118 if (depth <= 0) {
119 return Button(GeneratorString(data, size), [] {});
120 }
121
122 constexpr int value_max = 19;
123 value = (value % value_max + value_max) % value_max;
124 switch (value) {
125 case 0:
126 return Button(
127 GeneratorString(data, size), [] {},
128 GeneratorButtonOption(data, size));
129 case 1:
130 return Checkbox(GeneratorString(data, size), &g_bool);
131 case 2:
132 return Input(GeneratorString(data, size), GeneratorString(data, size));
133 case 3:
134 return Menu(&g_list, &g_int, GeneratorMenuOption(data, size));
135 case 4:
136 return Radiobox(&g_list, &g_int);
137 case 5:
138 return Toggle(&g_list, &g_int);
139 case 6:
140 return Slider(GeneratorString(data, size), &g_int,
141 GeneratorInt(data, size), GeneratorInt(data, size),
142 GeneratorInt(data, size));
143 case 7:
144 return ResizableSplitLeft(GeneratorComponent(data, size, depth - 1),
145 GeneratorComponent(data, size, depth - 1),
146 &g_int);
147 case 8:
148 return ResizableSplitRight(GeneratorComponent(data, size, depth - 1),
149 GeneratorComponent(data, size, depth - 1),
150 &g_int);
151 case 9:
152 return ResizableSplitTop(GeneratorComponent(data, size, depth - 1),
153 GeneratorComponent(data, size, depth - 1),
154 &g_int);
155 case 10:
156 return ResizableSplitBottom(GeneratorComponent(data, size, depth - 1),
157 GeneratorComponent(data, size, depth - 1),
158 &g_int);
159 case 11:
160 return Container::Vertical(GeneratorComponents(data, size, depth - 1));
161
162 case 12:
163 return Container::Vertical(GeneratorComponents(data, size, depth - 1),
164 &g_int);
165
166 case 13:
167 return Container::Horizontal(GeneratorComponents(data, size, depth - 1));
168 case 14:
169 return Container::Horizontal(GeneratorComponents(data, size, depth - 1),
170 &g_int);
171 case 15:
172 return Container::Tab(GeneratorComponents(data, size, depth - 1), &g_int);
173 case 16:
174 return Maybe(GeneratorComponent(data, size, depth - 1), &g_bool);
175 case 17:
176 return Dropdown(&g_list, &g_int);
177 case 18:
178 return Collapsible(GeneratorString(data, size),
179 GeneratorComponent(data, size, depth - 1),
180 GeneratorBool(data, size));
181 default:
182 assert(false);
183 }
184}
185
186Components GeneratorComponents(const char*& data, size_t& size, int depth) {
187 Components out;
188 if (depth > 0) {
189 while (size && GeneratorInt(data, size) % 2) {
190 out.push_back(GeneratorComponent(data, size, depth - 1));
191 }
192 }
193 return std::move(out);
194}
195
196} // namespace
197extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
198 g_bool = GeneratorBool(data, size);
199 g_int = GeneratorInt(data, size);
200 g_list = {
201 "test_1", "test_2", "test_3", "test_4", "test_5",
202 };
203
204 int depth = 10;
205 auto component = GeneratorComponent(data, size, depth);
206
207 int width = GeneratorInt(data, size);
208 int height = GeneratorInt(data, size);
209
210 width %= 500;
211 width += 500;
212
213 height %= 500;
214 height += 500;
215
216 auto screen =
217 Screen::Create(Dimension::Fixed(width), Dimension::Fixed(height));
218
219 // Générer quelques événements.
220 std::vector<Event> events;
221 auto parser =
222 TerminalInputParser([&](const Event& event) { events.push_back(event); });
223
224 for (size_t i = 0; i < size; ++i) {
225 parser.Add(data[i]);
226 }
227
228 for (const auto& event : events) {
229 component->OnEvent(event);
230 auto document = component->Render();
231 Render(screen, document);
232 }
233 return 0; // Les valeurs de retour non nulles sont réservées pour une utilisation future.
234}
int LLVMFuzzerTestOneInput(const char *data, size_t size)
auto screen
animation::Duration follower_duration
MenuEntryOption entries_option
animation::Duration follower_delay
UnderlineOption underline
animation::Duration leader_duration
AnimatedColorsOption animated_colors
animation::Duration leader_delay
Component Maybe(Component, const bool *show)
Décore un composant |child|. Il est affiché uniquement lorsque |show| est vrai.
Component ResizableSplitTop(Component main, Component back, int *main_size)
Une séparation verticale entre deux composants, configurable à l'aide de la souris.
Component Menu(MenuOption options)
Une liste de texte. L'élément sélectionné est mis en évidence.
Component Toggle(ConstStringListRef entries, int *selected)
Une liste horizontale d'éléments. L'utilisateur peut naviguer à travers eux.
Component Radiobox(RadioboxOption options)
Une liste d'éléments, où un seul peut être sélectionné.
Component Button(ButtonOption options)
Dessine un bouton. Exécute une fonction lors d'un clic.
Component Input(InputOption options={})
Une zone de saisie pour éditer du texte.
Component ResizableSplitRight(Component main, Component back, int *main_size)
Une séparation horizontale entre deux composants, configurable à l'aide de la souris.
Component Dropdown(ConstStringListRef entries, int *selected)
Un menu déroulant.
Component ResizableSplitBottom(Component main, Component back, int *main_size)
Une séparation verticale entre deux composants, configurable à l'aide de la souris.
Component Checkbox(CheckboxOption options)
Dessine un élément à cocher.
Component ResizableSplitLeft(Component main, Component back, int *main_size)
Une séparation horizontale entre deux composants, configurable à l'aide de la souris.
Option concernant une couleur potentiellement animée.
Option pour le composant AnimatedButton.
Représente un événement. Il peut s'agir d'un événement de touche, d'un redimensionnement de terminal,...
Definition event.hpp:28
Option pour le composant MenuEntry.
Option pour le composant Menu.
Option pour l'effet de soulignement.
Decorator size(WidthOrHeight, Constraint, int value)
Applique une contrainte sur la taille d'un élément.
Direction
Direction est une énumération qui représente les quatre directions cardinales.
Definition direction.hpp:13
static Color RGB(uint8_t red, uint8_t green, uint8_t blue)
Construit une couleur à partir de sa représentation RGB. https://en.wikipedia.org/wiki/RGB_color_mode...
static Screen Create(Dimensions dimension)
Color est une classe qui représente une couleur dans l'interface utilisateur du terminal.
Definition color.hpp:21
L'espace de noms FTXUI ftxui::
Definition animation.hpp:10
std::vector< Component > Components
std::wstring to_wstring(const std::string &s)
Convertit un std::string UTF8 en un std::wstring.
Definition string.cpp:1638
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
Un composant repliable. Il affiche une case à cocher avec une flèche. Une fois activé,...
Component Slider(SliderOption< T > options)
Un curseur dans n'importe quelle direction.
void Render(Screen &screen, const Element &element)
std::shared_ptr< ComponentBase > Component