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