File: underline-words.rs

package info (click to toggle)
rust-difference 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 312 kB
  • sloc: makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,454 bytes parent folder | download | duplicates (11)
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
53
54
extern crate term;
extern crate difference;
use difference::{Difference, Changeset};
use std::io::Write;

// Screenshot:
// https://raw.githubusercontent.com/johannhof/difference.rs/master/assets/word-underline.png

#[allow(unused_must_use)]
fn main() {
    let text1 = "Roses are red, violets are blue.";
    let text2 = "Roses are blue, violets are";

    let mut t = term::stdout().unwrap();

    let Changeset { diffs, .. } = Changeset::new(text1, text2, "");

    for c in &diffs {
        match *c {
            Difference::Same(ref z) => {
                t.fg(term::color::RED).unwrap();
                write!(t, "{}", z);
            }
            Difference::Rem(ref z) => {
                t.fg(term::color::WHITE).unwrap();
                t.bg(term::color::RED).unwrap();
                write!(t, "{}", z);
                t.reset().unwrap();
            }
            _ => (),
        }
    }
    t.reset().unwrap();

    writeln!(t, "");

    for c in &diffs {
        match *c {
            Difference::Same(ref z) => {
                t.fg(term::color::GREEN).unwrap();
                write!(t, "{}", z);
            }
            Difference::Add(ref z) => {
                t.fg(term::color::WHITE).unwrap();
                t.bg(term::color::GREEN).unwrap();
                write!(t, "{}", z);
                t.reset().unwrap();
            }
            _ => (),
        }
    }
    t.reset().unwrap();
    t.flush().unwrap();
}