Game-Terminal
view release on metacpan or search on metacpan
lib/Game/Terminal/Menu.pm view on Meta::CPAN
package Game::Terminal::Menu;
use 5.014;
use strict;
use warnings;
use Object::Proto::Sugar -types;
use Game::Terminal;
use Game::Terminal::Kit::Form;
use Game::Terminal::Kit::Keys;
use Game::Terminal::Kit::Log;
use Game::Terminal::Kit::Run;
use Game::Terminal::Kit::Screen;
use Text::ParseWords ();
our $VERSION = '0.04';
my %WORD = (0 => 'won or drew', 1 => 'lost', 2 => 'bad option');
my $LEGEND = 'up and down to choose, enter to set up and play, a letter to jump, q to quit';
my $MORE = 'k and j move too, space plays, home and end jump to the ends';
my %INK = (
title => '1',
count => '2',
key => '1',
board => '38;5;75',
card => '38;5;203',
tile => '38;5;220',
puzzle => '38;5;177',
arcade => '38;5;80',
detail => '38;5;245',
legend => '2',
set => '38;5;114',
own => '2',
play => '1;32',
good => '38;5;114',
bad => '38;5;203',
warn => '38;5;220',
);
has [qw/in out/] => (
is => 'rw'
);
has [qw/interactive colour picking/] => (
is => 'rw'
);
has [qw/keyboard screen/] => (
is => 'rw'
);
has keysource => (
is => 'rw',
isa => CodeRef
);
has pending => (
is => 'rw',
isa => ArrayRef,
lib/Game/Terminal/Menu.pm view on Meta::CPAN
return [] unless defined $line && length $line;
return [ Text::ParseWords::shellwords($line) ];
}
sub describe {
my ($self, $row, $status, $error) = @_;
return "$row->{key} died: $error" if defined $error;
$status = 0 unless defined $status && $status =~ m/^\d+$/;
my $word = $WORD{$status};
return "$row->{key} finished with status $status" . ($word ? " ($word)" : '');
}
sub launch {
my ($self, $row, $argv) = @_;
$self->leave_raw;
$self->clear;
my $common = $self->common;
my @flags = Game::Terminal::flags_for($row, $common);
my ($status, $error) = $self->runner->($row, [ @flags, @{ $argv || [] } ],
out => $self->out,
no_colour => (defined $common->{colour} && !$common->{colour}) ? 1 : 0,
);
my $out = $self->out;
print {$out} "\e[0m\e[?25h" if $self->interactive;
$self->picking(0) if $self->picking && !$self->enter_raw;
$self->note($self->describe($row, $status, $error),
defined $error ? 'bad' : !defined $status || $status eq '0' ? 'good' : $status eq '1' ? 'bad' : 'warn');
return $self;
}
sub start {
my ($self) = @_;
$self->picking(0) if $self->picking && !$self->enter_raw;
my $ok = eval {
Game::Terminal::Kit::Run::guard(sub {
while (1) {
my $row = $self->picking ? $self->choose : $self->prompt;
last unless $row;
my $argv = $self->picking ? $self->configure($row) : $self->ask_options($row);
next unless $argv;
$self->launch($row, $argv);
}
}, $self->keyboard);
1;
};
my $error = $@;
if ($self->interactive) {
my $out = $self->out;
print {$out} "\e[0m\e[?25h\n";
}
die $error unless $ok;
return 0;
}
1;
__END__
=head1 NAME
Game::Terminal::Menu - the menu the game command draws
=head1 VERSION
Version 0.02
=head1 SYNOPSIS
use Game::Terminal::Menu;
exit Game::Terminal::Menu->new->start;
my $menu = Game::Terminal::Menu->new(
common => { ascii => 1, colour => 0 },
keysource => sub { shift @keys },
runner => sub { my ($row, $argv, %seam) = @_; ... ; return (0, undef) },
);
=head1 DESCRIPTION
The roster of L<Game::Terminal> as a list on the screen: a cursor, the
highlighted game's class and version on a line underneath, the result of the
last game, and a legend. Enter opens the game under the cursor's options on a second screen
(L</configure>); enter on Play starts it B<in this process>, and when it
ends the menu is drawn again with its result and the options still set.
Everything the menu reads comes through L</read_key>, which takes the keys
from L<Term::ReadKey> when there is a terminal and from L</keysource> when a
test supplies them, so nothing here needs a tty to be tested. Without a
terminal, without Term::ReadKey, or with C<picking> off, the menu is a
numbered list and a prompt instead.
=head2 The screen
Game 21 games
> backgammon board 2 backgammon
brandubh board 2 the Irish 7 by 7 tafl game
...
xiangqi board 2 xiangqi, Chinese chess
backgammon: Game::Backgammon::Terminal 0.03, options: game backgammon --help
checkers finished with status 1 (lost)
up and down to choose, enter to play, a letter to jump, q to quit
The cursor's row is reverse video when colour is on. A game whose class is
not on C<@INC> is dimmed, marked C<(not installed)>, and skipped by the
cursor. When the terminal has fewer rows than the list needs the list is
windowed round the cursor with C<N further up> and C<N further down> lines.
=head2 The keys
Up and down (also C<k> and C<j>, and tab for down) move the cursor and wrap;
home and end go to the first and last game; enter or space starts the game
under the cursor; a letter or digit jumps to the next game whose name starts
with it, cycling, so C<s> twice goes from schnapsen to sixtysix; C<?> says
what else the keys do; C<q>, escape, EOF or an interrupt leave the menu.
=head2 Colour
Colour is on when the input is a terminal and C<NO_COLOR> is not set, and
off otherwise or when C<colour> is given false. With it on, the cursor's row
is reverse video; a game's name is bold and its family has a colour of its
own (board blue, card red, tile yellow, puzzle magenta, arcade cyan); a game
that is not installed is dim; the detail line is grey and the legend dim;
and the status line takes a tone from what happened: green for a win, a
draw or a quit, red for a loss or a game that died, yellow for a bad option
or a refused value. On the options screen a value the player set is green
and one left to the game is dim, and Play is green.
=head1 ATTRIBUTES
( run in 0.758 second using v1.01-cache-2.11-cpan-60884a8df76 )