Skip to content

Commit

Permalink
[Feature] Added hyperlink as a text style (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: Vedant <[email protected]>
  • Loading branch information
vrnimje authored Sep 17, 2023
1 parent 3cd8eb6 commit 3e8d916
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/text.qf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Vertical {
a
}
strikethrough Green Text("This text is Green")
Horizontal {
hyperlink Blue Text("Google", "https://www.google.com/")
}
Button {
"Exit",
"Exit"
Expand Down
19 changes: 17 additions & 2 deletions include/quick-ftxui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ enum text_style {
underlined,
underlinedDouble,
blink,
strikethrough
strikethrough,
hyperlink
};

enum colours {
Expand Down Expand Up @@ -182,6 +183,7 @@ struct dom_text {
text_style style = text_style::none;
colours color = colours::Default;
std::string content = "";
std::string link = "";
};

struct separator {
Expand Down Expand Up @@ -296,6 +298,7 @@ BOOST_FUSION_ADAPT_STRUCT(quick_ftxui_ast::dom_text,
(quick_ftxui_ast::text_style, style)
(quick_ftxui_ast::colours, color)
(std::string, content)
(std::string, link)
)

BOOST_FUSION_ADAPT_STRUCT(quick_ftxui_ast::separator,
Expand Down Expand Up @@ -652,6 +655,7 @@ struct node_printer : boost::static_visitor<> {
void operator()(quick_ftxui_ast::dom_text const &text) const {
// tab(indent + tabsize);
// std::cout << "nil: \"" << text << '"' << std::endl;

switch (text.style) {
case quick_ftxui_ast::text_style::none:
data->components.push_back(ftxui::Renderer([&] {
Expand Down Expand Up @@ -709,6 +713,16 @@ struct node_printer : boost::static_visitor<> {
}));
break;

case quick_ftxui_ast::text_style::hyperlink:
if (text.link.empty()) {
throw std::runtime_error("Hyperlink style used, but link not provided");
}
data->components.push_back(ftxui::Renderer([&] {
return (ftxui::text(text.content) | ftxui::hyperlink(text.link) |
ftxui::color(quick_ftxui_ast::resolveColour(text.color)));
}));
break;

default:
throw std::runtime_error("Should not reach here");
break;
Expand Down Expand Up @@ -893,6 +907,7 @@ struct parser
("inverted", quick_ftxui_ast::text_style::inverted)
("blink", quick_ftxui_ast::text_style::blink)
("strikethrough", quick_ftxui_ast::text_style::strikethrough)
("hyperlink", quick_ftxui_ast::text_style::hyperlink)
;
border_kw
.add
Expand Down Expand Up @@ -940,7 +955,7 @@ struct parser
str_var_decl %= qi::lit("str") >> identifier >> -('=' > quoted_string);

text_comp %= -(text_style_kw) >> -(color_kw) >> qi::lit("Text") >> '(' >>
quoted_string >> ')';
quoted_string >> -(',' >> quoted_string) >> ')';

sep_comp %= -(sep_kw) >> qi::lit("separator");

Expand Down

0 comments on commit 3e8d916

Please sign in to comment.