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>
5#include <ftxui/component/event.hpp>
6#include <vector>
7#include "ftxui/component/component.hpp"
8#include "ftxui/component/terminal_input_parser.hpp"
9
10 using namespace ftxui;
11 namespace {
12
13 bool 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
42 int 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
51 Color GeneratorColor(const char* data, size_t size) {
52 return Color::RGB(GeneratorInt(data, size), GeneratorInt(data, size),
53 GeneratorInt(data, size));
54}
55
56 AnimatedColorOption GeneratorAnimatedColorOption(const char* data,
57 size_t size) {
58 AnimatedColorOption option;
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
66 AnimatedColorsOption GeneratorAnimatedColorsOptions(const char* data,
67 size_t size) {
68 AnimatedColorsOption option;
69 option.background = GeneratorAnimatedColorOption(data, size);
70 option.foreground = GeneratorAnimatedColorOption(data, size);
71 return option;
72}
73
74 ButtonOption GeneratorButtonOption(const char* data, size_t size) {
75 ButtonOption option;
76 option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
77 return option;
78}
79
80 UnderlineOption 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
93 MenuEntryOption GeneratorMenuEntryOption(const char* data, size_t size) {
94 MenuEntryOption option;
95 option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
96 return option;
97}
98
99 MenuOption 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
107 bool g_bool;
108 int g_int;
109std::vector<std::string> g_list;
110
111 Components GeneratorComponents(const char*& data, size_t& size, int depth);
112
113 Component 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
183 Components 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
194 extern "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}
ftxui::TerminalInputParser
Definition terminal_input_parser.hpp:17
component.hpp
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const char *data, size_t size)
Definition component_fuzzer.cpp:194
event.hpp
ftxui::UnderlineOption::follower_duration
animation::Duration follower_duration
Definition component_options.hpp:45
ftxui::MenuOption::entries_option
MenuEntryOption entries_option
Definition component_options.hpp:99
ftxui::UnderlineOption::follower_delay
animation::Duration follower_delay
Definition component_options.hpp:46
ftxui::MenuOption::direction
Direction direction
Definition component_options.hpp:100
ftxui::MenuOption::underline
UnderlineOption underline
Definition component_options.hpp:98
ftxui::UnderlineOption::color_active
Color color_active
Definition component_options.hpp:35
ftxui::AnimatedColorOption::active
Color active
Definition component_options.hpp:66
ftxui::UnderlineOption::leader_duration
animation::Duration leader_duration
Definition component_options.hpp:43
ftxui::AnimatedColorOption::duration
animation::Duration duration
Definition component_options.hpp:67
ftxui::AnimatedColorOption::enabled
bool enabled
Definition component_options.hpp:64
ftxui::AnimatedColorOption::inactive
Color inactive
Definition component_options.hpp:65
ftxui::ButtonOption::animated_colors
AnimatedColorsOption animated_colors
Definition component_options.hpp:131
ftxui::UnderlineOption::color_inactive
Color color_inactive
Definition component_options.hpp:36
ftxui::UnderlineOption::leader_delay
animation::Duration leader_delay
Definition component_options.hpp:44
ftxui::Container::Horizontal
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Definition container.cpp:360
ftxui::Maybe
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition src/ftxui/component/maybe.cpp:74
ftxui::ResizableSplitTop
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Definition src/ftxui/component/resizable_split.cpp:283
ftxui::Menu
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Definition src/ftxui/component/menu.cpp:512
ftxui::Toggle
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Definition src/ftxui/component/menu.cpp:554
ftxui::Radiobox
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Definition src/ftxui/component/radiobox.cpp:204
ftxui::Button
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition src/ftxui/component/button.cpp:175
ftxui::Container::Vertical
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Definition container.cpp:317
ftxui::Input
Component Input(InputOption options={})
An input box for editing text.
Definition src/ftxui/component/input.cpp:571
ftxui::ResizableSplitRight
Component ResizableSplitRight(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Definition src/ftxui/component/resizable_split.cpp:249
ftxui::Dropdown
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
Definition src/ftxui/component/dropdown.cpp:22
ftxui::ResizableSplitBottom
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Definition src/ftxui/component/resizable_split.cpp:317
ftxui::Checkbox
Component Checkbox(CheckboxOption options)
Draw checkable element.
Definition src/ftxui/component/checkbox.cpp:108
ftxui::ResizableSplitLeft
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Definition src/ftxui/component/resizable_split.cpp:215
ftxui::Container::Tab
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...
Definition container.cpp:405
ftxui::AnimatedColorOption
Option about a potentially animated color.
Definition component_options.hpp:57
ftxui::ButtonOption
Option for the AnimatedButton component.
Definition component_options.hpp:113
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
ftxui::MenuEntryOption
Option for the MenuEntry component.
Definition component_options.hpp:78
ftxui::MenuOption
Option for the Menu component.
Definition component_options.hpp:86
ftxui::UnderlineOption
Option for the underline effect.
Definition component_options.hpp:32
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition src/ftxui/dom/size.cpp:89
ftxui::Direction
Direction
Direction is an enumeration that represents the four cardinal directions.
Definition direction.hpp:13
ftxui::Render
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
ftxui::Screen::Create
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:394
ftxui::Color::RGB
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 screen/color.cpp:147
ftxui::Color
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Components
std::vector< Component > Components
Definition component_base.hpp:25
ftxui::to_wstring
std::wstring to_wstring(const std::string &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:1637
ftxui::Collapsible
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...
Definition src/ftxui/component/collapsible.cpp:33
ftxui::Slider
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition src/ftxui/component/slider.cpp:346
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::AnimatedColorsOption
Definition component_options.hpp:71
ftxui::AnimatedColorsOption::foreground
AnimatedColorOption foreground
Definition component_options.hpp:73
ftxui::AnimatedColorsOption::background
AnimatedColorOption background
Definition component_options.hpp:72
terminal_input_parser.hpp