FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_fuzzer.cpp
Go to the documentation of this file.
1// Copyright 2021 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 <cassert>
5#include <vector>
8
9using namespace ftxui;
10namespace {
11
12bool GeneratorBool(const char*& data, size_t& size) {
13 if (size == 0) {
14 return false;
15 }
16
17 auto out = bool(data[0] % 2);
18 data++;
19 size--;
20 return out;
21}
22
23std::string GeneratorString(const char*& data, size_t& size) {
24 int index = 0;
25 while (index < size && data[index])
26 ++index;
27
28 auto out = std::string(data, data + index);
29 data += index;
30 size -= index;
31
32 // The input component do not support invalid UTF8 yet.
33 try {
35 } catch (...) {
36 return "0";
37 }
38 return std::move(out);
39}
40
41int GeneratorInt(const char* data, size_t size) {
42 if (size == 0)
43 return 0;
44 auto out = int(data[0]);
45 data++;
46 size--;
47 return out;
48}
49
50Color GeneratorColor(const char* data, size_t size) {
53}
54
56 size_t size) {
59 option.inactive = GeneratorColor(data, size);
60 option.active = GeneratorColor(data, size);
61 option.duration = std::chrono::milliseconds(GeneratorInt(data, size));
62 return option;
63}
64
66 size_t size) {
70 return option;
71}
72
73ButtonOption GeneratorButtonOption(const char* data, size_t size) {
76 return option;
77}
78
82 option.color_active = GeneratorColor(data, size);
83 option.color_inactive = GeneratorColor(data, size);
84 option.leader_duration = std::chrono::milliseconds(GeneratorInt(data, size));
85 option.follower_duration =
86 std::chrono::milliseconds(GeneratorInt(data, size));
87 option.leader_delay = std::chrono::milliseconds(GeneratorInt(data, size));
88 option.follower_delay = std::chrono::milliseconds(GeneratorInt(data, size));
89 return option;
90}
91
95 return option;
96}
97
98MenuOption GeneratorMenuOption(const char* data, size_t size) {
101 option.entries_option = GeneratorMenuEntryOption(data, size);
102 option.direction = static_cast<Direction>(GeneratorInt(data, size) % 4);
103 return option;
104}
105
106bool g_bool;
107int g_int;
108std::vector<std::string> g_list;
109
110Components GeneratorComponents(const char*& data, size_t& size, int depth);
111
112Component GeneratorComponent(const char*& data, size_t& size, int depth) {
113 depth--;
114 int value = GeneratorInt(data, size);
115 if (depth <= 0)
116 return Button(GeneratorString(data, size), [] {});
117
118 constexpr int value_max = 19;
119 value = (value % value_max + value_max) % value_max;
120 switch (value) {
121 case 0:
122 return Button(
123 GeneratorString(data, size), [] {},
125 case 1:
127 case 2:
129 case 3:
131 case 4:
132 return Radiobox(&g_list, &g_int);
133 case 5:
134 return Toggle(&g_list, &g_int);
135 case 6:
139 case 7:
142 &g_int);
143 case 8:
146 &g_int);
147 case 9:
150 &g_int);
151 case 10:
154 &g_int);
155 case 11:
157
158 case 12:
160 &g_int);
161
162 case 13:
164 case 14:
166 &g_int);
167 case 15:
169 case 16:
170 return Maybe(GeneratorComponent(data, size, depth - 1), &g_bool);
171 case 17:
172 return Dropdown(&g_list, &g_int);
173 case 18:
177 default:
178 assert(false);
179 }
180}
181
182Components GeneratorComponents(const char*& data, size_t& size, int depth) {
184 if (depth > 0) {
185 while (size && GeneratorInt(data, size) % 2) {
186 out.push_back(GeneratorComponent(data, size, depth - 1));
187 }
188 }
189 return std::move(out);
190}
191
192} // namespace
193extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
196 g_list = {
197 "test_1", "test_2", "test_3", "test_4", "test_5",
198 };
199
200 int depth = 10;
202
203 int width = GeneratorInt(data, size);
204 int height = GeneratorInt(data, size);
205
206 width %= 500;
207 width += 500;
208
209 height %= 500;
210 height += 500;
211
212 auto screen =
214
216 {
217 auto parser = TerminalInputParser(event_receiver->MakeSender());
218 for (size_t i = 0; i < size; ++i)
219 parser.Add(data[i]);
220 }
221
222 Task event;
223 while (event_receiver->Receive(&event)) {
224 component->OnEvent(std::get<Event>(event));
225 auto document = component->Render();
227 }
228 return 0; // Non-zero return values are reserved for future use.
229}
A class representing terminal colors.
Definition color.hpp:20
static Color RGB(uint8_t red, uint8_t green, uint8_t blue)
Build a Color from its RGB representation. https://en.wikipedia.org/wiki/RGB_color_model.
Definition color.cpp:153
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:388
int LLVMFuzzerTestOneInput(const char *data, size_t size)
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Component Tab(Components children, int *selector)
A list of components, where only one is drawn and interacted with at a time. The |selector| gives the...
Dimensions Fixed(int)
Definition screen.cpp:369
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:89
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition maybe.cpp:74
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Definition menu.cpp:512
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< ComponentBase > Component
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Definition menu.cpp:554
std::vector< Component > Components
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Definition radiobox.cpp:205
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition button.cpp:174
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:1637
Component Input(InputOption options={})
An input box for editing text.
Definition input.cpp:574
Component ResizableSplitRight(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
Definition dropdown.cpp:22
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition slider.cpp:346
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:14
AnimatedColorsOption animated_colors
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:72
Component Checkbox(CheckboxOption options)
Draw checkable element.
Definition checkbox.cpp:108
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
Option for the MenuEntry component.
Option about a potentially animated color.
Option for the AnimatedButton component.
AnimatedColorsOption animated_colors
Option for the Menu component.
UnderlineOption underline
‍The index of the selected entry.