| 1 | package Myriad::UI::Readline; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '1.001'; # VERSION | |||||||||||||||||||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | no indirect qw(fatal); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =encoding utf8 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Myriad::UI::Readline - L<Term::ReadLine> support for L<Myriad> | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Provides a basic line-based interface with history support. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Syntax::Keyword::Dynamically; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Term::ReadLine; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Scope::Guard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 METHODS | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head2 readline | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Returns the L<Term::ReadLine> instance. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub readline { shift->{readline} //= Term::ReadLine->new('myriad') } | ||||||||||||||||||||||||||
|
Subroutine
readline: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/3
+(shift())->{"readline"} //= "Term::ReadLine"->new("myriad")
MC/DC0%0/2
+(shift())->{"readline"} //= "Term::ReadLine"->new("myriad")
+(shift())->{"readline"}
"Term::ReadLine"->new("myriad")
Truth table0%0/3
A //= B
A= +(shift())->{"readline"}B= "Term::ReadLine"->new("myriad")
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head2 setup | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Prepares the L</readline> instance for usage. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | async sub setup { | ||||||||||||||||||||||||||
|
Subroutine
setup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 55 | 0 | my $f = $self->loop->new_future; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | $self->readline->event_loop(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | 0 | $f->get; | |||||||||||||||||||||||||||
| 58 | 0 | $f = $self->loop->new_future; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | }, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 60 | 0 | my ($fh) = @_; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $scope = Scope::Guard->new(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | $log->tracef('Cleaning up readline watched handle'); | |||||||||||||||||||||||||||
| 64 | 0 | $self->loop->unwatch_io( | |||||||||||||||||||||||||||
| 65 | handle => $fh | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | 0 | }); | |||||||||||||||||||||||||||
| 68 | 0 | $log->tracef('Watching readline handle'); | |||||||||||||||||||||||||||
| 69 | $self->loop->watch_io( | ||||||||||||||||||||||||||||
| 70 | handle => $fh, | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | on_read_ready => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $f->done unless $f->is_ready | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | }, | ||||||||||||||||||||||||||||
| 74 | 0 | ); | |||||||||||||||||||||||||||
| 75 | 0 | return [ $scope ]; | |||||||||||||||||||||||||||
| 76 | 0 | }); | |||||||||||||||||||||||||||
| 77 | 1 | } | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head2 cleanup | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Shut down the L</readline> instance before exit. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | async sub cleanup { | ||||||||||||||||||||||||||
|
Subroutine
cleanup: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $rl = delete $self->{readline} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $rl->event_loop(undef); | |||||||||||||||||||||||||||
| 89 | 1 | } | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head2 handle_item | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Used internally to process requests. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | async sub handle_item { | ||||||||||||||||||||||||||
|
Subroutine
handle_item: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 0 | my ($self, $src) = @_; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | if(blessed($src)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 100 | 0 | my $int = $self->loop->new_future; | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | $SIG{INT} = sub { # ideally would go through dynamically here | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 102 | 0 | $log->warnf("Ctrl-C"); | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | $int->fail('Interrupted') unless $int->is_ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | }; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | if($src->isa('Ryu::Source')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 106 | 0 | await Future->wait_any( | |||||||||||||||||||||||||||
| 107 | $src->say->completed, | ||||||||||||||||||||||||||||
| 108 | $int, | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | } elsif($src->isa('Future')) { | ||||||||||||||||||||||||||||
| 111 | 0 | for my $rslt (await Future->wait_any( | |||||||||||||||||||||||||||
| 112 | $src, | ||||||||||||||||||||||||||||
| 113 | $int | ||||||||||||||||||||||||||||
| 114 | )) { | ||||||||||||||||||||||||||||
| 115 | 0 | print "$rslt\n"; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 0 | $log->errorf('Unknown blessed instance returned: %s', $src); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } elsif(defined $src) { | ||||||||||||||||||||||||||||
| 121 | 0 | print "$src\n"; | |||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||
| 123 | 0 | print "<undef?>\n"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 1 | } | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 run | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Runs the event loop for readline processing. Only resolves | ||||||||||||||||||||||||||||
| 130 | after completion. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | async sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 136 | 0 | my $rl = $self->readline; | |||||||||||||||||||||||||||
| 137 | try { | ||||||||||||||||||||||||||||
| 138 | 0 | await $self->setup; | |||||||||||||||||||||||||||
| 139 | 0 | my $prompt = 'myriad> '; | |||||||||||||||||||||||||||
| 140 | 0 | my $active = 1; | |||||||||||||||||||||||||||
| 141 | my %command = ( | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | help => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 143 | 0 | return Future->done( | |||||||||||||||||||||||||||
| 144 | 'No help available, sorry' | ||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||
| 146 | }, | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | infinite => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 148 | 0 | return $self->loop->new_future; | |||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | exit => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 151 | 0 | $active = 0; | |||||||||||||||||||||||||||
| 152 | 0 | return Future->done( | |||||||||||||||||||||||||||
| 153 | 'Will exit eventually' | ||||||||||||||||||||||||||||
| 154 | ) | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 0 | ); | |||||||||||||||||||||||||||
| 157 | 0 | $command{quit} = $command{exit}; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # The call to ->readline will enter the event loop | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | while($active && defined(my $line = $rl->readline($prompt))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$active and defined(my $line = $rl->readline($prompt))
MC/DC0%0/2
$active and defined(my $line = $rl->readline($prompt))
$active
defined(my $line = $rl->readline($prompt))
Truth table0%0/3
A and B
A= $activeB= defined(my $line = $rl->readline($prompt))
| |||||||||||||||||||||||||||||
| 161 | try { | ||||||||||||||||||||||||||||
| 162 | 0 | my ($cmd, $args) = $line =~ /^(\S+)(?: (.*))?/s; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | if(my $code = $command{$cmd}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if(my $src = $code->($args)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | # Once the call to ->readline returns, we should no longer | ||||||||||||||||||||||||||||
| 166 | # be in the event loop, so this ->get will reënter the event | ||||||||||||||||||||||||||||
| 167 | # loop long enough to complete the request | ||||||||||||||||||||||||||||
| 168 | 0 | $self->handle_item($src)->get; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||
| 171 | 0 | $log->errorf("Unknown command: %s", $cmd); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | } catch ($e) { | |||||||||||||||||||||||||||
| 174 | 0 | $log->errorf('Failed to execute %s - %s', $line, $e); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | $rl->addhistory($line) if $line =~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 0 | } catch ($e) { | |||||||||||||||||||||||||||
| 179 | 0 | $log->errorf('Failure during readline loop - %s', $e); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 0 | await $self->cleanup; | |||||||||||||||||||||||||||
| 182 | 1 | } | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 1 | 1; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | See L<Myriad/CONTRIBUTORS> for full details. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||