Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

diff.mq

Text and array diffing utilities implemented as an mq module, built on top of mq's native Myers-diff engine — the same one that powers assert_eq's failure output.

Features

  • Line, word, character, and generic array diffing
  • added / removed / unchanged / has_changes / stat helpers to inspect a diff result
  • Renders a diff as unified-diff-style text or a fenced ```diff Markdown code block, ready to drop into a changelog or PR description
  • No hand-rolled LCS implementation — delegates to mq's native diff builtin for correctness and speed

Installation

Copy diff.mq to your mq module directory, or place it anywhere and reference it with -L.

cp diff.mq ~/.local/mq/config/

HTTP Import (no local installation needed)

If mq was built with the http-import feature, you can import directly from GitHub without any local setup:

mq -I raw 'import "github.com/harehare/diff.mq" | diff::diff_lines(read_file("old.md"), read_file("new.md"))'

Pin to a specific release with @vX.Y.Z:

mq -I raw 'import "github.com/harehare/diff.mq@v0.1.0" | ...'

Usage

mq -L /path/to/modules -I raw \
  'import "diff" | diff::diff_lines(read_file("old.md"), read_file("new.md"))'

If you copied it to the mq built-in module directory:

mq -I raw 'import "diff" | ...'

API

Computing a diff

Function Description
diff_lines(a, b) Diffs two strings line by line
diff_words(a, b) Diffs two strings word by word (whitespace-separated)
diff_chars(a, b) Diffs two strings character by character
diff_array(a, b) Diffs two arbitrary arrays of values (numbers, dicts, Markdown nodes, …)

Each returns an array of {tag, value} entries, where tag is one of "equal", "insert", "delete". Adjacent delete/insert pairs of strings also carry an inline field with a character-level sub-diff, useful for building your own word-level renderers.

Inspecting a diff result

Function Description
is_equal(entry) / is_added(entry) / is_removed(entry) Predicates on a single entry
added(result) / removed(result) / unchanged(result) Extract just the values with that tag
has_changes(result) true if the result contains any insert/delete
stat(result) {added, removed, unchanged} counts

Rendering

Function Description
to_unified(result) Renders as unified-diff-style text (+ , - , prefixed lines)
to_markdown(result) Wraps to_unified output in a fenced ```diff code block

Example

mq -L . -I raw \
  'import "diff" | diff::to_markdown(diff::diff_lines("a\nb\nc", "a\nx\nc"))' <<< "x"
  a
- b
+ x
  c

Check whether two revisions of a document changed at all, and by how much:

import "diff"
| let r = diff::diff_lines(old_text, new_text)
| if (diff::has_changes(r)): diff::stat(r) else: "no changes" end

Compatibility

Requires mq v0.6 or later (uses the native _diff builtin).

License

MIT

About

Text and array diffing utilities implemented as an mq module, built on top of mq's native Myers-diff engine — the same one that powers assert_eq's failure output.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors