From d0720a00a1f708bb495535d361a1a63f65234fc7 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 18 May 2016 09:25:57 +1200 Subject: [PATCH] Fall back to basic stdout if we can't unwrap a fancy terminal (#995) fixes #978 --- src/rustfmt_diff.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs index 9f529276df1..fe521029cb5 100644 --- a/src/rustfmt_diff.rs +++ b/src/rustfmt_diff.rs @@ -1,6 +1,7 @@ use std::collections::VecDeque; use diff; use term; +use std::io; #[derive(Debug, PartialEq)] pub enum DiffLine { @@ -87,8 +88,18 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec(diff: Vec, get_section_title: F) where F: Fn(u32) -> String { - let mut t = term::stdout().unwrap(); + if let Some(t) = term::stdout() { + print_diff_fancy(diff, get_section_title, t); + } else { + print_diff_basic(diff, get_section_title); + } +} +fn print_diff_fancy(diff: Vec, + get_section_title: F, + mut t: Box>) + where F: Fn(u32) -> String +{ for mismatch in diff { let title = get_section_title(mismatch.line_number); writeln!(t, "{}", title).unwrap(); @@ -112,3 +123,26 @@ pub fn print_diff(diff: Vec, get_section_title: F) t.reset().unwrap(); } } + +pub fn print_diff_basic(diff: Vec, get_section_title: F) + where F: Fn(u32) -> String +{ + for mismatch in diff { + let title = get_section_title(mismatch.line_number); + println!("{}", title); + + for line in mismatch.lines { + match line { + DiffLine::Context(ref str) => { + println!(" {}⏎", str); + } + DiffLine::Expected(ref str) => { + println!("+{}⏎", str); + } + DiffLine::Resulting(ref str) => { + println!("-{}⏎", str); + } + } + } + } +}