FTXUI  0.10.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_fuzzer.cpp
Go to the documentation of this file.
1#include <iostream>
2//#include "ftxui/component/event.hpp"
3//#include "ftxui/component/receiver.hpp"
4#include <vector>
7
8using namespace ftxui;
9namespace {
10
11bool GeneratorBool(const char*& data, size_t& size) {
12 if (size == 0)
13 return false;
14
15 auto out = bool(data[0] % 2);
16 data++;
17 size--;
18 return out;
19}
20
21std::string GeneratorString(const char*& data, size_t& size) {
22 int index = 0;
23 while (index < size && data[index])
24 ++index;
25
26 auto out = std::string(data, data + index);
27 data += index;
28 size -= index;
29
30 // The input component do not support invalid UTF8 yet.
31 try {
32 to_wstring(out);
33 } catch (...) {
34 return "0";
35 }
36 return std::move(out);
37}
38
39int GeneratorInt(const char* data, size_t size) {
40 if (size == 0)
41 return 0;
42 auto out = int(data[0]);
43 data++;
44 size--;
45 return out;
46}
47
48bool g_bool;
49int g_int;
50std::vector<std::string> g_list;
51
52Components GeneratorComponents(const char*& data, size_t& size, int depth);
53
54Component GeneratorComponent(const char*& data, size_t& size, int depth) {
55 depth--;
56 int value = GeneratorInt(data, size);
57 if (depth <= 0)
58 return Button(GeneratorString(data, size), [] {});
59
60 switch (value % 18) {
61 case 1:
62 return Checkbox(GeneratorString(data, size), &g_bool);
63 case 2:
64 return Input(GeneratorString(data, size), GeneratorString(data, size));
65 case 3:
66 return Menu(&g_list, &g_int);
67 case 4:
68 return Radiobox(&g_list, &g_int);
69 case 5:
70 return Toggle(&g_list, &g_int);
71 case 6:
72 return Slider(GeneratorString(data, size), &g_int,
73 GeneratorInt(data, size), GeneratorInt(data, size),
74 GeneratorInt(data, size));
75 case 7:
76 return ResizableSplitLeft(GeneratorComponent(data, size, depth - 1),
77 GeneratorComponent(data, size, depth - 1),
78 &g_int);
79 case 8:
80 return ResizableSplitRight(GeneratorComponent(data, size, depth - 1),
81 GeneratorComponent(data, size, depth - 1),
82 &g_int);
83 case 9:
84 return ResizableSplitTop(GeneratorComponent(data, size, depth - 1),
85 GeneratorComponent(data, size, depth - 1),
86 &g_int);
87 case 10:
88 return ResizableSplitBottom(GeneratorComponent(data, size, depth - 1),
89 GeneratorComponent(data, size, depth - 1),
90 &g_int);
91 case 11:
92 return Container::Vertical(GeneratorComponents(data, size, depth - 1));
93
94 case 12:
95 return Container::Vertical(GeneratorComponents(data, size, depth - 1),
96 &g_int);
97
98 case 13:
99 return Container::Horizontal(GeneratorComponents(data, size, depth - 1));
100 case 14:
101 return Container::Horizontal(GeneratorComponents(data, size, depth - 1),
102 &g_int);
103 case 15:
104 return Container::Tab(GeneratorComponents(data, size, depth - 1), &g_int);
105 case 16:
106 return Maybe(GeneratorComponent(data, size, depth - 1), &g_bool);
107 case 17:
108 return Dropdown(&g_list, &g_int);
109 default:
110 return Button(GeneratorString(data, size), [] {});
111 }
112}
113
114Components GeneratorComponents(const char*& data, size_t& size, int depth) {
115 Components out;
116 if (depth > 0) {
117 while (size && GeneratorInt(data, size) % 2) {
118 out.push_back(GeneratorComponent(data, size, depth - 1));
119 }
120 }
121 return std::move(out);
122}
123
124} // namespace
125extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
126 g_bool = GeneratorBool(data, size);
127 g_int = GeneratorInt(data, size);
128 g_list = {
129 "test_1", "test_2", "test_3", "test_4", "test_5",
130 };
131
132 int depth = 10;
133 auto component = GeneratorComponent(data, size, depth);
134
135 int width = GeneratorInt(data, size);
136 int height = GeneratorInt(data, size);
137
138 width %= 500;
139 width += 500;
140
141 height %= 500;
142 height += 500;
143
144 auto screen =
146
147 auto event_receiver = MakeReceiver<Event>();
148 {
149 auto parser = TerminalInputParser(event_receiver->MakeSender());
150 for (size_t i = 0; i < size; ++i)
151 parser.Add(data[i]);
152 }
153
154 Event event;
155 while (event_receiver->Receive(&event)) {
156 component->OnEvent(event);
157 auto document = component->Render();
158 Render(screen, document);
159 }
160 return 0; // Non-zero return values are reserved for future use.
161}
162
163// Copyright 2021 Arthur Sonzogni. All rights reserved.
164// Use of this source code is governed by the MIT license that can be found in
165// the LICENSE file.
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:345
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={})
Draw checkable element.
Definition checkbox.cpp:117
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition radiobox.cpp:190
Component Toggle(ConstStringListRef entries, int *selected, Ref< ToggleOption > option={})
An horizontal list of elements. The user can navigate through them.
Definition toggle.cpp:126
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:241
std::vector< Component > Components
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:303
Receiver< T > MakeReceiver()
Definition receiver.hpp:117
Component Button(ConstStringRef label, std::function< void()> on_click, Ref< ButtonOption >={})
Draw a button. Execute a function when clicked.
Definition button.cpp:90
Component Menu(ConstStringListRef entries, int *selected_, Ref< MenuOption >={})
A list of text. The focused element is selected.
Definition menu.cpp:173
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:7
Component Maybe(Component, bool *show)
Definition maybe.cpp:7
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Slider(ConstStringRef label, T *value, T min, T max, T increment)
An horizontal slider.
Definition slider.cpp:123
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition node.cpp:34
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:86
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
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:25