File Coverage

blib/lib/Devel/REPL/Plugin/FancyPrompt.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1 2     2   2194 use strict;
  2         6  
  2         61  
2 2     2   11 use warnings;
  2         4  
  2         121  
3             # ABSTRACT: Facilitate user-defined prompts
4              
5             our $VERSION = '1.003029';
6              
7             use Devel::REPL::Plugin;
8 2     2   10 use namespace::autoclean;
  2         4  
  2         14  
9 2     2   9290  
  2         5  
  2         20  
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              
63             =pod
64              
65             =encoding UTF-8
66              
67             =head1 NAME
68              
69             Devel::REPL::Plugin::FancyPrompt - Facilitate user-defined prompts
70              
71             =head1 VERSION
72              
73             version 1.003029
74              
75             =head1 SYNOPSIS
76              
77             use Devel::REPL;
78              
79             my $repl = Devel::REPL->new;
80             $repl->load_plugin('MultiLine::PPI'); # for indent depth
81             $repl->load_plugin('Packages'); # for current package
82             $repl->load_plugin('FancyPrompt');
83             $repl->run;
84              
85             =head1 DESCRIPTION
86              
87             FancyPrompt helps you write your own prompts. The default fancy prompt resembles
88             C<irb>'s default prompt. The default C<fancy_prompt> looks like this:
89              
90             re.pl(main):001:0> 2 + 2
91             4
92              
93             C<re.pl> is a constant. C<main> is the current package. The first number is how
94             many lines have been read so far. The second number (only if you have a
95             C<MultiLine> plugin) is how deep you are; intuitively, your indent level. This
96             default can be implemented with:
97              
98             $_REPL->fancy_prompt(sub {
99             my $self = shift;
100             sprintf 're.pl(%s):%03d%s> ',
101             $self->can('current_package') ? $self->current_package : 'main',
102             $self->lines_read,
103             $self->can('line_depth') ? ':' . $self->line_depth : '';
104             });
105              
106             C<current_package> is provided by L<Devel::REPL::Plugin::Packages> (which
107             tracks the current package). C<line_depth> is provided by a C<MultiLine> plugin
108             (probably C<MultiLine::PPI>).
109              
110             You may also set a C<fancy_continuation_prompt>. The default is very similar to
111             C<fancy_prompt>'s default (except C<*> instead of C<< > >>).
112              
113             =head1 SEE ALSO
114              
115             C<Devel::REPL>
116              
117             =head1 SUPPORT
118              
119             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Devel-REPL>
120             (or L<bug-Devel-REPL@rt.cpan.org|mailto:bug-Devel-REPL@rt.cpan.org>).
121              
122             There is also an irc channel available for users of this distribution, at
123             L<C<#devel> on C<irc.perl.org>|irc://irc.perl.org/#devel-repl>.
124              
125             =head1 AUTHOR
126              
127             Shawn M Moore, C<< <sartak at gmail dot com> >>
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             Copyright (C) 2007 by Shawn M Moore
132              
133             This library is free software; you can redistribute it and/or modify
134             it under the same terms as Perl itself.
135              
136             =cut