summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/ReadLine.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Reply/Plugin/ReadLine.pm')
-rw-r--r--lib/Reply/Plugin/ReadLine.pm190
1 files changed, 91 insertions, 99 deletions
diff --git a/lib/Reply/Plugin/ReadLine.pm b/lib/Reply/Plugin/ReadLine.pm
index 55baa38..a33eaea 100644
--- a/lib/Reply/Plugin/ReadLine.pm
+++ b/lib/Reply/Plugin/ReadLine.pm
@@ -1,9 +1,9 @@
-package Reply::Plugin::ReadLine;
+package main;
use strict;
use warnings;
# ABSTRACT: use Term::ReadLine for user input
-use base 'Reply::Plugin';
+use mop;
use File::HomeDir;
use File::Spec;
@@ -33,119 +33,111 @@ recommended if possible.
=cut
-sub new {
- my $class = shift;
- my %opts = @_;
-
- my $self = $class->SUPER::new(@_);
- $self->{term} = Term::ReadLine->new('Reply');
- my $history = $opts{history_file} || '.reply_history';
- $self->{history_file} = File::Spec->catfile(
- (File::Spec->file_name_is_absolute($history)
- ? ()
- : (File::HomeDir->my_data)),
- $history
- );
-
- $self->{rl_gnu} = $self->{term}->ReadLine eq 'Term::ReadLine::Gnu';
- $self->{rl_perl5} = $self->{term}->ReadLine eq 'Term::ReadLine::Perl5';
- $self->{rl_caroline} = $self->{term}->ReadLine eq 'Term::ReadLine::Caroline';
-
- if ($self->{rl_perl5}) {
- # output compatible with Term::ReadLine::Gnu
- $readline::rl_scroll_nextline = 0;
- }
-
- if ($self->{rl_perl5} || $self->{rl_gnu} || $self->{rl_caroline}) {
- $self->{term}->StifleHistory($opts{history_length})
- if defined $opts{history_length} && $opts{history_length} >= 0;
- }
-
- if (open my $fh, '<', $self->{history_file}) {
- for my $line (<$fh>) {
- chomp $line;
- $self->{term}->addhistory($line);
+class Reply::Plugin::ReadLine extends Reply::Plugin {
+ has $term = Term::ReadLine->new('Reply');
+ has $history_file = '.reply_history';
+ has $history_length = -1;
+
+ # XXX these should be able to be lazy, but defaults can't see attributes
+ # yet it seems
+ has $rl_gnu;
+ has $rl_perl5;
+ has $rl_caroline;
+
+ submethod BUILD ($opts) {
+ $rl_gnu = $term->ReadLine eq 'Term::ReadLine::Gnu';
+ $rl_perl5 = $term->ReadLine eq 'Term::ReadLine::Perl5';
+ $rl_caroline = $term->ReadLine eq 'Term::ReadLine::Caroline';
+
+ $history_file = File::Spec->catfile(
+ (File::Spec->file_name_is_absolute($history_file)
+ ? ()
+ : (File::HomeDir->my_data)),
+ $history_file
+ );
+
+ if ($rl_perl5) {
+ # output compatible with Term::ReadLine::Gnu
+ $readline::rl_scroll_nextline = 0;
}
- }
- else {
- my $e = $!;
- warn "Couldn't open $self->{history_file} for reading: $e"
- if -e $self->{history_file};
- }
-
- $self->_register_tab_complete;
- return $self;
-}
+ if ($rl_perl5 || $rl_gnu || $rl_caroline) {
+ $term->StifleHistory($history_length)
+ if $history_length >= 0;
+ }
-sub read_line {
- my $self = shift;
- my ($next, $prompt) = @_;
+ if (open my $fh, '<', $history_file) {
+ for my $line (<$fh>) {
+ chomp $line;
+ $term->addhistory($line);
+ }
+ }
+ else {
+ my $e = $!;
+ warn "Couldn't open $history_file for reading: $e"
+ if -e $history_file;
+ }
- return $self->{term}->readline($prompt);
-}
+ $self->_register_tab_complete;
+ }
-sub DESTROY {
- my $self = shift;
+ method read_line ($next, $prompt) {
+ $term->readline($prompt);
+ }
- return if defined $self->{history_length} && $self->{history_length} == 0;
+ submethod DEMOLISH {
+ return if $history_length == 0;
+ return unless $rl_gnu || $rl_perl5;
+ $term->WriteHistory($history_file)
+ or warn "Couldn't write history to $history_file";
+ }
- # XXX support more later
- return unless ($self->{rl_gnu} || $self->{rl_perl5} || $self->{rl_caroline});
+ method _register_tab_complete {
+ weaken(my $weakself = $self);
- $self->{term}->WriteHistory($self->{history_file})
- or warn "Couldn't write history to $self->{history_file}";
-}
+ if ($rl_gnu) {
+ $term->Attribs->{attempted_completion_function} = sub {
+ my ($text, $line, $start, $end) = @_;
-sub _register_tab_complete {
- my $self = shift;
+ # discard everything after the cursor for completion purposes
+ substr($line, $end) = '';
- my $term = $self->{term};
+ my @matches = $weakself->publish('tab_handler', $line);
+ my $match_index = 0;
- weaken(my $weakself = $self);
+ return $term->completion_matches($text, sub {
+ my ($text, $index) = @_;
+ return $matches[$index];
+ });
+ };
+ }
- if ($self->{rl_gnu}) {
- $term->Attribs->{attempted_completion_function} = sub {
- my ($text, $line, $start, $end) = @_;
+ if ($rl_perl5) {
+ $term->Attribs->{completion_function} = sub {
+ my ($text, $line, $start) = @_;
+ my $end = $start + length($text);
- # discard everything after the cursor for completion purposes
- substr($line, $end) = '';
+ # discard everything after the cursor for completion purposes
+ substr($line, $end) = '';
- my @matches = $weakself->publish('tab_handler', $line);
- my $match_index = 0;
+ my @matches = $weakself->publish('tab_handler', $line);
+ return scalar(@matches) ? @matches : ();
+ };
+ }
- return $term->completion_matches($text, sub {
- my ($text, $index) = @_;
- return $matches[$index];
+ if ($rl_caroline) {
+ $term->caroline->completion_callback(sub {
+ my ($line) = @_;
+
+ my @matches = $weakself->publish('tab_handler', $line);
+ # for variable completion, method name completion.
+ if (@matches && $line =~ /\W/) {
+ $line =~ s/[:\w]+\z//;
+ @matches = map { $line.$_ } @matches;
+ }
+ return scalar(@matches) ? @matches : ();
});
- };
- }
-
- if ($self->{rl_perl5}) {
- $term->Attribs->{completion_function} = sub {
- my ($text, $line, $start) = @_;
- my $end = $start + length($text);
-
- # discard everything after the cursor for completion purposes
- substr($line, $end) = '';
-
- my @matches = $weakself->publish('tab_handler', $line);
- return scalar(@matches) ? @matches : ();
- };
- }
-
- if ($self->{rl_caroline}) {
- $term->caroline->completion_callback(sub {
- my ($line) = @_;
-
- my @matches = $weakself->publish('tab_handler', $line);
- # for variable completion, method name completion.
- if (@matches && $line =~ /\W/) {
- $line =~ s/[:\w]+\z//;
- @matches = map { $line.$_ } @matches;
- }
- return scalar(@matches) ? @matches : ();
- });
+ }
}
}