| 1 | package Reply::Plugin::ReadLine; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | our $AUTHORITY = 'cpan:DOY'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | $Reply::Plugin::ReadLine::VERSION = '0.42'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: use Term::ReadLine for user input | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base 'Reply::Plugin'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use File::HomeDir; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Term::ReadLine; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | $self->{term} = Term::ReadLine->new('Reply'); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $history = $opts{history_file} || '.reply_history'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts{'history_file'} || '.reply_history'
MC/DC0%0/1
$opts{'history_file'} || '.reply_history'
$opts{'history_file'}
Truth table0%0/2
A
A= $opts{'history_file'}
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $history =~ s{^~/}{$ENV{HOME}/}; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | $self->{history_file} = File::Spec->catfile( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | (File::Spec->file_name_is_absolute($history) | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ? () | ||||||||||||||||||||||||||||||||||||||||||
| 27 | : (File::HomeDir->my_data)), | ||||||||||||||||||||||||||||||||||||||||||
| 28 | $history | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | $self->{rl_gnu} = $self->{term}->ReadLine eq 'Term::ReadLine::Gnu'; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | $self->{rl_perl5} = $self->{term}->ReadLine eq 'Term::ReadLine::Perl5'; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | $self->{rl_caroline} = $self->{term}->ReadLine eq 'Term::ReadLine::Caroline'; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ($self->{rl_perl5}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | # output compatible with Term::ReadLine::Gnu | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $readline::rl_scroll_nextline = 0; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($self->{rl_perl5} || $self->{rl_gnu} || $self->{rl_caroline}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->{"rl_perl5"} or $self->{"rl_gnu"} or $self->{"rl_caroline"}
$self->{"rl_perl5"} or $self->{"rl_gnu"}
MC/DC0%0/3
$self->{"rl_perl5"} or $self->{"rl_gnu"} or $self->{"rl_caroline"}
$self->{"rl_perl5"}
$self->{"rl_gnu"}
$self->{"rl_caroline"}
Truth table0%0/4
A or B or C
A= $self->{"rl_perl5"}B= $self->{"rl_gnu"}C= $self->{"rl_caroline"}
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | $self->{term}->StifleHistory($opts{history_length}) | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if defined $opts{history_length} && $opts{history_length} >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $opts{"history_length"} and $opts{"history_length"} >= 0
MC/DC0%0/2
defined $opts{"history_length"} and $opts{"history_length"} >= 0
defined $opts{"history_length"}
$opts{"history_length"} >= 0
Truth table0%0/3
A and B
A= defined $opts{"history_length"}B= $opts{"history_length"} >= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (open my $fh, '<', $self->{history_file}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | for my $line (<$fh>) { | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $self->{term}->addhistory($line); | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $e = $!; | |||||||||||||||||||||||||||||||||||||||||
| 53 | warn "Couldn't open $self->{history_file} for reading: $e" | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if -e $self->{history_file}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $self->_register_tab_complete; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub read_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_line: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my ($next, $prompt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $self->{term}->readline($prompt); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return if defined $self->{history_length} && $self->{history_length} == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"history_length"} and $self->{"history_length"} == 0
MC/DC0%0/2
defined $self->{"history_length"} and $self->{"history_length"} == 0
defined $self->{"history_length"}
$self->{"history_length"} == 0
Truth table0%0/3
A and B
A= defined $self->{"history_length"}B= $self->{"history_length"} == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # XXX support more later | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return unless ($self->{rl_gnu} || $self->{rl_perl5} || $self->{rl_caroline}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->{"rl_gnu"} or $self->{"rl_perl5"} or $self->{"rl_caroline"}
$self->{"rl_gnu"} or $self->{"rl_perl5"}
MC/DC0%0/3
$self->{"rl_gnu"} or $self->{"rl_perl5"} or $self->{"rl_caroline"}
$self->{"rl_gnu"}
$self->{"rl_perl5"}
$self->{"rl_caroline"}
Truth table0%0/4
A or B or C
A= $self->{"rl_gnu"}B= $self->{"rl_perl5"}C= $self->{"rl_caroline"}
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | $self->{term}->WriteHistory($self->{history_file}) | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | or warn "Couldn't write history to $self->{history_file}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub _register_tab_complete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_register_tab_complete: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $term = $self->{term}; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | weaken(my $weakself = $self); | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($self->{rl_gnu}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $term->Attribs->{attempted_completion_function} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my ($text, $line, $start, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # discard everything after the cursor for completion purposes | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | substr($line, $end) = ''; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my @matches = $weakself->publish('tab_handler', $line); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $match_index = 0; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return $term->completion_matches($text, sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my ($text, $index) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | return $matches[$index]; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($self->{rl_perl5}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $term->Attribs->{completion_function} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my ($text, $line, $start) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $end = $start + length($text); | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # discard everything after the cursor for completion purposes | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | substr($line, $end) = ''; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my @matches = $weakself->publish('tab_handler', $line); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | return scalar(@matches) ? @matches : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ($self->{rl_caroline}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $term->caroline->completion_callback(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my ($line) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my @matches = $weakself->publish('tab_handler', $line); | |||||||||||||||||||||||||||||||||||||||||
| 123 | # for variable completion, method name completion. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if (@matches && $line =~ /\W/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@matches and $line =~ /\W/
MC/DC0%0/2
@matches and $line =~ /\W/
@matches
$line =~ /\W/
Truth table0%0/3
A and B
A= @matchesB= $line =~ /\W/
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | $line =~ s/[:\w]+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | @matches = map { $line.$_ } @matches; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | return scalar(@matches) ? @matches : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||||||||||||||||