summaryrefslogtreecommitdiff
path: root/lib/Pod/To/Pager/BorderedBlock.pm6
blob: 06d2f5874dbfeccb8cfe7915e35dee8f2bca5a36 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/env false

use v6.c;

use Terminal::ANSIColor;

class Pod::To::Pager::BorderedBlock
{
	has Str %.box-characters =
		outer-top-left     => "┏",
		outer-top-right    => "┓",
		outer-bottom-left  => "┗",
		outer-bottom-right => "┛",
		outer-horizontal   => "━",
		outer-vertical     => "┃",
		seperator-left     => "┠",
		seperator-right    => "┨",
		seperator          => "─",
	;

	has Str $.content = "";
	has Str $.header = "";
	has Str $.footer = "";
	has Int $.minimum-width = 66;

	multi method render (
		--> Str
	) {
		self.render(
			$!content,
			header => $!header,
			footer => $!footer,
			minimum-width => $!minimum-width,
		);
	}

	multi method render (
		Str:D $content is copy,
		:$header = $!header,
		:$footer = $!header,
		:$minimum-width = $!minimum-width,
		--> Str
	) {
		# Trim content
		$content .= trim;

		# Calculate the maximum width of the content
		my Int $longest-string = 0;

		for $content.lines { $longest-string = colorstrip($_).chars if $longest-string < colorstrip($_).chars; }
		for $header.lines  { $longest-string = colorstrip($_).chars if $longest-string < colorstrip($_).chars; }
		for $footer.lines  { $longest-string = colorstrip($_).chars if $longest-string < colorstrip($_).chars; }

		$longest-string = $minimum-width if $longest-string < $minimum-width;

		# Create the top bar
		my Str $block = %!box-characters<outer-top-left>;
		$block ~= %!box-characters<outer-horizontal> x $longest-string;
		$block ~= %!box-characters<outer-top-right>;
		$block ~= "\n";

		# Add a header, if needed
		if ($header ne "") {
			for $header.lines {
				$block ~= self!wrap-line($_, $longest-string);
			}

			$block ~= %!box-characters<seperator-left>;
			$block ~= %!box-characters<seperator> x $longest-string;
			$block ~= %!box-characters<seperator-right>;
			$block ~= "\n";
		}

		# Add the main content
		for $content.lines {
			$block ~= self!wrap-line($_, $longest-string);
		}

		# Add a footer, if needed
		if ($footer ne "") {
			$block ~= %!box-characters<seperator-left>;
			$block ~= %!box-characters<seperator> x $longest-string;
			$block ~= %!box-characters<seperator-right>;
			$block ~= "\n";

			for $footer.lines {
				$block ~= self!wrap-line($_, $longest-string);
			}
		}

		# Create the bottom bar
		$block ~= %!box-characters<outer-bottom-left>;
		$block ~= %!box-characters<outer-horizontal> x $longest-string;
		$block ~= %!box-characters<outer-bottom-right>;
	}

	method !wrap-line (
		Str:D $line,
		Int:D $length,
		--> Str
	) {
		%!box-characters<outer-vertical> ~ $line ~ " " x ($length - colorstrip($line).chars) ~ %!box-characters<outer-vertical> ~ "\n";
	}
}

# vim: ft=perl6 noet