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() {
45 mismatch.lines.push(DiffLine::Context(line.to_owned()));
48 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
49 lines_since_mismatch = 0;
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);
57 while let Some(line) = context_queue.pop_front() {
58 mismatch.lines.push(DiffLine::Context(line.to_owned()));
61 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
63 lines_since_mismatch = 0;
65 diff::Result::Both(str, _) => {
66 if context_queue.len() >= context_size {
67 let _ = context_queue.pop_front();
70 if lines_since_mismatch < context_size {
71 mismatch.lines.push(DiffLine::Context(str.to_owned()));
73 context_queue.push_back(str);
77 lines_since_mismatch += 1;
82 results.push(mismatch);
88 pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F)
89 where F: Fn(u32) -> String
91 match term::stdout() {
92 Some(ref t) if isatty() && t.supports_color() => {
93 print_diff_fancy(diff, get_section_title, term::stdout().unwrap())
95 _ => print_diff_basic(diff, get_section_title),
98 // isatty shamelessly adapted from cargo.
100 fn isatty() -> bool {
103 unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
106 fn isatty() -> bool {
107 extern crate kernel32;
111 let handle = kernel32::GetStdHandle(winapi::winbase::STD_OUTPUT_HANDLE);
113 kernel32::GetConsoleMode(handle, &mut out) != 0
118 fn print_diff_fancy<F>(diff: Vec<Mismatch>,
119 get_section_title: F,
120 mut t: Box<term::Terminal<Output = io::Stdout>>)
121 where F: Fn(u32) -> String
123 for mismatch in diff {
124 let title = get_section_title(mismatch.line_number);
125 writeln!(t, "{}", title).unwrap();
127 for line in mismatch.lines {
129 DiffLine::Context(ref str) => {
131 writeln!(t, " {}⏎", str).unwrap();
133 DiffLine::Expected(ref str) => {
134 t.fg(term::color::GREEN).unwrap();
135 writeln!(t, "+{}⏎", str).unwrap();
137 DiffLine::Resulting(ref str) => {
138 t.fg(term::color::RED).unwrap();
139 writeln!(t, "-{}⏎", str).unwrap();
147 pub fn print_diff_basic<F>(diff: Vec<Mismatch>, get_section_title: F)
148 where F: Fn(u32) -> String
150 for mismatch in diff {
151 let title = get_section_title(mismatch.line_number);
152 println!("{}", title);
154 for line in mismatch.lines {
156 DiffLine::Context(ref str) => {
157 println!(" {}⏎", str);
159 DiffLine::Expected(ref str) => {
160 println!("+{}⏎", str);
162 DiffLine::Resulting(ref str) => {
163 println!("-{}⏎", str);