FTXUI  5.0.0
C++ functional terminal UI.
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 <utility> // for move
8 
9 #include "ftxui/dom/elements.hpp" // for Element, Decorator, hyperlink
10 #include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
11 #include "ftxui/screen/box.hpp" // for Box
12 #include "ftxui/screen/screen.hpp" // for Screen, Pixel
13 
14 namespace ftxui {
15 
16 namespace {
17 class Hyperlink : public NodeDecorator {
18  public:
19  Hyperlink(Element child, std::string link)
20  : NodeDecorator(std::move(child)), link_(std::move(link)) {}
21 
22  void Render(Screen& screen) override {
23  const uint8_t hyperlink_id = screen.RegisterHyperlink(link_);
24  for (int y = box_.y_min; y <= box_.y_max; ++y) {
25  for (int x = box_.x_min; x <= box_.x_max; ++x) {
26  screen.PixelAt(x, y).hyperlink = hyperlink_id;
27  }
28  }
29  NodeDecorator::Render(screen);
30  }
31 
32  std::string link_;
33 };
34 } // namespace
35 
36 /// @brief Make the rendered area clickable using a web browser.
37 /// The link will be opened when the user click on it.
38 /// This is supported only on a limited set of terminal emulator.
39 /// List: https://github.com/Alhadis/OSC8-Adoption/
40 /// @param link The link
41 /// @param child The input element.
42 /// @return The output element with the link.
43 /// @ingroup dom
44 ///
45 /// ### Example
46 ///
47 /// ```cpp
48 /// Element document =
49 /// hyperlink("https://github.com/ArthurSonzogni/FTXUI", "link");
50 /// ```
51 Element hyperlink(std::string link, Element child) {
52  return std::make_shared<Hyperlink>(std::move(child), std::move(link));
53 }
54 
55 /// @brief Decorate using an hyperlink.
56 /// The link will be opened when the user click on it.
57 /// This is supported only on a limited set of terminal emulator.
58 /// List: https://github.com/Alhadis/OSC8-Adoption/
59 /// @param link The link to redirect the users to.
60 /// @return The Decorator applying the hyperlink.
61 /// @ingroup dom
62 ///
63 /// ### Example
64 ///
65 /// ```cpp
66 /// Element document =
67 /// text("red") | hyperlink("https://github.com/Arthursonzogni/FTXUI");
68 /// ```
69 // NOLINTNEXTLINE
70 Decorator hyperlink(std::string link) {
71  return [link](Element child) { return hyperlink(link, std::move(child)); };
72 }
73 
74 } // namespace ftxui
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:32
std::function< Element(Element)> Decorator
Definition: elements.hpp:24
std::shared_ptr< Node > Element
Definition: elements.hpp:22
Decorator hyperlink(std::string link)
Decorate using an hyperlink. The link will be opened when the user click on it. This is supported onl...
Definition: hyperlink.cpp:70
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47