| 1 | # | ||
| 2 | # AI::ExpertSystem::Advanced::Viewer::Terminal | ||
| 3 | # | ||
| 4 | # Author(s): Pablo Fischer (pfischer@cpan.org) | ||
| 5 | # Created: 12/13/2009 15:44:23 PST 15:44:23 | ||
| 6 | package AI::ExpertSystem::Advanced::Viewer::Terminal; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | AI::ExpertSystem::Advanced::Viewer::Terminal - Viewer for terminal | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | Extends from L<AI::ExpertSystem::Advanced::Viewer::Base> and its main purpose is | ||
| 15 | to interact with a (console) terminal. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | use Moose; | ||
| 19 | use Term::UI; | ||
| 20 | use Term::ReadLine; | ||
| 21 | |||
| 22 | extends 'AI::ExpertSystem::Advanced::Viewer::Base'; | ||
| 23 | |||
| 24 | our $VERSION = '0.02'; | ||
| 25 | |||
| 26 | =head1 Attribtes | ||
| 27 | |||
| 28 | =over 4 | ||
| 29 | |||
| 30 | =item B<readline> | ||
| 31 | |||
| 32 | A L<Term::ReadLine> instance. | ||
| 33 | |||
| 34 | =back | ||
| 35 | |||
| 36 | =cut | ||
| 37 | has 'readline' => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => 'Term::ReadLine'); | ||
| 40 | |||
| 41 | =head1 Methods | ||
| 42 | |||
| 43 | =head2 B<debug($msg)> | ||
| 44 | |||
| 45 | Basically just prints the given C<$msg> but prepends the "DEBUG" string to it. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | sub debug { | ||
| 49 | my ($self, $msg) = @_; | ||
| 50 | print "DEBUG: $msg\n"; | ||
| 51 | } | ||
| 52 | |||
| 53 | =head2 B<print($msg)> | ||
| 54 | |||
| 55 | Simply prints the given C<$msg>. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | sub print { | ||
| 59 | my ($self, $msg) = @_; | ||
| 60 | print "$msg\n"; | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 B<print_error($msg)> | ||
| 64 | |||
| 65 | Will prepend the "ERROR:" word to the given message and then will call | ||
| 66 | C<print()>. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | sub print_error { | ||
| 70 | my ($self, $msg) = @_; | ||
| 71 | $self->print("ERROR: $msg"); | ||
| 72 | } | ||
| 73 | |||
| 74 | =head2 B<ask($message, @options)> | ||
| 75 | |||
| 76 | Will be used to ask the user for some information. It will receive a string, | ||
| 77 | the question to ask and an array of all the possible options. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | sub ask { | ||
| 81 | my ($self, $msg, $options) = @_; | ||
| 82 | |||
| 83 | my %valid_choices = ( | ||
| 84 | 'Y' => '+', | ||
| 85 | 'N' => '-', | ||
| 86 | 'U' => '~'); | ||
| 87 | |||
| 88 | my $reply = $self->{'readline'}->get_reply( | ||
| 89 | prompt => $msg . ' ', | ||
| 90 | choices => [qw|Y N U|]); | ||
| 91 | return $valid_choices{$reply}; | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 B<explain($yaml_summary)> | ||
| 95 | |||
| 96 | Explains what happened. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | sub explain { | ||
| 100 | my ($self, $summary) = @_; | ||
| 101 | |||
| 102 | print $summary; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | # Called when the object is created | ||
| 107 | sub BUILD { | ||
| 108 | my ($self) = @_; | ||
| 109 | |||
| 110 | $self->{'readline'} = Term::ReadLine->new('questions'); | ||
| 111 | } | ||
| 112 | |||
| 113 | =head1 AUTHOR | ||
| 114 | |||
| 115 | Pablo Fischer (pablo@pablo.com.mx). | ||
| 116 | |||
| 117 | =head1 COPYRIGHT | ||
| 118 | |||
| 119 | Copyright (C) 2010 by Pablo Fischer. | ||
| 120 | |||
| 121 | This library is free software; you can redistribute it and/or modify | ||
| 122 | it under the same terms as Perl itself. | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | 1; |