blib/lib/Game/TextMapper/Command/render.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 62 CC 4 SCAR 22.2
CC
4
Cov
0%
run
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 9.2
1 # Copyright (C) 2009-2021 Alex Schroeder <alex@gnu.org>
2 #
3 # This program is free software: you can redistribute it and/or modify it under
4 # the terms of the GNU Affero General Public License as published by the Free
5 # Software Foundation, either version 3 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Affero General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 =head1 NAME
17  
18 Game::TextMapper::Command::render
19  
20 =head1 SYNOPSIS
21  
22 text-mapper random [--hex|--square|help]
23  
24 =head1 DESCRIPTION
25  
26 This takes a map description on STDIN and prints the SVG on STDOUT.
27  
28 text-mapper random | text-mapper render > map.svg
29  
30 =head1 OPTIONS
31  
32 C<help> prints the man page.
33  
34 C<--hex> is the default: this uses L<Game::TextMapper::Mapper::Hex> to render a
35 hex map.
36  
37 C<--square> uses L<Game::TextMapper::Mapper::Square> to render a square map.
38  
39 This is important if the algorithm can produce both kinds of map, like
40 L<Game::TextMapper::Schroeder::Alpine>.
41  
42 =head1 EXAMPLES
43  
44 Hex map:
45  
46 text-mapper random Game::TextMapper::Schroeder::Alpine \
47 --role Game::TextMapper::Schroeder::Hex \
48 | text-mapper render > map.svg
49  
50 Square map:
51  
52 text-mapper random Game::TextMapper::Schroeder::Alpine \
53 --role Game::TextMapper::Schroeder::Square \
54 | text-mapper render --square > map.svg
55  
56 =cut
57  
58 package Game::TextMapper::Command::render;
59 use Modern::Perl '2018';
60 use Mojo::Base 'Mojolicious::Command';
61 use File::ShareDir 'dist_dir';
62 use Pod::Simple::Text;
63 use Getopt::Long qw(GetOptionsFromArray);
64 use Encode;
65  
66 has description => 'Render map from STDIN to STDOUT, as SVG (all UTF-8)';
67  
68 has usage => sub { my $self = shift; $self->extract_usage };
69  
70 sub run {
71 my ($self, @args) = @_;
72 my $dist_dir = $self->app->config('contrib') // dist_dir('Game-TextMapper');
73 my $hex;
74 my $square;
75 if (@args and $args[0] eq 'help') {
76 seek(DATA, 0, 0); # read from this file
77 my $parser = Pod::Simple::Text->new();
78 $parser->output_fh(*STDOUT);
79 $parser->parse_lines(<DATA>);
80 return 1;
81 }
82 GetOptionsFromArray (\@args, "hex" => \$hex, "square" => \$square);
83 warn "Unhandled arguments: @args\n" if @args;
84 my $mapper;
85 if ($square) {
86 $mapper = Game::TextMapper::Mapper::Square->new(dist_dir => $dist_dir, local_files => 1);
87 } else {
88 $mapper = Game::TextMapper::Mapper::Hex->new(dist_dir => $dist_dir, local_files => 1);
89 }
90 local $/ = undef;
91 $mapper->initialize(decode_utf8 <STDIN>);
92 print encode_utf8 $mapper->svg;
93 }
94  
95 1;
96  
97 __DATA__