| 1 | #-*- Mode: CPerl -*- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ## File: DDC::Format::Text.pm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | ## Description: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | ## + DDC Query utilities: output formatting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | package DDC::Format::Text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 26 | ▶ | use Text::Wrap qw(wrap); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 26 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 26 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | ## Globals | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 26 | our @ISA = qw(DDC::Format); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 26 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 26 | *isa = \&UNIVERSAL::isa; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ## Constructors, etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ## $fmt = $CLASS_OR_OBJ->new(%args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ## + %args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ## ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | ## start=>$previous_hit_num, ##-- pre-initial hit number (default=0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ## highlight=>[$pre,$post], ##-- highlighting substrings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ## columns=>$ncols, ##-- for text wrapping [default=80] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ## useMatchIds=>$bool, ##-- whether to use match-ids if available; undef (default) if non-trivial match-ids are specified | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ## wsAttr=>$attr, ##-- token attribute to use for preceding whitespace (default='ws') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ## ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my $that = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | highlight=>['__','__'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | columns=>80, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | useMatchIds=>undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | wsAttr=>'ws', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | @_ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | }, ref($that)||$that; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ## $fmt = $fmt->reset() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | ## + reset counters, etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub reset { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $_[0]{start}=0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | return $_[0]->SUPER::reset(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ## Helper functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ## $wsStr = $fmt->wsStr($token) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub wsStr { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wsStr: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my ($fmt,$w) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | return (!$fmt->{wsAttr} || !ref($w) || !defined($w->{$fmt->{wsAttr}}) || $w->{$fmt->{wsAttr}} ? ' ' : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
not $fmt->{"wsAttr"} && ref $w && defined $$w{$fmt->{"wsAttr"}} or $$w{$fmt->{"wsAttr"}}
$fmt->{'wsAttr'} && ref $w && defined $$w{$fmt->{'wsAttr'}}
$fmt->{"wsAttr"} and ref $w
MC/DC0%0/4
not $fmt->{"wsAttr"} && ref $w && defined $$w{$fmt->{"wsAttr"}} or $$w{$fmt->{"wsAttr"}}
$fmt->{"wsAttr"}
ref $w
defined $$w{$fmt->{'wsAttr'}}
$$w{$fmt->{"wsAttr"}}
Truth table0%0/5
not(A and B && C) or D
A= $fmt->{"wsAttr"}B= ref $wC= defined $$w{$fmt->{'wsAttr'}}D= $$w{$fmt->{"wsAttr"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ## $hitStr = $fmt->hitString($hit, $fieldName, $hitNumber, $useMatchIds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub hitString { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hitString: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my ($fmt,$hit,$fkey,$hnum,$useMatchIds) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $fkey = 'w' if (!defined($fkey)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $hnum = 0 if (!$hnum); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $Text::Wrap::columns = $fmt->{columns}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $ctx = $hit->{ctx_}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | my $ctxstr = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | join(' ', map {ref($_) ? $_->{$fkey} : $_} @{$ctx->[0]}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | .' ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | .join('', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $fmt->wsStr($_).($_->{hl_} ? "__$_->{$fkey}__".($useMatchIds ? "/$_->{hl_}" : '') : $_->{$fkey}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | } @{$ctx->[1]} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | .' ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | .join(' ', map {ref($_) ? $_->{$fkey} : $_} @{$ctx->[2]}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $ctxstr =~ s{(?:^\s+)|(?:\s*$)}{}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | return ("${hnum}: " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | .wrap('',(' ' x length("$hnum")).' ', $ctxstr)."\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | .join('', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | map { wrap("\t+ ", ("\t ".(' ' x length($_)).' '), "$_=\"$hit->{meta_}{$_}\"")."\n" } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | grep {$_ ne 'indices_' && defined($hit->{meta_}{$_})} sort keys %{$hit->{meta_}||{}}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$_ ne 'indices_' && defined $hit->{'meta_'}{$_}
$hit->{'meta_'} || {}
MC/DC0%0/2
$_ ne 'indices_' && defined $hit->{'meta_'}{$_}
$_ ne 'indices_'
defined $hit->{'meta_'}{$_}
MC/DC0%0/1
$hit->{'meta_'} || {}
$hit->{'meta_'}
Truth table0%0/3
A && B
A= $_ ne 'indices_'B= defined $hit->{'meta_'}{$_}
Truth table0%0/2
A
A= $hit->{'meta_'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | #."\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ## API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | ## $str = $fmt->toString($hitList) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub toString { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
toString: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my ($fmt,$hits) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($hits->{counts_} && @{$hits->{counts_}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$hits->{"counts_"} and @{$hits->{"counts_"};}
MC/DC0%0/2
$hits->{"counts_"} and @{$hits->{"counts_"};}
$hits->{"counts_"}
@{$hits->{"counts_"};}
Truth table0%0/3
A and B
A= $hits->{"counts_"}B= @{$hits->{"counts_"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | ##-- count-query: return tab-separated strings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | return join('', map {join("\t", @$_)."\n"} @{$hits->{counts_}}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | elsif ($hits->{hits_} && @{$hits->{hits_}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$hits->{"hits_"} and @{$hits->{"hits_"};}
MC/DC0%0/2
$hits->{"hits_"} and @{$hits->{"hits_"};}
$hits->{"hits_"}
@{$hits->{"hits_"};}
Truth table0%0/3
A and B
A= $hits->{"hits_"}B= @{$hits->{"hits_"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ##-- usual case: retrieve hit strings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my $useMatchIds = defined($fmt->{useMatchIds}) ? $fmt->{useMatchIds} : (grep {$_>0 && $_!=1} map {$_->{hl_}} map {@{$_->{ctx_}[1]}} @{$hits->{hits_}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_ > 0 && $_ != 1
MC/DC0%0/2
$_ > 0 && $_ != 1
$_ > 0
$_ != 1
Truth table0%0/3
A && B
A= $_ > 0B= $_ != 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | return join("\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $fmt->hitString($hits->{hits_}[$_], $hits->{defaultField}, $_+$hits->{start}, $useMatchIds) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | } (0..$#{$hits->{hits_}}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | ##-- unknown: return empty string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return "(no hits)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 26 | 1; ##-- be happy | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||