gliff
gliff — A text diffing library for Gleam.
Provides Myers and Patience diff algorithms, unified diff formatting, patch application, semantic cleanup, and inline highlighting.
Values
pub fn apply_patch(
text: String,
edits: List(types.Edit),
) -> Result(String, String)
Apply edit operations to a text string to produce the target text.
The fundamental property is:
apply_patch(old, diff(old, new)) == Ok(new)
Returns an error if the text doesn’t match the expected content described by the Equal and Delete operations.
pub fn apply_patch_fuzzy(
text: String,
edits: List(types.Edit),
tolerance tolerance: Float,
) -> Result(String, String)
Apply edit operations with fuzzy matching for context/delete lines.
When Equal or Delete lines don’t match exactly, accepts lines that
are similar enough based on character-level comparison.
Tolerance ranges from 0.0 (exact match, same as apply_patch) to
1.0 (accept any line). A value of 0.6 works well for most cases.
pub fn cleanup_semantic(
edits: List(types.Edit),
) -> List(types.Edit)
Eliminate trivial equalities and merge adjacent edits.
Removes small Equal sections that are surrounded by larger changes, absorbing them into the Delete and Insert operations. This produces fewer, larger edit blocks that are easier to read.
pub fn cleanup_semantic_lossless(
edits: List(types.Edit),
) -> List(types.Edit)
Shift edit boundaries to natural positions without changing semantics.
Moves edit boundaries to align with word boundaries, sentence endings, or blank lines based on a scoring system. The resulting diff applies identically but reads more naturally.
pub fn default_config() -> types.DiffConfig
Create a default diff configuration.
Returns DiffConfig(algorithm: Myers, cleanup: NoCleanup, max_iterations: 0)
where max_iterations: 0 means unlimited computation.
pub fn diff(old: String, new: String) -> List(types.Edit)
Compute a line-level diff between two strings using the Myers algorithm.
Returns a list of edit operations (Equal, Insert, Delete) where each operation contains one or more contiguous lines.
pub fn diff_chars(old: String, new: String) -> List(types.Edit)
Compute a character-level diff between two strings using the Myers algorithm.
Each element in the returned edit list represents one or more contiguous grapheme clusters that share the same edit type.
pub fn diff_chars_with(
old: String,
new: String,
config: types.DiffConfig,
) -> types.DiffResult
Compute a character-level diff with full configuration.
Same as diff_with but operates on grapheme clusters instead of lines.
pub fn diff_myers(old: String, new: String) -> List(types.Edit)
Compute a line-level diff using the Myers algorithm (explicit alias for diff).
pub fn diff_patience(
old: String,
new: String,
) -> List(types.Edit)
Compute a line-level diff using the Patience algorithm.
Patience diff anchors on lines that appear exactly once in both texts, then recursively diffs the gaps. This often produces more readable output for code changes where blocks are reordered.
pub fn diff_with(
old: String,
new: String,
config: types.DiffConfig,
) -> types.DiffResult
Compute a line-level diff with full configuration.
Allows selecting the algorithm, cleanup mode, and iteration budget.
Returns Complete if the diff finished normally, or Truncated if
the iteration budget was exceeded (in which case a crude but correct
fallback diff is returned).
pub fn diff_words(old: String, new: String) -> List(types.Edit)
Compute a word-level diff between two strings.
Tokenizes input by whitespace boundaries (preserving whitespace as separate tokens), then diffs the token sequences. Each edit contains one or more word/whitespace tokens.
pub fn from_unified(
input: String,
) -> Result(List(types.Hunk), String)
Parse a unified diff string into a list of hunks.
Accepts the standard format produced by diff -u or to_unified.
Returns an error if the input is malformed.
pub fn inline_highlight(
edits: List(types.Edit),
) -> List(types.InlineEdit)
Enrich line-level edits with character-level change highlighting.
For adjacent Delete/Insert pairs, computes a character-level sub-diff and returns spans marking which characters actually changed. Equal edits pass through as InlineEqual.
pub fn merge3(
base: String,
ours: String,
theirs: String,
) -> types.MergeResult
Perform a 3-way merge between two diverged versions of a base text.
Computes diff(base, ours) and diff(base, theirs), then combines the changes. When both sides modify the same region differently, a conflict is reported with Git-style conflict markers.
Returns MergeOk if the merge is clean, or MergeConflict with
the merged text (including <<<<<<</=======/>>>>>>> markers)
and a list of conflicts.
pub fn similarity(edits: List(types.Edit)) -> Float
Compute the similarity ratio between two texts from their diff result.
Returns a value between 0.0 (completely different) and 1.0 (identical).
Calculated as 2 * matching_elements / total_elements.
pub fn to_ansi(
edits: List(types.Edit),
old_name old_name: String,
new_name new_name: String,
) -> String
Render edits as a colored diff string using ANSI escape codes.
Deletions appear in red, insertions in green, and hunk headers in cyan. Output follows the same structure as unified diff format.
pub fn to_ansi_inline(edits: List(types.Edit)) -> String
Render edits with ANSI colors and inline character highlighting.
Changed characters within modified lines are rendered in bold, making it easy to see exactly what changed at a glance.
pub fn to_unified(
edits: List(types.Edit),
old_name old_name: String,
new_name new_name: String,
) -> String
Format a list of edits as a unified diff string.
The output matches the format produced by diff -u, with 3 lines of
context around each change. old_name and new_name appear in the
--- and +++ header lines.
pub fn to_unified_with(
edits: List(types.Edit),
old_name old_name: String,
new_name new_name: String,
context context: Int,
) -> String
Format edits as a unified diff with a custom number of context lines.
Same as to_unified but allows specifying how many unchanged lines
surround each change. Equivalent to diff -U<n>.