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