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