FTXUI  4.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_fuzzer.cpp
Go to the documentation of this file.
1#include <cassert>
2#include <iostream>
3#include <vector>
6
7using namespace ftxui;
8namespace {
9
10bool GeneratorBool(const char*& data, size_t& size) {
11 if (size == 0)
12 return false;
13
14 auto out = bool(data[0] % 2);
15 data++;
16 size--;
17 return out;
18}
19
20std::string GeneratorString(const char*& data, size_t& size) {
21 int index = 0;
22 while (index < size && data[index])
23 ++index;
24
25 auto out = std::string(data, data + index);
26 data += index;
27 size -= index;
28
29 // The input component do not support invalid UTF8 yet.
30 try {
31 to_wstring(out);
32 } catch (...) {
33 return "0";
34 }
35 return std::move(out);
36}
37
38int GeneratorInt(const char* data, size_t size) {
39 if (size == 0)
40 return 0;
41 auto out = int(data[0]);
42 data++;
43 size--;
44 return out;
45}
46
47Color GeneratorColor(const char* data, size_t size) {
48 return Color::RGB(GeneratorInt(data, size), GeneratorInt(data, size),
49 GeneratorInt(data, size));
50}
51
52AnimatedColorOption GeneratorAnimatedColorOption(const char* data,
53 size_t size) {
55 option.enabled = GeneratorBool(data, size);
56 option.inactive = GeneratorColor(data, size);
57 option.active = GeneratorColor(data, size);
58 option.duration = std::chrono::milliseconds(GeneratorInt(data, size));
59 return option;
60}
61
62AnimatedColorsOption GeneratorAnimatedColorsOptions(const char* data,
63 size_t size) {
65 option.background = GeneratorAnimatedColorOption(data, size);
66 option.foreground = GeneratorAnimatedColorOption(data, size);
67 return option;
68}
69
70ButtonOption GeneratorButtonOption(const char* data, size_t size) {
71 ButtonOption option;
72 option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
73 return option;
74}
75
76UnderlineOption GeneratorUnderlineOption(const char* data, size_t size) {
77 UnderlineOption option;
78 option.enabled = GeneratorBool(data, size);
79 option.color_active = GeneratorColor(data, size);
80 option.color_inactive = GeneratorColor(data, size);
81 option.leader_duration = std::chrono::milliseconds(GeneratorInt(data, size));
82 option.follower_duration =
83 std::chrono::milliseconds(GeneratorInt(data, size));
84 option.leader_delay = std::chrono::milliseconds(GeneratorInt(data, size));
85 option.follower_delay = std::chrono::milliseconds(GeneratorInt(data, size));
86 return option;
87}
88
89MenuEntryOption GeneratorMenuEntryOption(const char* data, size_t size) {
90 MenuEntryOption option;
91 option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
92 return option;
93}
94
95MenuOption GeneratorMenuOption(const char* data, size_t size) {
96 MenuOption option;
97 option.underline = GeneratorUnderlineOption(data, size);
98 option.entries = GeneratorMenuEntryOption(data, size);
99 option.direction =
100 static_cast<MenuOption::Direction>(GeneratorInt(data, size) % 4);
101 return option;
102}
103
104bool g_bool;
105int g_int;
106std::vector<std::string> g_list;
107
108Components GeneratorComponents(const char*& data, size_t& size, int depth);
109
110Component GeneratorComponent(const char*& data, size_t& size, int depth) {
111 depth--;
112 int value = GeneratorInt(data, size);
113 if (depth <= 0)
114 return Button(GeneratorString(data, size), [] {});
115
116 constexpr int value_max = 19;
117 value = (value % value_max + value_max) % value_max;
118 switch (value) {
119 case 0:
120 return Button(
121 GeneratorString(data, size), [] {},
122 GeneratorButtonOption(data, size));
123 case 1:
124 return Checkbox(GeneratorString(data, size), &g_bool);
125 case 2:
126 return Input(GeneratorString(data, size), GeneratorString(data, size));
127 case 3:
128 return Menu(&g_list, &g_int, GeneratorMenuOption(data, size));
129 case 4:
130 return Radiobox(&g_list, &g_int);
131 case 5:
132 return Toggle(&g_list, &g_int);
133 case 6:
134 return Slider(GeneratorString(data, size), &g_int,
135 GeneratorInt(data, size), GeneratorInt(data, size),
136 GeneratorInt(data, size));
137 case 7:
138 return ResizableSplitLeft(GeneratorComponent(data, size, depth - 1),
139 GeneratorComponent(data, size, depth - 1),
140 &g_int);
141 case 8:
142 return ResizableSplitRight(GeneratorComponent(data, size, depth - 1),
143 GeneratorComponent(data, size, depth - 1),
144 &g_int);
145 case 9:
146 return ResizableSplitTop(GeneratorComponent(data, size, depth - 1),
147 GeneratorComponent(data, size, depth - 1),
148 &g_int);
149 case 10:
150 return ResizableSplitBottom(GeneratorComponent(data, size, depth - 1),
151 GeneratorComponent(data, size, depth - 1),
152 &g_int);
153 case 11:
154 return Container::Vertical(GeneratorComponents(data, size, depth - 1));
155
156 case 12:
157 return Container::Vertical(GeneratorComponents(data, size, depth - 1),
158 &g_int);
159
160 case 13:
161 return Container::Horizontal(GeneratorComponents(data, size, depth - 1));
162 case 14:
163 return Container::Horizontal(GeneratorComponents(data, size, depth - 1),
164 &g_int);
165 case 15:
166 return Container::Tab(GeneratorComponents(data, size, depth - 1), &g_int);
167 case 16:
168 return Maybe(GeneratorComponent(data, size, depth - 1), &g_bool);
169 case 17:
170 return Dropdown(&g_list, &g_int);
171 case 18:
172 return Collapsible(GeneratorString(data, size),
173 GeneratorComponent(data, size, depth - 1),
174 GeneratorBool(data, size));
175 default:
176 assert(false);
177 }
178}
179
180Components GeneratorComponents(const char*& data, size_t& size, int depth) {
181 Components out;
182 if (depth > 0) {
183 while (size && GeneratorInt(data, size) % 2) {
184 out.push_back(GeneratorComponent(data, size, depth - 1));
185 }
186 }
187 return std::move(out);
188}
189
190} // namespace
191extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
192 g_bool = GeneratorBool(data, size);
193 g_int = GeneratorInt(data, size);
194 g_list = {
195 "test_1", "test_2", "test_3", "test_4", "test_5",
196 };
197
198 int depth = 10;
199 auto component = GeneratorComponent(data, size, depth);
200
201 int width = GeneratorInt(data, size);
202 int height = GeneratorInt(data, size);
203
204 width %= 500;
205 width += 500;
206
207 height %= 500;
208 height += 500;
209
210 auto screen =
212
213 auto event_receiver = MakeReceiver<Task>();
214 {
215 auto parser = TerminalInputParser(event_receiver->MakeSender());
216 for (size_t i = 0; i < size; ++i)
217 parser.Add(data[i]);
218 }
219
220 Task event;
221 while (event_receiver->Receive(&event)) {
222 component->OnEvent(std::get<Event>(event));
223 auto document = component->Render();
224 Render(screen, document);
225 }
226 return 0; // Non-zero return values are reserved for future use.
227}
228
229// Copyright 2021 Arthur Sonzogni. All rights reserved.
230// Use of this source code is governed by the MIT license that can be found in
231// the LICENSE file.
A class representing terminal colors.
Definition color.hpp:17
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:134
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:405
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)
Component Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option=CheckboxOption::Simple())
Draw checkable element.
Definition checkbox.cpp:112
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition radiobox.cpp:211
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true. @params child the compoennt to de...
Definition maybe.cpp:67
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Input(StringRef content, ConstStringRef placeholder, Ref< InputOption > option={})
An input box for editing text.
Definition input.cpp:337
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Definition menu.cpp:522
std::vector< Component > Components
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:1971
Receiver< T > MakeReceiver()
Definition receiver.hpp:131
Component Button(ConstStringRef label, std::function< void()> on_click, Ref< ButtonOption >=ButtonOption::Simple())
Draw a button. Execute a function when clicked.
Definition button.cpp:59
Component Menu(ConstStringListRef entries, int *selected_, Ref< MenuOption >=MenuOption::Vertical())
A list of text. The focused element is selected.
Definition menu.cpp:511
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)
Definition dropdown.cpp:14
Component Slider(SliderOption< T > options={})
A slider in any direction.
Definition slider.cpp:326
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:11
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:44
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:85
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
std::shared_ptr< ComponentBase > Component
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
MenuEntryOption entries
animation::Duration follower_duration
animation::Duration follower_delay
animation::Duration leader_duration
animation::Duration leader_delay