Skip to content

Commit

Permalink
Adding Menu options (#25)
Browse files Browse the repository at this point in the history
* adding toggle and functional buttons

* adding toggle and functional buttons

* button functionality using function pointers

* modified

* adding checkbox and toggle

* Adding options for button

* Adding button options

* Adding button options

* Adding button options

* Adding button options

* Adding button options

Signed-off-by: Khushi-Balia <[email protected]>

* Adding button options

Signed-off-by: Khushi-Balia <[email protected]>

* Adding menu options

* modifying test and examples

* Clang-format

---------

Signed-off-by: Khushi-Balia <[email protected]>
  • Loading branch information
Khushi-Balia authored Sep 23, 2023
1 parent 845c943 commit 9985ca6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
15 changes: 15 additions & 0 deletions examples/Menu.qf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ Vertical
[ "Physics" "Maths" "Chemistry" "Biology"],
0
}
Menu{
[ "Physics" "Maths" "Chemistry" "Biology"],
0,
Vertical_Animated
}
Menu{
[ "Computers" "History" "Geography" "English"],
0,
Horizontal_Animated
}
Menu{
[ "Computers" "History" "Geography" "English"],
0,
Toggle
}
Button{
"Button2",
"Exit",
Expand Down
67 changes: 64 additions & 3 deletions include/quick-ftxui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ struct menu;
struct toggle;

enum block_alignment { VERTICAL, HORIZONTAL };
enum menu_option {
Horizontal,
Horizontal_Animated,
Vertical,
Vertical_Animated,
Toggle,
NoOption
};
enum button_option { Ascii, Animated, Simple, NoOpt };

typedef boost::variant<
Expand Down Expand Up @@ -68,6 +76,7 @@ struct slider {
struct menu {
std::vector<std::string> entries;
int selected = 0;
menu_option opt = menu_option::NoOption;
};

struct toggle {
Expand Down Expand Up @@ -134,6 +143,7 @@ BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::slider,
BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::menu,
(std::vector<std::string>, entries)
(int, selected)
(client::quick_ftxui_ast::menu_option, opt)
)

BOOST_FUSION_ADAPT_STRUCT(client::quick_ftxui_ast::toggle,
Expand Down Expand Up @@ -165,6 +175,7 @@ void tab(int indent) {
struct component_meta_data {
ftxui::ScreenInteractive *screen;
ftxui::Components components;
ftxui::MenuOption *m_options;
ftxui::ButtonOption *options;
};

Expand Down Expand Up @@ -283,8 +294,48 @@ struct node_printer : boost::static_visitor<> {

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

switch (text.opt) {
case quick_ftxui_ast::menu_option::Horizontal: {
data->components.push_back(
ftxui::Menu(&text.entries, (int *)&text.selected,
data->m_options->Horizontal()));
break;
}
case quick_ftxui_ast::menu_option::Horizontal_Animated: {
data->components.push_back(
ftxui::Menu(&text.entries, (int *)&text.selected,
data->m_options->HorizontalAnimated()));
break;
}
case quick_ftxui_ast::menu_option::Vertical: {
data->components.push_back(
ftxui::Menu(&text.entries, (int *)&text.selected,
data->m_options->Vertical()));
break;
}
case quick_ftxui_ast::menu_option::Vertical_Animated: {
data->components.push_back(
ftxui::Menu(&text.entries, (int *)&text.selected,
data->m_options->VerticalAnimated()));
break;
}
case quick_ftxui_ast::menu_option::Toggle: {
data->components.push_back(ftxui::Menu(&text.entries,
(int *)&text.selected,
data->m_options->Toggle()));
break;
}
case quick_ftxui_ast::menu_option::NoOption: {
data->components.push_back(
ftxui::Menu(&text.entries, (int *)&text.selected,
data->m_options->Vertical()));
break;
}
default:
throw std::runtime_error("Should never reach here");
break;
}
}

void operator()(quick_ftxui_ast::toggle const &text) const {
Expand Down Expand Up @@ -359,6 +410,15 @@ struct parser
("Horizontal", quick_ftxui_ast::block_alignment::HORIZONTAL)
;

menuopt_kw
.add
("Horizontal", quick_ftxui_ast::menu_option::Horizontal)
("Vertical", quick_ftxui_ast::menu_option::Vertical)
("Horizontal_Animated", quick_ftxui_ast::menu_option::Horizontal_Animated)
("Vertical_Animated", quick_ftxui_ast::menu_option::Vertical_Animated)
("Toggle", quick_ftxui_ast::menu_option::Toggle)
;

buttonopt_kw
.add
("Ascii", quick_ftxui_ast::button_option::Ascii)
Expand All @@ -383,7 +443,7 @@ struct parser
qi::int_ >> '}';

menu_comp %= qi::lit("Menu") >> '{' >> '[' >> *quoted_string >> ']' >>
',' >> qi::int_ >> '}';
',' >> qi::int_ >> -(',' >> menuopt_kw) >> '}';

toggle_comp %= qi::lit("Toggle") >> '{' >> '[' >> *quoted_string >>
']' >> ',' >> qi::int_ >> '}';
Expand Down Expand Up @@ -413,6 +473,7 @@ struct parser
qi::rule<Iterator, quick_ftxui_ast::slider(), ascii::space_type>
slider_comp;
qi::symbols<char, quick_ftxui_ast::block_alignment> alignment_kw;
qi::symbols<char, quick_ftxui_ast::menu_option> menuopt_kw;
qi::symbols<char, quick_ftxui_ast::button_option> buttonopt_kw;
};
} // namespace quick_ftxui_parser
Expand Down
20 changes: 20 additions & 0 deletions tests/test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,28 @@ TEST_CASE("Parse Simple") {

REQUIRE(parse_helper("Vertical{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0}}"));
REQUIRE(parse_helper("Vertical{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Horizontal}}"));
REQUIRE(parse_helper("Vertical{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Vertical}}"));
REQUIRE(parse_helper("Vertical{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Horizontal_Animated}}"));
REQUIRE(parse_helper("Vertical{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Vertical_Animated}}"));
REQUIRE(parse_helper("Vertical{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Toggle}}"));
REQUIRE(parse_helper("Horizontal{Menu{[\"Physics\" \"Maths\" "
"\"Chemistry\" \"Biology\"], 0}}"));
REQUIRE(parse_helper("Horizontal{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Horizontal}}"));
REQUIRE(parse_helper("Horizontal{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Vertical}}"));
REQUIRE(parse_helper("Horizontal{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Horizontal_Animated}}"));
REQUIRE(parse_helper("Horizontal{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Vertical_Animated}}"));
REQUIRE(parse_helper("Horizontal{Menu{[\"Physics\" \"Maths\" \"Chemistry\" "
" \"Biology\"], 0, Toggle}}"));
REQUIRE(parse_helper("Vertical{Toggle{[\"Opt1\" \"Opt2\"], 1}}"));
REQUIRE(parse_helper("Horizontal{Toggle{[\"Opt1\" \"Opt2\"], 1}}"));
REQUIRE(parse_helper("Vertical{Toggle{[\"Opt1\" \"Opt2\"], 0}}"));
Expand Down

0 comments on commit 9985ca6

Please sign in to comment.