blib/lib/Zoidberg/Utils/Output.pm

filter: Statementstmt 38.6% 58 / 150 Branchbran 25.7% 17 / 66 Conditioncond 30.3% 10 / 33 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 66.6% 12 / 18 Podpod 75.0% 6 / 8 totaltotal 35.3% 104 / 294 CC 54 SCAR 44.5
CC
54
Cov
34%
output_sql
240.0
typed_output
86.3
output_list
72.0
CRAP 85.3
« blib/lib/Zoidberg.pm ↑ Summary blib/lib/Zoidberg/Fish/ReadLine.pm »
1  
2 package Zoidberg::Utils::Output;
3  
4 67 our $VERSION = '0.981';
5  
6 67 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 67 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 67 ▶use POSIX qw/floor ceil/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use Exporter::Tidy
10 67 ▶ default => [qw/output message debug complain/],
Subroutine BEGIN: called CC=1 SCAR=0.0
11 67 other => [qw/typed_output output_is_captured/];
12  
13 67 our %colours = ( # Copied from Term::ANSIScreen
14 'clear' => 0, 'reset' => 0,
15 'bold' => 1, 'dark' => 2,
16 'underline' => 4, 'underscore' => 4,
17 'blink' => 5, 'reverse' => 7,
18 'concealed' => 8,
19  
20 'black' => 30, 'on_black' => 40,
21 'red' => 31, 'on_red' => 41,
22 'green' => 32, 'on_green' => 42,
23 'yellow' => 33, 'on_yellow' => 43,
24 'blue' => 34, 'on_blue' => 44,
25 'magenta' => 35, 'on_magenta' => 45,
26 'cyan' => 36, 'on_cyan' => 46,
27 'white' => 37, 'on_white' => 47,
28 );
29  
30 0 ▶sub output_is_captured {
Subroutine output_is_captured: not called CC=2 SCAR=17.9
Pod documented
31 0 ▶ return $Zoidberg::CURRENT->{_builtin_output} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Zoidberg::CURRENT->{'_builtin_output'} ? : 0 0 0
32 }
33  
34 12 ▶sub output {
Subroutine output: called CC=2 SCAR=7.4
Pod documented
35 12 ▶ if ($Zoidberg::CURRENT->{_builtin_output}) { # capturing output from builtin
Branch: 1 branch
BranchTrueFalseTotal
if ($Zoidberg::CURRENT->{'_builtin_output'}) { } 12 0 12
36 12 push @{ $Zoidberg::CURRENT->{_builtin_output} }, @_;
37 12 return 1;
38 }
39 0 else { typed_output('output', @_) }
40 }
41  
42 0 ▶sub message {
Subroutine message: not called CC=2 SCAR=17.9
Pod documented
43 0 ▶ return 1 if ! $Zoidberg::CURRENT->{settings}{interactive};
Branch: 1 branch
BranchTrueFalseTotal
unless $Zoidberg::CURRENT->{"settings"}{"interactive"} 0 0 0
44 0 typed_output('message', @_);
45 }
46  
47 58190 ▶sub debug {
Subroutine debug: called CC=3 SCAR=14.2
Pod documented
48 58190 my $class = caller;
49 67 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 #local $Data::Dumper::Maxdepth = 2;
51 58190 ▶ return 1 unless $Zoidberg::CURRENT->{settings}{debug} || ${$class.'::DEBUG'};
Branch: 1 branch
BranchTrueFalseTotal
unless $Zoidberg::CURRENT->{"settings"}{"debug"} or ${$class . '::DEBUG';} 58190 0 58190
Condition33%1/3
$Zoidberg::CURRENT->{"settings"}{"debug"} or ${$class . '::DEBUG';}
l!l&&r!l&&!r
0058190
MC/DC0%0/2
$Zoidberg::CURRENT->{"settings"}{"debug"} or ${$class . '::DEBUG';}
$Zoidberg::CURRENT->{"settings"}{"debug"} ${$class . '::DEBUG';}
Truth table33%1/3
A or B
A= $Zoidberg::CURRENT->{"settings"}{"debug"}B= ${$class . '::DEBUG';}
ABresult
1X1
011
000
52 0 my $fh = select STDERR;
53 0 my @caller = caller;
54 0 typed_output('debug', "$caller[0]: $caller[2]: ", @_);
55 0 select $fh;
56 0 1;
57 }
58  
59 12 ▶sub complain { # strip @INC: for little less verbose output
Subroutine complain: called CC=4 SCAR=14.6
Pod documented
60 12 ▶ return 0 unless @_ || $@;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ or $@ 0 12 12
Condition33%1/3
@_ or $@
l!l&&r!l&&!r
0012
MC/DC0%0/2
@_ or $@
@_ $@
Truth table33%1/3
A or B
A= @_B= $@
ABresult
1X1
011
000
61 12 ▶ my @error = @_ ? (@_) : ($@);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 12 12
62 12 my $fh = select STDERR;
63 12 typed_output('error', map {s/\(\@INC contains\: (.*?)\)\s*//g; $_} @error);
64 12 select $fh;
65 12 1;
66 }
67  
68 12 ▶sub typed_output {
Subroutine typed_output: called CC=20 SCAR=44.6
Pod documented
69 12 my $type = shift;
70 12 my @dinge = @_;
71 12 ▶ return unless @dinge > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @dinge > 0 0 12 12
72  
73 12 ▶ $type = $Zoidberg::CURRENT->{settings}{output}{$type} || $type;
Condition33%1/3
$Zoidberg::CURRENT->{'settings'}{'output'}{$type} || $type
l!l&&r!l&&!r
1200
MC/DC0%0/2
$Zoidberg::CURRENT->{'settings'}{'output'}{$type} || $type
$Zoidberg::CURRENT->{'settings'}{'output'}{$type} $type
Truth table33%1/3
A || B
A= $Zoidberg::CURRENT->{'settings'}{'output'}{$type}B= $type
ABresult
1X1
011
000
74 12 ▶ return 1 if $type eq 'mute';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "mute" 0 12 12
75  
76 12 my $coloured;
77 print "\e[$colours{$type}m" and $coloured = 1
78 if exists $colours{$type}
79 12 ▶ and $Zoidberg::CURRENT->{settings}{interactive} and $ENV{CLICOLOR};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Zoidberg::Utils::Output::colours{$type} and $Zoidberg::CURRENT->{"settings"}{"interactive"} and $ENV{"CLICOLOR"} 0 12 12
Condition25%3/12
exists $Zoidberg::Utils::Output::colours{$type} && $Zoidberg::CURRENT->{'settings'}{'interactive'} && $ENV{'CLICOLOR'} && (print("\e[$Zoidberg::Utils::Output::colours{$type}m") && ($coloured = 1))
!ll&&!rl&&r
1200
exists $Zoidberg::Utils::Output::colours{$type} and $Zoidberg::CURRENT->{"settings"}{"interactive"} and $ENV{"CLICOLOR"}
!ll&&!rl&&r
1200
exists $Zoidberg::Utils::Output::colours{$type} and $Zoidberg::CURRENT->{"settings"}{"interactive"}
!ll&&!rl&&r
0120
print "\e[$Zoidberg::Utils::Output::colours{$type}m" and $coloured = 1
!ll&&!rl&&r
000
MC/DC0%0/5
exists $Zoidberg::Utils::Output::colours{$type} && $Zoidberg::CURRENT->{'settings'}{'interactive'} && $ENV{'CLICOLOR'} && (print("\e[$Zoidberg::Utils::Output::colours{$type}m") && ($coloured = 1))
exists $Zoidberg::Utils::Output::colours{$type} $Zoidberg::CURRENT->{"settings"}{"interactive"} $ENV{"CLICOLOR"} print "\e[$Zoidberg::Utils::Output::colours{$type}m" $coloured = 1
Truth table16%1/6
A and B and C && D and E
A= exists $Zoidberg::Utils::Output::colours{$type}B= $Zoidberg::CURRENT->{"settings"}{"interactive"}C= $ENV{"CLICOLOR"}D= print "\e[$Zoidberg::Utils::Output::colours{$type}m"E= $coloured = 1
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
80  
81 12 ▶ $dinge[-1] .= "\n" unless ref $dinge[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dinge[-1] 0 12 12
82 12 for (@dinge) {
83 12 ▶ $_ = $_->scalar() if ref($_) eq 'Zoidberg::Utils::Output::Scalar';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "Zoidberg::Utils::Output::Scalar" 0 12 12
84 0 ▶ unless (ref $_) { print $_ }
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_) { } 12 0 12
if (do { ref $_ eq 'ARRAY' and not grep({ref $_;} @$_) }) { } 0 12 12
elsif (ref $_ eq 'Zoidberg::Utils::Error') { } 12 0 12
elsif (ref($_) =~ /Zoidberg/) { } 0 0 0
85 0 ▶ elsif (ref($_) eq 'ARRAY' and ! grep { ref($_) } @$_) { output_list(@$_) }
Condition33%1/3
ref $_ eq "ARRAY" and not grep({ref $_;} @$_)
!ll&&!rl&&r
1200
MC/DC0%0/2
ref $_ eq "ARRAY" and not grep({ref $_;} @$_)
ref $_ eq "ARRAY" not grep({ref $_;} @$_)
Truth table33%1/3
A and not(B)
A= ref $_ eq "ARRAY"B= not grep({ref $_;} @$_)
ABresult
0X0
100
111
86 elsif (ref($_) eq 'Zoidberg::Utils::Error') {
87 0 ▶ if ($$_{debug}) { print map {s/^\$VAR1 = //; $_} Dumper $_ }
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'debug'}) { } 0 12 12
88 else {
89 12 ▶ next if $$_{silent} || $$_{printed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"silent"} or $_->{"printed"}++ 12 0 12
Condition33%1/3
$_->{"silent"} or $_->{"printed"}++
l!l&&r!l&&!r
1200
MC/DC0%0/2
$_->{"silent"} or $_->{"printed"}++
$_->{"silent"} $_->{"printed"}++
Truth table33%1/3
A or B
A= $_->{"silent"}B= $_->{"printed"}++
ABresult
1X1
011
000
90 0 print $_->stringify(format => 'gnu');
91 }
92 }
93 elsif (ref($_) =~ /Zoidberg/) {
94 0 complain 'Cowardly refusing to dump object of class '.ref($_);
95 }
96 0 else { print map {s/^\$VAR1 = //; $_} Dumper $_ }
97 }
98  
99 12 ▶ print "\e[$colours{reset}m" if $coloured;
Branch: 1 branch
BranchTrueFalseTotal
if $coloured 0 12 12
100
101 12 1;
102 }
103  
104 0 ▶sub output_list { # takes minimum number of rows, but fills cols first
Subroutine output_list: not called CC=8 SCAR=42.8
Pod undocumented
105 0 my (@items) = @_;
106 0 my $width = $ENV{COLUMNS};
107  
108 0 ▶ return print join("\n", @items), "\n" unless $Zoidberg::CURRENT->{settings}{interactive};
Branch: 1 branch
BranchTrueFalseTotal
unless $Zoidberg::CURRENT->{"settings"}{"interactive"} 0 0 0
109  
110 0 my $len = 0;
111 0 ▶ $_ > $len and $len = $_ for map {s/\t/ /g; length $_} @items;
Branch: 1 branch
BranchTrueFalseTotal
$_ > $len and $len = $_ 0 0 0
112 0 $len += 2; # spacing
113 0 ▶ return print join("\n", @items), "\n" if $width < (2 * $len); # rows == items
Branch: 1 branch
BranchTrueFalseTotal
if $width < 2 * $len 0 0 0
114 0 ▶ return print join(' ', @items), "\n" if $width > (@items * $len); # 1 row
Branch: 1 branch
BranchTrueFalseTotal
if $width > @items * $len 0 0 0
115  
116 0 my $cols = int($width / $len ) - 1; # 0 based
117 0 my $rows = int(@items / ($cols+1)); # 0 based ceil
118 0 ▶ $rows -= 1 unless @items % ($cols+1); # tune ceil
Branch: 1 branch
BranchTrueFalseTotal
unless @items % ($cols + 1) 0 0 0
119 0 my @rows;
120 0 for my $r (0 .. $rows) {
121 0 my @row = map { $items[ ($_ * ($rows+1)) + $r] } 0 .. $cols;
122 0 push @rows, join '', map { $_ .= ' 'x($len - length $_) } @row;
123 }
124 #print STDERR scalar(@items)." items, $len long, $width width, $cols+1 cols, $rows+1 rows\n";
125 0 print join("\n", @rows), "\n";
126 }
127  
128 0 ▶sub output_sql { # kan vast schoner
Subroutine output_sql: not called CC=15 SCAR=54.8
Pod undocumented
129 0 ▶ shift unless ref($_[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 0 0
130 0 my $width = $ENV{COLUMNS};
131 0 ▶ if (! $Zoidberg::CURRENT->{settings}{interactive} || !defined $width) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Zoidberg::CURRENT->{"settings"}{"interactive"} and defined $width) 0 0 0
Condition0%0/3
$Zoidberg::CURRENT->{"settings"}{"interactive"} and defined $width
!ll&&!rl&&r
000
MC/DC0%0/2
$Zoidberg::CURRENT->{"settings"}{"interactive"} and defined $width
$Zoidberg::CURRENT->{"settings"}{"interactive"} defined $width
Truth table0%0/3
A and B
A= $Zoidberg::CURRENT->{"settings"}{"interactive"}B= defined $width
ABresult
0X0
100
111
132 0 return (print join("\n", map {join(', ', @{$_})} @_)."\n");
133 }
134 0 my @records = @_;
135 0 my @longest = ();
136 0 @records = map {[map {s/\'/\\\'/g; "'".$_."'"} @{$_}]} @records; # escape quotes + safety quotes
137 0 foreach my $i (0..$#{$records[0]}) {
138 0 ▶ map {if (length($_) > $longest[$i]) {$longest[$i] = length($_);} } map {$_->[$i]} @records;
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $longest[$i]) 0 0 0
139 }
140 #print "debug: records: ".Dumper(\@records)." longest: ".Dumper(\@longest);
141 0 my $record_length = 0; # '[' + ']' - ', '
142 0 for (@longest) { $record_length += $_ + 2; } # length (', ') = 2
143 0 ▶ if ($record_length <= $width) { # it fits ! => horizontal lay-out
Branch: 1 branch
BranchTrueFalseTotal
if ($record_length <= $width) { } 0 0 0
144 0 my $cols = floor($width / ($record_length+2)); # we want two spaces to saperate coloms
145 0 my @strings = ();
146 0 for (@records) {
147 0 my @record = @{$_};
148 0 for (0..$#record-1) { $record[$_] .= ', '.(' 'x($longest[$_] - length($record[$_]))); }
149 0 $record[$#record] .= (' 'x($longest[$#record] - length($record[$#record])));
150 0 ▶ if ($cols > 1) { push @strings, "[".join('', @record)."]"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cols > 1) { } 0 0 0
151 0 else { print "[".join('', @record)."]\n"; }
152 }
153 0 ▶ if ($cols > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols > 1) 0 0 0
154 0 my $rows = ceil(($#strings+1) / $cols);
155 0 foreach my $i (0..$rows-1) {
156 0 for (0..$cols) { print $strings[$_*$rows+$i]." "; }
157 0 print "\n";
158 }
159 }
160 }
161 0 else { for (@records) { print "[\n ".join(",\n ", @{$_})."\n]\n"; } } # vertical lay-out
162 0 return 1;
163 }
164  
165 package Zoidberg::Utils::Output::Scalar;
166  
167 67 our $VERSION = '0.981';
168  
169 use overload
170 67 ▶ '""' => \&scalar,
Subroutine BEGIN: called CC=1 SCAR=0.0
171 'bool' => \&error,
172 '@{}' => \&array,
173 67 fallback => 'TRUE';
174  
175 192 ▶sub new { bless \[@_[1,2,3]], $_[0] }
Subroutine new: called CC=1 SCAR=0.0
176  
177 0 ▶sub error { my $s = ${ shift() }; $$s[0] }
Subroutine error: not called CC=1 SCAR=6.9
178  
179 30 ▶sub scalar {
Subroutine scalar: called CC=3 SCAR=11.0
180 30 my $s = ${ shift() };
181 30 ▶ $$s[1] = join "\n", @{$$s[2]} if ! defined $$s[1] and $$s[2];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $s->[1] and $s->[2] 7 23 30
Condition66%2/3
not defined $s->[1] and $s->[2]
!ll&&!rl&&r
2307
MC/DC50%1/2
not defined $s->[1] and $s->[2]
not defined $s->[1] $s->[2]
Truth table66%2/3
not(A) and B
A= not defined $s->[1]B= $s->[2]
ABresult
0X0
100
111
182 30 return $$s[1];
183 }
184  
185 0 ▶sub array {
Subroutine array: not called CC=4 SCAR=30.0
186 0 my $s = ${ shift() };
187 0 ▶ if (! defined $$s[2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s->[2]) 0 0 0
188 0 ▶ $$s[2] = (ref($$s[1]) eq 'ARRAY') ? $$s[1] :
Branch: 2 branches
BranchTrueFalseTotal
ref $s->[1] eq 'ARRAY' ? : 0 0 0
ref $s->[1] ? : 0 0 0
189 ref($$s[1]) ? [$$s[1]] : [ split /\n/, $$s[1] ];
190 }
191 0 return $$s[2];
192 }
193  
194  
195 67 1;
196  
197 __END__
« blib/lib/Zoidberg.pm ↑ Summary blib/lib/Zoidberg/Fish/ReadLine.pm »