1
Fork 0

Remove a bool for color in favor of the WriteColor trait wrapping colored and uncolored printing

This commit is contained in:
Oli Scherer 2023-07-26 13:58:50 +00:00
parent 0e7ec9683d
commit 51c22154f5
7 changed files with 49 additions and 25 deletions

View file

@ -3619,6 +3619,7 @@ dependencies = [
"rustc_session", "rustc_session",
"rustc_span", "rustc_span",
"smallvec", "smallvec",
"termcolor",
"thin-vec", "thin-vec",
"tracing", "tracing",
] ]

View file

@ -61,12 +61,15 @@ impl HumanReadableErrorType {
} }
pub fn new_emitter( pub fn new_emitter(
self, self,
dst: Box<dyn Write + Send>, mut dst: Box<dyn WriteColor + Send>,
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
) -> EmitterWriter { ) -> EmitterWriter {
let (short, color_config) = self.unzip(); let (short, color_config) = self.unzip();
let color = color_config.suggests_using_colors(); let color = color_config.suggests_using_colors();
EmitterWriter::new(dst, fallback_bundle, color).short_message(short) if !dst.supports_color() && color {
dst = Box::new(Ansi::new(dst));
}
EmitterWriter::new(dst, fallback_bundle).short_message(short)
} }
} }
@ -669,11 +672,10 @@ impl EmitterWriter {
} }
pub fn new( pub fn new(
dst: Box<dyn Write + Send>, dst: Box<dyn WriteColor + Send>,
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
colored: bool,
) -> EmitterWriter { ) -> EmitterWriter {
Self::create(Raw(dst, colored), fallback_bundle) Self::create(Raw(dst), fallback_bundle)
} }
fn maybe_anonymized(&self, line_num: usize) -> Cow<'static, str> { fn maybe_anonymized(&self, line_num: usize) -> Cow<'static, str> {
@ -2603,15 +2605,13 @@ fn emit_to_destination(
pub enum Destination { pub enum Destination {
Terminal(StandardStream), Terminal(StandardStream),
Buffered(BufferWriter), Buffered(BufferWriter),
// The bool denotes whether we should be emitting ansi color codes or not Raw(Box<(dyn WriteColor + Send)>),
Raw(Box<(dyn Write + Send)>, bool),
} }
pub enum WritableDst<'a> { pub enum WritableDst<'a> {
Terminal(&'a mut StandardStream), Terminal(&'a mut StandardStream),
Buffered(&'a mut BufferWriter, Buffer), Buffered(&'a mut BufferWriter, Buffer),
Raw(&'a mut (dyn Write + Send)), Raw(&'a mut (dyn WriteColor + Send)),
ColoredRaw(Ansi<&'a mut (dyn Write + Send)>),
} }
impl Destination { impl Destination {
@ -2637,8 +2637,7 @@ impl Destination {
let buf = t.buffer(); let buf = t.buffer();
WritableDst::Buffered(t, buf) WritableDst::Buffered(t, buf)
} }
Destination::Raw(ref mut t, false) => WritableDst::Raw(t), Destination::Raw(ref mut t) => WritableDst::Raw(t),
Destination::Raw(ref mut t, true) => WritableDst::ColoredRaw(Ansi::new(t)),
} }
} }
@ -2646,7 +2645,7 @@ impl Destination {
match *self { match *self {
Self::Terminal(ref stream) => stream.supports_color(), Self::Terminal(ref stream) => stream.supports_color(),
Self::Buffered(ref buffer) => buffer.buffer().supports_color(), Self::Buffered(ref buffer) => buffer.buffer().supports_color(),
Self::Raw(_, supports_color) => supports_color, Self::Raw(ref writer) => writer.supports_color(),
} }
} }
} }
@ -2706,8 +2705,7 @@ impl<'a> WritableDst<'a> {
match *self { match *self {
WritableDst::Terminal(ref mut t) => t.set_color(color), WritableDst::Terminal(ref mut t) => t.set_color(color),
WritableDst::Buffered(_, ref mut t) => t.set_color(color), WritableDst::Buffered(_, ref mut t) => t.set_color(color),
WritableDst::ColoredRaw(ref mut t) => t.set_color(color), WritableDst::Raw(ref mut t) => t.set_color(color),
WritableDst::Raw(_) => Ok(()),
} }
} }
@ -2715,8 +2713,7 @@ impl<'a> WritableDst<'a> {
match *self { match *self {
WritableDst::Terminal(ref mut t) => t.reset(), WritableDst::Terminal(ref mut t) => t.reset(),
WritableDst::Buffered(_, ref mut t) => t.reset(), WritableDst::Buffered(_, ref mut t) => t.reset(),
WritableDst::ColoredRaw(ref mut t) => t.reset(), WritableDst::Raw(ref mut t) => t.reset(),
WritableDst::Raw(_) => Ok(()),
} }
} }
} }
@ -2727,7 +2724,6 @@ impl<'a> Write for WritableDst<'a> {
WritableDst::Terminal(ref mut t) => t.write(bytes), WritableDst::Terminal(ref mut t) => t.write(bytes),
WritableDst::Buffered(_, ref mut buf) => buf.write(bytes), WritableDst::Buffered(_, ref mut buf) => buf.write(bytes),
WritableDst::Raw(ref mut w) => w.write(bytes), WritableDst::Raw(ref mut w) => w.write(bytes),
WritableDst::ColoredRaw(ref mut t) => t.write(bytes),
} }
} }
@ -2736,7 +2732,6 @@ impl<'a> Write for WritableDst<'a> {
WritableDst::Terminal(ref mut t) => t.flush(), WritableDst::Terminal(ref mut t) => t.flush(),
WritableDst::Buffered(_, ref mut buf) => buf.flush(), WritableDst::Buffered(_, ref mut buf) => buf.flush(),
WritableDst::Raw(ref mut w) => w.flush(), WritableDst::Raw(ref mut w) => w.flush(),
WritableDst::ColoredRaw(ref mut w) => w.flush(),
} }
} }
} }

