FTXUI  4.1.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 = static_cast<Direction>(GeneratorInt(data, size) % 4);
100 return option;
101}
102
103bool g_bool;
104int g_int;
105std::vector<std::string> g_list;
106
107Components GeneratorComponents(const char*& data, size_t& size, int depth);
108
109Component GeneratorComponent(const char*& data, size_t& size, int depth) {
110 depth--;
111 int value = GeneratorInt(data, size);
112 if (depth <= 0)
113 return Button(GeneratorString(data, size), [] {});
114
115 constexpr int value_max = 19;
116 value = (value % value_max + value_max) % value_max;
117 switch (value) {
118 case 0:
119 return Button(
120 GeneratorString(data, size), [] {},
121 GeneratorButtonOption(data, size));
122 case 1:
123 return Checkbox(GeneratorString(data, size), &g_bool);
124 case 2:
125 return Input(GeneratorString(data, size), GeneratorString(data, size));
126 case 3:
127 return Menu(&g_list, &g_int, GeneratorMenuOption(data, size));
128 case 4:
129 return Radiobox(&g_list, &g_int);
130 case 5:
131 return Toggle(&g_list, &g_int);
132 case 6:
133 return Slider(GeneratorString(data, size), &g_int,
134 GeneratorInt(data, size), GeneratorInt(data, size),
135 GeneratorInt(data, size));
136 case 7:
137 return ResizableSplitLeft(GeneratorComponent(data, size, depth - 1),
138 GeneratorComponent(data, size, depth - 1),
139 &g_int);
140 case 8:
141 return ResizableSplitRight(GeneratorComponent(data, size, depth - 1),
142 GeneratorComponent(data, size, depth - 1),
143 &g_int);
144 case 9:
145 return ResizableSplitTop(GeneratorComponent(data, size, depth - 1),
146 GeneratorComponent(data, size, depth - 1),
147 &g_int);
148 case 10:
149 return ResizableSplitBottom(GeneratorComponent(data, size, depth - 1),
150 GeneratorComponent(data, size, depth - 1),
151 &g_int);
152 case 11:
153 return Container::Vertical(GeneratorComponents(data, size, depth - 1));
154
155 case 12:
156 return Container::Vertical(GeneratorComponents(data, size, depth - 1),
157 &g_int);
158
159 case 13:
160 return Container::Horizontal(GeneratorComponents(data, size, depth - 1));
161 case 14:
162 return Container::Horizontal(GeneratorComponents(data, size, depth - 1),
163 &g_int);
164 case 15:
165 return Container::Tab(GeneratorComponents(data, size, depth - 1), &g_int);
166 case 16:
167 return Maybe(GeneratorComponent(data, size, depth - 1), &g_bool);
168 case 17:
169 return Dropdown(&g_list, &g_int);
170 case 18:
171 return Collapsible(GeneratorString(data, size),
172 GeneratorComponent(data, size, depth - 1),
173 GeneratorBool(data, size));
174 default:
175 assert(false);
176 }
177}
178
179Components GeneratorComponents(const char*& data, size_t& size, int depth) {
180 Components out;
181 if (depth > 0) {
182 while (size && GeneratorInt(data, size) % 2) {
183 out.push_back(GeneratorComponent(data, size, depth - 1));
184 }
185 }
186 return std::move(out);
187}
188
189} // namespace
190extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
191 g_bool = GeneratorBool(data, size);
192 g_int = GeneratorInt(data, size);
193 g_list = {
194 "test_1", "test_2", "test_3", "test_4", "test_5",
195 };
196
197 int depth = 10;
198 auto component = GeneratorComponent(data, size, depth);
199
200 int width = GeneratorInt(data, size);
201 int height = GeneratorInt(data, size);
202
203 width %= 500;
204 width += 500;
205
206 height %= 500;
207 height += 500;
208
209 auto screen =
211
212 auto event_receiver = MakeReceiver<Task>();
213 {
214 auto parser = TerminalInputParser(event_receiver->MakeSender());
215 for (size_t i = 0; i < size; ++i)
216 parser.Add(data[i]);
217 }
218
219 Task event;
220 while (event_receiver->Receive(&event)) {
221 component->OnEvent(std::get<Event>(event));
222 auto document = component->Render();
223 Render(screen, document);
224 }
225 return 0; // Non-zero return values are reserved for future use.
226}
227
228// Copyright 2021 Arthur Sonzogni. All rights reserved.
229// Use of this source code is governed by the MIT license that can be found in
230// the LICENSE file.
A class representing terminal colors.
Definition color.hpp:18
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:135
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:407
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
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:85
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:523
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:512
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:327
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
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