f6e98f57b0ef330ec982259761b947534121c690
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / FancyPrompt.pm
1 use strict;
2 use warnings;
3 package Devel::REPL::Plugin::FancyPrompt;
4
5 our $VERSION = '1.003027';
6
7 use Devel::REPL::Plugin;
8 use namespace::autoclean;
9
10 has 'fancy_prompt' => (
11   is => 'rw', lazy => 1,
12
13   # yes, this needs to be a double sub
14   default => sub {
15     sub {
16       my $self = shift;
17       sprintf 're.pl(%s):%03d%s> ',
18               $self->can('current_package') ? $self->current_package : 'main',
19               $self->lines_read,
20               $self->can('line_depth') ? ':' . $self->line_depth : '';
21     }
22   },
23 );
24
25 has 'fancy_continuation_prompt' => (
26   is => 'rw', lazy => 1,
27
28   # yes, this needs to be a double sub
29   default => sub {
30     sub {
31       my $self = shift;
32       sprintf 're.pl(%s):%03d:%d* ',
33               $self->can('current_package') ? $self->current_package : 'main',
34               $self->lines_read,
35               $self->line_depth,
36     }
37   },
38 );
39
40 has 'lines_read' => (
41   is => 'rw', lazy => 1, default => 0,
42 );
43
44 around 'prompt' => sub {
45   shift;
46   my $self = shift;
47   if ($self->can('line_depth') && $self->line_depth) {
48     return $self->fancy_continuation_prompt->($self);
49   }
50   else {
51     return $self->fancy_prompt->($self);
52   }
53 };
54
55 before 'read' => sub {
56   my $self = shift;
57   $self->lines_read($self->lines_read + 1);
58 };
59
60 1;
61
62 __END__
63
64 =head1 NAME
65
66 Devel::REPL::Plugin::FancyPrompt - facilitate user-defined prompts
67
68 =head1 SYNOPSIS
69
70     use Devel::REPL;
71
72     my $repl = Devel::REPL->new;
73     $repl->load_plugin('MultiLine::PPI'); # for indent depth
74     $repl->load_plugin('Packages');       # for current package
75     $repl->load_plugin('FancyPrompt');
76     $repl->run;
77
78 =head1 DESCRIPTION
79
80 FancyPrompt helps you write your own prompts. The default fancy prompt resembles
81 C<irb>'s default prompt. The default C<fancy_prompt> looks like this:
82
83     re.pl(main):001:0> 2 + 2
84     4
85
86 C<re.pl> is a constant. C<main> is the current package. The first number is how
87 many lines have been read so far. The second number (only if you have a
88 C<MultiLine> plugin) is how deep you are; intuitively, your indent level. This
89 default can be implemented with:
90
91     $_REPL->fancy_prompt(sub {
92       my $self = shift;
93       sprintf 're.pl(%s):%03d%s> ',
94               $self->can('current_package') ? $self->current_package : 'main',
95               $self->lines_read,
96               $self->can('line_depth') ? ':' . $self->line_depth : '';
97     });
98
99 C<current_package> is provided by L<Devel::REPL::Plugin::Packages> (which
100 tracks the current package). C<line_depth> is provided by a C<MultiLine> plugin
101 (probably C<MultiLine::PPI>).
102
103 You may also set a C<fancy_continuation_prompt>. The default is very similar to
104 C<fancy_prompt>'s default (except C<*> instead of C<< > >>).
105
106 =head1 SEE ALSO
107
108 C<Devel::REPL>
109
110 =head1 AUTHOR
111
112 Shawn M Moore, C<< <sartak at gmail dot com> >>
113
114 =head1 COPYRIGHT AND LICENSE
115
116 Copyright (C) 2007 by Shawn M Moore
117
118 This library is free software; you can redistribute it and/or modify
119 it under the same terms as Perl itself.
120
121 =cut
122