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