TextDiff

Hex.pm versionsHex DocsGitHub: CI statusCoveralls: coverageLicense: MIT

TextDiff returns a formatted diff between two strings.

Installation

The package can be installed by adding text_idff to your list of dependencies in mix.exs:

def deps do
  [
    {:text_diff, "~> 0.1"}
  ]
end

Example

iex> code = """
...> defmodule Foo do
...>   @moduledoc   false
...>
...>   def foo, do:  :foo
...>
...>   def three_times(x) do
...>     {x,
...>      x,x}
...>   end
...>
...>   def bar(x) do
...>     {:bar, x}
...>   end
...> end\
...> """
...> formatted = code |> Code.format_string!() |> IO.iodata_to_binary()
...> code
...> |> TextDiff.format(formatted, color: false)
...> |> IO.iodata_to_binary()
...> |> IO.puts()
"""
1  1   |defmodule Foo do
2    - |  @moduledoc   false
  2 + |  @moduledoc false
3  3   |
4    - |  def foo, do:  :foo
  4 + |  def foo, do: :foo
5  5   |
6  6   |  def three_times(x) do
7    - |    {x,
8    - |     x,x}
  7 + |    {x, x, x}
9  8   |  end
10  9   |
   ...|
"""

The colorised diff is shown below:

Colorised diff