]> git.lizzy.rs Git - rust.git/blob - src/rustfmt_diff.rs
print_diff: Don't print color codes if output is not a tty
[rust.git] / src / rustfmt_diff.rs
1 use std::collections::VecDeque;
2 use diff;
3 use term;
4 use std::io;
5
6 #[derive(Debug, PartialEq)]
7 pub enum DiffLine {
8     Context(String),
9     Expected(String),
10     Resulting(String),
11 }
12
13 #[derive(Debug, PartialEq)]
14 pub struct Mismatch {
15     pub line_number: u32,
16     pub lines: Vec<DiffLine>,
17 }
18
19 impl Mismatch {
20     fn new(line_number: u32) -> Mismatch {
21         Mismatch {
22             line_number: line_number,
23             lines: Vec::new(),
24         }
25     }
26 }
27
28 // Produces a diff between the expected output and actual output of rustfmt.
29 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
30     let mut line_number = 1;
31     let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
32     let mut lines_since_mismatch = context_size + 1;
33     let mut results = Vec::new();
34     let mut mismatch = Mismatch::new(0);
35
36     for result in diff::lines(expected, actual) {
37         match result {
38             diff::Result::Left(str) => {
39                 if lines_since_mismatch >= context_size {
40                     results.push(mismatch);
41                     mismatch = Mismatch::new(line_number - context_queue.len() as u32);
42                 }
43
44                 while let Some(line) = context_queue.pop_front() {
45                     mismatch.lines.push(DiffLine::Context(line.to_owned()));
46                 }
47
48                 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
49                 lines_since_mismatch = 0;
50             }
51             diff::Result::Right(str) => {
52                 if lines_since_mismatch >= context_size {
53                     results.push(mismatch);
54                     mismatch = Mismatch::new(line_number - context_queue.len() as u32);
55                 }
56
57                 while let Some(line) = context_queue.pop_front() {
58                     mismatch.lines.push(DiffLine::Context(line.to_owned()));
59                 }
60
61                 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
62                 line_number += 1;
63                 lines_since_mismatch = 0;
64             }
65             diff::Result::Both(str, _) => {
66                 if context_queue.len() >= context_size {
67                     let _ = context_queue.pop_front();
68                 }
69
70                 if lines_since_mismatch < context_size {
71                     mismatch.lines.push(DiffLine::Context(str.to_owned()));
72                 } else {
73                     context_queue.push_back(str);
74                 }
75
76                 line_number += 1;
77                 lines_since_mismatch += 1;
78             }
79         }
80     }
81
82     results.push(mismatch);
83     results.remove(0);
84
85     results
86 }
87
88 pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F)
89     where F: Fn(u32) -> String
90 {
91     match term::stdout() {
92         Some(_) if isatty() => print_diff_fancy(diff, get_section_title, term::stdout().unwrap()),
93         _ => print_diff_basic(diff, get_section_title),
94     }
95
96     // isatty shamelessly adapted from cargo.
97     #[cfg(unix)]
98     fn isatty() -> bool {
99         extern crate libc;
100
101         unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
102     }
103     #[cfg(windows)]
104     fn isatty() -> bool {
105         extern crate kernel32;
106         extern crate winapi;
107
108         unsafe {
109             let handle = kernel32::GetStdHandle(winapi::winbase::STD_OUTPUT_HANDLE);
110             let mut out = 0;
111             kernel32::GetConsoleMode(handle, &mut out) != 0
112         }
113     }
114 }
115
116 fn print_diff_fancy<F>(diff: Vec<Mismatch>,
117                        get_section_title: F,
118                        mut t: Box<term::Terminal<Output = io::Stdout>>)
119     where F: Fn(u32) -> String
120 {
121     for mismatch in diff {
122         let title = get_section_title(mismatch.line_number);
123         writeln!(t, "{}", title).unwrap();
124
125         for line in mismatch.lines {
126             match line {
127                 DiffLine::Context(ref str) => {
128                     t.reset().unwrap();
129                     writeln!(t, " {}⏎", str).unwrap();
130                 }
131                 DiffLine::Expected(ref str) => {
132                     t.fg(term::color::GREEN).unwrap();
133                     writeln!(t, "+{}⏎", str).unwrap();
134                 }
135                 DiffLine::Resulting(ref str) => {
136                     t.fg(term::color::RED).unwrap();
137                     writeln!(t, "-{}⏎", str).unwrap();
138                 }
139             }
140         }
141         t.reset().unwrap();
142     }
143 }
144
145 pub fn print_diff_basic<F>(diff: Vec<Mismatch>, get_section_title: F)
146     where F: Fn(u32) -> String
147 {
148     for mismatch in diff {
149         let title = get_section_title(mismatch.line_number);
150         println!("{}", title);
151
152         for line in mismatch.lines {
153             match line {
154                 DiffLine::Context(ref str) => {
155                     println!(" {}⏎", str);
156                 }
157                 DiffLine::Expected(ref str) => {
158                     println!("+{}⏎", str);
159                 }
160                 DiffLine::Resulting(ref str) => {
161                     println!("-{}⏎", str);
162                 }
163             }
164         }
165     }
166 }