| 1 | package Hailo::UI::ReadLine; | ||||||||||||||
| 2 | 3 | our $AUTHORITY = 'cpan:AVAR'; | |||||||||||||
| 3 | 3 | $Hailo::UI::ReadLine::VERSION = '0.73'; | |||||||||||||
| 4 | 3 | ▶ | use v5.28.0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use MooseX::StrictConstructor; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use Encode 'decode'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use Hailo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use Term::ReadLine; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 3 | ▶ | use Data::Dump 'dump'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 3 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 3 | with qw(Hailo::Role::Arguments | |||||||||||||
| 14 | Hailo::Role::UI); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | sub BUILD { | ||||||||||||||
| 17 | 3 | ▶ | $ENV{PERL_RL} = 'Perl o=0' unless $ENV{PERL_RL}; | ||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||
| 18 | 3 | return; | |||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | sub run { | ||||||||||||||
| 22 | 1 | ▶ | my ($self, $hailo) = @_; | ||||||||||||
|
Subroutine
run: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||
| 23 | 1 | my $name = 'Hailo'; | |||||||||||||
| 24 | 1 | my $term = Term::ReadLine->new($name); | |||||||||||||
| 25 | 1 | my $command = qr[ | |||||||||||||
| 26 | ^ | ||||||||||||||
| 27 | # A dot-prefix like in SQLite | ||||||||||||||
| 28 | \. | ||||||||||||||
| 29 | # We only have Hailo methods matching this | ||||||||||||||
| 30 | (?<method> [a-z_]+ ) | ||||||||||||||
| 31 | # Optional arguments. These'll be passed to eval() before being | ||||||||||||||
| 32 | # passed to the method | ||||||||||||||
| 33 | \s* | ||||||||||||||
| 34 | (?: (?<arguments>.+) )? | ||||||||||||||
| 35 | $]x; | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 1 | print $self->_intro; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 1 | ▶ | while (defined (my $line = $term->readline($name . '> '))) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 13 | $line = decode('utf8', $line); | |||||||||||||
| 41 | |||||||||||||||
| 42 | 13 | ▶ | if ($line =~ /$command/p) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 11 | ▶ | if ($+{method} eq 'help') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 44 | 1 | print $self->_help($hailo); | |||||||||||||
| 45 | } elsif ($+{method} =~ /^(?: quit | exit )$/xs) { | ||||||||||||||
| 46 | 1 | ▶ | say $hailo->reply("Dave, this conversation can serve no purpose anymore. Goodbye.") // "Bye!"; | ||||||||||||
|
Condition50%1/2
$hailo->reply("Dave, this conversation can serve no purpose anymore. Goodbye.") // "Bye!"
MC/DC0%0/1
$hailo->reply("Dave, this conversation can serve no purpose anymore. Goodbye.") // "Bye!"
$hailo->reply("Dave, this conversation can serve no purpose anymore. Goodbye.")
Truth table50%1/2
A
A= $hailo->reply("Dave, this conversation can serve no purpose anymore. Goodbye.")
| |||||||||||||||
| 47 | 1 | exit 0; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | 10 | my $meth = $+{method}; | |||||||||||||
| 50 | 10 | ▶ | my @args = defined $+{arguments} ? eval $+{arguments} : (); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | eval { | ||||||||||||||
| 53 | 10 | say dump $hailo->$meth(@args); | |||||||||||||
| 54 | 8 | 1; | |||||||||||||
| 55 | 10 | ▶ | } or do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 2 | ▶ | chomp(my $err = $@ || "Zombie Error"); | ||||||||||||
|
Condition50%1/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||
| 57 | 2 | say STDERR "Failed on <<${^MATCH}>>: <<$err>>"; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | } else { | ||||||||||||||
| 60 | 2 | my $answer = $hailo->learn_reply($line); | |||||||||||||
| 61 | 2 | ▶ | say $answer // "I don't know enough to answer you yet."; | ||||||||||||
|
Condition100%2/2
$answer // "I don't know enough to answer you yet."
MC/DC100%1/1
$answer // "I don't know enough to answer you yet."
$answer
Truth table100%2/2
A
A= $answer
| |||||||||||||||
| 62 | } | ||||||||||||||
| 63 | } | ||||||||||||||
| 64 | 0 | print "\n"; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 0 | return; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | sub _intro { | ||||||||||||||
| 70 | 1 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_intro: called CC=1 SCAR=0.0
| |||||||||||||||
| 71 | 1 | my $intro = <<"INTRO"; | |||||||||||||
| 72 | Welcome to the Hailo interactive shell | ||||||||||||||
| 73 | Enter ".help" to show the built-in commands. | ||||||||||||||
| 74 | Input that's not a command will be passed to Hailo to learn, and it'll | ||||||||||||||
| 75 | reply back. | ||||||||||||||
| 76 | INTRO | ||||||||||||||
| 77 | 1 | return $intro; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | sub _help { | ||||||||||||||
| 81 | 1 | ▶ | my ($self, $hailo) = @_; | ||||||||||||
|
Subroutine
_help: called CC=5 SCAR=16.1
| |||||||||||||||
| 82 | |||||||||||||||
| 83 | 1 | my $include = qr/ ^ _go /xs; | |||||||||||||
| 84 | 1 | my $exclude = qr/ | |||||||||||||
| 85 | _ | ||||||||||||||
| 86 | (?: | ||||||||||||||
| 87 | version | ||||||||||||||
| 88 | | order | ||||||||||||||
| 89 | | progress | ||||||||||||||
| 90 | | random_reply | ||||||||||||||
| 91 | | examples | ||||||||||||||
| 92 | | autosave | ||||||||||||||
| 93 | | brain | ||||||||||||||
| 94 | | class | ||||||||||||||
| 95 | ) | ||||||||||||||
| 96 | $/xs; | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 1 | my @attr; | |||||||||||||
| 99 | 1 | for my $attr ($hailo->meta->get_all_attributes) { | |||||||||||||
| 100 | # Only get attributes that are valid command-line options | ||||||||||||||
| 101 | 39 | ▶ | next unless $attr->name =~ $include; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | |||||||||||||||
| 103 | # We don't support changing these in mid-stream | ||||||||||||||
| 104 | 17 | ▶ | next if $attr->name =~ $exclude; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | |||||||||||||||
| 106 | push @attr => { | ||||||||||||||
| 107 | 6 | name => do { | |||||||||||||
| 108 | 6 | my $tmp = $attr->cmd_flag; | |||||||||||||
| 109 | 6 | $tmp =~ tr/-/_/; | |||||||||||||
| 110 | 6 | $tmp; | |||||||||||||
| 111 | }, | ||||||||||||||
| 112 | documentation => $attr->documentation, | ||||||||||||||
| 113 | }; | ||||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 1 | push @attr => { | |||||||||||||
| 117 | name => 'quit', | ||||||||||||||
| 118 | documentation => "Exit this chat session", | ||||||||||||||
| 119 | }; | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 1 | my $help = <<"HELP"; | |||||||||||||
| 122 | These are the commands we know about: | ||||||||||||||
| 123 | |||||||||||||||
| 124 | HELP | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 1 | my @sorted = sort { $a->{name} cmp $b->{name} } @attr; | |||||||||||||
| 127 | 1 | for my $cmd (@sorted) { | |||||||||||||
| 128 | 7 | $help .= sprintf " %-14s%s\n", '.'.$cmd->{name}, $cmd->{documentation}; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 1 | $help .= <<"HELP"; | |||||||||||||
| 132 | |||||||||||||||
| 133 | The commands are just method calls on a Hailo object. Any arguments to | ||||||||||||||
| 134 | them will be passed through eval() used as method arguments. E.g.: | ||||||||||||||
| 135 | |||||||||||||||
| 136 | .train "/tmp/megahal.trn" | ||||||||||||||
| 137 | Trained from 350 lines in 0.54 seconds; 654.04 lines/s | ||||||||||||||
| 138 | () | ||||||||||||||
| 139 | |||||||||||||||
| 140 | Return values are printed with Data::Dump: | ||||||||||||||
| 141 | |||||||||||||||
| 142 | .stats | ||||||||||||||
| 143 | (1311, 2997, 3580, 3563) | ||||||||||||||
| 144 | |||||||||||||||
| 145 | Any input not starting with "." will be passed through Hailo's | ||||||||||||||
| 146 | learn_reply method: | ||||||||||||||
| 147 | |||||||||||||||
| 148 | Hailo> Help, mommy! | ||||||||||||||
| 149 | Really? I can't. It's an ethical thing. | ||||||||||||||
| 150 | |||||||||||||||
| 151 | HELP | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 1 | return $help; | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | |||||||||||||||
| 156 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 157 | |||||||||||||||
| 158 | =encoding utf8 | ||||||||||||||
| 159 | |||||||||||||||
| 160 | =head1 NAME | ||||||||||||||
| 161 | |||||||||||||||
| 162 | Hailo::UI::ReadLine - A UI for L<Hailo|Hailo> using L<Term::ReadLine|Term::ReadLine> | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =head1 SYNOPSIS | ||||||||||||||
| 165 | |||||||||||||||
| 166 | This module is called internally by L<Hailo|Hailo>, it takes no options. | ||||||||||||||
| 167 | |||||||||||||||
| 168 | A ReadLine interface will be presented when calling L<hailo> on the | ||||||||||||||
| 169 | command-line with only a C<--brain> argument: | ||||||||||||||
| 170 | |||||||||||||||
| 171 | hailo --brain hailo.sqlite | ||||||||||||||
| 172 | |||||||||||||||
| 173 | =head1 DESCRIPTION | ||||||||||||||
| 174 | |||||||||||||||
| 175 | Presents a ReadLine interface using L<Term::ReadLine>, the | ||||||||||||||
| 176 | L<Term::ReadLine::Gnu> frontend will be used. | ||||||||||||||
| 177 | |||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||
| 179 | |||||||||||||||
| 180 | E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org> | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 183 | |||||||||||||||
| 184 | Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason. | ||||||||||||||
| 185 | |||||||||||||||
| 186 | This program is free software, you can redistribute it and/or modify | ||||||||||||||
| 187 | it under the same terms as Perl itself. | ||||||||||||||
| 188 | |||||||||||||||
| 189 | =cut | ||||||||||||||