FTXUI  0.8.1
C++ functional terminal UI.
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 
8 using namespace ftxui;
9 namespace {
10 
11 bool 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 
21 std::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 
39 int 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 
48 bool g_bool;
49 int g_int;
50 std::vector<std::string> g_list;
51 
52 Components GeneratorComponents(const char*& data, size_t& size, int depth);
53 
54 Component 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 % 16) {
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  default:
106  return Button(GeneratorString(data, size), [] {});
107  }
108 }
109 
110 Components GeneratorComponents(const char*& data, size_t& size, int depth) {
111  Components out;
112  if (depth > 0) {
113  while (size && GeneratorInt(data, size) % 2) {
114  out.push_back(GeneratorComponent(data, size, depth - 1));
115  }
116  }
117  return std::move(out);
118 }
119 
120 } // namespace
121 extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
122  g_bool = GeneratorBool(data, size);
123  g_int = GeneratorInt(data, size);
124  g_list = {
125  "test_1", "test_2", "test_3", "test_4", "test_5",
126  };
127 
128  int depth = 10;
129  auto component = GeneratorComponent(data, size, depth);
130 
131  int width = GeneratorInt(data, size);
132  int height = GeneratorInt(data, size);
133 
134  width %= 500;
135  width += 500;
136 
137  height %= 500;
138  height += 500;
139 
140  auto screen =
142 
143  auto event_receiver = MakeReceiver<Event>();
144  {
145  auto parser = TerminalInputParser(event_receiver->MakeSender());
146  for (size_t i = 0; i < size; ++i)
147  parser.Add(data[i]);
148  }
149 
150  Event event;
151  while (event_receiver->Receive(&event)) {
152  component->OnEvent(event);
153  auto document = component->Render();
154  Render(screen, document);
155  }
156  return 0; // Non-zero return values are reserved for future use.
157 }
158 
159 // Copyright 2021 Arthur Sonzogni. All rights reserved.
160 // Use of this source code is governed by the MIT license that can be found in
161 // the LICENSE file.
terminal_input_parser.hpp
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:239
ftxui::ResizableSplitRight
Component ResizableSplitRight(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Definition: resizable_split.cpp:296
ftxui
Definition: captured_mouse.hpp:6
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition: component_base.hpp:17
ftxui::to_wstring
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition: string.cpp:303
ftxui::ResizableSplitLeft
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Definition: resizable_split.cpp:266
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const char *data, size_t size)
Definition: component_fuzzer.cpp:121
component.hpp
ftxui::Checkbox
Component Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option={})
Draw checkable element.
Definition: checkbox.cpp:112
ftxui::Slider
Component Slider(ConstStringRef label, T *value, T min, T max, T increment)
An horizontal slider.
Definition: slider.cpp:123
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:284
ftxui::Dimension::Fixed
Dimensions Fixed(int)
Definition: screen.cpp:96
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:196
ftxui::Toggle
Component Toggle(ConstStringListRef entries, int *selected, Ref< ToggleOption > option={})
An horizontal list of elements. The user can navigate through them.
Definition: toggle.cpp:126
ftxui::ResizableSplitTop
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Definition: resizable_split.cpp:326
ftxui::size
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:86
ftxui::Button
Component Button(ConstStringRef label, std::function< void()> on_click, Ref< ButtonOption >={})
Draw a button. Execute a function when clicked.
Definition: button.cpp:90
ftxui::Screen::Create
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition: screen.cpp:115
ftxui::Components
std::vector< Component > Components
Definition: component_base.hpp:18
ftxui::Radiobox
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition: radiobox.cpp:193
ftxui::Render
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:34
ftxui::Menu
Component Menu(ConstStringListRef entries, int *selected_, Ref< MenuOption >={})
A list of text. The focused element is selected.
Definition: menu.cpp:173
ftxui::ResizableSplitBottom
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Definition: resizable_split.cpp:356
ftxui::TerminalInputParser
Definition: terminal_input_parser.hpp:16
ftxui::Input
Component Input(StringRef content, ConstStringRef placeholder, Ref< InputOption > option={})
An input box for editing text.
Definition: input.cpp:241
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:25