File Coverage

blib/lib/DDC/Format/Text.pm
Criterion Covered Total %
statement 13 53 24.5
branch 0 26 0.0
condition 0 12 0.0
subroutine 5 10 50.0
pod 4 5 80.0
total 22 106 20.7


line stmt bran cond sub pod time code
1             #-*- Mode: CPerl -*-
2              
3             ## File: DDC::Format::Text.pm
4             ## Author: Bryan Jurish
5             ## Description:
6             ## + DDC Query utilities: output formatting
7             ##======================================================================
8              
9             package DDC::Format::Text;
10 26     26   13428 use Text::Wrap qw(wrap);
  26         72964  
  26         1531  
11 26     26   229 use IO::File;
  26         52  
  26         2795  
12 26     26   161 use Carp;
  26         82  
  26         1159  
13 26     26   155 use strict;
  26         51  
  26         1155  
14              
15             ##======================================================================
16             ## Globals
17             our @ISA = qw(DDC::Format);
18              
19             BEGIN {
20 26     26   20836 *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             sub new {
36 0     0 1   my $that = shift;
37 0   0       return bless {
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             sub reset {
49 0     0 1   $_[0]{start}=0;
50 0           return $_[0]->SUPER::reset();
51             }
52              
53             ##======================================================================
54             ## Helper functions
55              
56             ## $wsStr = $fmt->wsStr($token)
57             sub wsStr {
58 0     0 0   my ($fmt,$w) = @_;
59 0 0 0       return (!$fmt->{wsAttr} || !ref($w) || !defined($w->{$fmt->{wsAttr}}) || $w->{$fmt->{wsAttr}} ? ' ' : '');
60             }
61              
62             ## $hitStr = $fmt->hitString($hit, $fieldName, $hitNumber, $useMatchIds)
63             sub hitString {
64 0     0 1   my ($fmt,$hit,$fkey,$hnum,$useMatchIds) = @_;
65 0 0         $fkey = 'w' if (!defined($fkey));
66 0 0         $hnum = 0 if (!$hnum);
67 0           $Text::Wrap::columns = $fmt->{columns};
68 0           my $ctx = $hit->{ctx_};
69             my $ctxstr = (
70 0 0         join(' ', map {ref($_) ? $_->{$fkey} : $_} @{$ctx->[0]})
  0            
71             .' '
72             .join('',
73             map {
74 0 0         $fmt->wsStr($_).($_->{hl_} ? "__$_->{$fkey}__".($useMatchIds ? "/$_->{hl_}" : '') : $_->{$fkey})
    0          
75 0           } @{$ctx->[1]}
76             )
77             .' '
78 0 0         .join(' ', map {ref($_) ? $_->{$fkey} : $_} @{$ctx->[2]})
  0            
  0            
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 0         grep {$_ ne 'indices_' && defined($hit->{meta_}{$_})} sort keys %{$hit->{meta_}||{}})
  0 0          
  0            
86             #."\n"
87             );
88             }
89              
90             ##======================================================================
91             ## API
92              
93             ## $str = $fmt->toString($hitList)
94             sub toString {
95 0     0 1   my ($fmt,$hits) = @_;
96 0 0 0       if ($hits->{counts_} && @{$hits->{counts_}}) {
  0 0 0        
97             ##-- count-query: return tab-separated strings
98 0           return join('', map {join("\t", @$_)."\n"} @{$hits->{counts_}});
  0            
  0            
99             }
100 0           elsif ($hits->{hits_} && @{$hits->{hits_}}) {
101             ##-- usual case: retrieve hit strings
102 0 0         my $useMatchIds = defined($fmt->{useMatchIds}) ? $fmt->{useMatchIds} : (grep {$_>0 && $_!=1} map {$_->{hl_}} map {@{$_->{ctx_}[1]}} @{$hits->{hits_}});
  0 0          
  0            
  0            
  0            
  0            
103             return join("\n",
104             map {
105 0           $fmt->hitString($hits->{hits_}[$_], $hits->{defaultField}, $_+$hits->{start}, $useMatchIds)
106 0           } (0..$#{$hits->{hits_}})
  0            
107             );
108             }
109             ##-- unknown: return empty string
110 0           return "(no hits)";
111             }
112              
113             1; ##-- be happy
114              
115             __END__