/
VisitorHelper.pm6
128 lines (121 loc) · 3.89 KB
/
VisitorHelper.pm6
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use Terminal::Table::String;
use Terminal::Table::Frame;
use Terminal::Table::Shader;
class VisitorHelper {
has %.callback-map;
method __check_callback_map($name) {
unless %!callback-map{$name}:exists {
%!callback-map{$name} = Array.new;
}
}
method add-helper(Str $name, &callback) {
self.__check_callback_map($name);
%!callback-map{$name}.push(&callback);
}
method FALLBACK($name, |c) {
for @(%!callback-map{$name}) -> &cb {
if c ~~ &cb.signature {
return &cb(|c);
}
}
fail "Not wrapper named: $name with signature {c.perl}!";
}
}
sub visitor-helper() returns VisitorHelper is export is rw {
state $helper = VisitorHelper.new;
$helper.add-helper("colour",
sub ($s, Bool $coloured) {
return do given $s.visibility {
when Visibility::VSPACE {
$s.to-space();
}
when Visibility::VFALSE {
"";
}
default {
$s.Str();
}
};
});
$helper.add-helper("colour",
sub ($s, $row, Bool $coloured) {
return do given $s.visibility {
when Visibility::VSPACE {
$s.to-space();
}
when Visibility::VFALSE {
"";
}
default {
if $s ~~ Line {
$s.get-line($row);
} else {
-> ($pleft, String $s, $pright) {
$pleft ~ (
?$coloured && $s.coloured() ??
Shader.colour($s.Str(), $s.style()) !! $s.Str()
) ~ $pright;
}($s.get-line($row));
}
}
};
});
$helper.add-helper("h-frame",
sub (@h-frame, Bool $coloured) {
return gather for @h-frame -> $f {
take &visitor-helper().colour($f, $coloured) unless
$f.check-visibility(Visibility::VFALSE)
};
});
$helper.add-helper("v-frame",
sub (@v-frame, @contents, Bool $coloured){
my @ret = [];
if +@v-frame > 0 && +@contents > 0 {
for ^@contents[0].height -> $row {
@ret.push(
gather {
for (@v-frame Z, @contents).flat -> $f-or-c {
unless $f-or-c.check-visibility(Visibility::VFALSE) {
take &visitor-helper().colour($f-or-c, $row, $coloured);
}
}
unless @v-frame[* - 1].check-visibility(Visibility::VFALSE) {
take &visitor-helper().colour(@v-frame[* - 1], $row, $coloured);
}
}
);
}
} elsif +@contents > 0 { #`( v-frame will be empty when style is none)
for ^@contents[0].height -> $row {
@ret.push(
gather {
for @contents -> $c {
unless $c.check-visibility(Visibility::VFALSE) {
take &visitor-helper().colour($c, $row, $coloured);
}
}
}
);
}
}
return @ret;
});
$helper.add-helper("generate",
sub (@h-frame, @v-frame, @contents, Bool $coloured) {
my @ret = [];
if +@h-frame > 0 {
@ret.push(gather for @h-frame -> $f {
unless $f.check-visibility(Visibility::VFALSE) {
take &visitor-helper().colour($f, $coloured);
}
});
}
if +@contents > 0 {
@ret.append(
&visitor-helper().v-frame(@v-frame, @contents, $coloured)
);
}
return @ret;
});
$helper;
}