Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/Dropdown.qf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Vertical{
Dropdown{
[ "Physics" "Maths" "Chemistry" "Biology"],
0
}
Slider{
"Slider 1: ",
50,
0,
100,
1
}
}
27 changes: 25 additions & 2 deletions include/quick-ftxui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ struct expression;
struct input;
struct slider;
struct menu;
struct dropdown;

enum block_alignment { VERTICAL, HORIZONTAL };

typedef boost::variant<
nil, boost::recursive_wrapper<button>, boost::recursive_wrapper<input>,
boost::recursive_wrapper<slider>, boost::recursive_wrapper<menu>,
boost::recursive_wrapper<expression>>
boost::recursive_wrapper<dropdown>, boost::recursive_wrapper<expression>>
node;

struct button {
Expand All @@ -66,6 +67,11 @@ struct menu {
int selected = 0;
};

struct dropdown {
std::vector<std::string> entries;
int selected = 0;
};

struct expression {
block_alignment align;
std::list<node> expr;
Expand Down Expand Up @@ -125,6 +131,11 @@ BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::menu,
(int, selected)
)

BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::dropdown,
(std::vector<std::string>, entries)
(int, selected)
)

BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::expression,
(client::quick_ftxui_ast::block_alignment, align)
(std::list<client::quick_ftxui_ast::node>, expr)
Expand Down Expand Up @@ -216,6 +227,12 @@ struct node_printer : boost::static_visitor<> {
ftxui::Menu(&text.entries, (int *)&text.selected));
}

void operator()(quick_ftxui_ast::dropdown const &text) const {
tab(indent + tabsize);
data->components.push_back(
ftxui::Dropdown(&text.entries, (int *)&text.selected));
}

void operator()(quick_ftxui_ast::nil const &text) const {
tab(indent + tabsize);
std::cout << "nil: \"" << text << '"' << std::endl;
Expand Down Expand Up @@ -297,7 +314,11 @@ struct parser
menu_comp %= qi::lit("Menu") >> '{' >> '[' >> *quoted_string >> ']' >>
',' >> qi::int_ >> '}';

node = button_comp | input_comp | slider_comp | menu_comp | expression;
dropdown_comp %= qi::lit("Dropdown") >> '{' >> '[' >> *quoted_string >>
']' >> ',' >> qi::int_ >> '}';

node = button_comp | input_comp | slider_comp | menu_comp |
dropdown_comp | expression;

expression = alignment_kw >> '{' >> *node >> '}';

Expand All @@ -314,6 +335,8 @@ struct parser
button_comp;
qi::rule<Iterator, quick_ftxui_ast::input(), ascii::space_type> input_comp;
qi::rule<Iterator, quick_ftxui_ast::menu(), ascii::space_type> menu_comp;
qi::rule<Iterator, quick_ftxui_ast::dropdown(), ascii::space_type>
dropdown_comp;
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, quick_ftxui_ast::slider(), ascii::space_type>
slider_comp;
Expand Down
8 changes: 8 additions & 0 deletions tests/test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ TEST_CASE("Parse Simple") {
" \"Biology\"], 0}}"));
REQUIRE(parse_helper("Horizontal{Menu{[\"Physics\" \"Maths\" "
"\"Chemistry\" \"Biology\"], 0}}"));
REQUIRE(!parse_helper(
"Vertical{Dropdown{[\"amool\" \"bmpp\" \"abcd\" \"hello\"]}, 0}"));
REQUIRE(!parse_helper(
"Horizontal{Dropdown{[\"amool\" \"bmpp\" \"abcd\" \"hello\"]}, 1}"));

// expect fail
REQUIRE(!parse_helper("\"amool\"{Button{\"amool\",\"bmpp\"}}"));
Expand All @@ -47,6 +51,10 @@ TEST_CASE("Parse Simple") {
REQUIRE(!parse_helper("Horizontal_{_Button{\"amool\",\"bmpp\"}_}"));
REQUIRE(!parse_helper("\"amool\"{Button{\"amool,\"bmpp\"}}"));
REQUIRE(!parse_helper("Vertical{Button{\"amool\" . \"bmpp\"}}"));
REQUIRE(!parse_helper(
"Vertical{Dropdown{[\"amool\" \"bmpp\" \"dmrr\" \"emss\"]}, 9}"));
REQUIRE(!parse_helper(
"Horizontal{Dropdown{[\"amool\" \"bmpp\" \"dmrr\",\"emss\"]}, 4}"));
}

TEST_CASE("Parse Complex") {
Expand Down