keep $VERSION right in the repo
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / FancyPrompt.pm
CommitLineData
1716b200 1use strict;
2use warnings;
b21e7551 3package Devel::REPL::Plugin::FancyPrompt;
4
54beb05d 5our $VERSION = '1.003027';
6
6a5409bc 7use Devel::REPL::Plugin;
aa8b7647 8use namespace::autoclean;
b21e7551 9
10has '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
25has '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
40has 'lines_read' => (
41 is => 'rw', lazy => 1, default => 0,
42);
43
44around '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
55before 'read' => sub {
56 my $self = shift;
57 $self->lines_read($self->lines_read + 1);
58};
59
601;
61
62__END__
63
64=head1 NAME
65
66Devel::REPL::Plugin::FancyPrompt - facilitate user-defined prompts
67
68=head1 SYNOPSIS
69
b21e7551 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
80FancyPrompt helps you write your own prompts. The default fancy prompt resembles
81C<irb>'s default prompt. The default C<fancy_prompt> looks like this:
82
83 re.pl(main):001:0> 2 + 2
84 4
85
86C<re.pl> is a constant. C<main> is the current package. The first number is how
87many lines have been read so far. The second number (only if you have a
88C<MultiLine> plugin) is how deep you are; intuitively, your indent level. This
89default 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
99C<current_package> is provided by L<Devel::REPL::Plugin::Packages> (which
100tracks the current package). C<line_depth> is provided by a C<MultiLine> plugin
101(probably C<MultiLine::PPI>).
102
103You may also set a C<fancy_continuation_prompt>. The default is very similar to
104C<fancy_prompt>'s default (except C<*> instead of C<< > >>).
105
106=head1 SEE ALSO
107
108C<Devel::REPL>
109
110=head1 AUTHOR
111
112Shawn M Moore, C<< <sartak at gmail dot com> >>
113
114=head1 COPYRIGHT AND LICENSE
115
116Copyright (C) 2007 by Shawn M Moore
117
118This library is free software; you can redistribute it and/or modify
119it under the same terms as Perl itself.
120
121=cut
122