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