| 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::HexDescribe::Command::rule | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | hex-describe rule [--table=B<table>] [--rule=B<rule>] [--limit=B<number>] | ||
| 23 | |||
| 24 | hex-describe rule --help | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Prints the random results of rules. | ||
| 29 | |||
| 30 | =head1 OPTIONS | ||
| 31 | |||
| 32 | C<--help> prints the man page. | ||
| 33 | |||
| 34 | C<--table> specifies the table to load. | ||
| 35 | |||
| 36 | Without C<--table>, print all the tables there are. | ||
| 37 | |||
| 38 | C<--rule> specifies the rule to use. | ||
| 39 | |||
| 40 | C<--text> specifies the text to use (with rules in square brackets). | ||
| 41 | |||
| 42 | Without C<--rule> or C<--text>, print all the rules there are. | ||
| 43 | |||
| 44 | C<--limit> limits the output to a certain number of entries. The default is 10. | ||
| 45 | |||
| 46 | C<--separator> changes the separator between entries. The default is two | ||
| 47 | newlines, three dashes, and two newlines ("\n\n---\n\n"). | ||
| 48 | |||
| 49 | =head1 EXAMPLES | ||
| 50 | |||
| 51 | Find all orc related rules: | ||
| 52 | |||
| 53 | C<hex-describe rule --table=schroeder | grep orc> | ||
| 54 | |||
| 55 | Print an orc tribe: | ||
| 56 | |||
| 57 | C<hex-describe rule --table=schroeder --rule="orcs" --limit=1> | ||
| 58 | |||
| 59 | Print ten orc names: | ||
| 60 | |||
| 61 | C<hex-describe rule --table=schroeder --rule="orc name" --separator=\n | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | package Game::HexDescribe::Command::rule; | ||
| 66 | |||
| 67 | use Modern::Perl '2018'; | ||
| 68 | use Mojo::Base 'Mojolicious::Command'; | ||
| 69 | use Pod::Simple::Text; | ||
| 70 | use Getopt::Long qw(GetOptionsFromArray); | ||
| 71 | use Game::HexDescribe::Utils qw(markdown describe_text parse_table load_table list_tables); | ||
| 72 | use Role::Tiny; | ||
| 73 | binmode(STDOUT, ':utf8'); | ||
| 74 | |||
| 75 | has description => 'Print the output of a rule to STDOUT'; | ||
| 76 | |||
| 77 | has usage => sub { my $self = shift; $self->extract_usage }; | ||
| 78 | |||
| 79 | sub run { | ||
| 80 | my ($self, @args) = @_; | ||
| 81 | my ($help, $table, $rule, $text, $limit, $separator); | ||
| 82 | GetOptionsFromArray ( | ||
| 83 | \@args, | ||
| 84 | "help" => \$help, | ||
| 85 | "table=s" => \$table, | ||
| 86 | "rule=s" => \$rule, | ||
| 87 | "text=s" => \$text, | ||
| 88 | "limit=i" => \$limit, | ||
| 89 | "separator=s" => \$separator); | ||
| 90 | if ($help) { | ||
| 91 | seek(DATA, 0, 0); # read from this file | ||
| 92 | my $parser = Pod::Simple::Text->new(); | ||
| 93 | $parser->output_fh(*STDOUT); | ||
| 94 | $parser->parse_lines(<DATA>); | ||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | my $dir = $self->app->config('contrib'); | ||
| 98 | if (not $table) { | ||
| 99 | say for list_tables($dir); | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | if (not $rule and not $text) { | ||
| 103 | say for keys %{parse_table(load_table($table, $dir))}; | ||
| 104 | return 1; | ||
| 105 | } | ||
| 106 | my $data = parse_table(load_table($table, $dir)); | ||
| 107 | $text //= "[$rule]\n" x ($limit || 10); | ||
| 108 | say join("\n", markdown(describe_text($text, $data), $separator)); | ||
| 109 | 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | __DATA__ |