Skip to content

Commit 55c0d18

Browse files
committed
Add primitive implementation
Signed-off-by: Shreyas Atre <shreyasatre16@gmail.com>
1 parent d2a42f5 commit 55c0d18

File tree

6 files changed

+107
-19
lines changed

6 files changed

+107
-19
lines changed

CMakeLists.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ project(quick-ftxui
77
find_package(Boost REQUIRED COMPONENTS regex)
88
set(CMAKE_CXX_STANDARD_REQUIRED 17)
99
set(CMAKE_CXX_STANDARD 17)
10+
set(CMAKE_BUILD_TYPE Debug)
1011

1112
# --- Fetch FTXUI --------------------------------------------------------------
1213
include(FetchContent)
@@ -35,22 +36,33 @@ if(QUICK_FTXUI_TESTS)
3536
FetchContent_MakeAvailable(Catch2)
3637

3738
add_executable(tests tests/test1.cpp)
38-
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
39+
target_link_libraries(tests
40+
PRIVATE Catch2::Catch2WithMain
41+
PRIVATE ftxui::screen
42+
PRIVATE ftxui::dom
43+
PRIVATE ftxui::component
44+
)
3945
target_include_directories(tests PRIVATE include)
4046
include(CTest)
4147
include(Catch)
4248
catch_discover_tests(tests)
4349
endif()
4450

4551
add_executable(quick-ftxui src/quick-ftxui.cpp)
46-
target_include_directories(quick-ftxui PRIVATE include ${Boost_INCLUDE_DIR})
52+
target_include_directories(quick-ftxui
53+
PRIVATE include
54+
PRIVATE ${Boost_INCLUDE_DIR}
55+
PRIVATE ${ftxui_SOURCE_DIR}
56+
)
4757

4858
target_link_libraries(quick-ftxui
4959
PRIVATE ${Boost_LIBRARIES}
50-
51-
# PRIVATE ftxui::screen
52-
# PRIVATE ftxui::dom
53-
# PRIVATE ftxui::component # Not needed for this example.
60+
PRIVATE ftxui::screen
61+
PRIVATE ftxui::dom
62+
PRIVATE ftxui::component # Not needed for this example.
5463
)
5564

65+
target_compile_options(quick-ftxui PRIVATE -fsanitize=address)
66+
target_link_options(quick-ftxui PRIVATE -fsanitize=address)
67+
5668
install(TARGETS quick-ftxui RUNTIME DESTINATION "bin")

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ It can parse the following code
2929
}
3030
```
3131

32+
[![asciicast](https://asciinema.org/a/537702.svg)](https://asciinema.org/a/537702)
33+
3234
## How it should look like
3335

3436
- `Button`, `Exit` are identifiers

examples/Button.qf

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
{
22
Button{
3-
"arg1",
4-
"arg2"
3+
"Button1",
4+
"Exit"
55
}
6-
}
6+
Button{
7+
"Button2",
8+
"Exit"
9+
}
10+
Button{
11+
"arg3",
12+
"Nothin"
13+
}
14+
}

examples/multiple_components.qf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
"arg5",
1212
"arg6"
1313
}
14+
Input{
15+
"arg6",
16+
"arg7",
17+
"arg8"
18+
}
1419
}

include/quick-ftxui.hpp

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
#include <boost/variant/apply_visitor.hpp>
88
#include <boost/variant/recursive_variant.hpp>
99

10+
#include "ftxui/component/component.hpp" // for Input, Renderer, Vertical
11+
#include "ftxui/component/component_base.hpp" // for ComponentBase
12+
#include "ftxui/component/component_options.hpp" // for InputOption
13+
#include "ftxui/component/mouse.hpp" // for ftxui
14+
#include "ftxui/component/screen_interactive.hpp"
15+
#include "ftxui/dom/elements.hpp" // for text, hbox, separator, Element, operator|, vbox, border
16+
#include "ftxui/screen/screen.hpp"
17+
#include "ftxui/util/ref.hpp" // for Ref
18+
1019
#include <iostream>
1120
#include <string>
1221

@@ -21,9 +30,10 @@ namespace quick_ftxui_ast {
2130
struct nil {};
2231
struct button;
2332
struct expression;
24-
// struct function;
33+
struct input;
2534

2635
typedef boost::variant<nil, boost::recursive_wrapper<button>,
36+
boost::recursive_wrapper<input>,
2737
boost::recursive_wrapper<expression>>
2838
node;
2939

@@ -32,6 +42,12 @@ struct button {
3242
std::string func;
3343
};
3444

45+
struct input {
46+
std::string placeholder;
47+
std::string temp;
48+
std::string option;
49+
};
50+
3551
struct expression {
3652
button first;
3753
std::list<node> rest;
@@ -49,6 +65,13 @@ inline std::ostream &operator<<(std::ostream &out, button b) {
4965
return out;
5066
}
5167

68+
// print function for debugging
69+
inline std::ostream &operator<<(std::ostream &out, input b) {
70+
out << "Placeholder: " << b.placeholder << " | Temp: " << b.temp
71+
<< " | Options: " << b.option;
72+
return out;
73+
}
74+
5275
} // namespace quick_ftxui_ast
5376
} // namespace client
5477

@@ -58,6 +81,11 @@ BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::button,
5881
(std::string, func)
5982
)
6083

84+
BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::input,
85+
(std::string, placeholder)
86+
(std::string, temp)
87+
(std::string, option)
88+
)
6189

6290
BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::expression,
6391
(client::quick_ftxui_ast::button, first)
@@ -80,24 +108,43 @@ void tab(int indent) {
80108
std::cout << ' ';
81109
}
82110

111+
struct component_meta_data {
112+
ftxui::ScreenInteractive *screen;
113+
ftxui::Components components;
114+
};
115+
83116
struct ast_printer {
84-
ast_printer(int indent = 0) : indent(indent) {}
117+
ast_printer(component_meta_data *data_, int indent = 0)
118+
: indent(indent), data(data_) {}
119+
ast_printer(component_meta_data *data_) : data(data_) {}
85120

86121
void operator()(quick_ftxui_ast::expression const &) const;
87122

88123
int indent;
124+
125+
component_meta_data *data;
89126
};
90127

91128
struct node_printer : boost::static_visitor<> {
92-
node_printer(int indent = 0) : indent(indent) {}
129+
node_printer(component_meta_data *data_, int indent = 0)
130+
: indent(indent), data(data_) {}
93131

94132
void operator()(client::quick_ftxui_ast::expression const &expr) const {
95-
ast_printer(indent + tabsize)(expr);
133+
ast_printer(data, indent + tabsize)(expr);
96134
}
97135

98136
void operator()(quick_ftxui_ast::button const &text) const {
99137
tab(indent + tabsize);
100138
std::cout << "button: " << text << std::endl;
139+
if (text.func == "Exit") {
140+
data->components.push_back(ftxui::Button(
141+
text.placeholder, data->screen->ExitLoopClosure()));
142+
}
143+
}
144+
145+
void operator()(quick_ftxui_ast::input const &text) const {
146+
tab(indent + tabsize);
147+
std::cout << "input: " << text << std::endl;
101148
}
102149

103150
void operator()(quick_ftxui_ast::nil const &text) const {
@@ -106,18 +153,19 @@ struct node_printer : boost::static_visitor<> {
106153
}
107154

108155
int indent;
156+
component_meta_data *data;
109157
};
110158

111159
void ast_printer::operator()(
112160
client::quick_ftxui_ast::expression const &expr) const {
113161
tab(indent);
114162
std::cout << "tag: "
115-
<< "Button" << std::endl;
163+
<< "Node" << std::endl;
116164
std::cout << '{' << std::endl;
117-
node_printer(indent).operator()(expr.first);
165+
node_printer(data, indent).operator()(expr.first);
118166

119167
for (quick_ftxui_ast::node const &node : expr.rest) {
120-
boost::apply_visitor(node_printer(indent), node);
168+
boost::apply_visitor(node_printer(data, indent), node);
121169
}
122170

123171
tab(indent);
@@ -162,7 +210,10 @@ struct parser
162210
button_comp %= qi::lit("Button") >> '{' >> quoted_string >> ',' >>
163211
quoted_string >> '}';
164212

165-
node = button_comp | expression;
213+
input_comp %= qi::lit("Input") >> '{' >> quoted_string >> ',' >>
214+
quoted_string >> ',' >> quoted_string >> '}';
215+
216+
node = button_comp | input_comp | expression;
166217

167218
expression = '{' >> button_comp >> *node >> '}';
168219

@@ -177,6 +228,7 @@ struct parser
177228
qi::rule<Iterator, quick_ftxui_ast::node(), ascii::space_type> node;
178229
qi::rule<Iterator, quick_ftxui_ast::button(), ascii::space_type>
179230
button_comp;
231+
qi::rule<Iterator, quick_ftxui_ast::input(), ascii::space_type> input_comp;
180232
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
181233
};
182234
} // namespace quick_ftxui_parser

src/quick-ftxui.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,17 @@ int main(int argc, char **argv) {
4242
std::cout << "-------------------------\n";
4343
std::cout << "Parsing succeeded\n";
4444
std::cout << source_code << " Parses OK: " << std::endl;
45-
client::quick_ftxui_parser::ast_printer p;
46-
p(expression);
45+
auto screen = ftxui::ScreenInteractive::Fullscreen();
46+
client::quick_ftxui_parser::component_meta_data data{&screen, {}};
47+
client::quick_ftxui_parser::ast_printer printer(&data, 0);
48+
printer(expression);
49+
if (data.components.size()) {
50+
auto component =
51+
ftxui::Container::Vertical(std::move(data.components));
52+
auto main_renderer = ftxui::Renderer(
53+
component, [&] { return ftxui::vbox({component->Render()}); });
54+
screen.Loop(main_renderer);
55+
}
4756
} else {
4857
std::cout << "-------------------------\n";
4958
std::cout << "Parsing failed\n";

0 commit comments

Comments
 (0)