Skip to content

Commit b7676f2

Browse files
committed
Add compiler flag to configure output coloring
This adds the flag --color, which allows the user to force coloring or turn it off. The default behavior stays the same as before (colorize, if output goes to tty). Why this is beneficial is explained in issue #12881. Please note that this commit doesn't include any regression tests. I thought about how I'd write a test for this and it doesn't seem to be worth the effort to me for a UI change like this. Fixes #12881.
1 parent 579e0a5 commit b7676f2

File tree

7 files changed

+46
-12
lines changed

7 files changed

+46
-12
lines changed

src/librustc/driver/config.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use syntax::ast;
2727
use syntax::ast::{IntTy, UintTy};
2828
use syntax::attr;
2929
use syntax::attr::AttrMetaMethods;
30+
use syntax::diagnostic::{ColorConfig, Auto, Always, Never};
3031
use syntax::parse;
3132
use syntax::parse::token::InternedString;
3233

@@ -92,6 +93,7 @@ pub struct Options {
9293
/// Crate id-related things to maybe print. It's (crate_id, crate_name, crate_file_name).
9394
pub print_metas: (bool, bool, bool),
9495
pub cg: CodegenOptions,
96+
pub color: ColorConfig,
9597
}
9698

9799
/// Some reasonable defaults
@@ -115,6 +117,7 @@ pub fn basic_options() -> Options {
115117
write_dependency_info: (false, None),
116118
print_metas: (false, false, false),
117119
cg: basic_codegen_options(),
120+
color: Auto,
118121
}
119122
}
120123

@@ -536,7 +539,11 @@ pub fn optgroups() -> Vec<getopts::OptGroup> {
536539
optmulti("F", "forbid", "Set lint forbidden", "OPT"),
537540
optmulti("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
538541
optmulti("Z", "", "Set internal debugging options", "FLAG"),
539-
optflag( "v", "version", "Print version info and exit")
542+
optflag("v", "version", "Print version info and exit"),
543+
optopt("", "color", "Configure coloring of output:
544+
auto = colorize, if output goes to a tty (default);
545+
always = always colorize output;
546+
never = never colorize output", "auto|always|never")
540547
)
541548
}
542549

@@ -707,6 +714,18 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
707714
matches.opt_present("crate-file-name"));
708715
let cg = build_codegen_options(matches);
709716

717+
let color = match matches.opt_str("color").as_ref().map(|s| s.as_slice()) {
718+
Some("auto") => Auto,
719+
Some("always") => Always,
720+
Some("never") => Never,
721+
722+
None => Auto,
723+
724+
Some(arg) => early_error(format!(
725+
"argument for --color must be auto, always or never (instead was `{}`)",
726+
arg))
727+
};
728+
710729
Options {
711730
crate_types: crate_types,
712731
gc: gc,
@@ -726,6 +745,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
726745
write_dependency_info: write_dependency_info,
727746
print_metas: print_metas,
728747
cg: cg,
748+
color: color
729749
}
730750
}
731751

src/librustc/driver/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn parse_crate_attrs(sess: &Session, input: &Input) ->
323323
}
324324

325325
pub fn early_error(msg: &str) -> ! {
326-
let mut emitter = diagnostic::EmitterWriter::stderr();
326+
let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto);
327327
emitter.emit(None, msg, diagnostic::Fatal);
328328
fail!(diagnostic::FatalError);
329329
}
@@ -368,7 +368,7 @@ fn monitor(f: proc():Send) {
368368
Err(value) => {
369369
// Task failed without emitting a fatal diagnostic
370370
if !value.is::<diagnostic::FatalError>() {
371-
let mut emitter = diagnostic::EmitterWriter::stderr();
371+
let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto);
372372

373373
// a .span_bug or .bug call has already printed what
374374
// it wants to print.

src/librustc/driver/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub fn build_session(sopts: config::Options,
196196
-> Session {
197197
let codemap = codemap::CodeMap::new();
198198
let diagnostic_handler =
199-
diagnostic::default_handler();
199+
diagnostic::default_handler(sopts.color);
200200
let span_diagnostic_handler =
201201
diagnostic::mk_span_handler(diagnostic_handler, codemap);
202202

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<StrBuf>)
7878

7979

8080
let codemap = syntax::codemap::CodeMap::new();
81-
let diagnostic_handler = syntax::diagnostic::default_handler();
81+
let diagnostic_handler = syntax::diagnostic::default_handler(syntax::diagnostic::Auto);
8282
let span_diagnostic_handler =
8383
syntax::diagnostic::mk_span_handler(diagnostic_handler, codemap);
8484

src/librustdoc/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn run(input: &str,
5353

5454

5555
let codemap = CodeMap::new();
56-
let diagnostic_handler = diagnostic::default_handler();
56+
let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto);
5757
let span_diagnostic_handler =
5858
diagnostic::mk_span_handler(diagnostic_handler, codemap);
5959

src/libsyntax/diagnostic.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ impl RenderSpan {
4949
}
5050
}
5151

52+
#[deriving(Clone)]
53+
pub enum ColorConfig {
54+
Auto,
55+
Always,
56+
Never
57+
}
58+
5259
pub trait Emitter {
5360
fn emit(&mut self, cmsp: Option<(&codemap::CodeMap, Span)>,
5461
msg: &str, lvl: Level);
@@ -176,8 +183,8 @@ pub fn mk_span_handler(handler: Handler, cm: codemap::CodeMap) -> SpanHandler {
176183
}
177184
}
178185

179-
pub fn default_handler() -> Handler {
180-
mk_handler(box EmitterWriter::stderr())
186+
pub fn default_handler(color_config: ColorConfig) -> Handler {
187+
mk_handler(box EmitterWriter::stderr(color_config))
181188
}
182189

183190
pub fn mk_handler(e: Box<Emitter:Send>) -> Handler {
@@ -257,9 +264,16 @@ enum Destination {
257264
}
258265

259266
impl EmitterWriter {
260-
pub fn stderr() -> EmitterWriter {
267+
pub fn stderr(color_config: ColorConfig) -> EmitterWriter {
261268
let stderr = io::stderr();
262-
if stderr.get_ref().isatty() {
269+
270+
let use_color = match color_config {
271+
Always => true,
272+
Never => false,
273+
Auto => stderr.get_ref().isatty()
274+
};
275+
276+
if use_color {
263277
let dst = match term::Terminal::new(stderr.unwrap()) {
264278
Ok(t) => Terminal(t),
265279
Err(..) => Raw(box io::stderr()),

src/libsyntax/parse/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use ast;
1515
use codemap::{Span, CodeMap, FileMap};
16-
use diagnostic::{SpanHandler, mk_span_handler, default_handler};
16+
use diagnostic::{SpanHandler, mk_span_handler, default_handler, Auto};
1717
use parse::attr::ParserAttr;
1818
use parse::parser::Parser;
1919

@@ -41,7 +41,7 @@ pub struct ParseSess {
4141

4242
pub fn new_parse_sess() -> ParseSess {
4343
ParseSess {
44-
span_diagnostic: mk_span_handler(default_handler(), CodeMap::new()),
44+
span_diagnostic: mk_span_handler(default_handler(Auto), CodeMap::new()),
4545
included_mod_stack: RefCell::new(Vec::new()),
4646
}
4747
}

0 commit comments

Comments
 (0)