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