File Coverage

blib/lib/Term/HiliteDiff.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 29 29 100.0


line stmt bran cond sub pod time code
1             package Term::HiliteDiff;
2             BEGIN {
3 5     5   141092 $Term::HiliteDiff::VERSION = '0.10';
4             }
5             # ABSTRACT: Highlights differences in text with ANSI escape codes
6              
7             ## no critic (RequireUseWarnings)
8             ## no critic (ProhibitPunctuationVars)
9              
10 5     5   42 use strict;
  5         8  
  5         142  
11 5     5   24 use vars qw( @EXPORT_OK %EXPORT_TAGS $DEFAULTOBJ );
  5         9  
  5         255  
12 5     5   2689 use Term::HiliteDiff::_impl ();
  5         15  
  5         127  
13              
14 5     5   27 use Exporter ();
  5         11  
  5         299  
15             *import = \&Exporter::import;
16             @EXPORT_OK = qw( watch hilite_diff );
17             %EXPORT_TAGS = ( all => \@EXPORT_OK );
18              
19             # Auto-export everything to main if I've been called as a -e program.
20             if ( $0 eq '-e' ) {
21             ## no critic (ProhibitMultiplePackages)
22             package main;
23             BEGIN {
24 5     5   783 $main::VERSION = '0.10';
25             }
26             Term::HiliteDiff->import(':all');
27             }
28              
29             # Here are some convenience functions for pretending this module isn't
30             # object oriented.
31             $DEFAULTOBJ = __PACKAGE__->new;
32              
33             sub hilite_diff {
34 2     2 1 17 return $DEFAULTOBJ->hilite_diff(@_);
35             }
36              
37             sub watch {
38 2     2 1 14 return $DEFAULTOBJ->watch(@_);
39             }
40              
41             # Hey, a class constructor.
42             sub new {
43 16     16 1 1307 return Term::HiliteDiff::_impl->new;
44             }
45              
46             # Blatantly copied this from errantstory.com
47             q[What's the point of dreaming I'm a girl if I don't get a cool lesbian scene?!];
48              
49              
50              
51             =pod
52              
53             =head1 NAME
54              
55             Term::HiliteDiff - Highlights differences in text with ANSI escape codes
56              
57             =head1 VERSION
58              
59             version 0.10
60              
61             =head1 SYNOPSIS
62              
63             # Prints a tab delimited file, with differences highlighted
64             use Term::HiliteDiff;
65             my $differ = Term::HiliteDiff->new;
66             while ( <> ) {
67             my $line = [ split /\t/, $_, -1 ];
68             my $diff = $differ->hilite_diff( $line );
69              
70             print join "\t", @$diff;
71             }
72              
73             OR as functions
74              
75             use Term::HiliteDiff qw( hilite_diff );
76             while ( <> ) {
77             my $line = [ split /\t/, $_, -1 ];
78             my $diff = hilite_diff( $line );
79              
80             print $diff
81             }
82              
83             =head1 DESCRIPTION
84              
85             Term::HiliteDiff prints or formats your input with the differences
86             highlighted. You can choose to update your display in place or let
87             things scroll past like tailing a log.
88              
89             You can choose to let it attempt to parse the columns out of your data
90             or just pass an array reference in.
91              
92             It highlights differences between subsequent lines/records of text. It
93             was directly inspired by the --difference mode provided by the
94             watch(1) program on Linux.
95              
96             =head1 INSTALLATION
97              
98             To install this module, run the following commands:
99              
100             perl Makefile.PL
101             make
102             make test
103             make install
104              
105             =head1 INPUT
106              
107             It accepts either a single array reference or a string. There is no
108             parsing with an array reference. Strings are split by tabs, pipes, or
109             lines.
110              
111             =over
112              
113             =item No interpretation
114              
115             use Term::HiliteDiff;
116             $obj = Term::HiliteDiff->new;
117              
118             for ( [qw[ Josh Jore jjore@cpan.org ]],
119             [qw[ Josh JORE jjore@cpan.org ]],
120             ) {
121             $diff = $obj->hilite_diff( $_ );
122             print join( "\t", @$diff ), "\n";
123             }
124              
125             =item Split by tabs
126              
127             It's OK if there's a newline at the end of your input.
128              
129             use Term::HiliteDiff;
130             $obj = Term::HiliteDiff->new;
131              
132             for ( "Josh\tJore\tjjore\@cpan.org\n",
133             "Josh\tJORE\tjjore\@cpan.org\n",
134             ) {
135             print $obj->hilite_diff( $_ );
136             }
137              
138             =item Split by pipes
139              
140             use Term::HiliteDiff;
141             $obj = Term::HiliteDiff->new;
142              
143             for ( "Josh|Jore|jjore\@cpan.org\n",
144             "Josh|JORE|jjore\@cpan.org\n",
145             ) {
146             print $obj->hilite_diff( $_ );
147             }
148              
149             =item Split by lines
150              
151             use Term::HiliteDiff;
152             $obj = Term::HiliteDiff->new;
153              
154             print $obj->hilite_diff( <
155             fname=Josh
156             lname=Jore
157             email=jjore@cpan.org
158             STRING
159             print $obj->hilite_diff( <
160             fname=Josh
161             lname=JJORE
162             email=jjore@cpan.org
163             STRING
164              
165             =item Split by any 'ole words
166              
167             use Term::HiliteDiff;
168             $obj = Term::HiliteDiff->new;
169              
170             for ( "Singing dang fol dee dido\n",
171             "Singing dang fol dee day\n",
172             ) {
173             print $obj->hilite_diff( $_ );
174             }
175              
176             =back
177              
178             =head1 OUTPUT
179              
180             Both the C and C method/functions return the
181             output in the same format as the input. If you passed in an array, you
182             get an array back but if you passed in something like a tab-delimited
183             string, the output is going to be formatted that way too.
184              
185             watch( \@input) }; # Array!
186             watch( " \t " ) ; # Tabs!
187             watch( ' | ' ) ; # Pipes!
188             watch( " \n " ) ; # Lines!
189              
190             Each column's value is compared to the previous value of the same
191             column. Changed values are marked up.
192              
193             Presently the only mark-up is to use the "reverse hilighting". If these
194             two rows were marked up then the middle value C would be
195             hilighted with reverse text.
196              
197             # The middle column!
198             Josh | Jore | jjore@cpan.org
199             Josh | JORE | jjore@cpan.org
200              
201             =head2 SCROLLING OUTPUT - hilite_diff
202              
203             =over
204              
205             =item $obj-Ehilite_diff( ARRAY )
206              
207             =item $obj-Ehilite_diff( TAB DELIMITED STRING )
208              
209             =item $obj-Ehilite_diff( PIPE DELIMITED STRING )
210              
211             =item $obj-Ehilite_diff( MULTI-LINE STRING )
212              
213             =item $obj-Ehilite_diff( ANY 'OLE WORDS )
214              
215             For output that scrolls past and merely annotated, use
216             C. Your input is left pretty much unchanged.
217              
218             =back
219              
220             =head2 REDRAWING OUTPUT
221              
222             =over
223              
224             =item $obj-Ehilite_diff( ARRAY )
225              
226             =item $obj-Ehilite_diff( TAB DELIMITED STRING )
227              
228             =item $obj-Ehilite_diff( PIPE DELIMITED STRING )
229              
230             =item $obj-Ehilite_diff( MULTI-LINE STRING )
231              
232             =item $obj-Ehilite_diff( ANY 'OLE WORDS )
233              
234             For output that updates in place, use C. The watch
235             method/function tags the first thing it sees with an ANSI code to save
236             the current cursor position and then tags all later output with
237             another ANSI escape code to jump back to the previous position.
238              
239             I've used this when watching a stream of screen-sized chunks of data
240             go by that were largely identical so I just wanted the changes
241             annotated but I didn't really want the screen to scroll upwards.
242              
243             C will also use a line-erasing escape code to ensure that
244             whenever newlines are printed that any printed clutter is being
245             cleaned up.
246              
247             Consider:
248              
249             use Data::Dumper qw( Dumper );
250             use Term::HiliteDiff qw( watch );
251             $Data::Dumper::Sortkeys = 1;
252              
253             @thingies = (
254             { a => 1, b => 2, c => 3 },
255             { a => 1, , c => 3 },
256             { a => 1, b => 2, c => 3 },
257             { a => 1, b => 2, },
258             );
259             $obj = Term::HiliteDiff->new;
260              
261             for my $thingie ( @thingies ) {
262             print $obj->watch( Data::Dumper::Dumper( $thingie ) );
263             }
264              
265             =back
266              
267             =head1 METHODS
268              
269             =over
270              
271             =item Term::HiliteDiff-Enew
272              
273             =back
274              
275             =head1 FUNCTIONS
276              
277             =over
278              
279             =item hilite_diff
280              
281             =item watch
282              
283             =back
284              
285             =head1 AUTOMATIC EXPORTS
286              
287             The C and C convenience functions are exported
288             when you've used this module from the command line. I think possibly
289             there ought to be an App::??? module to wrap these easy command-line
290             things up.
291              
292             perl -MTerm::HiliteDiff -pe '$_ = hilite_diff( $_ )'
293              
294             =head1 SUPPORT AND DOCUMENTATION
295              
296             After installing, you can find documentation for this module with the
297             perldoc command.
298              
299             perldoc Term::HiliteDiff
300              
301             You can also look for information at:
302              
303             =over
304              
305             =item RT, CPAN's request tracker L
306              
307             =item AnnoCPAN, Annotated CPAN documentation L
308              
309             =item CPAN Ratings L
310              
311             =item Search CPAN L
312              
313             =back
314              
315             =for emacs ## Local Variables:
316             ## mode: pod
317             ## mode: auto-fill
318             ## End:
319              
320             =head1 AUTHOR
321              
322             Josh Jore
323              
324             =head1 COPYRIGHT AND LICENSE
325              
326             This software is copyright (c) 2011 by Josh Jore.
327              
328             This is free software; you can redistribute it and/or modify it under
329             the same terms as the Perl 5 programming language system itself.
330              
331             =cut
332              
333              
334             __END__