How to create custom widgets
TUI++ exposes IRenderable, an interface that you can use for creating widgets.
cpp
#include <tuipp/widgets/IRenderable.hpp>
#include <tuipp/Console.hpp>
#include <tuipp/colors.hpp>
class MyWidget : public tuipp::widgets::IRenderable
{
public:
std::string content{};
MyWidget(std::string content) : content(content) {}
// you need to provide an implementation for render
virtual void render(const int& width) const {
std::cout << tuipp::colors::red << this->content
<< tuipp::colors::reset << std::endl;
}
// you need to implement this method because widgets like panel
// are gonna call it before rendering for calculating the layout
virtual int get_lenght(const int& width = 0) const {
return this->content.lenght();
}
};
int main() {
auto widget = MyWidget("hello!");
// Console::println automatically calls render()
tuipp::Console::println(widget);
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Output: hello!