From 84640f1b3b402d2a64a5005d757f7171df2d658d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 31 May 2013 04:51:29 -0500 Subject: add some configuration to a couple plugins --- lib/Reply/Plugin/Colors.pm | 7 ++++--- lib/Reply/Plugin/Editor.pm | 7 ++++++- lib/Reply/Plugin/Nopaste.pm | 5 +++++ lib/Reply/Plugin/Packages.pm | 3 ++- lib/Reply/Plugin/ReadLine.pm | 25 ++++++++++++++++++------- lib/Reply/Plugin/ResultCache.pm | 7 +++++-- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/Reply/Plugin/Colors.pm b/lib/Reply/Plugin/Colors.pm index 199d41e..8b68119 100644 --- a/lib/Reply/Plugin/Colors.pm +++ b/lib/Reply/Plugin/Colors.pm @@ -8,11 +8,12 @@ use Term::ANSIColor; sub new { my $class = shift; + my %opts = @_; my $self = $class->SUPER::new(@_); - $self->{error} = 'red'; - $self->{warning} = 'yellow'; - $self->{result} = 'green'; + $self->{error} = $opts{error} || 'red'; + $self->{warning} = $opts{warning} || 'yellow'; + $self->{result} = $opts{result} || 'green'; return $self; } diff --git a/lib/Reply/Plugin/Editor.pm b/lib/Reply/Plugin/Editor.pm index 76184d1..fe5c23e 100644 --- a/lib/Reply/Plugin/Editor.pm +++ b/lib/Reply/Plugin/Editor.pm @@ -10,9 +10,14 @@ use Proc::InvokeEditor; sub new { my $class = shift; + my %opts = @_; my $self = $class->SUPER::new(@_); - $self->{editor} = Proc::InvokeEditor->new; + $self->{editor} = Proc::InvokeEditor->new( + (defined $opts{editor} + ? (editors => [ $opts{editor} ]) + : ()) + ); $self->{current_text} = ''; return $self; diff --git a/lib/Reply/Plugin/Nopaste.pm b/lib/Reply/Plugin/Nopaste.pm index fdcead9..a3fa909 100644 --- a/lib/Reply/Plugin/Nopaste.pm +++ b/lib/Reply/Plugin/Nopaste.pm @@ -11,9 +11,11 @@ use App::Nopaste; sub new { my $class = shift; + my %opts = @_; my $self = $class->SUPER::new(@_); $self->{history} = ''; + $self->{service} = $opts{service}; return $self; } @@ -68,6 +70,9 @@ sub command_nopaste { text => $self->{history}, desc => $line, lang => 'perl', + (defined $self->{service} + ? (services => [ $self->{service} ]) + : ()), ); return ''; diff --git a/lib/Reply/Plugin/Packages.pm b/lib/Reply/Plugin/Packages.pm index 071e20b..8e1b85b 100644 --- a/lib/Reply/Plugin/Packages.pm +++ b/lib/Reply/Plugin/Packages.pm @@ -6,9 +6,10 @@ use base 'Reply::Plugin'; sub new { my $class = shift; + my %opts = @_; my $self = $class->SUPER::new(@_); - $self->{package} = 'main'; + $self->{package} = $opts{default_package} || 'main'; return $self; } diff --git a/lib/Reply/Plugin/ReadLine.pm b/lib/Reply/Plugin/ReadLine.pm index 29ad5a0..54c262b 100644 --- a/lib/Reply/Plugin/ReadLine.pm +++ b/lib/Reply/Plugin/ReadLine.pm @@ -8,15 +8,26 @@ use File::HomeDir; use File::Spec; use Term::ReadLine; -my $history = File::Spec->catfile(File::HomeDir->my_data, '.reply_history'); - 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 + ); + + if ($self->{term}->ReadLine eq 'Term::ReadLine::Gnu') { + $self->{term}->StifleHistory($opts{history_length}) + if defined $opts{history_length} && $opts{history_length} >= 0; + } - if (open my $fh, '<', $history) { + if (open my $fh, '<', $self->{history_file}) { for my $line (<$fh>) { chomp $line; $self->{term}->addhistory($line); @@ -24,8 +35,8 @@ sub new { } else { my $e = $!; - warn "Couldn't open $history for reading: $e" - if -e $history; + warn "Couldn't open $self->{history_file} for reading: $e" + if -e $self->{history_file}; } return $self; @@ -44,8 +55,8 @@ sub DESTROY { # XXX support more later return unless $self->{term}->ReadLine eq 'Term::ReadLine::Gnu'; - $self->{term}->WriteHistory($history) - or warn "Couldn't write history to $history"; + $self->{term}->WriteHistory($self->{history_file}) + or warn "Couldn't write history to $self->{history_file}"; } 1; diff --git a/lib/Reply/Plugin/ResultCache.pm b/lib/Reply/Plugin/ResultCache.pm index b43eff3..2a11a02 100644 --- a/lib/Reply/Plugin/ResultCache.pm +++ b/lib/Reply/Plugin/ResultCache.pm @@ -6,9 +6,11 @@ use base 'Reply::Plugin'; sub new { my $class = shift; + my %opts = @_; my $self = $class->SUPER::new(@_); $self->{results} = []; + $self->{result_name} = $opts{variable} || 'res'; return $self; } @@ -18,7 +20,7 @@ sub compile { my ($next, $line, %args) = @_; $args{environment} ||= {}; - $args{environment}{'@res'} = $self->{results}; + $args{environment}{'@' . $self->{result_name}} = $self->{results}; $next->($line, %args); } @@ -42,7 +44,8 @@ sub mangle_result { my $self = shift; my ($result) = @_; - return '$res[' . $#{ $self->{results} } . '] = ' . $result; + return '$' . $self->{result_name} . '[' . $#{ $self->{results} } . '] = ' + . $result; } 1; -- cgit v1.2.3-54-g00ecf