The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tk::DiffText - Perl/Tk composite widget for colorized diffs.

SYNOPSIS

  use Tk::DiffText;

  my $w = $mw->DiffText()->pack();

  $w->diff($file0, $file1);

DESCRIPTION

This module defines a composite widget that makes it simple to provide basic "diff" functionality to your Tk applications.

OPTIONS

-orient => 'horizontal'|'vertical'

Controls the arrangement of the text panes. Defaults to vertical.

-gutter => 0|1

Hides and displays the line number gutter. Defaults to 1.

-gutterforeground => color

Sets the gutter foreground color.

-gutterbackground => color

Sets the gutter background color.

-colors => {...}

Sets the colors used for highlighting diff results. The structure of the value hash is as follows:

  {
    add => [-fg => 'green'  ],               # tag for additions
    del => [-fg => 'red', -overstrike => 1], # tag for deletions
    mod => [-fg => 'blue'   ],               # tag for changes
    pad => [-bg => '#f0f0f0'],               # tag for blank line padding
  }

For each of the tags you can specify any option that is valid for use in a ROText widget tag: -foreground, -background, -overstrike, etc.

METHODS

diff

  $w->diff(
        $data1,
        $data2,
        -case        => 0,
        -whitespace  => 0,
        -granularity => 'line', # or 'word' 'char' or regexp
  );

Does a diff between $data1 and $data2 and loads the results into the text display. Normally these arguments are filenames but they can also be a reference to an array of lines of data, a string containing a slurped file, an open filehandle, a glob (which is interpreted as a filehandle), an IO::File object or any other object with a getlines method.

Setting either -case or -whitespace to 0 instructs the diff algorithm to ignore case and whitespace, respectively.

The -granularity option controls the level of detail at which the diff is performed. The default value, 'line,' shows differences between lines. Changing it to 'word' or 'char' will show differences within a line at the word or character level. You may also pass a qr// quoted regular expression or a string which will be interpreted as a regular expression.

Note: For performance reasons, diffs are always performed line-by-line first. Finer granularity settings are only applied to lines marked as changed by the initial comparison. This can lead to slightly different results than you would get if you did a single diff at the higher level of resolution. (The results aren't wrong, just different.)

BUGS

The API isn't settled yet. Comments welcome.

Some configuration settings (-gutter, -orient, -colors) are only valid at creation time and cannot be changed later.

AUTHOR

Michael J. Carman <mjcarman@mchsi.com>

COPYRIGHT AND LICENSE

Copyright (C) 2006 by Michael J. Carman

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.