Text

Text diff

What changed between two texts, by line or by word, as a unified diff, and how alike they are.

Version 1.0.0 · 4 functions · 1 crate

#diff#history#versions#text

Functions

What use diff.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use diff_lines, diff_words from diff (Quillo 0.6).

diff_lines(old: str, new: str) → list

The lines of old and new as changes: [{ op: "equal" | "insert" | "delete", text }], ready to show in green and red.

diff_words(old: str, new: str) → list

The same, word by word (spaces kept as their own changes), for a sentence or a title.

unified_diff(old: str, new: str, old_name: str, new_name: str, context: int) → str

A unified diff, as git diff writes one, with context lines around each change.

diff_ratio(old: str, new: str) → float

How alike two texts are, from 0 to 1, by their characters.

Get it

$ quillo install diff               # diff.qwer into this folder$ quillo install diff --example     # and its example, diff.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/diff.qwer$ curl -O https://quillolang.org/plugins/files/diff.qwe

Then, in your program, all of it or only what you call:

main.qwe
use diff.qweruse diff_lines, diff_words from diff      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
similar = "2"

Example

diff.qwe
// What changed between two versions of a page.////   quillo run diff.qwe      then open http://localhost:8080/changesuse "diff.qwer"let before = "Quillo is a language.\nIt runs on the server.\n"let after = "Quillo is a web language.\nIt runs on the server.\nAnd in the browser.\n"route GET /changes = diff_lines(before, after)route GET /patch = unified_diff(before, after, "page.md", "page.md", 3)route GET /alike = { ratio: diff_ratio(before, after) }
$ quillo run diff.qwe$ curl 'localhost:8080/changes'$ curl 'localhost:8080/patch'

Source

diff.qwer
//! Differences between two texts: line by line, word by word, as a unified diff, and how alike they are.//! version: 1.0.0//!//! [dependencies]//! similar = "2"use serde_json::{json, Value};use similar::{ChangeTag, TextDiff};fn changes<'a, 'b>(d: &TextDiff<'a, 'a, 'b, str>) -> Vec<Value> {    d.iter_all_changes().map(|c| {        let op = match c.tag() { ChangeTag::Equal => "equal", ChangeTag::Insert => "insert", ChangeTag::Delete => "delete" };        json!({ "op": op, "text": c.value() })    }).collect()}/// The lines of `old` and `new` as changes: `[{ op: "equal" | "insert" | "delete", text }]`, ready to show in green and red.pub fn diff_lines(old: &str, new: &str) -> Vec<Value> {    changes(&TextDiff::from_lines(old, new))}/// The same, word by word (spaces kept as their own changes), for a sentence or a title.pub fn diff_words(old: &str, new: &str) -> Vec<Value> {    changes(&TextDiff::from_words(old, new))}/// A unified diff, as `git diff` writes one, with `context` lines around each change.pub fn unified_diff(old: &str, new: &str, old_name: &str, new_name: &str, context: i64) -> String {    TextDiff::from_lines(old, new).unified_diff().context_radius(context.max(0) as usize).header(old_name, new_name).to_string()}/// How alike two texts are, from 0 to 1, by their characters.pub fn diff_ratio(old: &str, new: &str) -> f64 {    TextDiff::from_chars(old, new).ratio() as f64}
Thread
88Useful? Like it, or add to its thread below.

Thread 0 posts

On Text diff: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.