FTXUI 6.1.9
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>
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 auto out = std::string(data, data + index);
30 data += index;
31 size -= index;
32
33 // The input component do not support invalid UTF8 yet.
34 try {
35 to_wstring(out);
36 } catch (...) {
37 return "0";
38 }
39 return std::move(out);
40}
41
42int GeneratorInt(const char* data, size_t size) {
43 if (size == 0)
44 return 0;
45 auto out = int(data[0]);
46 data++;
47 size--;
48 return out;
49}
50
51Color GeneratorColor(const char* data, size_t size) {
52 return Color::RGB(GeneratorInt(data, size), GeneratorInt(data, size),
53 GeneratorInt(data, size));
54}
55
56AnimatedColorOption GeneratorAnimatedColorOption(const char* data,
57 size_t size) {
59 option.enabled = GeneratorBool(data, size);
60 option.inactive = GeneratorColor(data, size);
61 option.active = GeneratorColor(data, size);
62 option.duration = std::chrono::milliseconds(GeneratorInt(data, size));
63 return option;
64}
65
66AnimatedColorsOption GeneratorAnimatedColorsOptions(const char* data,
67 size_t size) {
69 option.background = GeneratorAnimatedColorOption(data, size);
70 option.foreground = GeneratorAnimatedColorOption(data, size);
71 return option;
72}
73
74ButtonOption GeneratorButtonOption(const char* data, size_t size) {
75 ButtonOption option;
76 option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
77 return option;
78}
79
80UnderlineOption GeneratorUnderlineOption(const char* data, size_t size) {
81 UnderlineOption option;
82 option.enabled = GeneratorBool(data, size);
83 option.color_active = GeneratorColor(data, size);
84 option.color_inactive = GeneratorColor(data, size);
85 option.leader_duration = std::chrono::milliseconds(GeneratorInt(data, size));
86 option.follower_duration =
87 std::chrono::milliseconds(GeneratorInt(data, size));
88 option.leader_delay = std::chrono::milliseconds(GeneratorInt(data, size));
89 option.follower_delay = std::chrono::milliseconds(GeneratorInt(data, size));
90 return option;
91}
92
93MenuEntryOption GeneratorMenuEntryOption(const char* data, size_t size) {
94 MenuEntryOption option;
95 option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
96 return option;
97}
98
99MenuOption GeneratorMenuOption(const char* data, size_t size) {
100 MenuOption option;
101 option.underline = GeneratorUnderlineOption(data, size);
102 option.entries_option = GeneratorMenuEntryOption(data, size);
103 option.direction = static_cast<Direction>(GeneratorInt(data, size) % 4);
104 return option;
105}
106
107bool g_bool;
108int g_int;
109std::vector<std::string> g_list;
110
111Components GeneratorComponents(const char*& data, size_t& size, int depth);
112
113Component GeneratorComponent(const char*& data, size_t& size, int depth) {
114 depth--;
115 int value = GeneratorInt(data, size);
116 if (depth <= 0)
117 return Button(GeneratorString(data, size), [] {});
118
119 constexpr int value_max = 19;
120 value = (value % value_max + value_max) % value_max;
121 switch (value) {
122 case 0:
123 return Button(
124 GeneratorString(data, size), [] {},
125 GeneratorButtonOption(data, size));
126 case 1:
127 return Checkbox(GeneratorString(data, size), &g_bool);
128 case 2:
129 return Input(GeneratorString(data, size), GeneratorString(data, size));
130 case 3:
131 return Menu(&g_list, &g_int, GeneratorMenuOption(data, size));
132 case 4:
133 return Radiobox(&g_list, &g_int);
134 case 5:
135 return Toggle(&g_list, &g_int);
136 case 6:
137 return Slider(GeneratorString(data, size), &g_int,
138 GeneratorInt(data, size), GeneratorInt(data, size),
139 GeneratorInt(data, size));
140 case 7:
141 return ResizableSplitLeft(GeneratorComponent(data, size, depth - 1),
142 GeneratorComponent(data, size, depth - 1),
143 &g_int);
144 case 8:
145 return ResizableSplitRight(GeneratorComponent(data, size, depth - 1),
146 GeneratorComponent(data, size, depth - 1),
147 &g_int);
148 case 9:
149 return ResizableSplitTop(GeneratorComponent(data, size, depth - 1),
150 GeneratorComponent(data, size, depth - 1),
151 &g_int);
152 case 10:
153 return ResizableSplitBottom(GeneratorComponent(data, size, depth - 1),
154 GeneratorComponent(data, size, depth - 1),
155 &g_int);
156 case 11:
157 return Container::Vertical(GeneratorComponents(data, size, depth - 1));
158
159 case 12:
160 return Container::Vertical(GeneratorComponents(data, size, depth - 1),
161 &g_int);
162
163 case 13:
164 return Container::Horizontal(GeneratorComponents(data, size, depth - 1));
165 case 14:
166 return Container::Horizontal(GeneratorComponents(data, size, depth - 1),
167 &g_int);
168 case 15:
169 return Container::Tab(GeneratorComponents(data, size, depth - 1), &g_int);
170 case 16:
171 return Maybe(GeneratorComponent(data, size, depth - 1), &g_bool);
172 case 17:
173 return Dropdown(&g_list, &g_int);
174 case 18:
175 return Collapsible(GeneratorString(data, size),
176 GeneratorComponent(data, size, depth - 1),
177 GeneratorBool(data, size));
178 default:
179 assert(false);
180 }
181}
182
183Components GeneratorComponents(const char*& data, size_t& size, int depth) {
184 Components out;
185 if (depth > 0) {
186 while (size && GeneratorInt(data, size) % 2) {
187 out.push_back(GeneratorComponent(data, size, depth - 1));
188 }
189 }
190 return std::move(out);
191}
192
193} // namespace
194extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
195 g_bool = GeneratorBool(data, size);
196 g_int = GeneratorInt(data, size);
197 g_list = {
198 "test_1", "test_2", "test_3", "test_4", "test_5",
199 };
200
201 int depth = 10;
202 auto component = GeneratorComponent(data, size, depth);
203
204 int width = GeneratorInt(data, size);
205 int height = GeneratorInt(data, size);
206
207 width %= 500;
208 width += 500;
209
210 height %= 500;
211 height += 500;
212
213 auto screen =
214 Screen::Create(Dimension::Fixed(width), Dimension::Fixed(height));
215
216 // Generate some events.
217 std::vector<Event> events;
218 auto parser =
219 TerminalInputParser([&](const Event& event) { events.push_back(event); });
220
221 for (size_t i = 0; i < size; ++i) {
222 parser.Add(data[i]);
223 }
224
225 for (const auto& event : events) {
226 component->OnEvent(event);
227 auto document = component->Render();
228 Render(screen, document);
229 }
230 return 0; // Non-zero return values are reserved for future use.
231}
int LLVMFuzzerTestOneInput(const char *data, size_t size)
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 Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
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.
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Component Input(InputOption options={})
An input box for editing text.
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.
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Checkbox(CheckboxOption options)
Draw checkable element.
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
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...
Option about a potentially animated color.
Option for the AnimatedButton component.
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
Option for the MenuEntry component.
Option for the Menu component.
Option for the underline effect.
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Direction
Direction is an enumeration that represents the four cardinal directions.
Definition direction.hpp:13
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:394
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.
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::vector< Component > Components
std::wstring to_wstring(const std::string &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:1637
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
A collapsible component. It displays a checkbox with an arrow. Once activated, the child is displayed...
Component Slider(SliderOption< T > options)
A slider in any direction.
std::shared_ptr< ComponentBase > Component