FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
hyperlink.cpp
Go to the documentation of this file.
1// Copyright 2023 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <cstdint> // for uint8_t
5#include <memory> // for make_shared
6#include <string> // for string
7#include <string_view>
8#include <utility> // for move
9
10#include "ftxui/dom/elements.hpp" // for Element, Decorator, hyperlink
11#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
12#include "ftxui/screen/box.hpp" // for Box
13#include "ftxui/screen/screen.hpp" // for Screen, Cell
14
15namespace ftxui {
16
17namespace {
18class Hyperlink : public NodeDecorator {
19 public:
20 Hyperlink(Element child, std::string link)
21 : NodeDecorator(std::move(child)), link_(std::move(link)) {}
22
23 void Render(Screen& screen) override {
24 const uint8_t hyperlink_id = screen.RegisterHyperlink(link_);
25 for (int y = box_.y_min; y <= box_.y_max; ++y) {
26 for (int x = box_.x_min; x <= box_.x_max; ++x) {
27 screen.CellAt(x, y).hyperlink = hyperlink_id;
28 }
29 }
31 }
32
33 std::string link_;
34};
35} // namespace
36
37/// @brief Make the rendered area clickable using a web browser.
38/// The link will be opened when the user clicks on it.
39/// This is supported only on a limited set of terminal emulators.
40/// List: https://github.com/Alhadis/OSC8-Adoption/
41/// @param link The link
42/// @param child The input element.
43/// @return The output element with the link.
44/// @ingroup dom
45///
46/// ### Example
47///
48/// ```cpp
49/// Element document =
50/// hyperlink("https://github.com/ArthurSonzogni/FTXUI", "link");
51/// ```
52Element hyperlink(std::string_view link, Element child) {
53 return std::make_shared<Hyperlink>(std::move(child), std::string(link));
54}
55
56/// @brief Decorate using a hyperlink.
57/// The link will be opened when the user clicks on it.
58/// This is supported only on a limited set of terminal emulators.
59/// List: https://github.com/Alhadis/OSC8-Adoption/
60/// @param link The link to redirect the users to.
61/// @return The Decorator applying the hyperlink.
62/// @ingroup dom
63///
64/// ### Example
65///
66/// ```cpp
67/// Element document =
68/// text("red") | hyperlink("https://github.com/Arthursonzogni/FTXUI");
69/// ```
70// NOLINTNEXTLINE
71Decorator hyperlink(std::string_view link) {
72 return [link = std::string(link)](Element child) {
73 return hyperlink(link, std::move(child));
74 };
75}
76
77} // namespace ftxui
friend void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:96
Decorator hyperlink(std::string_view link)
Decorate using a hyperlink. The link will be opened when the user clicks on it. This is supported onl...
Definition hyperlink.cpp:71
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::function< Element(Element)> Decorator
Definition elements.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:23