File Coverage

lib/Game/TextMapper/Command/render.pm
Criterion Covered Total %
statement 30 36 83.3
branch 3 6 50.0
condition 2 6 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 43 56 76.7


line stmt bran cond sub pod time code
1             # Copyright (C) 2009-2021 Alex Schroeder
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 .
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 prints the man page.
33              
34             C<--hex> is the default: this uses L to render a
35             hex map.
36              
37             C<--square> uses L to render a square map.
38              
39             This is important if the algorithm can produce both kinds of map, like
40             L.
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 1     1   8631 use Modern::Perl '2018';
  1         3  
  1         15  
60 1     1   346 use Mojo::Base 'Mojolicious::Command';
  1         1  
  1         8  
61 1     1   172 use File::ShareDir 'dist_dir';
  1         1  
  1         51  
62 1     1   4 use Pod::Simple::Text;
  1         1  
  1         24  
63 1     1   4 use Getopt::Long qw(GetOptionsFromArray);
  1         1  
  1         7  
64 1     1   115 use Encode;
  1         2  
  1         545  
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 1     1 1 118 my ($self, @args) = @_;
72 1   33     5 my $dist_dir = $self->app->config('contrib') // dist_dir('Game-TextMapper');
73 1         136 my $hex;
74             my $square;
75 1 50 33     5 if (@args and $args[0] eq 'help') {
76 0         0 seek(DATA, 0, 0); # read from this file
77 0         0 my $parser = Pod::Simple::Text->new();
78 0         0 $parser->output_fh(*STDOUT);
79 0         0 $parser->parse_lines();
80 0         0 return 1;
81             }
82 1         5 GetOptionsFromArray (\@args, "hex" => \$hex, "square" => \$square);
83 1 50       406 warn "Unhandled arguments: @args\n" if @args;
84 1         3 my $mapper;
85 1 50       4 if ($square) {
86 0         0 $mapper = Game::TextMapper::Mapper::Square->new(dist_dir => $dist_dir, local_files => 1);
87             } else {
88 1         23 $mapper = Game::TextMapper::Mapper::Hex->new(dist_dir => $dist_dir, local_files => 1);
89             }
90 1         18 local $/ = undef;
91 1         45 $mapper->initialize(decode_utf8 );
92 1         6 print encode_utf8 $mapper->svg;
93             }
94              
95             1;
96              
97             __DATA__