-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathline-by-line.rs
More file actions
52 lines (46 loc) · 1.57 KB
/
line-by-line.rs
File metadata and controls
52 lines (46 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
extern crate term;
extern crate difference;
use difference::{Difference, Changeset};
use std::io::Write;
/*
* The only thing to do here is to create a diff based on line
* splits (passing the newline character as a split symbol)
* and iterate over the results, matching and formatting them based
* on the type of `Difference`.
*
* Screenshot:
* https://raw.githubusercontent.com/johannhof/difference.rs/master/assets/git-style.png
*/
#[allow(unused_must_use)]
#[cfg_attr(feature = "cargo-clippy", allow(needless_range_loop))]
fn main() {
let text1 = "Roses are red, violets are blue,\n\
I wrote this library here,\n\
just for you.\n\
(It's true).";
let text2 = "Roses are red, violets are blue,\n\
I wrote this documentation here,\n\
just for you.\n\
(It's quite true).";
// Compare both texts, the third parameter defines the split level.
let Changeset { diffs, .. } = Changeset::new(text1, text2, "\n");
let mut t = term::stdout().unwrap();
for i in 0..diffs.len() {
match diffs[i] {
Difference::Same(ref x) => {
t.reset().unwrap();
writeln!(t, " {}", x);
}
Difference::Add(ref x) => {
t.fg(term::color::GREEN).unwrap();
writeln!(t, "+{}", x);
}
Difference::Rem(ref x) => {
t.fg(term::color::RED).unwrap();
writeln!(t, "-{}", x);
}
}
}
t.reset().unwrap();
t.flush().unwrap();
}