Skip to content

Commit

Permalink
[Feature] Added Radiobox Component (#31)
Browse files Browse the repository at this point in the history
Signed-off-by: Vedant <[email protected]>
  • Loading branch information
vrnimje authored Oct 18, 2023
1 parent 375425d commit 09e2c53
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ https://github.com/vrnimje/quick-ftxui/assets/103848930/715c821b-b259-4e2b-ab25-

</details>

</details>

<details><summary>Radiobox</summary>

[Example](./examples/radiobox.qf#L5-8)

![](./assets/image-13.png)

</details>

* #### Supports FTXUI's basic DOM elements (more coming with future releases)

<details><summary>Text</summary>
Expand All @@ -117,7 +127,7 @@ https://github.com/vrnimje/quick-ftxui/assets/103848930/715c821b-b259-4e2b-ab25-

<details><summary>Paragraph</summary>

Similar to text, but adapts to size of terminal windoe
Similar to text, but adapts to size of terminal window

[Example](./examples/paragraph.qf)

Expand Down
Binary file added assets/image-13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions examples/radiobox.qf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Vertical {
// Vertical radiobox
int x
int y
Red Radiobox {
["Mechanical", "Chemistry", "Physics", "Electrical", ],
x
}
Heavy separator
Horizontal {
// In block
Text("Select your branch")
YellowLight Radiobox {
["Mechanical", "Chemistry", "Physics", "Electrical", ],
y
}
}
Blue Button {
"Exit",
"Exit"
}
}
42 changes: 38 additions & 4 deletions include/quick-ftxui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct slider;
struct menu;
struct toggle;
struct dropdown;
struct radiobox;
struct int_variable_decl;
struct str_variable_decl;

Expand Down Expand Up @@ -133,8 +134,9 @@ 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<dropdown>,
boost::recursive_wrapper<expression>, boost::recursive_wrapper<dom_text>,
boost::recursive_wrapper<separator>, boost::recursive_wrapper<paragraph>,
boost::recursive_wrapper<radiobox>, boost::recursive_wrapper<expression>,
boost::recursive_wrapper<dom_text>, boost::recursive_wrapper<separator>,
boost::recursive_wrapper<paragraph>,
boost::recursive_wrapper<int_variable_decl>,
boost::recursive_wrapper<str_variable_decl>>
node;
Expand Down Expand Up @@ -182,6 +184,12 @@ struct dropdown {
std::string selected;
};

struct radiobox {
colours color = colours::Default;
std::vector<std::string> entries;
std::string selected;
};

struct dom_text {
colours color = colours::Default;
text_style style = text_style::none;
Expand Down Expand Up @@ -297,6 +305,12 @@ BOOST_FUSION_ADAPT_STRUCT(quick_ftxui_ast::dropdown,
(std::string, selected)
)

BOOST_FUSION_ADAPT_STRUCT(quick_ftxui_ast::radiobox,
(quick_ftxui_ast::colours, color)
(std::vector <std::string> , entries)
(std::string, selected)
)

BOOST_FUSION_ADAPT_STRUCT(quick_ftxui_ast::dom_text,
(quick_ftxui_ast::colours, color)
(quick_ftxui_ast::text_style, style)
Expand Down Expand Up @@ -659,6 +673,21 @@ struct node_printer : boost::static_visitor<> {
}
}

void operator()(quick_ftxui_ast::radiobox const &text) const {
// tab(indent + tabsize);

ftxui::Color radio_clr = quick_ftxui_ast::resolveColour(text.color);

if (auto It = quick_ftxui_ast::numbers.find(std::string(text.selected));
It != quick_ftxui_ast::numbers.end()) {
data->components.push_back(
ftxui::Radiobox(&text.entries, (int *)(&It->second)) |
ftxui::color(radio_clr));
} else {
throw std::runtime_error("Variable " + text.selected + " not found");
}
}

void operator()(quick_ftxui_ast::nil const &text) const {
// tab(indent + tabsize);
// std::cout << "nil: \"" << text << '"' << std::endl;
Expand Down Expand Up @@ -958,6 +987,9 @@ struct parser
drpdwn_comp %= -(color_kw) >> qi::lit("Dropdown") >> '{' >> '[' >>
+(quoted_string >> ',') >> ']' >> ',' >> identifier >> '}';

radiobox_comp %= -(color_kw) >> qi::lit("Radiobox") >> '{' >> '[' >>
+(quoted_string >> ',') >> ']' >> ',' >> identifier >> '}';

int_var_decl %= qi::lit("int") >> identifier >> -('=' > qi::int_);

str_var_decl %= qi::lit("str") >> identifier >> -('=' > quoted_string);
Expand All @@ -979,8 +1011,8 @@ struct parser
skipper = single_line_comment | multi_line_comment;

node = button_comp | input_comp | slider_comp | menu_comp | toggle_comp |
drpdwn_comp | text_comp | int_var_decl | str_var_decl | sep_comp |
para_comp | skipper | expression;
drpdwn_comp | radiobox_comp | text_comp | int_var_decl |
str_var_decl | sep_comp | para_comp | skipper | expression;

expression = -(border_kw) >> alignment_kw >> '{' >> *node >> '}';

Expand All @@ -999,6 +1031,8 @@ struct parser
qi::rule<Iterator, quick_ftxui_ast::toggle(), ascii::space_type> toggle_comp;
qi::rule<Iterator, quick_ftxui_ast::dropdown(), ascii::space_type>
drpdwn_comp;
qi::rule<Iterator, quick_ftxui_ast::radiobox(), ascii::space_type>
radiobox_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> slider_comp;
Expand Down

0 comments on commit 09e2c53

Please sign in to comment.