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;
17 #[derive(Debug, PartialEq)]
24 #[derive(Debug, PartialEq)]
27 pub lines: Vec<DiffLine>,
31 fn new(line_number: u32) -> Mismatch {
33 line_number: line_number,
39 // Produces a diff between the expected output and actual output of rustfmt.
40 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
41 let mut line_number = 1;
42 let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
43 let mut lines_since_mismatch = context_size + 1;
44 let mut results = Vec::new();
45 let mut mismatch = Mismatch::new(0);
47 for result in diff::lines(expected, actual) {
49 diff::Result::Left(str) => {
50 if lines_since_mismatch >= context_size {
51 results.push(mismatch);
52 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
55 while let Some(line) = context_queue.pop_front() {
56 mismatch.lines.push(DiffLine::Context(line.to_owned()));
59 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
60 lines_since_mismatch = 0;
62 diff::Result::Right(str) => {
63 if lines_since_mismatch >= context_size {
64 results.push(mismatch);
65 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
68 while let Some(line) = context_queue.pop_front() {
69 mismatch.lines.push(DiffLine::Context(line.to_owned()));
72 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
74 lines_since_mismatch = 0;
76 diff::Result::Both(str, _) => {
77 if context_queue.len() >= context_size {
78 let _ = context_queue.pop_front();
81 if lines_since_mismatch < context_size {
82 mismatch.lines.push(DiffLine::Context(str.to_owned()));
84 context_queue.push_back(str);
88 lines_since_mismatch += 1;
93 results.push(mismatch);
99 pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F)
101 F: Fn(u32) -> String,
103 match term::stdout() {
104 Some(ref t) if isatty() && t.supports_color() => {
105 print_diff_fancy(diff, get_section_title, term::stdout().unwrap())
107 _ => print_diff_basic(diff, get_section_title),
111 fn print_diff_fancy<F>(
113 get_section_title: F,
114 mut t: Box<term::Terminal<Output = io::Stdout>>,
116 F: Fn(u32) -> String,
118 for mismatch in diff {
119 let title = get_section_title(mismatch.line_number);
120 writeln!(t, "{}", title).unwrap();
122 for line in mismatch.lines {
124 DiffLine::Context(ref str) => {
126 writeln!(t, " {}⏎", str).unwrap();
128 DiffLine::Expected(ref str) => {
129 t.fg(term::color::GREEN).unwrap();
130 writeln!(t, "+{}⏎", str).unwrap();
132 DiffLine::Resulting(ref str) => {
133 t.fg(term::color::RED).unwrap();
134 writeln!(t, "-{}⏎", str).unwrap();
142 pub fn print_diff_basic<F>(diff: Vec<Mismatch>, get_section_title: F)
144 F: Fn(u32) -> String,
146 for mismatch in diff {
147 let title = get_section_title(mismatch.line_number);
148 println!("{}", title);
150 for line in mismatch.lines {
152 DiffLine::Context(ref str) => {
153 println!(" {}⏎", str);
155 DiffLine::Expected(ref str) => {
156 println!("+{}⏎", str);
158 DiffLine::Resulting(ref str) => {
159 println!("-{}⏎", str);