about summary refs log tree commit diff
path: root/src/rustfmt_diff.rs
diff options
context:
space:
mode:
authorNick Cameron <nrc@ncameron.org>2016-05-18 09:25:57 +1200
committerMarcus Klaas de Vries <mail@marcusklaas.nl>2016-05-17 23:25:57 +0200
commitd0720a00a1f708bb495535d361a1a63f65234fc7 (patch)
tree26838dfc20632dd7a3e6499d5b131e1fea448792 /src/rustfmt_diff.rs
parentc8ba6ee5a2e92822b9d805c3d30b18c38de6c250 (diff)
Fall back to basic stdout if we can't unwrap a fancy terminal (#995)
fixes #978
Diffstat (limited to 'src/rustfmt_diff.rs')
-rw-r--r--src/rustfmt_diff.rs36
1 files changed, 35 insertions, 1 deletions
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<Misma
 pub fn print_diff<F>(diff: Vec<Mismatch>, 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<F>(diff: Vec<Mismatch>,
+                       get_section_title: F,
+                       mut t: Box<term::Terminal<Output = io::Stdout>>)
+    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<F>(diff: Vec<Mismatch>, get_section_title: F)
         t.reset().unwrap();
     }
 }
+
+pub fn print_diff_basic<F>(diff: Vec<Mismatch>, 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);
+                }
+            }
+        }
+    }
+}