Use color code logic for fatal errors
We create a helper function to contain the simple logic and call it from
both sites where we're reporting something.
@@ -7,7 +7,7 @@ pub type Src = String;
pub type Name = String;
pub fn fatal_compiler_bug(msg: &str) -> ! {
- let buffer_writer = termcolor::BufferWriter::stderr(termcolor::ColorChoice::Always);
+ let buffer_writer = error_buffer_writer();
let mut buffer = buffer_writer.buffer();
use std::io::Write;
use termcolor::{Color, ColorSpec, WriteColor};
@@ -1017,16 +1017,21 @@ but it cannot be found.",
}
pub fn pretty_print(&self) {
+ let buffer_writer = error_buffer_writer();
+ let mut buffer = buffer_writer.buffer();
+ self.pretty(&mut buffer);
+ buffer_writer.print(&buffer).unwrap();
+ }
+}
+
+fn error_buffer_writer() -> termcolor::BufferWriter {
+ // Don't add color codes to the output if standard error isn't connected to a terminal
let color_choice = if atty::is(atty::Stream::Stderr) {
termcolor::ColorChoice::Auto
} else {
termcolor::ColorChoice::Never
};
- let buffer_writer = termcolor::BufferWriter::stderr(color_choice);
- let mut buffer = buffer_writer.buffer();
- self.pretty(&mut buffer);
- buffer_writer.print(&buffer).unwrap();
- }
+ termcolor::BufferWriter::stderr(color_choice)
}
fn std_io_error_kind_text(kind: &std::io::ErrorKind) -> String {
