summaryrefslogtreecommitdiff
path: root/lib/TextPrinter.pm
blob: d786b867ce4baae7c5e6eed1442ede05365000e6 (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
package TextPrinter; 
use strict;
use warnings;
use Term::ANSIColor;
 
{
my %colours = (
    'black' => [
        map { color($_,'on_black') }
            'dark yellow','dark green','dark blue'
        ],
    'white' => [
        map { color($_,'on_white') }
            'green''blue''yellow'
        ],
);
 
my $strip_length = 0;
 
sub rotate {
    my ($arr) = @_;
 
    my $el = shift @$arr;
    push @$arr$el;
    return;
}
 
sub next_colour {
    my ($which) = @_;
 
    if ($strip_length < 1) {
        $strip_length = 1+int(rand(7));
        rotate($colours{$which});
    }
    --$strip_length;
    return $colours{$which}->[0];
}
}
 
 
sub draw_cell {
    print next_colour(shift eq '*' ? 'black' : 'white'),
        chr(65+int(rand(26)));
}
{
my $white = color('black','on_white');
my $reset = color('reset');
sub draw_row {
    my ($row,$scale)=@_;
    $scale||=1;
    print $white,' 'x(3*$scale);
    for my $cell (@$row) {
        draw_cell($cellfor 1..$scale;
    }
    print $white,' 'x(3*$scale);
    print $reset,"\n";
}
sub draw_empty_row {
    my ($row,$scale)=@_;
    $scale||=1;
 
    print $white,' 'x((6+@$row)*$scale),$reset,"\n" for 1..(3*$scale);
}
}
 
sub draw_whole {
    my ($arr,$scale)=@_;
    $scale||=1;
 
    draw_empty_row($arr->[0],$scale);
 
    for my $row (@$arr) {
        draw_row($row,$scalefor 1..$scale;
    }
 
    draw_empty_row($arr->[0],$scale);
}
 
1;