1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use std::collections::VecDeque;
16 #[derive(Debug, PartialEq)]
23 #[derive(Debug, PartialEq)]
26 pub lines: Vec<DiffLine>,
30 fn new(line_number: u32) -> Mismatch {
32 line_number: line_number,
38 // Produces a diff between the expected output and actual output of rustfmt.
39 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
40 let mut line_number = 1;
41 let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
42 let mut lines_since_mismatch = context_size + 1;
43 let mut results = Vec::new();
44 let mut mismatch = Mismatch::new(0);
46 for result in diff::lines(expected, actual) {
48 diff::Result::Left(str) => {
49 if lines_since_mismatch >= context_size {
50 results.push(mismatch);
51 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
54 while let Some(line) = context_queue.pop_front() {
55 mismatch.lines.push(DiffLine::Context(line.to_owned()));
58 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
59 lines_since_mismatch = 0;
61 diff::Result::Right(str) => {
62 if lines_since_mismatch >= context_size {
63 results.push(mismatch);
64 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
67 while let Some(line) = context_queue.pop_front() {
68 mismatch.lines.push(DiffLine::Context(line.to_owned()));
71 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
73 lines_since_mismatch = 0;
75 diff::Result::Both(str, _) => {
76 if context_queue.len() >= context_size {
77 let _ = context_queue.pop_front();
80 if lines_since_mismatch < context_size {
81 mismatch.lines.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)
100 F: Fn(u32) -> String,
102 match term::stdout() {
103 Some(ref t) if isatty() && t.supports_color() => {
104 print_diff_fancy(diff, get_section_title, term::stdout().unwrap())
106 _ => print_diff_basic(diff, get_section_title),
109 // isatty shamelessly adapted from cargo.
111 fn isatty() -> bool {
114 unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
117 fn isatty() -> bool {
118 extern crate kernel32;
122 let handle = kernel32::GetStdHandle(winapi::winbase::STD_OUTPUT_HANDLE);
124 kernel32::GetConsoleMode(handle, &mut out) != 0
129 fn print_diff_fancy<F>(
131 get_section_title: F,
132 mut t: Box<term::Terminal<Output = io::Stdout>>,
134 F: Fn(u32) -> String,
136 for mismatch in diff {
137 let title = get_section_title(mismatch.line_number);
138 writeln!(t, "{}", title).unwrap();
140 for line in mismatch.lines {
142 DiffLine::Context(ref str) => {
144 writeln!(t, " {}⏎", str).unwrap();
146 DiffLine::Expected(ref str) => {
147 t.fg(term::color::GREEN).unwrap();
148 writeln!(t, "+{}⏎", str).unwrap();
150 DiffLine::Resulting(ref str) => {
151 t.fg(term::color::RED).unwrap();
152 writeln!(t, "-{}⏎", str).unwrap();
160 pub fn print_diff_basic<F>(diff: Vec<Mismatch>, get_section_title: F)
162 F: Fn(u32) -> String,
164 for mismatch in diff {
165 let title = get_section_title(mismatch.line_number);
166 println!("{}", title);
168 for line in mismatch.lines {
170 DiffLine::Context(ref str) => {
171 println!(" {}⏎", str);
173 DiffLine::Expected(ref str) => {
174 println!("+{}⏎", str);
176 DiffLine::Resulting(ref str) => {
177 println!("-{}⏎", str);