Skip to content

Commit

Permalink
Add RadioBox component (#23)
Browse files Browse the repository at this point in the history
* Add RadioBox component

* Remove redundant test file

* Update quick-ftxui.hpp

---------

Signed-off-by: Krishna Narayanan [email protected]
  • Loading branch information
Krishna-13-cyber authored Sep 23, 2023
1 parent 9985ca6 commit 158d02d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
14 changes: 14 additions & 0 deletions examples/RadioBox.qf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Vertical{
Radiobox{
[ "Physics" "Maths" "Chemistry" "Biology"],
0
}
Menu{
[ "Physics" "Maths" "Chemistry" "Biology"],
0
}
Button{
"Button2",
"Exit"
}
}
26 changes: 24 additions & 2 deletions include/quick-ftxui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct input;
struct slider;
struct menu;
struct toggle;
struct radio;

enum block_alignment { VERTICAL, HORIZONTAL };
enum menu_option {
Expand All @@ -50,7 +51,8 @@ enum button_option { Ascii, Animated, Simple, NoOpt };
typedef boost::variant<
nil, boost::recursive_wrapper<button>, boost::recursive_wrapper<input>,
boost::recursive_wrapper<slider>, boost::recursive_wrapper<menu>,
boost::recursive_wrapper<toggle>, boost::recursive_wrapper<expression>>
boost::recursive_wrapper<toggle>, boost::recursive_wrapper<radio>,
boost::recursive_wrapper<expression>>
node;

struct button {
Expand Down Expand Up @@ -84,6 +86,11 @@ struct toggle {
int selected;
};

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

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

BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::radio,
(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 @@ -344,6 +356,12 @@ struct node_printer : boost::static_visitor<> {
ftxui::Toggle(&text.entries, (int *)&text.selected));
}

void operator()(quick_ftxui_ast::radio const &text) const {
tab(indent + tabsize);
data->components.push_back(
ftxui::Radiobox(&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 @@ -448,8 +466,11 @@ struct parser
toggle_comp %= qi::lit("Toggle") >> '{' >> '[' >> *quoted_string >>
']' >> ',' >> qi::int_ >> '}';

radio_comp %= qi::lit("Radiobox") >> '{' >> '[' >> *quoted_string >>
']' >> ',' >> qi::int_ >> '}';

node = button_comp | input_comp | slider_comp | menu_comp |
toggle_comp | expression;
toggle_comp | radio_comp | expression;

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

Expand All @@ -468,6 +489,7 @@ struct parser
qi::rule<Iterator, quick_ftxui_ast::menu(), ascii::space_type> menu_comp;
qi::rule<Iterator, quick_ftxui_ast::toggle(), ascii::space_type>
toggle_comp;
qi::rule<Iterator, quick_ftxui_ast::radio(), ascii::space_type> radio_comp;
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, std::string(), ascii::space_type> button_function;
qi::rule<Iterator, quick_ftxui_ast::slider(), ascii::space_type>
Expand Down
9 changes: 9 additions & 0 deletions tests/test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ TEST_CASE("Parse Simple") {
REQUIRE(parse_helper("Horizontal{Toggle{[\"Opt1\" \"Opt2\"], 1}}"));
REQUIRE(parse_helper("Vertical{Toggle{[\"Opt1\" \"Opt2\"], 0}}"));
REQUIRE(parse_helper("Horizontal{Toggle{[\"Opt1\" \"Opt2\"], 0}}"));
REQUIRE(parse_helper("Horizontal{Slider{\"amool\", 2, 5, 100, 1}}"));
REQUIRE(parse_helper(
"Horizontal{Radiobox{[\"amool\" \"amool\" \"hello\"], 0}}"));
REQUIRE(
parse_helper("Vertical{Radiobox{[ \"amol\" \"amool\" \"hello\"], 1}}"));

//expect fail
REQUIRE(!parse_helper("\"amool\"{Button{\"amool\",\"bmpp\",\"cmqq\"}}"));
Expand All @@ -82,6 +87,10 @@ TEST_CASE("Parse Simple") {
REQUIRE(!parse_helper("Vertical{Button{\"amool\" . \"bmpp\" . Ascii}}"));
REQUIRE(parse_helper("Vertical{Toggle{[\"Opt1\" \"Opt2\"], 3}}"));
REQUIRE(parse_helper("Vertical{Toggle{[\"Opt1\" \"Opt2\"], 8}}"));
REQUIRE(
parse_helper("Vertical{Radiobox{[ \"amol\" \"amool\" \"hello\"], 9}}"));
REQUIRE(parse_helper("Vertical{Toggle{[\"Opt1\" \"Opt2\"], 3}}"));
REQUIRE(parse_helper("Vertical{Toggle{[\"Opt1\" \"Opt2\"], 8}}"));
}

TEST_CASE("Parse Complex") {
Expand Down

0 comments on commit 158d02d

Please sign in to comment.