Skip to content

Commit bfaf6ce

Browse files
committed
Enable or disable colored output according to CLICOLOR(_FORCE)
1 parent 3ff17e7 commit bfaf6ce

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/librustc_errors/emitter.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_data_structures::fx::FxHashMap;
2424
use rustc_data_structures::sync::Lrc;
2525
use std::borrow::Cow;
2626
use std::io::prelude::*;
27-
use std::io;
27+
use std::{env, io};
2828
use std::cmp::{min, max, Reverse};
2929
use std::path::Path;
3030
use termcolor::{StandardStream, ColorChoice, ColorSpec, BufferWriter, Ansi};
@@ -458,7 +458,11 @@ impl ColorConfig {
458458
}
459459
}
460460
ColorConfig::Never => ColorChoice::Never,
461-
ColorConfig::Auto if atty::is(atty::Stream::Stderr) => {
461+
ColorConfig::Auto
462+
if env::var("CLICOLOR_FORCE").unwrap_or("0".to_string()) != "0"
463+
|| (atty::is(atty::Stream::Stderr)
464+
&& env::var("CLICOLOR").unwrap_or("1".to_string()) != "0") =>
465+
{
462466
ColorChoice::Auto
463467
}
464468
ColorConfig::Auto => ColorChoice::Never,

src/libterm/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
html_playground_url = "https://play.rust-lang.org/",
3535
test(attr(deny(warnings))))]
3636
#![deny(missing_docs)]
37+
#![feature(result_map_or)]
3738

3839
#![cfg_attr(windows, feature(libc))]
3940

src/libterm/terminfo/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::color;
1313
use crate::Terminal;
1414

1515
use searcher::get_dbpath_for_term;
16-
use parser::compiled::{parse, msys_terminfo};
16+
use parser::compiled::{parse, ansi_terminfo};
1717
use parm::{expand, Variables, Param};
1818

1919
/// A parsed terminfo database entry.
@@ -69,14 +69,18 @@ impl fmt::Display for Error {
6969
impl TermInfo {
7070
/// Creates a TermInfo based on current environment.
7171
pub fn from_env() -> Result<TermInfo, Error> {
72+
if env::var("CLICOLOR_FORCE").unwrap_or("0".to_string()) != "0" {
73+
return Ok(ansi_terminfo());
74+
}
7275
let term = match env::var("TERM") {
7376
Ok(name) => TermInfo::from_name(&name),
7477
Err(..) => return Err(Error::TermUnset),
7578
};
7679

77-
if term.is_err() && env::var("MSYSCON").ok().map_or(false, |s| "mintty.exe" == s) {
80+
if term.is_err() && (env::var("MSYSCON").map_or(false, |s| "mintty.exe" == s) ||
81+
env::var("CLICOLOR").unwrap_or("0".to_string()) != "0") {
7882
// msys terminal
79-
Ok(msys_terminfo())
83+
Ok(ansi_terminfo())
8084
} else {
8185
term
8286
}

src/libterm/terminfo/parser/compiled.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,9 @@ pub fn parse(file: &mut dyn io::Read, longnames: bool) -> Result<TermInfo, Strin
316316
})
317317
}
318318

319-
/// Creates a dummy TermInfo struct for msys terminals
320-
pub fn msys_terminfo() -> TermInfo {
319+
/// Create a dummy TermInfo struct which only supports ISO 6429 (ANSI) color sequences. This is
320+
/// used for msys and when CLICOLOR(_FORCE) is set.
321+
pub fn ansi_terminfo() -> TermInfo {
321322
let mut strings = HashMap::new();
322323
strings.insert("sgr0".to_string(), b"\x1B[0m".to_vec());
323324
strings.insert("bold".to_string(), b"\x1B[1m".to_vec());

0 commit comments

Comments
 (0)