blib/lib/Game/CharacterSheetGenerator/Command/random.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 80 CC 5 SCAR 25.5
CC
5
Cov
0%
run
30.0
init
2.0
BEGIN
2.0
CRAP 12.8
1 # Copyright (C) 2022 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::CharacterSheetGenerator::Command::random
19  
20 =head1 SYNOPSIS
21  
22 character-sheet-generator random [language] [format] [key=value ...]
23 character-sheet-generator random help
24  
25 =head1 DESCRIPTION
26  
27 This prints a random character SVG document to STDOUT.
28  
29 character-sheet-generator random > test.svg
30  
31 =head1 OPTIONS
32  
33 The supported languages are C<en> and C<de>. English is the default.
34  
35 The supported formats are C<SVG> and C<text>. SVG is the default.
36  
37 You can provide additional key-value pairs. These can be combined. Class names
38 are case sensitive.
39  
40 ```
41 character-sheet-generator random de text name=Alex
42 character-sheet-generator random en text class=elf
43 character-sheet-generator random en text class=elf name=Alex
44 ```
45  
46 C<help> prints the man page.
47  
48 =cut
49  
50 package Game::CharacterSheetGenerator::Command::random;
51  
52 use Modern::Perl '2018';
53 use Mojo::Base 'Mojolicious::Command';
54 use Game::CharacterSheetGenerator;
55 use Pod::Simple::Text;
56 use Role::Tiny;
57 use Encode::Locale;
58 use Encode;
59  
60 binmode(STDOUT, ':utf8');
61  
62 has description => 'Print a random character sheet to STDOUT';
63  
64 has usage => sub { my $self = shift; $self->extract_usage };
65  
66 sub init {
67 my %char = ();
68 my @provided;
69 for my $arg (@_) {
70 my ($key, $value) = split(/=/, $arg, 2);
71 push(@provided, $key);
72 $char{$key} = decode(locale => $value);
73 }
74 $char{provided} = \@provided;
75 return \%char;
76 }
77  
78 sub run {
79 my ($self, $lang, $format, @args) = @_;
80 $lang ||= 'en';
81 $format ||= 'SVG';
82 if ($lang eq 'help') {
83 seek(DATA, 0, 0); # read from this file
84 my $parser = Pod::Simple::Text->new();
85 $parser->output_fh(*STDOUT);
86 $parser->parse_lines(<DATA>);
87 return 1;
88 }
89 my $char = init(@args);
90 Game::CharacterSheetGenerator::random_parameters($char, $lang, "portrait");
91 Game::CharacterSheetGenerator::compute_data($char, $lang);
92 if ($format eq 'SVG') {
93 my $svg = Game::CharacterSheetGenerator::svg_transform(undef, Game::CharacterSheetGenerator::svg_read($char));
94 print $svg->toString();
95 } elsif ($format eq 'text') {
96 for my $key (@{$char->{provided}}) {
97 next unless defined $char->{$key};
98 for my $value (split(/\\\\/, $char->{$key})) {
99 say "$key: $value";
100 }
101 }
102 } else {
103 warn "Unknown format '$format'\n";
104 }
105 return 1;
106 }
107  
108 1;
109  
110 __DATA__