| 1 | package Term::HiliteDiff; | ||||||||||
| 2 | 5 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | $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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use vars qw( @EXPORT_OK %EXPORT_TAGS $DEFAULTOBJ ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 5 | ▶ | use Term::HiliteDiff::_impl (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 5 | ▶ | use Exporter (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 5 | *import = \&Exporter::import; | |||||||||
| 16 | 5 | @EXPORT_OK = qw( watch hilite_diff ); | |||||||||
| 17 | 5 | %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||
| 18 | |||||||||||
| 19 | # Auto-export everything to main if I've been called as a -e program. | ||||||||||
| 20 | 5 | ▶ | if ( $0 eq '-e' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | ## no critic (ProhibitMultiplePackages) | ||||||||||
| 22 | package main; | ||||||||||
| 23 | 5 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 5 | $main::VERSION = '0.10'; | |||||||||
| 25 | } | ||||||||||
| 26 | 0 | Term::HiliteDiff->import(':all'); | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | # Here are some convenience functions for pretending this module isn't | ||||||||||
| 30 | # object oriented. | ||||||||||
| 31 | 5 | $DEFAULTOBJ = __PACKAGE__->new; | |||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | sub hilite_diff { | ||||||||
|
Subroutine
hilite_diff: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | 2 | return $DEFAULTOBJ->hilite_diff(@_); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 2 | ▶ | sub watch { | ||||||||
|
Subroutine
watch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 38 | 2 | return $DEFAULTOBJ->watch(@_); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | # Hey, a class constructor. | ||||||||||
| 42 | 16 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 16 | return Term::HiliteDiff::_impl->new; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | # Blatantly copied this from errantstory.com | ||||||||||
| 47 | 5 | 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( <<STRING ); | ||||||||||
| 155 | fname=Josh | ||||||||||
| 156 | lname=Jore | ||||||||||
| 157 | email=jjore@cpan.org | ||||||||||
| 158 | STRING | ||||||||||
| 159 | print $obj->hilite_diff( <<STRING ); | ||||||||||
| 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<hilite_diff> and C<watch> 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<JORE> 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-E<gt>hilite_diff( ARRAY ) | ||||||||||
| 206 | |||||||||||
| 207 | =item $obj-E<gt>hilite_diff( TAB DELIMITED STRING ) | ||||||||||
| 208 | |||||||||||
| 209 | =item $obj-E<gt>hilite_diff( PIPE DELIMITED STRING ) | ||||||||||
| 210 | |||||||||||
| 211 | =item $obj-E<gt>hilite_diff( MULTI-LINE STRING ) | ||||||||||
| 212 | |||||||||||
| 213 | =item $obj-E<gt>hilite_diff( ANY 'OLE WORDS ) | ||||||||||
| 214 | |||||||||||
| 215 | For output that scrolls past and merely annotated, use | ||||||||||
| 216 | C<hilite_diff>. Your input is left pretty much unchanged. | ||||||||||
| 217 | |||||||||||
| 218 | =back | ||||||||||
| 219 | |||||||||||
| 220 | =head2 REDRAWING OUTPUT | ||||||||||
| 221 | |||||||||||
| 222 | =over | ||||||||||
| 223 | |||||||||||
| 224 | =item $obj-E<gt>hilite_diff( ARRAY ) | ||||||||||
| 225 | |||||||||||
| 226 | =item $obj-E<gt>hilite_diff( TAB DELIMITED STRING ) | ||||||||||
| 227 | |||||||||||
| 228 | =item $obj-E<gt>hilite_diff( PIPE DELIMITED STRING ) | ||||||||||
| 229 | |||||||||||
| 230 | =item $obj-E<gt>hilite_diff( MULTI-LINE STRING ) | ||||||||||
| 231 | |||||||||||
| 232 | =item $obj-E<gt>hilite_diff( ANY 'OLE WORDS ) | ||||||||||
| 233 | |||||||||||
| 234 | For output that updates in place, use C<watch>. 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<watch> 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-E<gt>new | ||||||||||
| 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<watch> and C<hilite_diff> 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Term-HiliteDiff> | ||||||||||
| 306 | |||||||||||
| 307 | =item AnnoCPAN, Annotated CPAN documentation L<http://annocpan.org/dist/Term-HiliteDiff> | ||||||||||
| 308 | |||||||||||
| 309 | =item CPAN Ratings L<http://cpanratings.perl.org/d/Term-HiliteDiff> | ||||||||||
| 310 | |||||||||||
| 311 | =item Search CPAN L<http://search.cpan.org/dist/Term-HiliteDiff> | ||||||||||
| 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 <jjore@cpan.org> | ||||||||||
| 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__ | ||||||||||