summaryrefslogtreecommitdiffstats
path: root/vendor/similar/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:47:55 +0000
commit2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4 (patch)
tree033cc839730fda84ff08db877037977be94e5e3a /vendor/similar/examples
parentInitial commit. (diff)
downloadcargo-upstream.tar.xz
cargo-upstream.zip
Adding upstream version 0.70.1+ds1.upstream/0.70.1+ds1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/similar/examples')
-rw-r--r--vendor/similar/examples/close-matches.rs15
-rw-r--r--vendor/similar/examples/large.rs8
-rw-r--r--vendor/similar/examples/nonstring.rs13
-rw-r--r--vendor/similar/examples/original-slices.rs11
-rw-r--r--vendor/similar/examples/patience.rs48
-rw-r--r--vendor/similar/examples/serde.rs15
-rw-r--r--vendor/similar/examples/terminal-inline.rs60
-rw-r--r--vendor/similar/examples/terminal.rs20
-rw-r--r--vendor/similar/examples/udiff.rs24
9 files changed, 214 insertions, 0 deletions
diff --git a/vendor/similar/examples/close-matches.rs b/vendor/similar/examples/close-matches.rs
new file mode 100644
index 0000000..ebe283a
--- /dev/null
+++ b/vendor/similar/examples/close-matches.rs
@@ -0,0 +1,15 @@
+use similar::get_close_matches;
+
+fn main() {
+ let words = vec![
+ "apple",
+ "appu",
+ "appal",
+ "apparitor",
+ "beer",
+ "beeb",
+ "beeline",
+ ];
+ println!("{:?}", get_close_matches("app", &words, 3, 0.7));
+ println!("{:?}", get_close_matches("bee", &words, 3, 0.7));
+}
diff --git a/vendor/similar/examples/large.rs b/vendor/similar/examples/large.rs
new file mode 100644
index 0000000..08200f8
--- /dev/null
+++ b/vendor/similar/examples/large.rs
@@ -0,0 +1,8 @@
+use similar::TextDiff;
+
+fn main() {
+ let x = "abc".repeat(2000);
+ let y = "abd".repeat(2000);
+ let diff = TextDiff::from_chars(&x, &y);
+ println!("{}", diff.unified_diff());
+}
diff --git a/vendor/similar/examples/nonstring.rs b/vendor/similar/examples/nonstring.rs
new file mode 100644
index 0000000..ca102e2
--- /dev/null
+++ b/vendor/similar/examples/nonstring.rs
@@ -0,0 +1,13 @@
+use similar::{capture_diff_slices, Algorithm};
+
+fn main() {
+ let old = vec![1, 2, 3];
+ let new = vec![1, 2, 4];
+ let ops = capture_diff_slices(Algorithm::Myers, &old, &new);
+
+ for op in ops {
+ for change in op.iter_changes(&old, &new) {
+ println!("{:?}", change);
+ }
+ }
+}
diff --git a/vendor/similar/examples/original-slices.rs b/vendor/similar/examples/original-slices.rs
new file mode 100644
index 0000000..bdca420
--- /dev/null
+++ b/vendor/similar/examples/original-slices.rs
@@ -0,0 +1,11 @@
+use similar::utils::diff_chars;
+use similar::Algorithm;
+
+fn main() {
+ let old = "1234567890abcdef".to_string();
+ let new = "0123456789Oabzdef".to_string();
+
+ for (change_tag, value) in diff_chars(Algorithm::Myers, &old, &new) {
+ println!("{}{:?}", change_tag, value);
+ }
+}
diff --git a/vendor/similar/examples/patience.rs b/vendor/similar/examples/patience.rs
new file mode 100644
index 0000000..4b898a6
--- /dev/null
+++ b/vendor/similar/examples/patience.rs
@@ -0,0 +1,48 @@
+use similar::{Algorithm, TextDiff};
+
+const OLD: &str = r#"
+[
+ (
+ Major,
+ 2,
+ ),
+ (
+ Minor,
+ 20,
+ ),
+ (
+ Value,
+ 0,
+ ),
+]
+"#;
+const NEW: &str = r#"
+[
+ (
+ Major,
+ 2,
+ ),
+ (
+ Minor,
+ 0,
+ ),
+ (
+ Value,
+ 0,
+ ),
+ (
+ Value,
+ 1,
+ ),
+]
+"#;
+
+fn main() {
+ println!(
+ "{}",
+ TextDiff::configure()
+ .algorithm(Algorithm::Patience)
+ .diff_lines(OLD, NEW)
+ .unified_diff()
+ );
+}
diff --git a/vendor/similar/examples/serde.rs b/vendor/similar/examples/serde.rs
new file mode 100644
index 0000000..98a5818
--- /dev/null
+++ b/vendor/similar/examples/serde.rs
@@ -0,0 +1,15 @@
+use similar::TextDiff;
+
+fn main() {
+ let diff = TextDiff::from_lines(
+ "Hello World\nThis is the second line.\nThis is the third.",
+ "Hallo Welt\nThis is the second line.\nThis is life.\nMoar and more",
+ );
+
+ let all_changes = diff
+ .ops()
+ .iter()
+ .flat_map(|op| diff.iter_changes(op))
+ .collect::<Vec<_>>();
+ println!("{}", serde_json::to_string_pretty(&all_changes).unwrap());
+}
diff --git a/vendor/similar/examples/terminal-inline.rs b/vendor/similar/examples/terminal-inline.rs
new file mode 100644
index 0000000..4c5d74f
--- /dev/null
+++ b/vendor/similar/examples/terminal-inline.rs
@@ -0,0 +1,60 @@
+use std::fmt;
+use std::fs::read;
+use std::process::exit;
+
+use console::{style, Style};
+use similar::{ChangeTag, TextDiff};
+
+struct Line(Option<usize>);
+
+impl fmt::Display for Line {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self.0 {
+ None => write!(f, " "),
+ Some(idx) => write!(f, "{:<4}", idx + 1),
+ }
+ }
+}
+
+fn main() {
+ let args: Vec<_> = std::env::args_os().collect();
+ if args.len() != 3 {
+ eprintln!("usage: terminal-inline [old] [new]");
+ exit(1);
+ }
+
+ let old = read(&args[1]).unwrap();
+ let new = read(&args[2]).unwrap();
+ let diff = TextDiff::from_lines(&old, &new);
+
+ for (idx, group) in diff.grouped_ops(3).iter().enumerate() {
+ if idx > 0 {
+ println!("{:-^1$}", "-", 80);
+ }
+ for op in group {
+ for change in diff.iter_inline_changes(op) {
+ let (sign, s) = match change.tag() {
+ ChangeTag::Delete => ("-", Style::new().red()),
+ ChangeTag::Insert => ("+", Style::new().green()),
+ ChangeTag::Equal => (" ", Style::new().dim()),
+ };
+ print!(
+ "{}{} |{}",
+ style(Line(change.old_index())).dim(),
+ style(Line(change.new_index())).dim(),
+ s.apply_to(sign).bold(),
+ );
+ for (emphasized, value) in change.iter_strings_lossy() {
+ if emphasized {
+ print!("{}", s.apply_to(value).underlined().on_black());
+ } else {
+ print!("{}", s.apply_to(value));
+ }
+ }
+ if change.missing_newline() {
+ println!();
+ }
+ }
+ }
+ }
+}
diff --git a/vendor/similar/examples/terminal.rs b/vendor/similar/examples/terminal.rs
new file mode 100644
index 0000000..1a90327
--- /dev/null
+++ b/vendor/similar/examples/terminal.rs
@@ -0,0 +1,20 @@
+use console::Style;
+use similar::{ChangeTag, TextDiff};
+
+fn main() {
+ let diff = TextDiff::from_lines(
+ "Hello World\nThis is the second line.\nThis is the third.",
+ "Hallo Welt\nThis is the second line.\nThis is life.\nMoar and more",
+ );
+
+ for op in diff.ops() {
+ for change in diff.iter_changes(op) {
+ let (sign, style) = match change.tag() {
+ ChangeTag::Delete => ("-", Style::new().red()),
+ ChangeTag::Insert => ("+", Style::new().green()),
+ ChangeTag::Equal => (" ", Style::new()),
+ };
+ print!("{}{}", style.apply_to(sign).bold(), style.apply_to(change));
+ }
+ }
+}
diff --git a/vendor/similar/examples/udiff.rs b/vendor/similar/examples/udiff.rs
new file mode 100644
index 0000000..3d8eb9c
--- /dev/null
+++ b/vendor/similar/examples/udiff.rs
@@ -0,0 +1,24 @@
+use std::fs::read;
+use std::io;
+use std::process::exit;
+
+use similar::TextDiff;
+
+fn main() {
+ let args: Vec<_> = std::env::args_os().collect();
+ if args.len() != 3 {
+ eprintln!("usage: udiff [old] [new]");
+ exit(1);
+ }
+
+ let old = read(&args[1]).unwrap();
+ let new = read(&args[2]).unwrap();
+ TextDiff::from_lines(&old, &new)
+ .unified_diff()
+ .header(
+ &args[1].as_os_str().to_string_lossy(),
+ &args[2].as_os_str().to_string_lossy(),
+ )
+ .to_writer(io::stdout())
+ .unwrap();
+}