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