| 1 | package Reply::Plugin::Nopaste; | ||
| 2 | our $AUTHORITY = 'cpan:DOY'; | ||
| 3 | $Reply::Plugin::Nopaste::VERSION = '0.42'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | # ABSTRACT: command to nopaste a transcript of the current session | ||
| 7 | |||
| 8 | use base 'Reply::Plugin'; | ||
| 9 | |||
| 10 | use App::Nopaste; | ||
| 11 | |||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | my %opts = @_; | ||
| 16 | |||
| 17 | my $self = $class->SUPER::new(@_); | ||
| 18 | $self->{history} = ''; | ||
| 19 | $self->{service} = $opts{service}; | ||
| 20 | |||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub prompt { | ||
| 25 | my $self = shift; | ||
| 26 | my ($next, @args) = @_; | ||
| 27 | my $prompt = $next->(@args); | ||
| 28 | $self->{prompt} = $prompt; | ||
| 29 | return $prompt; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub read_line { | ||
| 33 | my $self = shift; | ||
| 34 | my ($next, @args) = @_; | ||
| 35 | my $line = $next->(@args); | ||
| 36 | $self->{line} = "$line\n" if defined $line; | ||
| 37 | return $line; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub print_error { | ||
| 41 | my $self = shift; | ||
| 42 | my ($next, $error) = @_; | ||
| 43 | $self->{result} = $error; | ||
| 44 | $next->($error); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub print_result { | ||
| 48 | my $self = shift; | ||
| 49 | my ($next, @result) = @_; | ||
| 50 | $self->{result} = @result ? join('', @result) . "\n" : ''; | ||
| 51 | $next->(@result); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub loop { | ||
| 55 | my $self = shift; | ||
| 56 | my ($continue) = @_; | ||
| 57 | |||
| 58 | my $prompt = delete $self->{prompt}; | ||
| 59 | my $line = delete $self->{line}; | ||
| 60 | my $result = delete $self->{result}; | ||
| 61 | |||
| 62 | $self->{history} .= "$prompt$line$result" | ||
| 63 | if defined $prompt | ||
| 64 | && defined $line | ||
| 65 | && defined $result; | ||
| 66 | |||
| 67 | $continue; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub command_nopaste { | ||
| 71 | my $self = shift; | ||
| 72 | my ($line) = @_; | ||
| 73 | |||
| 74 | $line = "Reply session" unless length $line; | ||
| 75 | |||
| 76 | print App::Nopaste->nopaste( | ||
| 77 | text => $self->{history}, | ||
| 78 | desc => $line, | ||
| 79 | lang => 'perl', | ||
| 80 | (defined $self->{service} | ||
| 81 | ? (services => [ $self->{service} ]) | ||
| 82 | : ()), | ||
| 83 | ) . "\n"; | ||
| 84 | |||
| 85 | return ''; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | 1; | ||
| 90 | |||
| 91 | __END__ |