walles / riff

A diff filter highlighting which line parts have changed
MIT License
253 stars 5 forks source link

Highlight line expansion #17

Closed walles closed 3 years ago

walles commented 3 years ago

In this case, the added line containing stream.write_all(text.as_bytes()) should have all the new chars highlighted, and the old line should have the removed .unwrap(); part highlighted.

diff --git a/src/main.rs b/src/main.rs
index 55a7c18..8826249 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -90,7 +91,15 @@ fn get_fixed_highlight(line: &str) -> &str {
 }

 fn print(stream: &mut BufWriter<&mut dyn Write>, text: &str) {
-    stream.write_all(text.as_bytes()).unwrap();
+    if let Err(error) = stream.write_all(text.as_bytes()) {
+        if error.kind() == ErrorKind::BrokenPipe {
+            // This is fine, somebody probably just quit their pager before it
+            // was done reading our output.
+            exit(0);
+        }
+
+        panic!("Error writing diff to pager: {:?}", error);
+    }
 }

 fn println(stream: &mut BufWriter<&mut dyn Write>, text: &str) {