Adding #pastetitle command to Nopaste plugin
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Nopaste.pm
1 package Devel::REPL::Plugin::Nopaste;
2
3 use Devel::REPL::Plugin;
4 use MooseX::AttributeHelpers;
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     provides  => {
20         append => 'add_to_session',
21     },
22 );
23
24 has paste_title => (
25     metaclass => 'String',
26     is        => 'rw',
27     isa       => 'Str',
28     lazy      => 1,
29     default   => 'Devel::REPL session',
30 );
31
32 before eval => sub {
33     my $self = shift;
34     my $line = shift;
35
36     # prepend each line with #
37     $line =~ s/^/# /mg;
38
39     $self->add_to_session($line . "\n");
40 };
41
42 around eval => sub {
43     my $orig = shift;
44     my $self = shift;
45     my $line = shift;
46
47     my @ret = $orig->($self, $line, @_);
48     my @ret_as_str = map {
49         if (!defined($_)) {
50             '';
51         } elsif (blessed($_) && $_->can('stringify')) {
52             $_->stringify();
53         } else {
54             $_;
55         }
56     } @ret;
57
58     $self->add_to_session(join("\n", @ret_as_str) . "\n\n");
59
60     return @ret;
61 };
62
63 sub command_nopaste {
64     my $self = shift;
65
66     require App::Nopaste;
67     return App::Nopaste->nopaste(
68         text => $self->complete_session,
69         desc => $self->paste_title(),
70         lang => "perl",
71     );
72 }
73
74 sub command_pastetitle {
75     my ( $self, undef, $title ) = @_;
76
77     $self->paste_title( $title );
78 }
79
80 1;
81
82 __END__
83
84 =head1 NAME
85
86 Devel::REPL::Plugin::Nopaste - #nopaste to upload session's input and output
87
88 =head1 COMMANDS
89
90 This module provides these commands to your Devel::REPL shell:
91
92 =head2 #nopaste
93
94 The C<#nopaste> sends a transcript of your session to a nopaste
95 site.
96
97 =head2 #pastetitle
98
99 The C<#pastetitle> command allows you to set the title of the paste on
100 the nopaste site. For example:
101
102 C<#pastetitle example of some code>
103
104 defaults to 'Devel::REPL session'
105
106 =head1 AUTHOR
107
108 Shawn M Moore, C<< <sartak at gmail dot com> >>
109
110 =head1 CONTRIBUTORS
111
112 =over 4
113
114 =item Andrew Moore - C<< <amoore@cpan.org> >>
115
116 =back
117
118 =cut
119