diffy

repository·main·Indexed 23 days ago

https://github.com/samg/diffy

A Ruby library for generating diffs between strings or files by leveraging the Unix diff tool. It supports multiple output formats including plain text, ANSI color for terminals, and HTML with optional character-level highlighting. Features include side-by-side comparisons via Diffy::SplitDiff, Enumerable support for iterating over diff lines and chunks, and customizable diff engine options.

Tokens
2K
Snippets
7
Records
18
Agent score
78%

What's inside diffy

  1. Install Diffy

    main

    On Unix

    Install the gem directly using rubygems:

    gem install diffy

    On Windows

    1. Ensure you have a working diff command in your system PATH. Options include:
      • Installing Diff::LCS (includes ldiff).
      • Using RubyInstaller with the devkit installed.
      • Installing unxutils (note: uses diff 2.7, which may cause whitespace handling differences).
      • Installing gnuwin32 components individually (note: delivers diff 2.8, which is recommended for compatibility).
    2. Install the gem:
    gem install diffy
    gem install diffy
  2. Generate HTML diff output

    main

    To generate HTML, pass :html or :html_simple to the to_s method. For character-level highlighting, use :html.

    Diffy provides CSS for styling these outputs via Diffy::CSS. A colorblind-safe palette is available in Diffy::CSS_COLORBLIND_1.

    puts Diffy::Diff.new("foo\n", "Foo\n").to_s(:html)
    puts Diffy::CSS
  3. Generate a basic diff between strings

    main
    Use Diffy::Diff.new(string1, string2) to create a diff object between two strings. By default, calling .to_s on the object returns a plain text diff with + and - prefixes.
  4. Iterate over diff lines and chunks

    main

    Diffy::Diff implements the Enumerable interface, allowing you to iterate over the resulting lines or groups of changes.

    • #each: Iterates over every line in the diff. Lines starting with +, -, or indicate additions, deletions, or unchanged lines respectively.
    • #each_chunk: Iterates over groupings of additions, deletions, and unchanged lines.
    Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n").each do |line|
      case line
      when /^\+/ then puts "line #{line.chomp} added"
      when /^-/ then puts "line #{line.chomp} removed"
      end
    end
    
    Diffy::Diff.new("foo\nbar\nbang\nbaz\n", "foo\nbar\nbing\nbong\n").each_chunk.to_a
  5. Diff files instead of strings

    main

    To compare the contents of two files rather than treating the arguments as raw strings, pass the :source => 'files' option.

    puts Diffy::Diff.new('/tmp/foo', '/tmp/bar', :source => 'files')
  6. Perform side-by-side (split) comparisons

    main

    Use the Diffy::SplitDiff class to create a split view where deletions are on the left and insertions are on the right. Unlike Diffy::Diff, you use #left and #right methods to retrieve the sides.

    Usage: Diffy::SplitDiff.new(string1, string2, options = {})

    Options like :format can be passed to change the output of the split sides.

    Diffy::SplitDiff.new(string1, string2, :format => :html).left
    Diffy::SplitDiff.new(string1, string2, :format => :html).right
  7. Configure Diffy diff options

    main

    You can customize the behavior of the diff engine using several options during initialization of Diffy::Diff.new:

    • :include_diff_info => true: Includes full diff metadata (headers, hunk ranges).
    • :allow_empty_diff => false: Returns the full text of the first input if no differences are found (default is an empty string).
    • :context => integer: Sets the number of lines of context around changes (defaults to 10000).
    • :diff => "string": Overrides the command line options passed to the underlying Unix diff (e.g., "-w"). Note: this is ignored if :context is also provided.
    • :include_plus_and_minus_in_html => true: Includes the +, -, and symbols at the start of lines in HTML output.
    • :ignore_crlf => true: When using HTML output, ignores CRLF differences.

    You can also set global defaults using Diffy::Diff.default_options = { ... }.

    Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n", :include_diff_info => true).to_s(:text)
    Diffy::Diff.default_options.merge!(:source => 'files')
  8. Configure Diffy output formats

    main

    You can specify the output format by passing a symbol to Diffy::Diff#to_s. You can also set a global default format using Diffy::Diff.default_format = :format.

    Supported Formats:

    • :text - Plain text output.
    • :color - ANSI colorized text for terminals.
    • :html - HTML output with inline character-level highlighting (GitHub-style).
    • :html_simple - HTML output without inline highlighting (better for performance or simplicity).
    Diffy::Diff.default_format = :html
  9. Initialize a Diff object with Diffy::Diff

    main
    To generate a diff, instantiate Diffy::Diff with two inputs (either strings or file paths) and an optional hash of configuration options. By default, the inputs are treated as strings. You can globally configure the default options or format for all new Diff objects using Diffy::Diff.default_options= and Diffy::Diff.default_format=.
  10. Split diffs into left and right components with SplitDiff

    main

    The Diffy::SplitDiff class allows you to generate a diff and then split it into two distinct parts: the 'left' side (original/deletions) and the 'right' side (new/additions). This is useful for creating side-by-side comparison views.

    To use SplitDiff, initialize it with the left string, the right string, and an optional options hash. You must specify a valid :format in the options (e.g., :color, :html, :html_simple, or :text).

    Once initialized, you can call .left and .right to retrieve the respective components.

  11. Generate ANSI color output for terminal display

    main
    Use the color method to generate a diff string with ANSI color codes. This is suitable for displaying diffs directly in a terminal. It applies specific colors to diff metadata (gray), additions (green), deletions (red), and hunk headers (cyan).