File Coverage

blib/lib/Devel/GDB/Reflect/PrettyPrinter.pm
Criterion Covered Total %
statement 6 66 9.0
branch 0 36 0.0
condition n/a
subroutine 2 11 18.1
pod 0 5 0.0
total 8 118 6.7


line stmt bran cond sub pod time code
1             package Devel::GDB::Reflect::PrettyPrinter;
2              
3 1     1   6 use warnings;
  1         2  
  1         45  
4 1     1   6 use strict;
  1         1  
  1         1137  
5              
6             our $PAD = " " x 4;
7              
8             sub new($;$$$$)
9             {
10 0     0 0   my $class = shift;
11 0           my ($parent, $open_brace, $separator, $close_brace) = @_;
12              
13 0 0         defined($open_brace) or $open_brace = "[";
14 0 0         defined($separator) or $separator = ",";
15 0 0         defined($close_brace) or $close_brace = "]";
16              
17             # Add some space around the separator: ensure there's a space before it
18             # (unless it's a comma or semicolon), and a space after it
19 0           $separator =~ s/^([^\s,;])/ $1/;
20 0           $separator =~ s/([^\s])$/$1 /;
21              
22             my $self = bless
23             {
24             q => [],
25             first => 1,
26 0           fh => \do { local *FH },
  0            
27             open_brace => $open_brace,
28             close_brace => $close_brace,
29             separator => $separator,
30             };
31              
32 0 0         if(defined $parent)
33             {
34 0           $self->{parent} = $parent;
35             }
36             else
37             {
38 0     0     $self->{raw_print} = sub { local $\ = local $, = ""; print @_ };
  0            
  0            
39             }
40              
41 0           tie *{$self->{fh}}, $class, $self;
  0            
42              
43 0           return $self;
44             }
45              
46             sub flush_queue
47             {
48 0     0 0   my $self = shift;
49              
50 0 0         if($self->{q})
51             {
52 0           $self->{parent}->push("");
53 0 0         $self->{parent}->print("$self->{open_brace}\n${PAD}")
54             if $self->{open_brace} =~ /\S/;
55              
56 0           foreach(@{$self->{q}})
  0            
57             {
58 0 0         $self->{parent}->print("$self->{separator}\n${PAD}") unless $self->{first};
59 0           $self->{parent}->print($_);
60 0           $self->{first} = 0;
61             }
62             }
63              
64 0           undef $self->{q};
65             }
66              
67             sub push
68             {
69 0     0 0   my $self = shift;
70 0           my ($text) = @_;
71              
72 0 0         return $self->{raw_print}->($text) if defined($self->{raw_print});
73              
74 0 0         if($text =~ /\n/)
75             {
76 0           $self->flush_queue();
77             }
78              
79 0 0         if(defined $self->{q})
80             {
81 0           push @{$self->{q}}, @_;
  0            
82             }
83             else
84             {
85 0           $text =~ s/\n+$//;
86 0           $text =~ s/\n/\n${PAD}/g;
87 0 0         $self->{parent}->print("$self->{separator}\n${PAD}") unless $self->{first};
88 0           $self->{parent}->print($text);
89              
90 0           $self->{first} = 0;
91             }
92             }
93              
94             sub print
95             {
96 0     0 0   my $self = shift;
97 0           my ($text) = @_;
98              
99 0 0         return $self->{raw_print}->($text) if defined($self->{raw_print});
100              
101 0           $self->flush_queue();
102              
103 0           $text =~ s/\n/\n${PAD}/g;
104 0           $self->{parent}->print($text);
105             }
106              
107             sub finish($;$)
108             {
109 0     0 0   my $self = shift;
110 0           my ($with_newline) = @_;
111              
112 0 0         return if defined($self->{raw_print});
113              
114 0 0         my $eol = $with_newline ? "\n" : "";
115              
116 0 0         if(defined $self->{q})
117             {
118             # Add space between open_brace, queue contents, and close_brace,
119             # but only one space if the queue is empty. No space if neither
120             # open_brace nor close_brace contain any printing characters.
121              
122 0 0         my $sp = ("$self->{open_brace}$self->{close_brace}" =~ /\S/) ? " " : "";
123 0 0         my $inner = @{$self->{q}} ? ($sp . join($self->{separator}, @{$self->{q}}) . $sp) : $sp;
  0            
  0            
124              
125 0           $self->{parent}->push($self->{open_brace} . $inner . $self->{close_brace} . $eol);
126             }
127             else
128             {
129 0 0         $self->{parent}->print("\n$self->{close_brace}")
130             if $self->{close_brace} =~ /\S/;
131             }
132             };
133              
134             # Tiehandle interface
135 0     0     sub TIEHANDLE { $_[1] }
136 0     0     sub PRINT { shift->push(@_) }
137 0     0     sub PRINTF { shift->push(sprintf(shift, @_)) }
138              
139             1