1 use std::collections::VecDeque;
6 #[derive(Debug, PartialEq)]
13 #[derive(Debug, PartialEq)]
16 pub lines: Vec<DiffLine>,
20 fn new(line_number: u32) -> Mismatch {
22 line_number: line_number,
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);
36 for result in diff::lines(expected, actual) {
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);
44 while let Some(line) = context_queue.pop_front() {
47 .push(DiffLine::Context(line.to_owned()));
52 .push(DiffLine::Resulting(str.to_owned()));
53 lines_since_mismatch = 0;
55 diff::Result::Right(str) => {
56 if lines_since_mismatch >= context_size {
57 results.push(mismatch);
58 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
61 while let Some(line) = context_queue.pop_front() {
64 .push(DiffLine::Context(line.to_owned()));
69 .push(DiffLine::Expected(str.to_owned()));
71 lines_since_mismatch = 0;
73 diff::Result::Both(str, _) => {
74 if context_queue.len() >= context_size {
75 let _ = context_queue.pop_front();
78 if lines_since_mismatch < context_size {
81 .push(DiffLine::Context(str.to_owned()));
83 context_queue.push_back(str);
87 lines_since_mismatch += 1;
92 results.push(mismatch);
98 pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F)
99 where F: Fn(u32) -> String
101 match term::stdout() {
102 Some(ref t) if isatty() && t.supports_color() => {
103 print_diff_fancy(diff, get_section_title, term::stdout().unwrap())
105 _ => print_diff_basic(diff, get_section_title),
108 // isatty shamelessly adapted from cargo.
110 fn isatty() -> bool {
113 unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
116 fn isatty() -> bool {
117 extern crate kernel32;
121 let handle = kernel32::GetStdHandle(winapi::winbase::STD_OUTPUT_HANDLE);
123 kernel32::GetConsoleMode(handle, &mut out) != 0
128 fn print_diff_fancy<F>(diff: Vec<Mismatch>,
129 get_section_title: F,
130 mut t: Box<term::Terminal<Output = io::Stdout>>)
131 where F: Fn(u32) -> String
133 for mismatch in diff {
134 let title = get_section_title(mismatch.line_number);
135 writeln!(t, "{}", title).unwrap();
137 for line in mismatch.lines {
139 DiffLine::Context(ref str) => {
141 writeln!(t, " {}⏎", str).unwrap();
143 DiffLine::Expected(ref str) => {
144 t.fg(term::color::GREEN).unwrap();
145 writeln!(t, "+{}⏎", str).unwrap();
147 DiffLine::Resulting(ref str) => {
148 t.fg(term::color::RED).unwrap();
149 writeln!(t, "-{}⏎", str).unwrap();
157 pub fn print_diff_basic<F>(diff: Vec<Mismatch>, get_section_title: F)
158 where F: Fn(u32) -> String
160 for mismatch in diff {
161 let title = get_section_title(mismatch.line_number);
162 println!("{}", title);
164 for line in mismatch.lines {
166 DiffLine::Context(ref str) => {
167 println!(" {}⏎", str);
169 DiffLine::Expected(ref str) => {
170 println!("+{}⏎", str);
172 DiffLine::Resulting(ref str) => {
173 println!("-{}⏎", str);