View file

@ -10,6 +10,7 @@
// FIXME: spec the JSON output properly. // FIXME: spec the JSON output properly.
use rustc_span::source_map::{FilePathMapping, SourceMap}; use rustc_span::source_map::{FilePathMapping, SourceMap};
use termcolor::{ColorSpec, WriteColor};
use crate::emitter::{Emitter, HumanReadableErrorType}; use crate::emitter::{Emitter, HumanReadableErrorType};
use crate::registry::Registry; use crate::registry::Registry;
@ -356,6 +357,19 @@ impl Diagnostic {
self.0.lock().unwrap().flush() self.0.lock().unwrap().flush()
} }
} }
impl WriteColor for BufWriter {
fn supports_color(&self) -> bool {
false
}
fn set_color(&mut self, _spec: &ColorSpec) -> io::Result<()> {
Ok(())
}
fn reset(&mut self) -> io::Result<()> {
Ok(())
}
}
let buf = BufWriter::default(); let buf = BufWriter::default();
let output = buf.clone(); let output = buf.clone();
je.json_rendered je.json_rendered

View file

@ -27,3 +27,4 @@ rustc_span = { path = "../rustc_span" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.12" thin-vec = "0.2.12"
tracing = "0.1" tracing = "0.1"
termcolor = "1.2"

View file

@ -9,6 +9,7 @@ use rustc_span::{BytePos, Span};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::emitter::EmitterWriter; use rustc_errors::emitter::EmitterWriter;
use rustc_errors::{Handler, MultiSpan, PResult}; use rustc_errors::{Handler, MultiSpan, PResult};
use termcolor::WriteColor;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
@ -29,10 +30,9 @@ fn create_test_handler() -> (Handler, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) {
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE], vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
false, false,
); );
let emitter = let emitter = EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle)
EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle, false) .sm(Some(source_map.clone()))
.sm(Some(source_map.clone())) .diagnostic_width(Some(140));
.diagnostic_width(Some(140));
let handler = Handler::with_emitter(Box::new(emitter)); let handler = Handler::with_emitter(Box::new(emitter));
(handler, source_map, output) (handler, source_map, output)
} }
@ -156,6 +156,20 @@ pub(crate) struct Shared<T: Write> {
pub data: Arc<Mutex<T>>, pub data: Arc<Mutex<T>>,
} }
impl<T: Write> WriteColor for Shared<T> {
fn supports_color(&self) -> bool {
false
}
fn set_color(&mut self, _spec: &termcolor::ColorSpec) -> io::Result<()> {
Ok(())
}
fn reset(&mut self) -> io::Result<()> {
Ok(())
}
}
impl<T: Write> Write for Shared<T> { impl<T: Write> Write for Shared<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.data.lock().unwrap().write(buf) self.data.lock().unwrap().write(buf)

View file

@ -562,7 +562,7 @@ pub(crate) fn make_test(
.diagnostic_width(Some(80)) .diagnostic_width(Some(80))
.supports_color(); .supports_color();
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle, false); let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle);
// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings(); let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
@ -738,7 +738,7 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
false, false,
); );
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle, false); let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle);
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings(); let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
let sess = ParseSess::with_span_handler(handler, sm); let sess = ParseSess::with_span_handler(handler, sm);

View file

@ -719,7 +719,6 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
let emitter = EmitterWriter::new( let emitter = EmitterWriter::new(
Box::new(io::sink()), Box::new(io::sink()),
fallback_bundle, fallback_bundle,
false,
); );
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings(); let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
let sess = ParseSess::with_span_handler(handler, sm); let sess = ParseSess::with_span_handler(handler, sm);