18using Charset = std::array<std::string, 6>;
21static Charsets simple_border_charset = {
22 Charset{
"┌",
"┐",
"└",
"┘",
"─",
"│"},
23 Charset{
"┏",
"┓",
"┗",
"┛",
"╍",
"╏"},
24 Charset{
"┏",
"┓",
"┗",
"┛",
"━",
"┃"},
25 Charset{
"╔",
"╗",
"╚",
"╝",
"═",
"║"},
26 Charset{
"╭",
"╮",
"╰",
"╯",
"─",
"│"},
27 Charset{
" ",
" ",
" ",
" ",
" ",
" "},
31class Border :
public Node {
35 std::optional<Color> foreground_color = std::nullopt)
36 :
Node(std::move(children)),
37 charset_(simple_border_charset[style]),
38 foreground_color_(foreground_color) {}
41 std::optional<Color> foreground_color_;
43 void ComputeRequirement()
override {
58 void SetBox(Box box)
override {
62 title_box.x_min = box.x_min + 1;
63 title_box.x_max = box.x_max - 1;
64 title_box.y_min = box.y_min;
65 title_box.y_max = box.y_min;
75 void Render(Screen& screen)
override {
90 Pixel& p1 = screen.PixelAt(x,
box_.
y_min);
91 Pixel& p2 = screen.PixelAt(x,
box_.
y_max);
92 p1.character = charset_[4];
93 p2.character = charset_[4];
98 Pixel& p3 = screen.PixelAt(
box_.
x_min, y);
99 Pixel& p4 = screen.PixelAt(
box_.
x_max, y);
100 p3.character = charset_[5];
101 p4.character = charset_[5];
112 if (foreground_color_) {
114 screen.PixelAt(x,
box_.
y_min).foreground_color = *foreground_color_;
115 screen.PixelAt(x,
box_.
y_max).foreground_color = *foreground_color_;
118 screen.PixelAt(
box_.
x_min, y).foreground_color = *foreground_color_;
119 screen.PixelAt(
box_.
x_max, y).foreground_color = *foreground_color_;
126class BorderPixel :
public Node {
128 BorderPixel(
Elements children, Pixel pixel)
129 :
Node(std::move(children)), pixel_(std::move(pixel)) {}
134 void ComputeRequirement()
override {
149 void SetBox(Box box)
override {
153 title_box.x_min = box.x_min + 1;
154 title_box.x_max = box.x_max - 1;
155 title_box.y_min = box.y_min;
156 title_box.y_max = box.y_min;
166 void Render(Screen& screen)
override {
223 return std::make_shared<Border>(unpack(std::move(child)),
ROUNDED);
230 return [pixel](
Element child) {
231 return std::make_shared<BorderPixel>(unpack(std::move(child)), pixel);
239 return [style](
Element child) {
240 return std::make_shared<Border>(unpack(std::move(child)), style);
248 return [foreground_color](
Element child) {
249 return std::make_shared<Border>(unpack(std::move(child)),
ROUNDED,
258 return [style, foreground_color](
Element child) {
259 return std::make_shared<Border>(unpack(std::move(child)), style,
296 return std::make_shared<Border>(unpack(std::move(child)),
DASHED);
331 return std::make_shared<Border>(unpack(std::move(child)),
LIGHT);
366 return std::make_shared<Border>(unpack(std::move(child)),
HEAVY);
401 return std::make_shared<Border>(unpack(std::move(child)),
DOUBLE);
436 return std::make_shared<Border>(unpack(std::move(child)),
ROUNDED);
471 return std::make_shared<Border>(unpack(std::move(child)),
EMPTY);
496 return std::make_shared<Border>(unpack(std::move(content), std::move(title)),
A class representing terminal colors.
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
virtual void ComputeRequirement()
Compute how much space an elements needs.
Element borderDouble(Element)
Draw a double border around the element.
std::function< Element(Element)> Decorator
Element borderDashed(Element)
Draw a light border around the element.
std::shared_ptr< Node > Element
Element borderRounded(Element)
Draw a rounded border around the element.
Element window(Element title, Element content)
Draw window with a title and a border around the element.
std::vector< Element > Elements
std::array< Charset, 6 > Charsets
Element borderHeavy(Element)
Draw a heavy border around the element.
Element borderLight(Element)
Draw a dashed border around the element.
Decorator borderWith(const Pixel &)
Same as border but with a constant Pixel around the element.
Decorator borderStyled(BorderStyle)
Same as border but with different styles.
std::array< std::string, 6 > Charset
Element border(Element)
Draw a border around the element.
Element borderEmpty(Element)
Draw an empty border around the element.
A unicode character and its associated style.