summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/CollapseStack.pm
blob: e60c0f582ced2842b78da19e326d9f6e317d7e69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package Reply::Plugin::CollapseStack;
use strict;
use warnings;
# ABSTRACT: display error stack traces only on demand

use base 'Reply::Plugin';

{
    local @SIG{qw(__DIE__ __WARN__)};
    require Carp::Always;
}

=head1 SYNOPSIS

  ; .replyrc
  [CollapseStack]
  num_lines = 1

=head1 DESCRIPTION

This plugin hides stack traces until you specifically request them
with the C<#stack> command.

The number of lines of stack to always show is configurable; specify
the C<num_lines> option.

=cut

sub new {
    my $class = shift;
    my %opts = @_;

    my $self = $class->SUPER::new(@_);
    $self->{num_lines} = $opts{num_lines} || 1;

    return $self;
}

sub compile {
    my $self = shift;
    my ($next, @args) = @_;

    local $SIG{__DIE__} = \&Carp::Always::_die;
    $next->(@args);
}

sub execute {
    my $self = shift;
    my ($next, @args) = @_;

    local $SIG{__DIE__} = \&Carp::Always::_die;
    $next->(@args);
}

sub mangle_error {
    my $self = shift;
    my $error = shift;

    $self->{full_error} = $error;

    my @lines = split /\n/, $error;
    if (@lines > $self->{num_lines}) {
        splice @lines, $self->{num_lines};
        $error = join "\n", @lines, "    (Run #stack to see the full trace)\n";
    }

    return $error;
}

sub command_stack {
    my $self = shift;

    # XXX should use print_error here
    print($self->{full_error} || "No stack to display.\n");

    return '';
}

=for Pod::Coverage
  command_stack

=cut

1;