Add [Read|Write]History for Term::ReadLine::Perl
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Nopaste.pm
CommitLineData
24cc824f 1package Devel::REPL::Plugin::Nopaste;
2
6a5409bc 3use Devel::REPL::Plugin;
24cc824f 4use MooseX::AttributeHelpers;
76cd9acd 5use Moose::Util::TypeConstraints;
24cc824f 6use namespace::clean -except => [ 'meta' ];
a478012d 7use Scalar::Util qw(blessed);
24cc824f 8
3a400715 9sub BEFORE_PLUGIN {
10 my $self = shift;
11 $self->load_plugin('Turtles');
12}
24cc824f 13
14has complete_session => (
15 metaclass => 'String',
16 is => 'rw',
17 isa => 'Str',
8995c74b 18 lazy => 1,
24cc824f 19 default => '',
20 provides => {
21 append => 'add_to_session',
22 },
23);
24
da4881b1 25has paste_title => (
da4881b1 26 is => 'rw',
27 isa => 'Str',
28 lazy => 1,
29 default => 'Devel::REPL session',
30);
31
76cd9acd 32has 'nopaste_format' => (
33 is => 'rw',
34 isa => enum( [qw[ comment_code comment_output ]] ),
35 lazy => 1,
36 default => 'comment_output',
37);
38
a4c582b6 39before eval => sub {
24cc824f 40 my $self = shift;
41 my $line = shift;
42
76cd9acd 43 if ( $self->nopaste_format() eq 'comment_code' ) {
44 # prepend each line with #
45 $line =~ s/^/# /mg;
46 }
24cc824f 47
a4c582b6 48 $self->add_to_session($line . "\n");
49};
50
51around eval => sub {
52 my $orig = shift;
53 my $self = shift;
54 my $line = shift;
55
56 my @ret = $orig->($self, $line, @_);
a478012d 57 my @ret_as_str = map {
58 if (!defined($_)) {
59 '';
60 } elsif (blessed($_) && $_->can('stringify')) {
61 $_->stringify();
62 } else {
63 $_;
64 }
65 } @ret;
66
76cd9acd 67 if ( $self->nopaste_format() eq 'comment_output' ) {
68 # prepend each line with #
69 map { $_ =~ s/^/# /mg } @ret_as_str;
70 }
71
a478012d 72 $self->add_to_session(join("\n", @ret_as_str) . "\n\n");
24cc824f 73
74 return @ret;
75};
76
77sub command_nopaste {
78 my $self = shift;
79
80 require App::Nopaste;
81 return App::Nopaste->nopaste(
82 text => $self->complete_session,
da4881b1 83 desc => $self->paste_title(),
24cc824f 84 lang => "perl",
85 );
86}
87
da4881b1 88sub command_pastetitle {
89 my ( $self, undef, $title ) = @_;
90
91 $self->paste_title( $title );
92}
93
24cc824f 941;
95
cfd1094b 96__END__
97
98=head1 NAME
99
100Devel::REPL::Plugin::Nopaste - #nopaste to upload session's input and output
101
da4881b1 102=head1 COMMANDS
103
104This module provides these commands to your Devel::REPL shell:
105
106=head2 #nopaste
107
108The C<#nopaste> sends a transcript of your session to a nopaste
109site.
110
111=head2 #pastetitle
112
113The C<#pastetitle> command allows you to set the title of the paste on
114the nopaste site. For example:
115
116C<#pastetitle example of some code>
117
118defaults to 'Devel::REPL session'
119
76cd9acd 120=head1 CONFIGURATION
121
122=head2 nopaste_format
123
124The format sent to the nopaste server can be adjusted with the
125C<nopaste_format> option. By default, the output of each perl
126statement is commented out, and the perl statements themselves are
127not. This can be reversed by setting the C<nopaste_format> attribute
128to C<comment_code> like this in your re.pl file:
129
130C<< $_REPL->nopaste_format( 'comment_code' ); >>
131
132The default of commenting out the output would be set like this:
133
134C<< $_REPL->nopaste_format( 'comment_output' ); >>
135
136These options can be set during a Devel::REPL session, but only affect
137the future parts of the session, not the past parts.
138
30b459d4 139=head1 AUTHOR
140
141Shawn M Moore, C<< <sartak at gmail dot com> >>
142
da4881b1 143=head1 CONTRIBUTORS
144
145=over 4
146
147=item Andrew Moore - C<< <amoore@cpan.org> >>
148
149=back
150
cfd1094b 151=cut
152